Simplify code

This commit is contained in:
Fabian Becker 2015-12-31 18:33:52 +01:00
parent c00c075c18
commit ed563d485f
4 changed files with 24 additions and 24 deletions

View File

@ -126,7 +126,7 @@ public class EAIndividualComparator implements Comparator<AbstractEAIndividual>,
boolean o1domO2, o2domO1; boolean o1domO2, o2domO1;
if (preferFeasible) { // check constraint violation first? if (preferFeasible) { // check constraint violation first?
int constrViolComp = ((AbstractEAIndividual) o1).compareConstraintViolation((AbstractEAIndividual) o2); int constrViolComp = o1.compareConstraintViolation(o2);
if (constrViolComp > 0) { if (constrViolComp > 0) {
return -1; return -1;
} else if (constrViolComp < 0) { } else if (constrViolComp < 0) {
@ -135,8 +135,8 @@ public class EAIndividualComparator implements Comparator<AbstractEAIndividual>,
// otherwise both do not violate, so regard fitness // otherwise both do not violate, so regard fitness
} }
if (indyDataKey != null && (indyDataKey.length() > 0)) { // check specific key if (indyDataKey != null && (indyDataKey.length() > 0)) { // check specific key
double[] fit1 = (double[]) ((AbstractEAIndividual) o1).getData(indyDataKey); double[] fit1 = (double[]) o1.getData(indyDataKey);
double[] fit2 = (double[]) ((AbstractEAIndividual) o2).getData(indyDataKey); double[] fit2 = (double[]) o2.getData(indyDataKey);
if ((fit1 == null) || (fit2 == null)) { if ((fit1 == null) || (fit2 == null)) {
throw new RuntimeException("Unknown individual data key " + indyDataKey + ", unable to compare individuals (" + this.getClass().getSimpleName() + ")"); throw new RuntimeException("Unknown individual data key " + indyDataKey + ", unable to compare individuals (" + this.getClass().getSimpleName() + ")");
} }
@ -152,13 +152,13 @@ public class EAIndividualComparator implements Comparator<AbstractEAIndividual>,
} }
} else { } else {
if (fitCriterion < 0) { if (fitCriterion < 0) {
o1domO2 = ((AbstractEAIndividual) o1).isDominating((AbstractEAIndividual) o2); o1domO2 = o1.isDominating(o2);
o2domO1 = ((AbstractEAIndividual) o2).isDominating((AbstractEAIndividual) o1); o2domO1 = o2.isDominating(o1);
} else { } else {
if (((AbstractEAIndividual) o1).getFitness()[fitCriterion] == ((AbstractEAIndividual) o2).getFitness()[fitCriterion]) { if (o1.getFitness()[fitCriterion] == o2.getFitness()[fitCriterion]) {
return 0; return 0;
} }
return (((AbstractEAIndividual) o1).getFitness()[fitCriterion] < ((AbstractEAIndividual) o2).getFitness()[fitCriterion]) ? -1 : 1; return (o1.getFitness()[fitCriterion] < o2.getFitness()[fitCriterion]) ? -1 : 1;
} }
} }
if (o1domO2 ^ o2domO1) { if (o1domO2 ^ o2domO1) {

View File

@ -49,7 +49,7 @@ public class ESIndividualDoubleData extends AbstractEAIndividual implements Inte
this.genotype[i] = individual.genotype[i]; this.genotype[i] = individual.genotype[i];
this.range[i][0] = individual.range[i][0]; this.range[i][0] = individual.range[i][0];
this.range[i][1] = individual.range[i][1]; this.range[i][1] = individual.range[i][1];
}; }
// cloning the members of AbstractEAIndividual // cloning the members of AbstractEAIndividual
this.age = individual.age; this.age = individual.age;

View File

@ -81,8 +81,8 @@ public class ArchivingNSGAIISMeasure extends ArchivingNSGAII {
// determine right not assigned neighbor // determine right not assigned neighbor
for (right = i + 1; (assigned[right]); right++) ; for (right = i + 1; (assigned[right]); right++) ;
v[i] = (((AbstractEAIndividual) frontArray[right]).getFitness(0) - ((AbstractEAIndividual) frontArray[i]).getFitness(0)) * v[i] = (frontArray[right].getFitness(0) - frontArray[i].getFitness(0)) *
(((AbstractEAIndividual) frontArray[left]).getFitness(1) - ((AbstractEAIndividual) frontArray[i]).getFitness(1)); (frontArray[left].getFitness(1) - frontArray[i].getFitness(1));
left = i; left = i;
i = right; i = right;
@ -99,7 +99,7 @@ public class ArchivingNSGAIISMeasure extends ArchivingNSGAII {
} }
} }
assigned[minIndex] = true; assigned[minIndex] = true;
((AbstractEAIndividual) frontArray[minIndex]).putData("HyperCube", (double) e); frontArray[minIndex].putData("HyperCube", (double) e);
} }

View File

