From 7530638ce24b193d341da19b5a2bbb5428b59264 Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Wed, 13 Aug 2008 08:17:07 +0000 Subject: [PATCH] Merging mk branch rev 152:153 --- .../server/go/populations/Population.java | 29 ++++++-- .../go/problems/AbstractProblemDouble.java | 7 +- .../strategies/ParticleSwarmOptimization.java | 72 ++----------------- src/eva2/tools/Mathematics.java | 34 +++++++-- src/wsi/ra/math/Jama/Matrix.java | 11 +++ 5 files changed, 73 insertions(+), 80 deletions(-) diff --git a/src/eva2/server/go/populations/Population.java b/src/eva2/server/go/populations/Population.java index e877c29e..65b8e324 100644 --- a/src/eva2/server/go/populations/Population.java +++ b/src/eva2/server/go/populations/Population.java @@ -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 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()); } /** diff --git a/src/eva2/server/go/problems/AbstractProblemDouble.java b/src/eva2/server/go/problems/AbstractProblemDouble.java index 83b90612..c5862c41 100644 --- a/src/eva2/server/go/problems/AbstractProblemDouble.java +++ b/src/eva2/server/go/problems/AbstractProblemDouble.java @@ -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; diff --git a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java index 6be88ba5..ae5befbb 100644 --- a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java +++ b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java @@ -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 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