Skip to content

Commit

Permalink
Add a new batch algorithm (PSO)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaillandier committed Jul 31, 2021
1 parent 2695535 commit ac35479
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 29 deletions.
38 changes: 19 additions & 19 deletions msi.gama.core/.classpath
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
<accessrules>
<accessrule kind="accessible" pattern="*/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gaml">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
<accessrules>
<accessrule kind="accessible" pattern="*/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gaml">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion msi.gama.core/src/msi/gama/kernel/batch/HillClimbing.java
Expand Up @@ -147,7 +147,7 @@ public ParametersSet findBestSolution(final IScope scope) throws GamaRuntimeExce
// public void initializeFor(final IScope scope, final BatchAgent agent) throws GamaRuntimeException {
// super.initializeFor(scope, agent);
// }

@Override
protected void initParams(final IScope scope) {
final IExpression maxItExp = getFacet(ITER_MAX);
Expand Down
Expand Up @@ -43,7 +43,7 @@ public List<Chromosome> initializePop(final IScope scope, final List<IParameter.
if (algo.isMaximize) {
Collections.reverse(populationInitOrd);
}
return populationInitOrd.subList(0, populationDim);
return populationInitOrd.subList(0, populationDim - 1);
}

}
Expand Up @@ -43,7 +43,7 @@ public abstract class ParamSpaceExploAlgorithm extends Symbol implements IExplor
public final static String[] COMBINATIONS = new String[] { "maximum", "minimum", "average" };
@SuppressWarnings ("rawtypes") public static final Class[] CLASSES =
{ GeneticAlgorithm.class, SimulatedAnnealing.class, HillClimbing.class, TabuSearch.class,
TabuSearchReactive.class, ExhaustiveSearch.class };
TabuSearchReactive.class, ExhaustiveSearch.class, Swarm.class};

static {
AbstractGamlAdditions._constants(COMBINATIONS);
Expand All @@ -55,8 +55,8 @@ public abstract class ParamSpaceExploAlgorithm extends Symbol implements IExplor
protected boolean isMaximize;
protected BatchAgent currentExperiment;
// protected IScope scope;
private ParametersSet bestSolution = null;
private Double bestFitness = null;
protected ParametersSet bestSolution = null;
protected Double bestFitness = null;
protected short combination;

protected abstract ParametersSet findBestSolution(IScope scope) throws GamaRuntimeException;
Expand All @@ -78,7 +78,7 @@ protected void initializeTestedSolutions() {
testedSolutions = new HashMap<ParametersSet, Double>();
}

void initParams() {
protected void initParams() {
GAMA.run(new InScope.Void() {

@Override
Expand All @@ -88,7 +88,7 @@ public void process(final IScope scope) {
});
}

void initParams(final IScope scope) {}
protected void initParams(final IScope scope) {}

public ParamSpaceExploAlgorithm(final IDescription desc) {
super(desc);
Expand Down Expand Up @@ -122,7 +122,7 @@ public void run(final IScope scope) {
@Override
public void setChildren(final Iterable<? extends ISymbol> commands) {}

protected boolean isMaximize() {
public boolean isMaximize() {
return isMaximize;
}

Expand All @@ -145,6 +145,7 @@ public Object value() {
});
}


@Override
public Double getBestFitness() {
return bestFitness;
Expand Down
135 changes: 135 additions & 0 deletions msi.gama.core/src/msi/gama/kernel/batch/Particle.java
@@ -0,0 +1,135 @@
package msi.gama.kernel.batch;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import msi.gama.kernel.experiment.BatchAgent;
import msi.gama.kernel.experiment.IParameter;
import msi.gama.kernel.experiment.ParametersSet;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.runtime.IScope;
import msi.gaml.operators.Cast;

/**
* Represents a particle from the Particle Swarm Optimization algorithm.
*/
class Particle {

private ParametersSet position; // Current position.
private ParametersSet velocity;
private ParametersSet bestPosition; // Personal best solution.
private double bestEval; // Personal best value.

protected HashMap<ParametersSet, Double> testedSolutions;

BatchAgent currentExperiment;
final Map<String, GamaPoint> parameters;

ParamSpaceExploAlgorithm algo;
/**
* Construct a Particle with a random starting position.
* @param beginRange the minimum xyz values of the position (inclusive)
* @param endRange the maximum xyz values of the position (exclusive)
*/
Particle (IScope scope, BatchAgent agent, ParamSpaceExploAlgorithm algorithm, HashMap<ParametersSet, Double> testedSolutionsMap) {
currentExperiment = agent;
algo = algorithm;
this.testedSolutions = testedSolutionsMap;
final List<IParameter.Batch> v = agent.getParametersToExplore();
parameters = new HashMap<>();
for (IParameter p : v ) {
GamaPoint minMax = new GamaPoint(
p.getMinValue(scope) != null ? Cast.asFloat(scope,p.getMinValue(scope)) : Double.NEGATIVE_INFINITY,
p.getMaxValue(scope) != null ? Cast.asFloat(scope,p.getMaxValue(scope)) : Double.POSITIVE_INFINITY);
parameters.put(p.getName(),minMax);
}
position = new ParametersSet(scope, v, true);
velocity = new ParametersSet(scope, v, true);
bestPosition = new ParametersSet(velocity);
bestEval = eval();
}

/**
* The evaluation of the current position.
* @return the evaluation
*/
private double eval () {
Double fitness = testedSolutions.get(position);
if (fitness == null) {
fitness = currentExperiment.launchSimulationsWithSolution(position);
}
testedSolutions.put(position, fitness);

/**/

return fitness.doubleValue();
}

/**
* Update the personal best if the current evaluation is better.
*/
void updatePersonalBest () {
double eval = eval();
if (algo.isMaximize() && eval > bestEval
|| !algo.isMaximize() && eval < bestEval) {
bestEval = eval;
bestPosition = new ParametersSet(position);
}
}

/**
* Get the position of the particle.
* @return the x position
*/
ParametersSet getPosition () {
return position;
}

/**
* Getthe velocity of the particle.
* @return the velocity
*/
ParametersSet getVelocity () {
return velocity;
}

/**
* Get the personal best solution.
* @return the best position
*/
ParametersSet getBestPosition() {
return bestPosition;
}

/**
* Get the value of the personal best solution.
* @return the evaluation
*/
double getBestEval () {
return bestEval;
}

/**
* Update the position of a particle by adding its velocity to its position.
*/
void updatePosition (IScope scope) {

for (String key : position.keySet()) {
GamaPoint p = parameters.get(key);
double val = Cast.asFloat(scope, position.get(key)) + Cast.asFloat(scope, velocity.get(key));
val = Math.min(Math.max(val, Cast.asFloat(scope, p.x)), p.y);
position.put(key, val );
}
}

/**
* Set the velocity of the particle.
* @param velocity the new velocity
*/
void setVelocity (ParametersSet velocity) {
this.velocity = velocity;
}


}
2 changes: 1 addition & 1 deletion msi.gama.core/src/msi/gama/kernel/batch/SelectionBest.java
Expand Up @@ -31,6 +31,6 @@ public List<Chromosome> select(final IScope scope, final List<Chromosome> popula
if (maximize) {
Collections.reverse(nextGen);
}
return nextGen.subList(0, populationDim);
return nextGen.subList(0, populationDim - 1);
}
}

0 comments on commit ac35479

Please sign in to comment.