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.populations.Population;
import eva2.tools.EVAERROR;
/** This abstract implementation gives some general
* methods for retrieving and cleaning fitness values.
@ -70,34 +71,42 @@ public abstract class AbstractSelProb implements InterfaceSelectionProbability,
tmpList = new ArrayList();
for (int j = 0; j < inputs.length; 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[]) {
for (int m = 0; m < ((double[])obj).length; m++) {
tmpList.add(new Double(((double[])obj)[m]));
}
continue;
}
if (obj instanceof Double) {
tmpList.add((Double)obj);
continue;
}
if (obj instanceof float[]) {
for (int m = 0; m < ((float[])obj).length; m++) {
tmpList.add(new Double(((float[])obj)[m]));
}
continue;
}
if (obj instanceof Float) {
tmpList.add((Float)obj);
continue;
}
if (obj instanceof long[]) {
for (int m = 0; m < ((long[])obj).length; m++) {
tmpList.add(new Double(((long[])obj)[m]));
}
continue;
}
if (obj instanceof Long) {
tmpList.add((Long)obj);
continue;
}
if (obj instanceof int[]) {
for (int m = 0; m < ((int[])obj).length; m++) {
tmpList.add(new Double(((int[])obj)[m]));
}
continue;
}
if (obj instanceof Integer) {
tmpList.add((Integer)obj);

View File

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

View File

@ -35,7 +35,12 @@ public class Serializer {
static public void store(Serializable o, File f) throws IOException {
FileOutputStream file = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(o);
try {
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.close();
file.close();

View File

@ -11,6 +11,7 @@ import java.util.Locale;
import wsi.ra.math.Jama.util.Maths;
import eva2.gui.BeanInspector;
import eva2.tools.Mathematics;
import eva2.tools.Pair;
@ -232,6 +233,17 @@ public class Matrix implements Cloneable, java.io.Serializable {
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.
@return Two-dimensional array copy of matrix elements.
*/
@ -239,9 +251,10 @@ public class Matrix implements Cloneable, java.io.Serializable {
public double[][] getArrayCopy () {
double[][] C = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
System.arraycopy(A[i], 0, C[i], 0, n);
// for (int j = 0; j < n; j++) {
// C[i][j] = A[i][j];
// }
}
return C;
}
@ -271,6 +284,10 @@ public class Matrix implements Cloneable, java.io.Serializable {
return vals;
}
public double[] getRowShallow(int i) {
return A[i];
}
/** Make a one-dimensional row packed copy of the internal array.
@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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import eva2.tools.EVAHELP;
import eva2.tools.Mathematics;
public class RNG extends Random {
@ -165,8 +167,9 @@ public class RNG extends Random {
public static double randomDouble(double lo,double hi) {
return (hi-lo)*random.nextDouble()+lo;
}
/**
*
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double[] lo,double[] hi) {
double[] xin = new double[lo.length];
@ -174,14 +177,35 @@ public class RNG extends Random {
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i];
return xin;
}
/**
*
* Create a uniform random vector within the given bounds.
*/
public static double[] randomDoubleArray(double lo,double hi,int size) {
double[] xin = new double[size];
for (int i=0;i<size;i++)
xin[i] = (hi-lo)*random.nextDouble()+lo;
return xin;
public static double[] randomDoubleArray(double[][] range) {
double[] xin = new double[range.length];
for (int i=0;i<xin.length;i++)
xin[i] = (range[i][1]-range[i][0])*random.nextDouble()+range[i][0];
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];
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.
*
* @param n
* @param dev
* @return
*/
public static double[] gaussianVector(int n, double dev) {
public static double[] gaussianVector(int n, double dev, boolean normalize) {
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++) {
result[i] = RNG.gaussianDouble(dev);
}
Mathematics.normVect(result, result);
if (normalize) Mathematics.normVect(result, 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.
*
@ -324,28 +386,13 @@ public class RNG extends Random {
* @param upper
* @return
*/
public static double[] randomVector(int n, double lower, double upper) {
double[] result = new double[n];
for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(lower, upper);
}
return result;
}
// public static double[] randomVector(int n, double lower, double upper) {
// double[] result = new double[n];
// for (int i = 0; i < result.length; i++) {
// result[i] = RNG.randomDouble(lower, upper);
// }
// 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;
}
}