Ant-file changes, some mathematics refactored.

This commit is contained in:
Marcel Kronfeld 2008-04-18 07:55:18 +00:00
parent b72d2a86b4
commit fdbfa5fe85
6 changed files with 320 additions and 325 deletions

View File

@ -535,17 +535,6 @@
</b></i></font>]]>
</header>
<!--
<footer><![CDATA[javaeva<br>
<A HREF="http://www.ra.cs.uni-tuebingen.de/forschung/"
target="_top">JavaEvA
</A><BR>
E-Mail:
<A HREF="mailto:ulmer@informatik.uni-tuebingen.de">
ulmer@informatik.uni-tuebingen.de
</A>]]>
</footer>
-->
<footer><![CDATA[eva2<br>
<A HREF="http://www.ra.cs.uni-tuebingen.de/forschung/"
target="_top">EvA2
@ -578,92 +567,92 @@
</taskdef>
<pmd shortFilenames="true" rulesetfiles="rulesets/basic.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_basic.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_basic.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_basic.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_basic.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/braces.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_braces.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_braces.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_braces.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_braces.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/codesize.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_codesize.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_codesize.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_codesize.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_codesize.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/controversial.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_controversial.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_controversial.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_controversial.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_controversial.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/coupling.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_coupling.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_coupling.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_coupling.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_coupling.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/finalizers.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_finalizers.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_finalizers.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_finalizers.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_finalizers.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/imports.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_imports.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_imports.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_imports.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_imports.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/naming.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_naming.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_naming.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_naming.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_naming.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/strictexception.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_strictexception.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_strictexception.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_strictexception.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_strictexception.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/strings.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_strings.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_strings.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_strings.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_strings.html</echo>
<pmd shortFilenames="true" rulesetfiles="rulesets/unusedcode.xml" failonerror="no">
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4JavaEVA-${DSTAMP}_unusedcode.html"/>
<formatter type="summaryhtml" toFile="${build.directory}/pmdReport4EvA2-${DSTAMP}_unusedcode.html"/>
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</pmd>
<echo>result written to ${basedir}/${build.directory}/pmdReport4JavaEVA-${DSTAMP}_unusedcode.html</echo>
<echo>result written to ${basedir}/${build.directory}/pmdReport4EvA2-${DSTAMP}_unusedcode.html</echo>
<!-- check for duplicated code -->
<echo>WARNING: use environment variable: ANT_OPTS=-Xmx512m or this task will fail.</echo>
@ -676,12 +665,12 @@
</fileset>
</classpath>
</taskdef>
<cpd format="text" minimumTokenCount="200" outputFile="${build.directory}/duplicatedCodeReport4JavaEVA-${DSTAMP}.txt">
<cpd format="text" minimumTokenCount="200" outputFile="${build.directory}/duplicatedCodeReport4EvA2-${DSTAMP}.txt">
<fileset dir="${source.directory}">
<include name="**/*.java"/>
</fileset>
</cpd>
<echo>result written to ${basedir}/${build.directory}/duplicatedCodeReport4JavaEVA-${DSTAMP}.txt</echo>
<echo>result written to ${basedir}/${build.directory}/duplicatedCodeReport4EvA2-${DSTAMP}.txt</echo>
</target>
@ -712,13 +701,13 @@ Number (CCN) maintenance
abortOnFail="false"
ccnPerFuncMax="30"
generateReport="true"
outputfile="${build.directory}/javancssReport4JavaEVA-${DSTAMP}.txt"
outputfile="${build.directory}/javancssReport4EvA2-${DSTAMP}.txt"
format="text"
packageMetrics="true"
classMetrics="false"
functionMetrics="false"
/>
<echo>result written to ${basedir}/${build.directory}/javancssReport4JavaEVA-${DSTAMP}.txt</echo>
<echo>result written to ${basedir}/${build.directory}/javancssReport4EvA2-${DSTAMP}.txt</echo>
</target>
<target name="jalopy" description="SOFTWAREDESIGN: source code formatter using the JOELib convention.">
@ -825,8 +814,8 @@ Number (CCN) maintenance
<target name="findbugs" depends="compile" description="SOFTWAREDESIGN: Run bug detector for JOELib and comfortable GUI to analyze results.">
<echo>Starts findBugs ...</echo>
<antcall target="findbugs.calculate"/>
<echo>result written to ${basedir}/${build.directory}/javaeva-fb.xml, now you can open this file with the findBugs GUI ...</echo>
<echo>open with 'File->LoadBugs' then open '${basedir}/${build.directory}/javaeva-fb.xml'.</echo>
<echo>result written to ${basedir}/${build.directory}/EvA2-fb.xml, now you can open this file with the findBugs GUI ...</echo>
<echo>open with 'File->LoadBugs' then open '${basedir}/${build.directory}/EvA2-fb.xml'.</echo>
<antcall target="findbugsGUI"/>
</target>
</project>

View File

@ -3,6 +3,7 @@ package eva2.server.go.operators.crossover;
import java.util.ArrayList;
import wsi.ra.math.RNG;
import eva2.gui.Plot;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.ESIndividualDoubleData;
@ -10,7 +11,7 @@ import eva2.server.go.individuals.InterfaceESIndividual;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import wsi.ra.math.RNG;
import eva2.tools.Mathematics;
/**
* Created by IntelliJ IDEA.
@ -67,13 +68,13 @@ public class CrossoverESPCX implements InterfaceCrossover, java.io.Serializable
System.arraycopy(((InterfaceESIndividual)result[i]).getDGenotype(), 0, children[i], 0, parents[i].length);
}
double[] g = this.getMeanVector(parents), tmpD;
double[] g = Mathematics.getMeanVector(parents), tmpD;
double w, v;
ArrayList subSpace;
// now determine the offsprings
for (int i = 0; i < children.length; i++) {
// direction vector
tmpD = this.getVectorFromTo(g, parents[i]);
tmpD = Mathematics.getVectorFromTo(g, parents[i]);
subSpace = this.getCoordinates(g, i, parents);
@ -103,7 +104,7 @@ public class CrossoverESPCX implements InterfaceCrossover, java.io.Serializable
for (int j = 1; j < subSpace.size(); j++) {
tmpD = (double[])subSpace.get(j);
w = RNG.gaussianDouble(this.m_Zeta);
children[i] = this.addVector(children[i], this.scalarMultVector(w, tmpD));
children[i] = Mathematics.vvAdd(children[i], Mathematics.scalarMultVector(w, tmpD));
}
}
@ -121,19 +122,19 @@ public class CrossoverESPCX implements InterfaceCrossover, java.io.Serializable
double[] tmpVec, toro;
double tmpD;
tmpVec = this.subVector(parents[index], mean);
tmpVec = Mathematics.subVector(parents[index], mean);
result.add(tmpVec);
for (int i = 0; i < parents.length; i++) {
if (i != index) {
tmpVec = this.subVector(parents[i], mean);
tmpVec = Mathematics.subVector(parents[i], mean);
if (this.isValidVec(tmpVec)) {
// apply the infamous Gram-Schmidt
for (int j = 0; j < result.size(); j++) {
toro = (double[]) result.get(j);
tmpD = this.multVector(toro, tmpVec)/this.multVector(toro, toro);
toro = this.scalarMultVector(tmpD, toro);
tmpVec = this.subVector(tmpVec, toro);
tmpD = Mathematics.vvMult(toro, tmpVec)/Mathematics.vvMult(toro, toro);
toro = Mathematics.scalarMultVector(tmpD, toro);
tmpVec = Mathematics.subVector(tmpVec, toro);
}
if (this.isValidVec(tmpVec)) result.add(tmpVec);
}
@ -144,78 +145,21 @@ public class CrossoverESPCX implements InterfaceCrossover, java.io.Serializable
double tmpMean;
for (int i = 1; i < result.size(); i++) {
toro = (double[]) result.get(i);
toro = this.getNormalizedVector(toro);
toro = Mathematics.getNormalizedVector(toro);
tmpMean = 0;
for (int j = 0; j < parents.length; j++) {
if (j != index) {
tmpMean += Math.abs(this.multVector(toro, this.subVector(parents[j], mean)));
tmpMean += Math.abs(Mathematics.vvMult(toro, Mathematics.subVector(parents[j], mean)));
}
}
tmpMean = tmpMean/((double)(result.size()-1));
toro = this.scalarMultVector(tmpMean, toro);
toro = Mathematics.scalarMultVector(tmpMean, toro);
result.set(i, toro);
}
return result;
}
private double[] randomVector(int n) {
double[] result = new double[n];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(1);
}
return this.getNormalizedVector(result);
}
private double[] subVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] - b[i];
}
return result;
}
private double[] addVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] + b[i];
}
return result;
}
private double multVector(double[] a, double[] b) {
double result = 0;
for (int i = 0; i < a.length; i++) {
result += a[i]*b[i];
}
return result;
}
private double[] scalarMultVector(double a, double[] b) {
double[] result = new double[b.length];
for (int i = 0; i < b.length; i++) {
result[i]= a*b[i];
}
return result;
}
private double[] getNormalizedVector(double[] d) {
double[] result = new double[d.length];
double sum = 0;
for (int i = 0; i < d.length; i++) {
result[i] = d[i];
sum += Math.pow(d[i], 2);
}
sum = Math.sqrt(sum);
for (int i = 0; i < d.length; i++) {
result[i] = result[i]/sum;
}
return result;
}
private boolean isValidVec(double[] d) {
double sum = 0;
for (int i = 0; i < d.length; i++) {
@ -227,37 +171,6 @@ public class CrossoverESPCX implements InterfaceCrossover, java.io.Serializable
return true;
}
/** This method return a vector from a to b
* @param a first vector
* @param b second vectors
* @return the vector from a to b
*/
private double[] getVectorFromTo(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < result.length; i++) {
result[i] = b[i] - a[i];
}
return result;
}
/** This method returns a mean vector from a whole number of vectors
* @param d d[i] the vectors, d[i][j] the jth coordinate of the ith vector
* @return The mean vector.
*/
private double[] getMeanVector(double[][] d) {
double[] result = new double[d[0].length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
result[j] += d[i][j];
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i]/((double)d.length);
}
return result;
}
/** This method allows you to evaluate wether two crossover operators
* are actually the same.
* @param crossover The other crossover operator

View File

@ -10,6 +10,7 @@ import eva2.server.go.individuals.InterfaceESIndividual;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.tools.Mathematics;
import wsi.ra.math.RNG;
/**
@ -39,7 +40,7 @@ public class CrossoverESSPX implements InterfaceCrossover, java.io.Serializable
}
/** This method performs crossover on two individuals. If the individuals do
* not implement InterfaceGAIndividual, then nothing will happen.
* not implement InterfaceESIndividual, then nothing will happen.
* @param indy1 The first individual
* @param partners The second individual
*/
@ -66,37 +67,27 @@ public class CrossoverESSPX implements InterfaceCrossover, java.io.Serializable
}
double r;
double[] g = this.getMeanVector(parents);
double[] g = Mathematics.getMeanVector(parents);
double[][] Y = new double[parents.length][], C;
// calculate the Y vectors
for (int i = 0; i < parents.length; i++) {
Y[i] = this.addVector(g, this.scalarMultVector(this.m_Epsilon, this.subVector(parents[i], g)));
Y[i] = Mathematics.vvAdd(g, Mathematics.scalarMultVector(this.m_Epsilon, Mathematics.subVector(parents[i], g)));
}
// Plot plot1 = new javaeva.gui.Plot("SBX Test", "x", "y", true);
// plot1.setUnconnectedPoint(-2, -2, 0);
// plot1.setUnconnectedPoint(2, 2, 0);
// for (int i = 0; i < Y.length; i++) {
// plot1.setConnectedPoint(g[0], g[1], 1);
// plot1.setConnectedPoint(Y[i][0], Y[i][1], 1);
// plot1.setConnectedPoint(g[0], g[1], 1);
// plot1.setUnconnectedPoint(parents[i][0], parents[i][1], 0);
// }
// now for each child the C vectors and the result
for (int i = 0; i < children.length; i++) {
C = new double[Y.length][];
C[0] = this.nullVector(parents[0].length);
C[0] = Mathematics.nullVector(parents[0].length);
for (int j = 1; j < Y.length; j++) {
r = Math.pow(RNG.randomDouble(0, 1), 1/((double)j));
C[j] = this.addVector(Y[j-1], C[j-1]);
C[j] = this.subVector(C[j], Y[j]);
C[j] = this.scalarMultVector(r, C[j]);
C[j] = Mathematics.vvAdd(Y[j-1], C[j-1]);
C[j] = Mathematics.subVector(C[j], Y[j]);
C[j] = Mathematics.scalarMultVector(r, C[j]);
//C[j] = this.scalarMultVector(r, this.subVector(Y[j-1], this.addVector(Y[j], C[j-1])));
}
// now the children results from
children[i] = this.addVector(Y[Y.length-1], C[C.length-1]);
children[i] = Mathematics.vvAdd(Y[Y.length-1], C[C.length-1]);
}
// write the result back
@ -108,62 +99,13 @@ public class CrossoverESSPX implements InterfaceCrossover, java.io.Serializable
return result;
}
private double[] nullVector(int n) {
double[] result = new double[n];
for (int i = 0; i < result.length; i++) {
result[i] = 0;
}
return result;
}
private double[] subVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] - b[i];
}
return result;
}
private double[] addVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] + b[i];
}
return result;
}
private double[] scalarMultVector(double a, double[] b) {
double[] result = new double[b.length];
for (int i = 0; i < b.length; i++) {
result[i]= a*b[i];
}
return result;
}
/** This method returns a mean vector from a whole number of vectors
* @param d d[i] the vectors, d[i][j] the jth coordinate of the ith vector
* @return The mean vector.
*/
private double[] getMeanVector(double[][] d) {
double[] result = new double[d[0].length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
result[j] += d[i][j];
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i]/((double)d.length);
}
return result;
}
/** This method allows you to evaluate wether two crossover operators
* are actually the same.
* @param crossover The other crossover operator
*/
public boolean equals(Object crossover) {
if (crossover instanceof CrossoverESUNDX) return true;
if (crossover instanceof CrossoverESSPX) return true;
else return false;
}

