Deduplicate norm() method and use the one provided by Mathematics
This commit is contained in:
parent
6ea7dc46f0
commit
b2c0ae9488
@ -4,6 +4,7 @@ import eva2.optimization.individuals.AbstractEAIndividual;
|
||||
import eva2.tools.EVAERROR;
|
||||
import eva2.tools.math.Mathematics;
|
||||
import eva2.util.annotation.Description;
|
||||
import eva2.util.annotation.Parameter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@ -19,8 +20,7 @@ import java.io.Serializable;
|
||||
public class DoubleIntegralMetric implements InterfaceDistanceMetric, Serializable {
|
||||
boolean oneNormed = true; // if true, the vectors are normed to unity sum before comparison.
|
||||
|
||||
public DoubleIntegralMetric() {
|
||||
}
|
||||
public DoubleIntegralMetric() {}
|
||||
|
||||
public DoubleIntegralMetric(boolean normed) {
|
||||
oneNormed = normed;
|
||||
@ -35,10 +35,8 @@ public class DoubleIntegralMetric implements InterfaceDistanceMetric, Serializab
|
||||
public double distance(AbstractEAIndividual indy1, AbstractEAIndividual indy2) {
|
||||
double[] dIndy1 = null, dIndy2 = null;
|
||||
|
||||
if (dIndy1 == null || dIndy2 == null) {
|
||||
dIndy1 = AbstractEAIndividual.getDoublePositionShallow(indy1);
|
||||
dIndy2 = AbstractEAIndividual.getDoublePositionShallow(indy2);
|
||||
}
|
||||
dIndy1 = AbstractEAIndividual.getDoublePositionShallow(indy1);
|
||||
dIndy2 = AbstractEAIndividual.getDoublePositionShallow(indy2);
|
||||
|
||||
if (oneNormed) {
|
||||
double l1 = Mathematics.sum(dIndy1);
|
||||
@ -53,9 +51,8 @@ public class DoubleIntegralMetric implements InterfaceDistanceMetric, Serializab
|
||||
}
|
||||
|
||||
double sum1 = 0, sum2 = 0;
|
||||
double tmpDiff = 0, sqrDiffSum = 0;
|
||||
// System.out.println(Mathematics.sum(dIndy1));
|
||||
// System.out.println(Mathematics.sum(dIndy2));
|
||||
double tmpDiff, sqrDiffSum = 0;
|
||||
|
||||
for (int i = 0; (i < dIndy1.length) && (i < dIndy2.length); i++) {
|
||||
sum1 += dIndy1[i];
|
||||
sum2 += dIndy2[i];
|
||||
@ -73,12 +70,8 @@ public class DoubleIntegralMetric implements InterfaceDistanceMetric, Serializab
|
||||
return oneNormed;
|
||||
}
|
||||
|
||||
@Parameter(description = "If true, both vectors are normed to unity sum before comparison.")
|
||||
public void setOneNormed(boolean normedByLength) {
|
||||
this.oneNormed = normedByLength;
|
||||
}
|
||||
|
||||
public String oneNormedTipText() {
|
||||
return "If true, both vectors are normed to unity sum before comparison.";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package eva2.optimization.operator.distancemetric;
|
||||
|
||||
import eva2.gui.BeanInspector;
|
||||
import eva2.optimization.individuals.*;
|
||||
import eva2.tools.math.Mathematics;
|
||||
import eva2.util.annotation.Description;
|
||||
|
||||
import java.util.BitSet;
|
||||
@ -130,7 +131,6 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
|
||||
if ((indy1 instanceof InterfaceDataTypePermutation) && (indy2 instanceof InterfaceDataTypePermutation)) {
|
||||
int[] dIndy1, dIndy2;
|
||||
String s1 = "", s2 = "";
|
||||
// double tmpResult = 0;
|
||||
for (int p = 0; p < ((InterfaceDataTypePermutation) indy1).getPermutationData().length; p++) {
|
||||
dIndy1 = ((InterfaceDataTypePermutation) indy1).getPermutationData()[p];
|
||||
dIndy2 = ((InterfaceDataTypePermutation) indy2).getPermutationData()[p];
|
||||
@ -192,7 +192,7 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
|
||||
return result / d1.length;
|
||||
}
|
||||
if (indy instanceof InterfaceDataTypeDouble) {
|
||||
result = norm(((InterfaceDataTypeDouble) indy).getDoubleData());
|
||||
result = Mathematics.norm(((InterfaceDataTypeDouble) indy).getDoubleData());
|
||||
return result;
|
||||
}
|
||||
if (indy instanceof InterfaceDataTypePermutation) {
|
||||
@ -208,20 +208,6 @@ public class PhenotypeMetric implements InterfaceDistanceMetric, java.io.Seriali
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the 2 norm of a given vector.
|
||||
*
|
||||
* @param v1
|
||||
* @return
|
||||
*/
|
||||
public static double norm(double[] v1) {
|
||||
double result = 0;
|
||||
for (int i = 0; i < v1.length; i++) {
|
||||
result += Math.pow(v1[i], 2);
|
||||
}
|
||||
return Math.sqrt(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will return a naming String
|
||||
*
|
||||
|
@ -15,15 +15,11 @@ import eva2.util.annotation.Description;
|
||||
@Description("This is an experimental method for individuals using global ES mutation.")
|
||||
public class SigmaSingleMetricGlobalMutation implements InterfaceDistanceMetric, java.io.Serializable {
|
||||
|
||||
public SigmaSingleMetricGlobalMutation() {
|
||||
}
|
||||
|
||||
public SigmaSingleMetricGlobalMutation(SigmaSingleMetricGlobalMutation a) {
|
||||
}
|
||||
public SigmaSingleMetricGlobalMutation() {}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
return new SigmaSingleMetricGlobalMutation(this);
|
||||
return new SigmaSingleMetricGlobalMutation();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -248,9 +248,9 @@ public class PostProcess {
|
||||
Population result = new Population();
|
||||
for (int i = 0; i < pop.size(); i++) {
|
||||
indy = pop.getEAIndividual(i);
|
||||
if (bSmallerEq && (PhenotypeMetric.norm(indy.getFitness()) <= fitNorm)) {
|
||||
if (bSmallerEq && (Mathematics.norm(indy.getFitness()) <= fitNorm)) {
|
||||
result.add(indy);
|
||||
} else if (!bSmallerEq && (PhenotypeMetric.norm(indy.getFitness()) > fitNorm)) {
|
||||
} else if (!bSmallerEq && (Mathematics.norm(indy.getFitness()) > fitNorm)) {
|
||||
result.add(indy);
|
||||
}
|
||||
}
|
||||
@ -279,7 +279,7 @@ public class PostProcess {
|
||||
if ((crit >= 0) && (crit < indy.getFitness().length)) {
|
||||
curFit = indy.getFitness(crit);
|
||||
} else {
|
||||
curFit = PhenotypeMetric.norm(indy.getFitness());
|
||||
curFit = Mathematics.norm(indy.getFitness());
|
||||
}
|
||||
|
||||
if (bSmallerEq && (curFit <= fitThresh)) {
|
||||
@ -1058,7 +1058,7 @@ public class PostProcess {
|
||||
plot = draw("After " + stepsDone + " steps (" + params.getPPMethod() + ")" + ((params.getPostProcessClusterSigma() > 0) ? " and second clustering" : ""), null, outputPop, null, problem);
|
||||
}
|
||||
// ##### some statistics
|
||||
double upBnd = PhenotypeMetric.norm(outputPop.getWorstEAIndividual().getFitness()) * 1.1;
|
||||
double upBnd = Mathematics.norm(outputPop.getWorstEAIndividual().getFitness()) * 1.1;
|
||||
upBnd = Math.pow(10, Math.floor(Math.log10(upBnd) + 1));
|
||||
double lowBnd = 0;
|
||||
int fitCrit = 0; // use first fitness criterion
|
||||
|
Loading…
x
Reference in New Issue
Block a user