Merge MK 263:264: Some new functions (coming with NES tests)

This commit is contained in:
Marcel Kronfeld 2009-04-02 09:43:58 +00:00
parent ce386ba34c
commit 3f4d270524
5 changed files with 140 additions and 35 deletions

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.populations.Population; import eva2.server.go.populations.Population;
import eva2.tools.EVAERROR;
/** This abstract implementation gives some general /** This abstract implementation gives some general
* methods for retrieving and cleaning fitness values. * methods for retrieving and cleaning fitness values.
@ -70,34 +71,42 @@ public abstract class AbstractSelProb implements InterfaceSelectionProbability,
tmpList = new ArrayList(); tmpList = new ArrayList();
for (int j = 0; j < inputs.length; j++) { for (int j = 0; j < inputs.length; j++) {
obj = tmpIndy.getData(inputs[j]); obj = tmpIndy.getData(inputs[j]);
if (obj==null) EVAERROR.errorMsgOnce("Error: could not get data by key " + inputs[j] + " from individual in AbstractSelProb");
if (obj instanceof double[]) { if (obj instanceof double[]) {
for (int m = 0; m < ((double[])obj).length; m++) { for (int m = 0; m < ((double[])obj).length; m++) {
tmpList.add(new Double(((double[])obj)[m])); tmpList.add(new Double(((double[])obj)[m]));
} }
continue;
} }
if (obj instanceof Double) { if (obj instanceof Double) {
tmpList.add((Double)obj); tmpList.add((Double)obj);
continue;
} }
if (obj instanceof float[]) { if (obj instanceof float[]) {
for (int m = 0; m < ((float[])obj).length; m++) { for (int m = 0; m < ((float[])obj).length; m++) {
tmpList.add(new Double(((float[])obj)[m])); tmpList.add(new Double(((float[])obj)[m]));
} }
continue;
} }
if (obj instanceof Float) { if (obj instanceof Float) {
tmpList.add((Float)obj); tmpList.add((Float)obj);
continue;
} }
if (obj instanceof long[]) { if (obj instanceof long[]) {
for (int m = 0; m < ((long[])obj).length; m++) { for (int m = 0; m < ((long[])obj).length; m++) {
tmpList.add(new Double(((long[])obj)[m])); tmpList.add(new Double(((long[])obj)[m]));
} }
continue;
} }
if (obj instanceof Long) { if (obj instanceof Long) {
tmpList.add((Long)obj); tmpList.add((Long)obj);
continue;
} }
if (obj instanceof int[]) { if (obj instanceof int[]) {
for (int m = 0; m < ((int[])obj).length; m++) { for (int m = 0; m < ((int[])obj).length; m++) {
tmpList.add(new Double(((int[])obj)[m])); tmpList.add(new Double(((int[])obj)[m]));
} }
continue;
} }
if (obj instanceof Integer) { if (obj instanceof Integer) {
tmpList.add((Integer)obj); tmpList.add((Integer)obj);

View File

@ -402,6 +402,7 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
resultPop.setPopulationSize(resultPop.size()); resultPop.setPopulationSize(resultPop.size());
} }
resultPop = PostProcess.postProcess(ppp, resultPop, (AbstractOptimizationProblem)goParams.getProblem(), listener); resultPop = PostProcess.postProcess(ppp, resultPop, (AbstractOptimizationProblem)goParams.getProblem(), listener);
resPop = resultPop;
return resultPop; return resultPop;
} else return null; } else return null;
} }

View File

@ -35,7 +35,12 @@ public class Serializer {
static public void store(Serializable o, File f) throws IOException { static public void store(Serializable o, File f) throws IOException {
FileOutputStream file = new FileOutputStream(f); FileOutputStream file = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(file);
try {
out.writeObject(o); out.writeObject(o);
} catch (java.io.NotSerializableException e) {
System.err.println("Error: Object " + o.getClass() + " is not serializable - run settings cannot be stored.");
e.printStackTrace();
}
out.flush(); out.flush();
out.close(); out.close();
file.close(); file.close();

View File

@ -11,6 +11,7 @@ import java.util.Locale;
import wsi.ra.math.Jama.util.Maths; import wsi.ra.math.Jama.util.Maths;
import eva2.gui.BeanInspector; import eva2.gui.BeanInspector;
import eva2.tools.Mathematics;
import eva2.tools.Pair; import eva2.tools.Pair;
@ -232,6 +233,17 @@ public class Matrix implements Cloneable, java.io.Serializable {
return A; return A;
} }
/**
* Produce a matrix with the diagonal entries of the instance. All others are set to zero.
*
* @return a diagonal matrix
*/
public Matrix getDiagonalMatrix() {
double[][] D = new double[m][n];
for (int i=0; i<Math.min(m,n); i++) D[i][i]=A[i][i];
return new Matrix(D);
}
/** Copy the internal two-dimensional array. /** Copy the internal two-dimensional array.
@return Two-dimensional array copy of matrix elements. @return Two-dimensional array copy of matrix elements.
*/ */
@ -239,9 +251,10 @@ public class Matrix implements Cloneable, java.io.Serializable {
public double[][] getArrayCopy () { public double[][] getArrayCopy () {
double[][] C = new double[m][n]; double[][] C = new double[m][n];
for (int i = 0; i < m; i++) { for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) { System.arraycopy(A[i], 0, C[i], 0, n);
C[i][j] = A[i][j]; // for (int j = 0; j < n; j++) {
} // C[i][j] = A[i][j];
// }
} }
return C; return C;
} }
@ -271,6 +284,10 @@ public class Matrix implements Cloneable, java.io.Serializable {
return vals; return vals;
} }
public double[] getRowShallow(int i) {
return A[i];
}
/** Make a one-dimensional row packed copy of the internal array. /** Make a one-dimensional row packed copy of the internal array.
@return Matrix elements packed in a one-dimensional array by rows. @return Matrix elements packed in a one-dimensional array by rows.
*/ */
@ -1177,4 +1194,30 @@ public class Matrix implements Cloneable, java.io.Serializable {
} }
} }
/**
* Subtract a line from the indicated line of this matrix in place.
*
* @param rowIndex
* @param B
*/
public void rowSubtract(int rowIndex, double[] v) {
if ((v.length != n) || (rowIndex<0) || (rowIndex>=m)) throw new IllegalArgumentException("Invalid matrix dimensions for rowMinus!");
rowSubtract(rowIndex, rowIndex, v);
}
/**
* Subtract a line from each line of this matrix in place.
*
* @param rowIndex
* @param B
*/
public void rowSubtract(double[] v) {
if ((v.length != n)) throw new IllegalArgumentException("Invalid matrix dimensions for rowMinus!");
rowSubtract(0, m-1, v);
}
private void rowSubtract(int start, int end, double[] v) {
for (int i=start; i<=end; i++) Mathematics.vvSub(A[i], v, A[i]);
}
} }

