Merging mk branch rev 152:153
This commit is contained in:
parent
0f07c0a03f
commit
7530638ce2
@ -436,8 +436,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
|
|||||||
/**
|
/**
|
||||||
* This method returns the n current best individuals from the population, where
|
* This method returns the n current best individuals from the population, where
|
||||||
* the sorting criterion is delivered by an AbstractEAIndividualComparator.
|
* the sorting criterion is delivered by an AbstractEAIndividualComparator.
|
||||||
* There are less than n individuals returned if the population is smaller than n.
|
* @see getSortedNIndividuals(int n, boolean bBestOrWorst, Population res)
|
||||||
* This does not check constraints!
|
|
||||||
*
|
*
|
||||||
* @param n number of individuals to look out for
|
* @param n number of individuals to look out for
|
||||||
* @param bBestOrWorst if true, the best n are returned, else the worst n individuals
|
* @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) {
|
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())) {
|
if ((n < 0) || (n>super.size())) {
|
||||||
System.err.println("invalid request to getSortedNIndividuals: n="+n + ", size is " + super.size());
|
System.err.println("invalid request to getSortedNIndividuals: n="+n + ", size is " + super.size());
|
||||||
n = super.size();
|
n = super.size();
|
||||||
@ -452,14 +469,12 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
|
|||||||
int skip = 0;
|
int skip = 0;
|
||||||
if (!bBestOrWorst) skip = super.size()-n;
|
if (!bBestOrWorst) skip = super.size()-n;
|
||||||
|
|
||||||
Population result = new Population(n);
|
|
||||||
ArrayList<AbstractEAIndividual> sorted = getSorted();
|
ArrayList<AbstractEAIndividual> sorted = getSorted();
|
||||||
|
res.clear();
|
||||||
for (int i = skip; i < skip+n; i++) {
|
for (int i = skip; i < skip+n; i++) {
|
||||||
result.add(sorted.get(i));
|
res.add(sorted.get(i));
|
||||||
}
|
}
|
||||||
result.setPopulationSize(result.size());
|
res.setPopulationSize(res.size());
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,7 +46,12 @@ public abstract class AbstractProblemDouble extends AbstractOptimizationProblem
|
|||||||
return x;
|
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) {
|
public void evaluate(AbstractEAIndividual individual) {
|
||||||
double[] x;
|
double[] x;
|
||||||
double[] fitness;
|
double[] fitness;
|
||||||
|
@ -36,6 +36,9 @@ import wsi.ra.math.Jama.Matrix;
|
|||||||
* whether the new individual would violate range constraints, if so
|
* whether the new individual would violate range constraints, if so
|
||||||
* the velocity vector is reduced.
|
* 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.
|
* Created by IntelliJ IDEA.
|
||||||
* User: streiche
|
* User: streiche
|
||||||
* Date: 28.10.2004
|
* Date: 28.10.2004
|
||||||
@ -589,9 +592,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
|
|||||||
*/
|
*/
|
||||||
protected boolean isIndividualToUpdate(AbstractEAIndividual indy) {
|
protected boolean isIndividualToUpdate(AbstractEAIndividual indy) {
|
||||||
double[] bestFitness = (double[]) indy.getData(partBestFitKey);
|
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));
|
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.
|
* Main velocity update step.
|
||||||
*
|
*
|
||||||
@ -688,12 +680,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
|
|||||||
return curVelocity;
|
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) {
|
protected double[] getAcceleration(double[] personalBestPos, double[] neighbourBestPos, double[] curPosition, double[][] range) {
|
||||||
double[] accel = new double[curPosition.length];
|
double[] accel = new double[curPosition.length];
|
||||||
double chi;
|
double chi;
|
||||||
@ -719,21 +705,13 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
|
|||||||
|
|
||||||
Matrix cogVecB = new Matrix(curPosition.length, 1);
|
Matrix cogVecB = new Matrix(curPosition.length, 1);
|
||||||
Matrix socVecB = 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++) {
|
for (int i = 0; i < personalBestPos.length; i++) {
|
||||||
cogVecB.set(i, 0, (personalBestPos[i]-curPosition[i]));
|
cogVecB.set(i, 0, (personalBestPos[i]-curPosition[i]));
|
||||||
socVecB.set(i, 0, (neighbourBestPos[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);
|
Matrix cogRandB = getOrientedGaussianRandomVectorB(cogVecB, 5);
|
||||||
// GVector cogRand = getOrientedGaussianRandomVector(cogVec, 5);
|
|
||||||
Matrix socRandB = getOrientedGaussianRandomVectorB(socVecB, 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++) {
|
for (int i = 0; i < curPosition.length; i++) {
|
||||||
if (algType.getSelectedTag().getID()==1) chi=m_InertnessOrChi;
|
if (algType.getSelectedTag().getID()==1) chi=m_InertnessOrChi;
|
||||||
@ -812,29 +790,6 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
|
|||||||
else return val;
|
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) {
|
protected void printMatrix(Matrix M) {
|
||||||
for (int i=0; i<M.getRowDimension(); i++) {
|
for (int i=0; i<M.getRowDimension(); i++) {
|
||||||
for (int j=0; j<M.getColumnDimension(); j++) System.out.print(" " + M.get(i, j));
|
for (int j=0; j<M.getColumnDimension(); j++) System.out.print(" " + M.get(i, j));
|
||||||
@ -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.
|
* In the topology range for the given index, find the best stored individual and return its position.
|
||||||
*
|
*
|
||||||
|
@ -337,13 +337,25 @@ public class Mathematics {
|
|||||||
*/
|
*/
|
||||||
public static double[] vvSub(double[] a, double[] b) {
|
public static double[] vvSub(double[] a, double[] b) {
|
||||||
double[] result = new double[a.length];
|
double[] result = new double[a.length];
|
||||||
for (int i = 0; i < a.length; i++) {
|
vvSub(a, b, result);
|
||||||
result[i] = a[i] - b[i];
|
|
||||||
}
|
|
||||||
return 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 a first vector
|
||||||
* @param b second vectors
|
* @param b second vectors
|
||||||
* @return the vector from a to b
|
* @return the vector from a to b
|
||||||
@ -393,6 +405,20 @@ public class Mathematics {
|
|||||||
return res;
|
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).
|
* Scalar product of two vectors returning sum_i (a_i * b_i).
|
||||||
*
|
*
|
||||||
|
@ -9,6 +9,8 @@ import java.text.DecimalFormatSymbols;
|
|||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import eva2.gui.BeanInspector;
|
||||||
|
|
||||||
import wsi.ra.math.Jama.util.Maths;
|
import wsi.ra.math.Jama.util.Maths;
|
||||||
|
|
||||||
|
|
||||||
@ -762,6 +764,15 @@ public class Matrix implements Cloneable, java.io.Serializable {
|
|||||||
return X;
|
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
|
/** Multiply a matrix by a scalar in place, A = s*A
|
||||||
@param s scalar
|
@param s scalar
|
||||||
@return replace A by s*A
|
@return replace A by s*A
|
||||||
|
Loading…
x
Reference in New Issue
Block a user