@ -919,24 +919,24 @@ public class ParticleSwarmOptimization extends AbstractOptimizer implements java
} }
break; break;
case tree: // Sorted Tree case tree: // Sorted Tree
sortedIndex = (Integer) ((AbstractEAIndividual) sortedPop[index]).getData(sortedIndexKey); sortedIndex = (Integer) sortedPop[index].getData(sortedIndexKey);
if (sortedIndex > 0) { // its found and its not the root. root has no parent to check for if (sortedIndex > 0) { // its found and its not the root. root has no parent to check for
k = getParentIndex(topologyRange, sortedIndex, pop.size()); k = getParentIndex(topologyRange, sortedIndex, pop.size());
compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual) sortedPop[k], useHistoric); compareAndSetAttractor(localBestFitness, localBestPosition, sortedPop[k], useHistoric);
} }
if (treeStruct == 1) { // loop all children if (treeStruct == 1) { // loop all children
if (isComplete(sortedIndex, pop.size())) { // the node has full degree if (isComplete(sortedIndex, pop.size())) { // the node has full degree
k = topologyRange * sortedIndex + 1; // this is the offset of the nodes children k = topologyRange * sortedIndex + 1; // this is the offset of the nodes children
for (int i = 0; i < topologyRange; i++) { for (int i = 0; i < topologyRange; i++) {
compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual) sortedPop[k + i], useHistoric); compareAndSetAttractor(localBestFitness, localBestPosition, sortedPop[k + i], useHistoric);
} }
} else if (isIncomplete(sortedIndex, pop.size())) { // the node does not have full degree but might have orphans } else if (isIncomplete(sortedIndex, pop.size())) { // the node does not have full degree but might have orphans
int numOrphs = numOrphans(sortedIndex, pop.size()); int numOrphs = numOrphans(sortedIndex, pop.size());
if (numOrphs > 0) { if (numOrphs > 0) {
k = indexOfFirstOrphan(sortedIndex, pop.size()); k = indexOfFirstOrphan(sortedIndex, pop.size());
for (int i = 0; i < numOrphs; i++) { for (int i = 0; i < numOrphs; i++) {
compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual) sortedPop[k], useHistoric); compareAndSetAttractor(localBestFitness, localBestPosition, sortedPop[k], useHistoric);
k += treeLastFullLevelNodeCnt; // hop to next (possible) orphan index k += treeLastFullLevelNodeCnt; // hop to next (possible) orphan index
} }
} }
@ -1318,7 +1318,7 @@ public class ParticleSwarmOptimization extends AbstractOptimizer implements java
found = false; found = false;
superfluous = false; superfluous = false;
for (int i = 0; i < leaders.size(); i++) { for (int i = 0; i < leaders.size(); i++) {
dist = metric.distance((AbstractEAIndividual) sortedPop[cur], leaders.get(i)); dist = metric.distance(sortedPop[cur], leaders.get(i));
//System.out.println("dist is "+dist); //System.out.println("dist is "+dist);
if ((swarmRadius * 2.) > dist) { // a formal leader is found if ((swarmRadius * 2.) > dist) { // a formal leader is found
int sSize = (Integer) (leaders.get(i)).getData(multiSwSizeKey); int sSize = (Integer) (leaders.get(i)).getData(multiSwSizeKey);
@ -1330,22 +1330,22 @@ public class ParticleSwarmOptimization extends AbstractOptimizer implements java
} else { } else {
found = true; found = true;
// assign to leader, update swarm size // assign to leader, update swarm size
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwTypeKey, leaders.get(i)); sortedPop[cur].putData(multiSwTypeKey, leaders.get(i));
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwSizeKey, -1); sortedPop[cur].putData(multiSwSizeKey, -1);
leaders.get(i).putData(multiSwSizeKey, 1 + sSize); leaders.get(i).putData(multiSwSizeKey, 1 + sSize);
break; break;
} }
} }
} }
if (!found) { // new leader is found if (!found) { // new leader is found
leaders.add(((AbstractEAIndividual) sortedPop[cur])); leaders.add(sortedPop[cur]);
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwTypeKey, sortedPop[cur]); sortedPop[cur].putData(multiSwTypeKey, sortedPop[cur]);
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwSizeKey, 1); sortedPop[cur].putData(multiSwSizeKey, 1);
} else if (superfluous) { } else if (superfluous) {
//System.out.println("reinitializing " + cur); //System.out.println("reinitializing " + cur);
((AbstractEAIndividual) sortedPop[cur]).putData(partTypeKey, resetType); sortedPop[cur].putData(partTypeKey, resetType);
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwTypeKey, sortedPop[cur]); sortedPop[cur].putData(multiSwTypeKey, sortedPop[cur]);
((AbstractEAIndividual) sortedPop[cur]).putData(multiSwSizeKey, 1); sortedPop[cur].putData(multiSwSizeKey, 1);
} }
cur++; cur++;
} }