Move all AbstractEAComparators to actually use that as base object
This commit is contained in:
parent
8cecc16fea
commit
80afb485b8
@ -19,7 +19,7 @@ import java.util.Comparator;
|
|||||||
* @see AbstractEAIndividual#isDominatingFitness(double[], double[])
|
* @see AbstractEAIndividual#isDominatingFitness(double[], double[])
|
||||||
*/
|
*/
|
||||||
@eva2.util.annotation.Description(value = "A comparator class for general EA individuals. Compares individuals based on their fitness in context of minimization.")
|
@eva2.util.annotation.Description(value = "A comparator class for general EA individuals. Compares individuals based on their fitness in context of minimization.")
|
||||||
public class EAIndividualComparator implements Comparator<Object>, Serializable {
|
public class EAIndividualComparator implements Comparator<AbstractEAIndividual>, Serializable {
|
||||||
// flag whether a data field should be used.
|
// flag whether a data field should be used.
|
||||||
private String indyDataKey = "";
|
private String indyDataKey = "";
|
||||||
private int fitCriterion = -1;
|
private int fitCriterion = -1;
|
||||||
@ -122,7 +122,7 @@ public class EAIndividualComparator implements Comparator<Object>, Serializable
|
|||||||
* @return -1 if the first is dominant, 1 if the second is dominant, otherwise 0
|
* @return -1 if the first is dominant, 1 if the second is dominant, otherwise 0
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(AbstractEAIndividual o1, AbstractEAIndividual o2) {
|
||||||
boolean o1domO2, o2domO1;
|
boolean o1domO2, o2domO1;
|
||||||
|
|
||||||
if (preferFeasible) { // check constraint violation first?
|
if (preferFeasible) { // check constraint violation first?
|
||||||
|
@ -9,7 +9,7 @@ import java.util.Comparator;
|
|||||||
* Compare two AbstractEAIndividuals by their distance to a reference individual.
|
* Compare two AbstractEAIndividuals by their distance to a reference individual.
|
||||||
* Usable to sort by a distance.
|
* Usable to sort by a distance.
|
||||||
*/
|
*/
|
||||||
public class IndividualDistanceComparator implements Comparator<Object>, Serializable {
|
public class IndividualDistanceComparator implements Comparator<AbstractEAIndividual>, Serializable {
|
||||||
|
|
||||||
private AbstractEAIndividual refIndy = null;
|
private AbstractEAIndividual refIndy = null;
|
||||||
private InterfaceDistanceMetric distMetric = null;
|
private InterfaceDistanceMetric distMetric = null;
|
||||||
@ -30,9 +30,9 @@ public class IndividualDistanceComparator implements Comparator<Object>, Seriali
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(AbstractEAIndividual o1, AbstractEAIndividual o2) {
|
||||||
double d1 = distMetric.distance((AbstractEAIndividual) o1, refIndy);
|
double d1 = distMetric.distance(o1, refIndy);
|
||||||
double d2 = distMetric.distance((AbstractEAIndividual) o2, refIndy);
|
double d2 = distMetric.distance(o2, refIndy);
|
||||||
|
|
||||||
if (d1 == d2) {
|
if (d1 == d2) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -10,7 +10,7 @@ import java.util.Comparator;
|
|||||||
*
|
*
|
||||||
* @author mkron
|
* @author mkron
|
||||||
*/
|
*/
|
||||||
public class IndividualWeightedFitnessComparator implements Comparator<Object>, Serializable {
|
public class IndividualWeightedFitnessComparator implements Comparator<AbstractEAIndividual>, Serializable {
|
||||||
/**
|
/**
|
||||||
* Generated serial version identifier
|
* Generated serial version identifier
|
||||||
*/
|
*/
|
||||||
@ -75,9 +75,9 @@ public class IndividualWeightedFitnessComparator implements Comparator<Object>,
|
|||||||
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compare(Object o1, Object o2) {
|
public int compare(AbstractEAIndividual o1, AbstractEAIndividual o2) {
|
||||||
double[] f1 = ((AbstractEAIndividual) o1).getFitness();
|
double[] f1 = o1.getFitness();
|
||||||
double[] f2 = ((AbstractEAIndividual) o2).getFitness();
|
double[] f2 = o2.getFitness();
|
||||||
|
|
||||||
double score1 = calcScore(f1);
|
double score1 = calcScore(f1);
|
||||||
double score2 = calcScore(f2);
|
double score2 = calcScore(f2);
|
||||||
|
@ -52,7 +52,7 @@ public class ArchivingNSGAIISMeasure extends ArchivingNSGAII {
|
|||||||
|
|
||||||
public void calculateCrowdingDistance(Population front) {
|
public void calculateCrowdingDistance(Population front) {
|
||||||
|
|
||||||
Object[] frontArray = front.toArray();
|
AbstractEAIndividual[] frontArray = front.toArray(new AbstractEAIndividual[front.size()]);
|
||||||
boolean[] assigned = new boolean[frontArray.length];
|
boolean[] assigned = new boolean[frontArray.length];
|
||||||
|
|
||||||
double[] v = new double[frontArray.length];
|
double[] v = new double[frontArray.length];
|
||||||
@ -68,8 +68,8 @@ public class ArchivingNSGAIISMeasure extends ArchivingNSGAII {
|
|||||||
Arrays.sort(frontArray, new EAIndividualComparator(0));
|
Arrays.sort(frontArray, new EAIndividualComparator(0));
|
||||||
|
|
||||||
|
|
||||||
((AbstractEAIndividual) frontArray[0]).putData("HyperCube", Double.MAX_VALUE); //die beiden aussen bekommen maximal wert als measure
|
frontArray[0].putData("HyperCube", Double.MAX_VALUE); //die beiden aussen bekommen maximal wert als measure
|
||||||
((AbstractEAIndividual) frontArray[frontArray.length - 1]).putData("HyperCube", Double.MAX_VALUE);
|
frontArray[frontArray.length - 1].putData("HyperCube", Double.MAX_VALUE);
|
||||||
v[0] = Double.MAX_VALUE;
|
v[0] = Double.MAX_VALUE;
|
||||||
v[frontArray.length - 1] = Double.MAX_VALUE;
|
v[frontArray.length - 1] = Double.MAX_VALUE;
|
||||||
|
|
||||||
|
@ -1038,7 +1038,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
* @param comparator indicate whether constraints should be regarded
|
* @param comparator indicate whether constraints should be regarded
|
||||||
* @return The index of the best (worst) individual.
|
* @return The index of the best (worst) individual.
|
||||||
*/
|
*/
|
||||||
public int getIndexOfBestOrWorstIndividual(boolean bBest, Comparator<Object> comparator) {
|
public int getIndexOfBestOrWorstIndividual(boolean bBest, Comparator<AbstractEAIndividual> comparator) {
|
||||||
ArrayList<?> sorted = getSorted(comparator);
|
ArrayList<?> sorted = getSorted(comparator);
|
||||||
if (bBest) {
|
if (bBest) {
|
||||||
return indexOf(sorted.get(0));
|
return indexOf(sorted.get(0));
|
||||||
@ -1051,7 +1051,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
return getIndexOfBestOrWorstIndividual(true, comparator);
|
return getIndexOfBestOrWorstIndividual(true, comparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractEAIndividual getBestEAIndividual(Comparator<Object> comparator) {
|
public AbstractEAIndividual getBestEAIndividual(Comparator<AbstractEAIndividual> comparator) {
|
||||||
int index = getIndexOfBestOrWorstIndividual(true, comparator);
|
int index = getIndexOfBestOrWorstIndividual(true, comparator);
|
||||||
return getEAIndividual(index);
|
return getEAIndividual(index);
|
||||||
}
|
}
|
||||||
@ -1199,7 +1199,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
* fitness first
|
* fitness first
|
||||||
* @see #getSortedNIndividuals(int, boolean, Population, Comparator)
|
* @see #getSortedNIndividuals(int, boolean, Population, Comparator)
|
||||||
*/
|
*/
|
||||||
public Population getSortedBestFirst(Comparator<Object> comp) {
|
public Population getSortedBestFirst(Comparator<AbstractEAIndividual> comp) {
|
||||||
Population result = this.cloneWithoutInds();
|
Population result = this.cloneWithoutInds();
|
||||||
getSortedNIndividuals(size(), true, result, comp);
|
getSortedNIndividuals(size(), true, result, comp);
|
||||||
result.synchSize();
|
result.synchSize();
|
||||||
@ -1219,7 +1219,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
* @param comp the Comparator to use with individuals
|
* @param comp the Comparator to use with individuals
|
||||||
* @param res The m sorted best or worst individuals, where m <= n (will be added to res)
|
* @param res The m sorted best or worst individuals, where m <= n (will be added to res)
|
||||||
*/
|
*/
|
||||||
public void getSortedNIndividuals(int n, boolean bBestOrWorst, Population res, Comparator<Object> comp) {
|
public void getSortedNIndividuals(int n, boolean bBestOrWorst, Population res, Comparator<AbstractEAIndividual> comp) {
|
||||||
if ((n < 0) || (n > super.size())) {
|
if ((n < 0) || (n > super.size())) {
|
||||||
// this may happen, treat it gracefully
|
// this may happen, treat it gracefully
|
||||||
//System.err.println("invalid request to getSortedNIndividuals: n="+n + ", size is " + super.size());
|
//System.err.println("invalid request to getSortedNIndividuals: n="+n + ", size is " + super.size());
|
||||||
@ -1291,7 +1291,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
* on AbstractEAIndividual instances.
|
* on AbstractEAIndividual instances.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected ArrayList<AbstractEAIndividual> sortBy(Comparator<Object> comp) {
|
protected ArrayList<AbstractEAIndividual> sortBy(Comparator<AbstractEAIndividual> comp) {
|
||||||
if (super.isEmpty()) {
|
if (super.isEmpty()) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@ -1320,7 +1320,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
* @param comp The comparator
|
* @param comp The comparator
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public ArrayList<AbstractEAIndividual> getSorted(Comparator<Object> comp) {
|
public ArrayList<AbstractEAIndividual> getSorted(Comparator<AbstractEAIndividual> comp) {
|
||||||
if (!comp.equals(lastSortingComparator) || (sortedArr == null) || (super.modCount != lastQModCount)) {
|
if (!comp.equals(lastSortingComparator) || (sortedArr == null) || (super.modCount != lastQModCount)) {
|
||||||
ArrayList<AbstractEAIndividual> sArr = sortBy(comp);
|
ArrayList<AbstractEAIndividual> sArr = sortBy(comp);
|
||||||
if (sortedArr == null) {
|
if (sortedArr == null) {
|
||||||
@ -1340,7 +1340,7 @@ public class Population extends ArrayList<AbstractEAIndividual> implements Popul
|
|||||||
*
|
*
|
||||||
* @see #getSorted(java.util.Comparator)
|
* @see #getSorted(java.util.Comparator)
|
||||||
*/
|
*/
|
||||||
public Population getSortedPop(Comparator<Object> comp) {
|
public Population getSortedPop(Comparator<AbstractEAIndividual> comp) {
|
||||||
Population pop = this.cloneWithoutInds();
|
Population pop = this.cloneWithoutInds();
|
||||||
ArrayList<AbstractEAIndividual> sortedIndies = getSorted(comp);
|
ArrayList<AbstractEAIndividual> sortedIndies = getSorted(comp);
|
||||||
pop.addAll(sortedIndies);
|
pop.addAll(sortedIndies);
|
||||||
|
@ -44,7 +44,7 @@ import java.util.Vector;
|
|||||||
public class ParticleSwarmOptimization extends AbstractOptimizer implements java.io.Serializable, InterfaceAdditionalPopulationInformer {
|
public class ParticleSwarmOptimization extends AbstractOptimizer implements java.io.Serializable, InterfaceAdditionalPopulationInformer {
|
||||||
|
|
||||||
public enum PSOType { Inertness, Constriction }
|
public enum PSOType { Inertness, Constriction }
|
||||||
Object[] sortedPop = null;
|
AbstractEAIndividual[] sortedPop = null;
|
||||||
protected AbstractEAIndividual bestIndividual = null;
|
protected AbstractEAIndividual bestIndividual = null;
|
||||||
protected boolean checkRange = true;
|
protected boolean checkRange = true;
|
||||||
protected boolean checkSpeedLimit = false;
|
protected boolean checkSpeedLimit = false;
|
||||||
@ -1299,7 +1299,7 @@ public class ParticleSwarmOptimization extends AbstractOptimizer implements java
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((topology == PSOTopology.multiSwarm) || (topology == PSOTopology.tree)) {
|
if ((topology == PSOTopology.multiSwarm) || (topology == PSOTopology.tree)) {
|
||||||
sortedPop = pop.toArray();
|
sortedPop = pop.toArray(new AbstractEAIndividual[pop.size()]);
|
||||||
if ((topology == PSOTopology.multiSwarm) || (treeStruct >= 2)) {
|
if ((topology == PSOTopology.multiSwarm) || (treeStruct >= 2)) {
|
||||||
Arrays.sort(sortedPop, new EAIndividualComparator());
|
Arrays.sort(sortedPop, new EAIndividualComparator());
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user