View File

@ -10,6 +10,7 @@ import eva2.server.go.individuals.InterfaceESIndividual;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.tools.Mathematics;
import wsi.ra.math.RNG;
/**
@ -41,7 +42,7 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
}
/** This method performs crossover on two individuals. If the individuals do
* not implement InterfaceGAIndividual, then nothing will happen.
* not implement InterfaceESIndividual, then nothing will happen.
* @param indy1 The first individual
* @param partners The second individual
*/
@ -71,7 +72,7 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
for (int i = 1; i < parents.length; i++) {
nParents[i-1] = parents[i];
}
double[] g = this.getMeanVector(nParents), tmpD;
double[] g = Mathematics.getMeanVector(nParents), tmpD;
double w, v;
ArrayList givenCoordinates = this.getGivenCoordinates(g, nParents);
ArrayList missingCorrdinates = this.getMissingCoordinates(g, parents[0], givenCoordinates);
@ -84,13 +85,13 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
for (int j = 0; j < givenCoordinates.size(); j++) {
tmpD = (double[])givenCoordinates.get(j);
w = RNG.gaussianDouble(this.m_Zeta);
children[i] = this.addVector(children[i], this.scalarMultVector(w, tmpD));
children[i] = Mathematics.vvAdd(children[i], Mathematics.scalarMultVector(w, tmpD));
}
// now the missing stuff
for (int j = 0; j < missingCorrdinates.size(); j++) {
tmpD = (double[])missingCorrdinates.get(j);
w = RNG.gaussianDouble(this.m_Eta);
children[i] = this.addVector(children[i], this.scalarMultVector(w, tmpD));
children[i] = Mathematics.vvAdd(children[i], Mathematics.scalarMultVector(w, tmpD));
}
}
@ -109,19 +110,19 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
double tmpD;
for (int i = 0; i < parents.length; i++) {
tmpVec = this.subVector(parents[i], mean);
if (this.isValidVec(tmpVec)) {
tmpVec = Mathematics.subVector(parents[i], mean);
if (Mathematics.isValidVec(tmpVec)) {
if (result.size() == 0) {
result.add(tmpVec);
} else {
// apply the infamous Gram-Schmidt
for (int j = 0; j < result.size(); j++) {
toro = (double[]) result.get(j);
tmpD = this.multVector(toro, tmpVec)/this.multVector(toro, toro);
toro = this.scalarMultVector(tmpD, toro);
tmpVec = this.subVector(tmpVec, toro);
tmpD = Mathematics.vvMult(toro, tmpVec)/Mathematics.vvMult(toro, toro);
toro = Mathematics.scalarMultVector(tmpD, toro);
tmpVec = Mathematics.subVector(tmpVec, toro);
}
if (this.isValidVec(tmpVec)) result.add(tmpVec);
if (Mathematics.isValidVec(tmpVec)) result.add(tmpVec);
}
}
}
@ -138,18 +139,18 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
for (int i = 0; i < given.size(); i++) completeList.add(given.get(i));
while (completeList.size() < mean.length) {
tmpVec = this.randomVector(mean.length);
if (this.isValidVec(tmpVec)) {
tmpVec = RNG.gaussianVector(mean.length, 1.);
if (Mathematics.isValidVec(tmpVec)) {
// apply the infamous Gram-Schmidt
for (int j = 0; j < completeList.size(); j++) {
toro = (double[]) completeList.get(j);
tmpD = this.multVector(toro, tmpVec)/this.multVector(toro, toro);
toro = this.scalarMultVector(tmpD, toro);
tmpVec = this.subVector(tmpVec, toro);
tmpD = Mathematics.vvMult(toro, tmpVec)/Mathematics.vvMult(toro, toro);
toro = Mathematics.scalarMultVector(tmpD, toro);
tmpVec = Mathematics.subVector(tmpVec, toro);
}
if (this.isValidVec(tmpVec)) {
tmpVec = this.getNormalizedVector(tmpVec);
tmpVec = this.scalarMultVector(this.multVector(theOther, tmpVec), tmpVec);
if (Mathematics.isValidVec(tmpVec)) {
tmpVec = Mathematics.getNormalizedVector(tmpVec);
tmpVec = Mathematics.scalarMultVector(Mathematics.vvMult(theOther, tmpVec), tmpVec);
result.add(tmpVec);
completeList.add(tmpVec);
}
@ -159,106 +160,7 @@ public class CrossoverESUNDX implements InterfaceCrossover, java.io.Serializable
return result;
}
private double[] randomVector(int n) {
double[] result = new double[n];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(1);
}
return this.getNormalizedVector(result);
}
private double[] subVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] - b[i];
}
return result;
}
private double[] addVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] + b[i];
}
return result;
}
private double multVector(double[] a, double[] b) {
double result = 0;
for (int i = 0; i < a.length; i++) {
result += a[i]*b[i];
}
return result;
}
private double[] scalarMultVector(double a, double[] b) {
double[] result = new double[b.length];
for (int i = 0; i < b.length; i++) {
result[i]= a*b[i];
}
return result;
}
private double[] getNormalizedVector(double[] d) {
double[] result = new double[d.length];
double sum = 0;
for (int i = 0; i < d.length; i++) {
result[i] = d[i];
sum += Math.pow(d[i], 2);
}
sum = Math.sqrt(sum);
for (int i = 0; i < d.length; i++) {
result[i] = result[i]/sum;
}
return result;
}
private boolean isValidVec(double[] d) {
double sum = 0;
for (int i = 0; i < d.length; i++) {
if (Double.isNaN(d[i])) return false;
sum += Math.pow(d[i],2);
}
if (Double.isNaN(sum)) return false;
if (Math.abs(sum) < 0.000000000000000001) return false;
return true;
}
/** This method return a vector from a to b
* @param a first vector
* @param b second vectors
* @return the vector from a to b
*/
private double[] getVectorFromTo(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < result.length; i++) {
result[i] = b[i] - a[i];
}
return result;
}
/** This method returns a mean vector from a whole number of vectors
* @param d d[i] the vectors, d[i][j] the jth coordinate of the ith vector
* @return The mean vector.
*/
private double[] getMeanVector(double[][] d) {
double[] result = new double[d[0].length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
result[j] += d[i][j];
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i]/((double)d.length);
}
return result;
}
/** This method allows you to evaluate wether two crossover operators
/** This method allows you to evaluate whether two crossover operators
* are actually the same.
* @param crossover The other crossover operator
*/

