Commit 52b89a5c authored by Klaus Hamberger's avatar Klaus Hamberger
Browse files

Improved closure rate formula for higher-order circuits (open chain number divided by order).

Debugged circuit search by cumulative formula ("<").
Debugged restriction "SOME" for open chain search. 

git-svn-id: svn+ssh://svn.code.sf.net/p/tip-puck/code/trunk@2333 fbf60bc9-78ff-467a-80f5-d9f1d8e511fc
parent d5586103
......@@ -3,25 +3,21 @@ package org.tip.puck.census.chains;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.AbstractWriter;
import oldcore.trash.Clusterable;
import oldcore.trash.OldRing;
import oldcore.trash.RingGroupMap;
import org.tip.puck.census.workers.CircuitFinder;
import org.tip.puck.census.workers.SiblingMode;
import org.tip.puck.census.workers.SymmetryType;
import org.tip.puck.net.Attribute;
import org.tip.puck.net.Families;
import org.tip.puck.net.Family;
import org.tip.puck.net.Gender;
import org.tip.puck.net.Individual;
import org.tip.puck.net.Net;
import org.tip.puck.net.workers.IndividualValuator;
import org.tip.puck.partitions.Cluster;
import org.tip.puck.util.MathUtils;
import org.tip.puck.util.Numberable;
import org.tip.puck.util.Value;
import oldcore.trash.OldRing;
import oldcore.trash.RingGroupMap;
......@@ -45,7 +41,13 @@ public class Chain extends ArrayList<Individual> implements Comparable<Chain>, N
//
return result;
}*/
/**
*
*/
private static final long serialVersionUID = -7338230463348261936L;
int id;
......@@ -99,7 +101,7 @@ public class Chain extends ArrayList<Individual> implements Comparable<Chain>, N
/**
* the sequence of direction indices (1 ascending, -1 descending, O marriage)
*/
List<Integer> directions = new ArrayList<Integer>();
private List<Integer> directions = new ArrayList<Integer>();
// String signature;
//replace by dim()? - for the moment only calculated for chains that are constructed incrementally and not by subchain composition
......@@ -313,7 +315,16 @@ public class Chain extends ArrayList<Individual> implements Comparable<Chain>, N
// chain.pivots = pivots;
return chain;
}
public Chain clone (int id){
Chain result;
result = clone();
result.setId(id);
//
return result;
}
public int compareTo(Chain c){
int result;
......@@ -2682,6 +2693,12 @@ public class Chain extends ArrayList<Individual> implements Comparable<Chain>, N
return signature(Notation.NUMBERS);
}
public void setDir(int position, int dir){
directions.set(position, dir);
}
// Old constructors and add methods (harmonize!)
// replaced by ChainMaker#concatenateInv
......
......@@ -3,15 +3,15 @@ package org.tip.puck.census.chains;
import java.util.ArrayList;
import java.util.List;
import oldcore.trash.OldRing;
import oldcore.trash.RingGroupMap;
import org.opengis.geometry.primitive.Ring;
import org.tip.puck.census.workers.CircuitFinder;
import org.tip.puck.census.workers.SiblingMode;
import org.tip.puck.net.FiliationType;
import org.tip.puck.net.Gender;
import org.tip.puck.net.Individual;
import org.tip.puck.util.Value;
import oldcore.trash.OldRing;
import oldcore.trash.RingGroupMap;
public class ChainMaker {
......@@ -276,9 +276,13 @@ public class ChainMaker {
result = null;
if (kinString!=null && kinString.charAt(0)!='<') {
if (Chain.notation(kinString)==Notation.VECTOR) {
result = ChainMaker.fromVector(getVector(kinString));
} else {
result = new Chain();
kinString = ChainMaker.standardize(kinString);
......@@ -287,8 +291,10 @@ public class ChainMaker {
int d = 0;
// result.order=0;
for (int i=0;i<kinString.length();i++) {
try {
Gender gender = Gender.valueOf(Integer.parseInt(kinString.substring(i,i+1)));
if (gender == Gender.UNKNOWN){
result.add(new Couple(k),d);
} else {
......@@ -297,7 +303,9 @@ public class ChainMaker {
// result.directions.add(d);
k = k+1;
if (d==0) d=1;
} catch (NumberFormatException nfe) {
n = n+1;
d = -(n%2);
/* if (d==0) {
......@@ -310,7 +318,7 @@ public class ChainMaker {
result.getSubchains();
}
}
//
return result;
}
......@@ -420,6 +428,25 @@ public class ChainMaker {
return result;
}
public static int getOrder(Value key){
int result;
result = 0;
if (key.isVector()){
result = key.vectorValue().order();
} else if (key.isString()) {
result = ChainMaker.getVector(key.stringValue()).order();
}
//
return result;
}
/**
* inserts a ring into this ring at ego's parent position and returns the resulting ring<p>
* ego of ring r is parent of ego of this ring
......@@ -438,7 +465,7 @@ public class ChainMaker {
if (outerChain.get(1).getGender()!=innerChain.getLast().getGender()){
innerChain = innerChain.reflect();
}
innerChain.directions.set(0,1);
innerChain.setDir(0,1);
Chain chain = concatenate(new Chain(outerChain.getFirst(),1),innerChain);
outerChain.truncate(true);
result = compose(chain,outerChain);
......@@ -616,27 +643,57 @@ public class ChainMaker {
//
return result;
}
/**
* replaces the ith character of a String by another character
*
* @param source
* the string
* @param position
* the position of the character
* @param character
* the replacing character
* @return the string with the character replaced
* @see groups.RingGroup#transform(String)
*/
private static String replaceCharAt(final String source, final int position, final char character) {
String result;
result = source.substring(0, position) + character + source.substring(position + 1);
//
return result;
}
/**
* transforms the Chain formula
* <p>
* an intermediary step in the process of Chain formula development
*
* @param s
* @param chain
* the formula to be transformed
* @return the transformed formula
* @see groups.RingGroup#RingGroup(boolean, String, int, int, int)
*/
public static String transform(String s) {
s = s.replaceAll("\\(\\)", "\\(X\\)");
int n = s.length() - 3;
public static String transform(String chain) {
String result;
result = chain;
result = result.replaceAll("\\(\\)", "\\(X\\)");
int n = result.length() - 3;
for (int i = 1; i < n; i++) {
if (s.charAt(i) != '(' || s.charAt(i - 1) == '.') {
continue;// || n==i || s.charAt(i+2)=='.') continue;
if (result.charAt(i) == '(' && result.charAt(i - 1) != '.' && result.charAt(i - 1) != 'H' && result.charAt(i - 1) != 'F') {
result = replaceCharAt(result, i + 1, 'Y');
}
s = CircuitFinder.replaceCharAt(s, i + 1, 'Y');
}
return s;
//
return result;
}
}
......@@ -630,46 +630,99 @@ public class CircuitFinder {
//
return result;
}
private List<Chain> getChainModels(String schema){
List<Chain> result = new ArrayList<Chain>();
if (schema!=null){
for (String subSchema : schema.split("\\s")) {
if (subSchema!=null && subSchema.length() > 0) {
if (subSchema.charAt(0) == '<') {
for (String partSchema : formulae(subSchema.substring(1))) {
develop(result, ChainMaker.transform(partSchema), 0);
}
} else {
develop(result, ChainMaker.transform(subSchema), 0);
}
}
}
}
//
return result;
}
/* private List<Chain> getChainModels1(String schema){
/* private List<Chain> getChainModels(String schema){
List<Chain> result = new ArrayList<Chain>();
if (schema!=null && schema.length() > 0){
if (schema.charAt(0) == '<') {
for (String form : formulae(schema.substring(1))) {
for (String s : form.split("\\s")) {
develop(result, transform(s), 0);
if (schema!=null){
for (String subSchema : schema.split("\\s")) {
if (subSchema!=null && subSchema.length() > 0) {
if (subSchema.charAt(0) == '<') {
String[] consanguineSchemas = subSchema.substring(1).split("\\.");
List<Chain>[] consanguineResults = new ArrayList[consanguineSchemas.length];
int i = 0;
for (String consanguineSchema : consanguineSchemas){
consanguineResults[i] = new ArrayList<Chain>();
for (String partSchema : formulae(consanguineSchema)) {
System.out.println("Part "+consanguineSchema+" "+partSchema);
develop(consanguineResults[i], ChainMaker.transform(partSchema), 0);
}
//
i++;
}
List<Chain> preresult = new ArrayList<Chain>();
result.addAll(consanguineResults[0]);
for (int j=1; j<consanguineSchemas.length;j++){
preresult = result;
result = new ArrayList<Chain>();
for (Chain chain1 : preresult){
for (Chain chain2 : consanguineResults[j]){
// Warning: not checked for order > 2, perhaps higher renumbering necessary
Chain chain3 = ChainMaker.concatenateWithMarriage(chain1, chain2.cloneFromId(chain1.size()+1));
result.add(chain3);
}
}
}
} else {
System.out.println("Part "+subSchema);
develop(result, ChainMaker.transform(subSchema), 0);
}
}
} else {
for (String s : schema.split("\\s")) {
develop(result, transform(s), 0);
}
}
}
//
return result;
}*/
private static ArrayList<String> formulae(String str) {
ArrayList<String> result;
......@@ -700,6 +753,7 @@ public class CircuitFinder {
}
}
//
return result;
}
......@@ -737,6 +791,9 @@ public class CircuitFinder {
result = new ArrayList<String>();
str = str.replaceAll("\\(\\)", "\\(X\\)");
char first = str.charAt(0);
char last = str.charAt(str.length()-1);
ArrayList<String> leftStrings = new ArrayList<String>();
ArrayList<String> rightStrings = new ArrayList<String>();
......@@ -750,13 +807,13 @@ public class CircuitFinder {
for (int i=0;i<left;i++){
leftString += "X";
leftStrings.add(leftString);
leftStrings.add(first+leftString.substring(1));
}
for (int i=0;i<right;i++){
rightString += "X";
rightStrings.add(rightString);
rightStrings.add(rightString.substring(0, rightString.length()-1)+last);
}
for (String leftStr : leftStrings){
......@@ -765,6 +822,10 @@ public class CircuitFinder {
String totalStr = leftStr+"(X)"+rightStr;
if (totalStr.equals("(X)") && first==last){
totalStr = "("+first+")";
}
if (!result.contains(totalStr) && !result.contains(inverse(totalStr))){
result.add(totalStr);
......@@ -830,26 +891,38 @@ public class CircuitFinder {
* @param k
*/
private void develop(List<Chain> models, final String str, final int k) {
if (ChainMaker.isWellFormed(str)) {
for (int i = k; i < str.length(); i++) {
if (str.charAt(i) == 'X') {
develop(models, fill(str, "X", "H"), i);
develop(models, fill(str, "X", "F"), i);
//
return;
}
if (str.charAt(i) == 'Y') {
if (sib != SiblingMode.ALL) {
develop(models, fill(str, "Y", "X"), k);
}
if (sib != SiblingMode.NONE) {
develop(models, fill(str, "Y", ""), k);
}
//
return;
}
}
// introduce refined homosexuality definition (inner or outer)
if (!ChainMaker.containsHomosexualMarriages(crossSex, str)) {
Chain model = ChainMaker.fromString(str);
Chain model = ChainMaker.fromString(str);
model.setSymmetry(symmetry);
models.add(model.standard());
}
......@@ -869,25 +942,7 @@ public class CircuitFinder {
}
/**
* replaces the ith character of a String by another character
*
* @param s
* the string
* @param i
* the position of the character
* @param c
* the replacing character
* @return the string with the character replaced
* @see groups.RingGroup#transform(String)
*/
public static String replaceCharAt(final String s, final int i, final char c) {
return s.substring(0, i) + c + s.substring(i + 1);
}
void begin() {
time = System.currentTimeMillis();
}
......@@ -1325,7 +1380,13 @@ public class CircuitFinder {
}
break;
case INVERTIBLE:
result = !targetDomain.contains(chain.getFirst()) && !targetDomain.contains(chain.get(1));
if (closingRelationType.equals("OPEN")){
result = !targetDomain.contains(chain.getFirst()) && !targetDomain.contains(chain.getLast());
} else {
result = !targetDomain.contains(chain.getFirst()) && !targetDomain.contains(chain.get(1));
}
break;
case INVARIABLE:
result = !targetDomain.contains(chain.getFirst());
......@@ -2673,6 +2734,8 @@ public class CircuitFinder {
public Double[] getClosureRate(Value key){
Double[] result;
int order = ChainMaker.getOrder(key);
if (!openChainFrequencies){
result = null;
} else {
......@@ -2684,17 +2747,23 @@ public class CircuitFinder {
openChainFrequency = getNrOpenChainsForKey(key);
} else {
Vector vector = key.vectorValue();
if (vector.order()==1){
if (order==1){
openChainFrequency = getNrOpenChainsForKey(key);
} else {
for (int i=0;i<2*vector.order();i++){
openChainFrequency += getNrOpenChainsForKey(new Value(vector.transform(i)));
// Set<Vector> checked = new HashSet<Vector>();
for (int i=0;i<2*order;i++){
Vector perspective = vector.transform(i);
// if (!checked.contains(perspective)){
openChainFrequency += getNrOpenChainsForKey(new Value(perspective));
// checked.add(perspective);
// }
}
}
}
result[0] = new Double(circuitFrequency);
result[1] = new Double(openChainFrequency);
result[2] = MathUtils.percent(circuitFrequency, openChainFrequency);
result[2] = MathUtils.percent(result[0], result[1]/new Double(order));
}
//
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment