This commit is contained in:
Andreas Dräger 2009-11-30 11:53:33 +00:00
parent d55016e36e
commit 9843be0855
5 changed files with 455 additions and 422 deletions

View File

@ -79,7 +79,7 @@ import eva2.tools.math.RNG;
* @version 0.1 * @version 0.1
* @since 2.0 * @since 2.0
* @author mkron * @author mkron
* @author Andreas Dr&auml;ger <andreas.draeger@uni-tuebingen.de> * @author <a href="mailto:andreas.draeger@uni-tuebingen.de">Andreas Dr&auml;ger</a>
* @date 17.04.2007 * @date 17.04.2007
*/ */
public class OptimizerFactory { public class OptimizerFactory {

View File

@ -11,33 +11,18 @@ package eva2.gui;
*/ */
import java.awt.BorderLayout;
import java.awt.Component; import java.awt.Component;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.IntrospectionException; import java.beans.IntrospectionException;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.MethodDescriptor; import java.beans.MethodDescriptor;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport; import java.beans.PropertyChangeSupport;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor; import java.beans.PropertyEditor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
@ -45,18 +30,8 @@ import java.util.ArrayList;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import eva2.EvAInfo; import eva2.EvAInfo;
import eva2.client.EvAClient; import eva2.client.EvAClient;
import eva2.tools.EVAHELP;
import eva2.tools.ReflectPackage; import eva2.tools.ReflectPackage;
import eva2.tools.jproxy.RMIProxyLocal; import eva2.tools.jproxy.RMIProxyLocal;

View File

@ -54,6 +54,11 @@ import eva2.tools.math.Jama.Matrix;
*/ */
public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Serializable, InterfaceAdditionalPopulationInformer { public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Serializable, InterfaceAdditionalPopulationInformer {
/**
* Generated serial version uid.
*/
private static final long serialVersionUID = -149996122795669589L;
protected Population m_Population = new Population(); protected Population m_Population = new Population();
Object[] sortedPop = null; Object[] sortedPop = null;
protected AbstractEAIndividual m_BestIndividual; protected AbstractEAIndividual m_BestIndividual;

View File

@ -14,7 +14,6 @@ package eva2.tools.jproxy;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/*==========================================================================* /*==========================================================================*
* CLASS DECLARATION * CLASS DECLARATION
*==========================================================================*/ *==========================================================================*/

View File

@ -1,52 +1,63 @@
package eva2.tools.math; package eva2.tools.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 {
/**
*
*/
private static final long serialVersionUID = 1565216859128723844L;
private static Random random; private static Random random;
private static long randomSeed; private static long randomSeed;
/** /**
* *
*/ */
static { static {
randomSeed=System.currentTimeMillis(); randomSeed = System.currentTimeMillis();
random=new Random(randomSeed); random = new Random(randomSeed);
} }
/** /**
* *
*/ */
public static void setRandomSeed(long new_seed){ public static void setRandomSeed(long new_seed) {
//counter++; // counter++;
randomSeed=new_seed; randomSeed = new_seed;
if (randomSeed == 0) setRandomSeed(); if (randomSeed == 0)
else random = new Random(randomSeed); setRandomSeed();
else
random = new Random(randomSeed);
} }
/** /**
* Set the random seed without replacing zero with current system time. * Set the random seed without replacing zero with current system time.
*/ */
public static void setRandomSeedStrict(long new_seed){ public static void setRandomSeedStrict(long new_seed) {
randomSeed=new_seed; randomSeed = new_seed;
random.setSeed(randomSeed); random.setSeed(randomSeed);
} }
/** /**
* *
*/ */
public static void setRandomSeed() { public static void setRandomSeed() {
randomSeed=System.currentTimeMillis(); randomSeed = System.currentTimeMillis();
random=new Random(randomSeed); random = new Random(randomSeed);
} }
/** /**
* *
*/ */
public static void setRandom(Random base_random) { public static void setRandom(Random base_random) {
random=base_random; random = base_random;
} }
/** /**
* *
*/ */
@ -58,51 +69,63 @@ public class RNG extends Random {
* Returns 0 or 1 evenly distributed. * Returns 0 or 1 evenly distributed.
*/ */
public static int randomInt() { public static int randomInt() {
return randomInt(0,1); return randomInt(0, 1);
} }
/** /**
* Returns an evenly distributes int value between zero and * Returns an evenly distributes int value between zero and upperLim-1.
* upperLim-1. *
* @param upperLim upper exclusive limit of the random int * @param upperLim
* upper exclusive limit of the random int
*/ */
public static int randomInt(int upperLim) { public static int randomInt(int upperLim) {
return randomInt(0,upperLim-1); return randomInt(0, upperLim - 1);
} }
/** This method returns a evenly distributed int value. /**
* The boundarys are included. * This method returns a evenly distributed int value. The boundarys are
* @param lo Lower bound. * included.
* @param hi Upper bound. *
* @param lo
* Lower bound.
* @param hi
* Upper bound.
* @return int * @return int
*/ */
public static int randomInt(int lo,int hi) { public static int randomInt(int lo, int hi) {
if (hi<lo) { if (hi < lo) {
System.err.println("Invalid boundary values! Returning zero."); System.err.println("Invalid boundary values! Returning zero.");
return -1; return -1;
} }
int result = (Math.abs(random.nextInt())%(hi-lo+1))+lo; int result = (Math.abs(random.nextInt()) % (hi - lo + 1)) + lo;
if ((result < lo) || (result > hi)) { if ((result < lo) || (result > hi)) {
System.err.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi); System.err.println("Error, invalid value " + result
result = Math.abs(random.nextInt()%(hi-lo+1))+lo; + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / "
+ hi);
result = Math.abs(random.nextInt() % (hi - lo + 1)) + lo;
} }
return result; return result;
} }
/** This method returns a random permutation of n int values /**
* @param length The number of int values * This method returns a random permutation of n int values
*
* @param length
* The number of int values
* @return The permutation [0-length-1] * @return The permutation [0-length-1]
*/ */
public static int[] randomPermutation(int length) { public static int[] randomPermutation(int length) {
boolean[] validList = new boolean[length]; boolean[] validList = new boolean[length];
int[] result = new int[length]; int[] result = new int[length];
int index; int index;
for (int i = 0; i < validList.length; i++) validList[i] = true; for (int i = 0; i < validList.length; i++)
validList[i] = true;
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
index = randomInt(0, length-1); index = randomInt(0, length - 1);
while (!validList[index]) { while (!validList[index]) {
index++; index++;
if (index == length) index = 0; if (index == length)
index = 0;
} }
validList[index] = false; validList[index] = false;
result[i] = index; result[i] = index;
@ -110,8 +133,11 @@ public class RNG extends Random {
return result; return result;
} }
/** This method returns a random permutation of n int values /**
* @param length The number of int values * This method returns a random permutation of n int values
*
* @param length
* The number of int values
* @return The permutation [0-length-1] * @return The permutation [0-length-1]
*/ */
public static int[] randomPerm(int length) { public static int[] randomPerm(int length) {
@ -120,14 +146,15 @@ public class RNG extends Random {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
intList.add(new Integer(i)); intList.add(new Integer(i));
} }
for (int i = 0; i < length-1; i++) { for (int i = 0; i < length - 1; i++) {
int index = randomInt(intList.size()); int index = randomInt(intList.size());
result[i] = intList.get(index); result[i] = intList.get(index);
intList.remove(index); intList.remove(index);
} }
if (intList.size()>1) System.err.println("Error in randomPerm!"); if (intList.size() > 1)
result[length-1] = intList.get(0); System.err.println("Error in randomPerm!");
result[length - 1] = intList.get(0);
return result; return result;
} }
@ -135,47 +162,51 @@ public class RNG extends Random {
* Returns a random long between 0 and Long.MAX_VALUE-1 (inclusively). * Returns a random long between 0 and Long.MAX_VALUE-1 (inclusively).
*/ */
public static long randomLong() { public static long randomLong() {
return randomLong(0,Long.MAX_VALUE-1); return randomLong(0, Long.MAX_VALUE - 1);
} }
/** /**
* Returns a random long between the given values (inclusively). * Returns a random long between the given values (inclusively).
*/ */
public static long randomLong(long lo,long hi) { public static long randomLong(long lo, long hi) {
return (Math.abs(random.nextLong())%(hi-lo+1))+lo; return (Math.abs(random.nextLong()) % (hi - lo + 1)) + lo;
} }
/** /**
* *
*/ */
public static float randomFloat() { public static float randomFloat() {
return random.nextFloat(); return random.nextFloat();
} }
/** /**
* *
*/ */
public static float randomFloat(float lo,float hi) { public static float randomFloat(float lo, float hi) {
return (hi-lo)*random.nextFloat()+lo; return (hi - lo) * random.nextFloat() + lo;
} }
/** /**
* A random double value between 0 and 1. * A random double value between 0 and 1.
*/ */
public static double randomDouble() { public static double randomDouble() {
return random.nextDouble(); return random.nextDouble();
} }
/** /**
* *
*/ */
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. * 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];
for (int i=0;i<lo.length;i++) for (int i = 0; i < lo.length; i++)
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i]; xin[i] = (hi[i] - lo[i]) * random.nextDouble() + lo[i];
return xin; return xin;
} }
@ -184,43 +215,48 @@ public class RNG extends Random {
*/ */
public static double[] randomDoubleArray(double[][] range) { public static double[] randomDoubleArray(double[][] range) {
double[] xin = new double[range.length]; double[] xin = new double[range.length];
for (int i=0;i<xin.length;i++) for (int i = 0; i < xin.length; i++)
xin[i] = (range[i][1]-range[i][0])*random.nextDouble()+range[i][0]; 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. * Create a uniform random double vector within the given bounds (inclusive)
* in every dimension.
* *
* @param lower * @param lower
* @param upper * @param upper
* @param size * @param size
* @return * @return
*/ */
public static double[] randomDoubleArray(double lower, double upper, int size) { public static double[] randomDoubleArray(double lower, double upper,
int size) {
double[] result = new double[size]; double[] result = new double[size];
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;
// double[] xin = new double[size]; // double[] xin = new double[size];
// for (int i=0;i<size;i++) // for (int i=0;i<size;i++)
// xin[i] = (hi-lo)*random.nextDouble()+lo; // xin[i] = (hi-lo)*random.nextDouble()+lo;
// return xin; // return xin;
} }
/** /**
* *
*/ */
public static double[] randomDoubleArray(double[] lo,double[] hi,double[] xin) { public static double[] randomDoubleArray(double[] lo, double[] hi,
//counter++; double[] xin) {
for (int i=0;i<lo.length;i++) // counter++;
xin[i] = (hi[i]-lo[i])*random.nextDouble()+lo[i]; for (int i = 0; i < lo.length; 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. * Create a uniform random integer vector within the given bounds
* (inclusive) in every dimension.
* *
* @param n * @param n
* @param lower * @param lower
@ -239,14 +275,15 @@ public class RNG extends Random {
* *
*/ */
public static boolean randomBoolean() { public static boolean randomBoolean() {
//counter++; // counter++;
return (randomInt()==1); return (randomInt() == 1);
} }
/** /**
* *
*/ */
public static int randomBit() { public static int randomBit() {
//counter++; // counter++;
return randomInt(); return randomInt();
} }
@ -257,91 +294,105 @@ public class RNG extends Random {
* @return true with probability p, else false * @return true with probability p, else false
*/ */
public static boolean flipCoin(double p) { public static boolean flipCoin(double p) {
//counter++; // counter++;
return (randomDouble()<p ? true : false); return (randomDouble() < p ? true : false);
} }
/** /**
* *
*/ */
public static float gaussianFloat(float dev) { public static float gaussianFloat(float dev) {
//counter++; // counter++;
return (float)random.nextGaussian()*dev; return (float) random.nextGaussian() * dev;
} }
/** /**
* Return a Gaussian double with mean 0 and deviation dev. * Return a Gaussian double with mean 0 and deviation dev.
* *
* @param dev the deviation of the distribution. * @param dev
* the deviation of the distribution.
* @return a Gaussian double with mean 0 and given deviation. * @return a Gaussian double with mean 0 and given deviation.
*/ */
public static double gaussianDouble(double dev) { public static double gaussianDouble(double dev) {
//counter++; // counter++;
return random.nextGaussian()*dev; return random.nextGaussian() * dev;
} }
/** /**
* *
*/ */
public static float exponentialFloat(float mean) { public static float exponentialFloat(float mean) {
//counter++; // counter++;
return (float)(-mean*Math.log(randomDouble())); return (float) (-mean * Math.log(randomDouble()));
} }
/** /**
* *
*/ */
public static double exponentialDouble(double mean) { public static double exponentialDouble(double mean) {
//counter++; // counter++;
return -mean*Math.log(randomDouble()); return -mean * Math.log(randomDouble());
} }
/** /**
* Returns a vector denoting a random point around the center * Returns a vector denoting a random point around the center - inside a
* - inside a hypersphere of uniform distribution if nonUnif=0, * hypersphere of uniform distribution if nonUnif=0, - inside a hypersphere
* - inside a hypersphere of non-uniform distribution if nonUnif > 0, * of non-uniform distribution if nonUnif > 0, - inside a D-Gaussian if
* - inside a D-Gaussian if nonUnif < 0. * nonUnif < 0. For case 2, the nonUnif parameter is used as standard
* For case 2, the nonUnif parameter is used as standard deviation (instead of 1/D), the parameter * deviation (instead of 1/D), the parameter is not further used in the
* is not further used in the other two cases. * other two cases. Original code by Maurice Clerc, from the TRIBES package
* Original code by Maurice Clerc, from the TRIBES package
* *
* @param center center point of the distribution * @param center
* @param radius radius of the distribution * center point of the distribution
* @param nonUnif kind of distribution * @param radius
* radius of the distribution
* @param nonUnif
* kind of distribution
* *
**/ **/
public static double[] randHypersphere(double[] center, double radius, double nonUnif) { public static double[] randHypersphere(double[] center, double radius,
double nonUnif) {
double[] x = new double[center.length]; double[] x = new double[center.length];
int j; int j;
double xLen, r; double xLen, r;
int D=center.length; int D = center.length;
// ----------------------------------- Step 1. Direction // ----------------------------------- Step 1. Direction
xLen = 0; xLen = 0;
for (j=0; j<D; j++) { for (j = 0; j < D; j++) {
r = gaussianDouble(1); r = gaussianDouble(1);
x[j] = r; x[j] = r;
xLen += x[j]*x[j]; xLen += x[j] * x[j];
} }
xLen=Math.sqrt(xLen); xLen = Math.sqrt(xLen);
//----------------------------------- Step 2. Random radius // ----------------------------------- Step 2. Random radius
r=randomDouble(); r = randomDouble();
if (nonUnif < 0) r = gaussianDouble(r/2); // D-Gaussian if (nonUnif < 0)
else if (nonUnif > 0) r = Math.pow(r,nonUnif); // non-uniform hypersphere r = gaussianDouble(r / 2); // D-Gaussian
else r=Math.pow(r,1./D); // Real hypersphere else if (nonUnif > 0)
r = Math.pow(r, nonUnif); // non-uniform hypersphere
else
r = Math.pow(r, 1. / D); // Real hypersphere
for (j=0;j<D;j++) { for (j = 0; j < D; j++) {
x[j] = center[j]+radius*r*x[j]/xLen; x[j] = center[j] + radius * r * x[j] / xLen;
} }
return x; return x;
} }
/** /**
* Adds Gaussian noise to a double vector * Adds Gaussian noise to a double vector
* @param v the double vector *
* @param dev the Gaussian deviation * @param v
* the double vector
* @param dev
* the Gaussian deviation
*/ */
public static void addNoise(double[] v, double dev) { public static void addNoise(double[] v, double dev) {
for (int i=0; i<v.length; i++) { for (int i = 0; i < v.length; i++) {
// add noise to the value // add noise to the value
v[i] += gaussianDouble(dev); v[i] += gaussianDouble(dev);
} }
@ -366,52 +417,56 @@ public class RNG extends Random {
* @param n * @param n
* @return * @return
*/ */
public static double[] gaussianVector(double dev, double[] result, boolean normalize) { 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);
} }
if (normalize) Mathematics.normVect(result, result); if (normalize)
Mathematics.normVect(result, result);
return result; return result;
} }
// public static int testRndInt(long seed, int bits) {
// public static int testRndInt(long seed, int bits) { // return (int)(seed >>> (48 - bits));
// return (int)(seed >>> (48 - bits)); // }
// }
// //
// public static int testRandomInt(int lo, int hi, long seed) { // public static int testRandomInt(int lo, int hi, long seed) {
// if (hi<lo) { // if (hi<lo) {
// System.err.println("Invalid boundary values! Returning zero."); // System.err.println("Invalid boundary values! Returning zero.");
// return -1; // return -1;
// } // }
// int result = (Math.abs(testRndInt(seed,32))%(hi-lo+1))+lo; // int result = (Math.abs(testRndInt(seed,32))%(hi-lo+1))+lo;
// if ((result < lo) || (result > hi)) { // if ((result < lo) || (result > hi)) {
// System.err.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi); // System.err.println("Error, invalid value " + result +
// System.out.println("Error, invalid value " + result + " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi); // " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi);
// } // System.out.println("Error, invalid value " + result +
// return result; // " in RNG.randomInt! boundaries were lo/hi: " + lo + " / " + hi);
// } // }
// return result;
// }
// //
// public static void testRand(long initSeed) { // public static void testRand(long initSeed) {
// for (long seed=initSeed; seed<=Long.MAX_VALUE; seed++) { // for (long seed=initSeed; seed<=Long.MAX_VALUE; seed++) {
// int rnd = testRandomInt(0,8,seed); // int rnd = testRandomInt(0,8,seed);
// if (seed % 100000000 == 0) System.out.println("Seed at " + seed); // if (seed % 100000000 == 0) System.out.println("Seed at " + seed);
// } // }
// } // }
// public static void main(String[] args) { // public static void main(String[] args) {
// testRand(24000000000l); // testRand(24000000000l);
// System.out.println("RNG Done"); // System.out.println("RNG Done");
// double[] v = new double[2]; // double[] v = new double[2];
// for (int i=0; i<1000; i++) { // for (int i=0; i<1000; i++) {
// gaussianVector(1., v, false); // gaussianVector(1., v, false);
// EVAHELP.logString(Arrays.toString(v)+"\n", "randtest.dat"); // EVAHELP.logString(Arrays.toString(v)+"\n", "randtest.dat");
//// System.out.println(Arrays.toString(v)); // // 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.
* *
* @param n * @param n
* @param lower * @param lower
@ -419,11 +474,10 @@ public class RNG extends Random {
* @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;
} // }
}