View File

@ -1,8 +1,10 @@
package wsi.ra.math; package wsi.ra.math;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
import eva2.tools.EVAHELP;
import eva2.tools.Mathematics; import eva2.tools.Mathematics;
public class RNG extends Random { public class RNG extends Random {
@ -165,8 +167,9 @@ public class RNG extends Random {
public static double randomDouble(double lo,double hi) { public static double randomDouble(double lo,double hi) {
return (hi-lo)*random.nextDouble()+lo; return (hi-lo)*random.nextDouble()+lo;
} }
/** /**
* * Create a uniform random vector within the given bounds.
*/ */
public static double[] randomDoubleArray(double[] lo,double[] hi) { public static double[] randomDoubleArray(double[] lo,double[] hi) {
double[] xin = new double[lo.length]; double[] xin = new double[lo.length];
@ -174,16 +177,37 @@ public class RNG extends Random {
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i]; xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin; return xin;
} }
/** /**
* * Create a uniform random vector within the given bounds.
*/ */
public static double[] randomDoubleArray(double lo,double hi,int size) { public static double[] randomDoubleArray(double[][] range) {
double[] xin = new double[size]; double[] xin = new double[range.length];
for (int i=0;i<size;i++) for (int i=0;i<xin.length;i++)
xin[i] = (hi-lo)*random.nextDouble()+lo; xin[i] = (range[i][1]-range[i][0])*random.nextDouble()+range[i][0];
return xin; return xin;
} }
/**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension.
*
* @param lower
* @param upper
* @param size
* @return
*/
public static double[] randomDoubleArray(double lower, double upper, int size) {
double[] result = new double[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(lower, upper);
}
return result;
// double[] xin = new double[size];
// for (int i=0;i<size;i++)
// xin[i] = (hi-lo)*random.nextDouble()+lo;
// return xin;
}
/** /**
* *
*/ */
@ -193,6 +217,23 @@ public class RNG extends Random {
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i]; xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin; return xin;
} }
/**
* Create a uniform random integer vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
public static int[] randomIntArray(int lower, int upper, int size) {
int[] result = new int[size];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomInt(lower, upper);
}
return result;
}
/** /**
* *
*/ */
@ -305,17 +346,38 @@ public class RNG extends Random {
* Create a normalized random vector with gaussian random double entries. * Create a normalized random vector with gaussian random double entries.
* *
* @param n * @param n
* @param dev
* @return * @return
*/ */
public static double[] gaussianVector(int n, double dev) { public static double[] gaussianVector(int n, double dev, boolean normalize) {
double[] result = new double[n]; double[] result = new double[n];
gaussianVector(dev, result, normalize);
return result;
}
/**
* Create a normalized random vector with gaussian random double entries.
*
* @param n
* @return
*/
public static double[] gaussianVector(double dev, double[] result, boolean normalize) {
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
result[i] = RNG.gaussianDouble(dev); result[i] = RNG.gaussianDouble(dev);
} }
Mathematics.normVect(result, result); if (normalize) Mathematics.normVect(result, result);
return result; return result;
} }
public static void main(String[] args) {
double[] v = new double[2];
for (int i=0; i<1000; i++) {
gaussianVector(1., v, false);
EVAHELP.logString(Arrays.toString(v)+"\n", "randtest.dat");
// System.out.println(Arrays.toString(v));
}
}
/** /**
* Create a uniform random double vector within the given bounds (inclusive) in every dimension. * Create a uniform random double vector within the given bounds (inclusive) in every dimension.
* *
@ -324,28 +386,13 @@ public class RNG extends Random {
* @param upper * @param upper
* @return * @return
*/ */
public static double[] randomVector(int n, double lower, double upper) { // public static double[] randomVector(int n, double lower, double upper) {
double[] result = new double[n]; // double[] result = new double[n];
for (int i = 0; i < result.length; i++) { // for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(lower, upper); // result[i] = RNG.randomDouble(lower, upper);
} // }
return result; // return result;
} // }
/**
* Create a uniform random integer vector within the given bounds (inclusive) in every dimension.
*
* @param n
* @param lower
* @param upper
* @return
*/
public static int[] randomVector(int n, int lower, int upper) {
int[] result = new int[n];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomInt(lower, upper);
}
return result;
}
} }