View File

@ -285,5 +285,237 @@ public class Mathematics {
b[i][j] /= det;
return b;
}
/** This method return a vector from a to b
* @param a first vector
* @param b second vectors
* @return the vector from a to b
*/
public static double[] getVectorFromTo(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < result.length; i++) {
result[i] = b[i] - a[i];
}
return result;
}
/** This method returns a mean vector from a whole array of vectors.
* @param d d[i] the vectors, d[i][j] the jth coordinate of the ith vector
* @return The mean vector.
*/
public static double[] getMeanVector(double[][] d) {
double[] result = new double[d[0].length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
result[j] += d[i][j];
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i]/((double)d.length);
}
return result;
}
/**
* Normalize the given vector to a length of 1 returning a deep copy.
* @param d
* @return
*/
public static double[] getNormalizedVector(double[] d) {
double[] result = new double[d.length];
System.arraycopy(d, 0, result, 0, d.length);
normalizeVector(result);
return result;
}
/**
* Normalize the given vector to a length of 1 in place.
* @param d
* @return
*/
public static void normalizeVector(double[] d) {
double sum = 0;
for (int i = 0; i < d.length; i++) {
sum += Math.pow(d[i], 2);
}
sum = Math.sqrt(sum);
for (int i = 0; i < d.length; i++) {
d[i] = d[i]/sum;
}
}
/**
* Subtract vectors returning a new vector c = a - b.
*
* @param a
* @param b
* @return a new vector c = a - b
*/
public static double[] subVector(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] - b[i];
}
return result;
}
/**
* Add vectors returning a new vector c = a + b;
* @param a
* @param b
* @return a new vector c = a + b
*/
public static double[] vvAdd(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] + b[i];
}
return result;
}
/**
* Add vectors in place setting res = v1 + v2.
*
* @param v1
* @param v2
* @return vector addition
*/
public static void vvAdd(double[] v1, double[] v2, double[] res) {
for (int i = 0; i < v1.length; i++)
res[i] = v1[i] + v2[i];
}
/**
* Add each entry of a vector with a scalar in a new vector.
*
* @param s
* @param v
* @return
*/
public static double[] svAdd (double s, double[] v) {
double[] res = new double[v.length];
for (int i = 0; i < v.length; i++) {
res[i] = v[i] + s;
}
return res;
}
/**
* Scalar product of two vectors returning sum_i (a_i * b_i).
*
* @param a
* @param b
* @return
*/
public static double vvMult(double[] a, double[] b) {
double result = 0;
for (int i = 0; i < a.length; i++) {
result += a[i]*b[i];
}
return result;
}
/**
* Multiplies (scales) every element of the array v with s returning a new vector.
*
* @param s
* a scalar
* @param v
* an array to be multiplied with s.
* @return a scaled array.
*/
public static double[] svMult(double s, double[] v) {
double[] res = new double[v.length];
for (int i = 0; i < v.length; i++) {
res[i] = v[i] * s;
}
return res;
}
/**
* Multiplies (scales) every element of the array v with s in place.
*
* @param s a scalar
* @param v an array to be multiplied with s.
* @return a scaled array.
*/
public static void svMult(double s, double[] v, double[] res) {
for (int i = 0; i < v.length; i++) {
res[i] = v[i] * s;
}
}
/**
* Return a new vector which is c = (v_i/s).
*
* @param s
* @param v
* @return
*/
public static double[] svDiv(double s, double[] v) {
double[] res = new double[v.length];
for (int i = 0; i < v.length; i++) {
res[i] = v[i] / s;
}
return res;
}
/**
* Divide by scalar in place, res_i = v_i/s.
*
* @param s
* @param v
* @return
*/
public static void svDiv(double s, double[] v, double[] res) {
for (int i = 0; i < v.length; i++) {
res[i] = v[i] / s;
}
}
/**
* Multiply a vector b with scalar a in a new vector c.
*
* @param a
* @param b
* @return
*/
public static double[] scalarMultVector(double a, double[] b) {
double[] result = new double[b.length];
for (int i = 0; i < b.length; i++) {
result[i]= a*b[i];
}
return result;
}
/**
* Return a vector of given length containing zeroes.
* @param n
* @return
*/
public static double[] nullVector(int n) {
double[] result = new double[n];
Arrays.fill(result, 0, result.length-1, 0.);
return result;
}
/**
* Returns false if a vector contains NaN, its squared sum is NaN
* or the absolute sum is smaller than 10^-18.
* @param d
* @return
*/
public static boolean isValidVec(double[] d) {
double sum = 0;
for (int i = 0; i < d.length; i++) {
if (Double.isNaN(d[i])) return false;
sum += Math.pow(d[i],2);
}
if (Double.isNaN(sum)) return false;
if (Math.abs(sum) < 0.000000000000000001) return false;
return true;
}
}

View File

@ -2,6 +2,8 @@ package wsi.ra.math;
import java.util.Random;
import eva2.tools.Mathematics;
public class RNG extends Random {
private static Random random;
private static long randomSeed;
@ -264,5 +266,20 @@ public class RNG extends Random {
v[i] += gaussianDouble(dev);
}
}
/**
* Create a random vector with gaussian random double entries.
*
* @param n
* @return
*/
public static double[] gaussianVector(int n, double dev) {
double[] result = new double[n];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(dev);
}
Mathematics.normalizeVector(result);
return result;
}
}