Merging mk branch rev 152:153

This commit is contained in:
Marcel Kronfeld 2008-08-13 08:17:07 +00:00
parent 0f07c0a03f
commit 7530638ce2
5 changed files with 73 additions and 80 deletions

View File

@ -436,8 +436,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
/**
* This method returns the n current best individuals from the population, where
* the sorting criterion is delivered by an AbstractEAIndividualComparator.
* There are less than n individuals returned if the population is smaller than n.
* This does not check constraints!
* @see getSortedNIndividuals(int n, boolean bBestOrWorst, Population res)
*
* @param n number of individuals to look out for
* @param bBestOrWorst if true, the best n are returned, else the worst n individuals
@ -445,6 +444,24 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
*
*/
public Population getSortedNIndividuals(int n, boolean bBestOrWorst) {
Population result = new Population(n);
getSortedNIndividuals(n, bBestOrWorst, result);
return result;
}
/**
* This method returns the n current best individuals from the population, where
* the sorting criterion is delivered by an AbstractEAIndividualComparator.
* There are less than n individuals returned if the population is smaller than n.
* This does not check constraints!
*
* @param n number of individuals to look out for
* @param bBestOrWorst if true, the best n are returned, else the worst n individuals
* @param res sorted result population, will be cleared
* @return The m sorted best or worst individuals, where m <= n
*
*/
public void getSortedNIndividuals(int n, boolean bBestOrWorst, Population res) {
if ((n < 0) || (n>super.size())) {
System.err.println("invalid request to getSortedNIndividuals: n="+n + ", size is " + super.size());
n = super.size();
@ -452,14 +469,12 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
int skip = 0;
if (!bBestOrWorst) skip = super.size()-n;
Population result = new Population(n);
ArrayList<AbstractEAIndividual> sorted = getSorted();
res.clear();
for (int i = skip; i < skip+n; i++) {
result.add(sorted.get(i));
res.add(sorted.get(i));
}
result.setPopulationSize(result.size());
return result;
res.setPopulationSize(res.size());
}
/**

View File

@ -46,7 +46,12 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
return x;
}
@Override
/**
* When implementing a double problem, inheriting classes should not override this method (or only
* extend it) and do the fitness calculations in the method eval(double[]).
*
* @see eval(double[] x)
*/
public void evaluate(AbstractEAIndividual individual) {
double[] x;
double[] fitness;

View File

@ -36,6 +36,9 @@ import wsi.ra.math.Jama.Matrix;
* whether the new individual would violate range constraints, if so
* the velocity vector is reduced.
*
* Possible topologies are: "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random"
* in that order starting by 0.
*
* Created by IntelliJ IDEA.
* User: streiche
* Date: 28.10.2004
@ -589,9 +592,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
*/
protected boolean isIndividualToUpdate(AbstractEAIndividual indy) {
double[] bestFitness = (double[]) indy.getData(partBestFitKey);
// TODO Multi-obj.?
// if (bestFitness.length>1) System.err.println("warning, multi-objective fitness disregarded here (PSO)");
// return (indy.getFitness()[0] <= bestFitness[0]);
return (AbstractEAIndividual.isDominatingFitnessNotEqual(indy.getFitness(), bestFitness));
}
@ -611,14 +611,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
}
private double vecLen(double[] vec) {
double sum = 0.;
for (int i=0; i<vec.length; i++) {
sum += vec[i]*vec[i];
}
return Math.sqrt(sum);
}
/**
* Main velocity update step.
*
@ -687,12 +679,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
return curVelocity;
}
private double getVecNorm(double[] vect) {
double res = 0.;
for (int i=0; i<vect.length; i++) res += vect[i]*vect[i];
return Math.sqrt(res);
}
protected double[] getAcceleration(double[] personalBestPos, double[] neighbourBestPos, double[] curPosition, double[][] range) {
double[] accel = new double[curPosition.length];
@ -719,22 +705,14 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
Matrix cogVecB = new Matrix(curPosition.length, 1);
Matrix socVecB = new Matrix(curPosition.length, 1);
// GVector cogVec = new GVector(curPosition.length);
// GVector socVec = new GVector(curPosition.length);
for (int i = 0; i < personalBestPos.length; i++) {
cogVecB.set(i, 0, (personalBestPos[i]-curPosition[i]));
socVecB.set(i, 0, (neighbourBestPos[i]-curPosition[i]));
// cogVec.setElement(i, (personalBestPos[i]-curPosition[i]));
// socVec.setElement(i, (neighbourBestPos[i]-curPosition[i]));
}
Matrix cogRandB = getOrientedGaussianRandomVectorB(cogVecB, 5);
// GVector cogRand = getOrientedGaussianRandomVector(cogVec, 5);
Matrix socRandB = getOrientedGaussianRandomVectorB(socVecB, 5);
// GVector socRand = getOrientedGaussianRandomVector(socVec, 5);
//System.out.println(cogVec.norm() + " " + cogRand.norm() + " / " + socVec.norm() + " " + socRand.norm());
for (int i = 0; i < curPosition.length; i++) {
if (algType.getSelectedTag().getID()==1) chi=m_InertnessOrChi;
else chi = 1.;
@ -811,29 +789,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
else if (val > max) return max;
else return val;
}
// protected GVector getOrientedGaussianRandomVector(GVector dir, double scale) {
// double len = dir.norm();
// int dim = dir.getSize();
//
// GVector resVec = new GVector(dim);
// GVector randVec = new GVector(dim);
//
// if (len > 0) {
// // initialize random vector
// randVec.setElement(0, len/2.+RNG.gaussianDouble(len/2));
// //randVec.setElement(0, RNG.randomDouble(0, len));
// for (int i=1; i<dim; i++) {
// randVec.setElement(i, RNG.gaussianDouble(len/(scale*2)));
// }
//
// GMatrix rotation = nrotate(dir);
// rotation.transpose();
// printGMatrix(rotation);
// resVec.mul(rotation, randVec);
// }
// return resVec;
// }
protected void printMatrix(Matrix M) {
for (int i=0; i<M.getRowDimension(); i++) {
@ -842,25 +797,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
}
}
// private void printGMatrix(GMatrix M) {
// for (int i=0; i<M.getNumRow(); i++) {
// for (int j=0; j<M.getNumCol(); j++) System.out.print(" " + M.getElement(i, j));
// System.out.println("");
// }
// }
//
// protected GVector getUnitVec(int dim, int k) {
// GVector v = new GVector(dim);
// v.setElement(k, 1.);
// return v;
// }
// protected void switchElements(GVector vec, int i, int j) {
// double val1 = vec.getElement(i);
// vec.setElement(i, vec.getElement(j));
// vec.setElement(j, val1);
// }
/**
* In the topology range for the given index, find the best stored individual and return its position.
*

View File

@ -337,13 +337,25 @@ public class Mathematics {
*/
public static double[] vvSub(double[] a, double[] b) {
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] - b[i];
}
vvSub(a, b, result);
return result;
}
/** This method return a vector from a to b
/**
* Subtract vectors returning a new vector c = a - b.
*
* @param a
* @param b
* @return a new vector c = a - b
*/
public static void vvSub(double[] a, double[] b, double[] res) {
for (int i = 0; i < a.length; i++) {
res[i] = a[i] - b[i];
}
}
/**
* This method return a vector from a to b
* @param a first vector
* @param b second vectors
* @return the vector from a to b
@ -392,6 +404,20 @@ public class Mathematics {
}
return res;
}
/**
* Add vectors scaled: res[i] = s*v[i] + w[i]
*
* @param s
* @param v
* @param w
* @return
*/
public static void svvAddScaled(double s, double[] v, double[] w, double[] res) {
for (int i = 0; i < v.length; i++) {
res[i] = s*v[i] + w[i];
}
}
/**
* Scalar product of two vectors returning sum_i (a_i * b_i).

View File

@ -9,6 +9,8 @@ import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import eva2.gui.BeanInspector;
import wsi.ra.math.Jama.util.Maths;
@ -761,6 +763,15 @@ public class Matrix implements Cloneable, java.io.Serializable {
}
return X;
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i=0; i<m; i++) {
sb.append(BeanInspector.toString(A[i]));
sb.append("\n");
}
return sb.toString();
}
/** Multiply a matrix by a scalar in place, A = s*A
@param s scalar