Skip to content

Commit

Permalink
by taking into account the status of the experiment (batch & headless).
Browse files Browse the repository at this point in the history
Please test if it has the desired behavior.
  • Loading branch information
AlexisDrogoul committed Mar 12, 2023
1 parent d612936 commit ba1f1ee
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Exploration.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.9.0).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
Expand Down Expand Up @@ -241,7 +241,7 @@ public void explore(final IScope scope) throws GamaRuntimeException {
};
if (sets.isEmpty()) { sets.add(new ParametersSet()); }

if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue()) {
if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
currentExperiment.launchSimulationsWithSolution(sets);
} else {
for (ParametersSet sol : sets) { currentExperiment.launchSimulationsWithSolution(sol); }
Expand Down Expand Up @@ -370,10 +370,12 @@ private List<Object> getParameterSwip(final IScope scope, final Batch var) {
while (minValue <= maxValue) {
if (stepValue >= 0) {
res.add(minValue);
minValue = minValue + (int) stepValue + (Random.opFlip(scope, stepValue - (int) stepValue) ? 1 : 0);
minValue = minValue + (int) stepValue
+ (Random.opFlip(scope, stepValue - (int) stepValue) ? 1 : 0);
} else {
res.add(maxValue);
maxValue = maxValue + (int) stepValue - (Random.opFlip(scope, stepValue - (int) stepValue) ? 1 : 0);
maxValue = maxValue + (int) stepValue
- (Random.opFlip(scope, stepValue - (int) stepValue) ? 1 : 0);
}
}
break;
Expand All @@ -391,9 +393,8 @@ private List<Object> getParameterSwip(final IScope scope, final Batch var) {
if (stepFloatValue >= 0) {
res.add(minFloatValue);
minFloatValue = minFloatValue + stepFloatValue;

}
else {

} else {
res.add(maxFloatValue);
maxFloatValue = maxFloatValue + stepFloatValue;
}
Expand All @@ -402,26 +403,26 @@ private List<Object> getParameterSwip(final IScope scope, final Batch var) {
case IType.DATE:
GamaDate dateValue = GamaDateType.staticCast(scope, var.getMinValue(scope), null, false);
GamaDate maxDateValue = GamaDateType.staticCast(scope, var.getMaxValue(scope), null, false);
Double stepVal = Cast.asFloat(scope, var.getStepValue(scope));
Double stepVal = Cast.asFloat(scope, var.getStepValue(scope));
while (dateValue.isSmallerThan(maxDateValue, false)) {
if (stepVal > 0) {
res.add(dateValue);
dateValue = dateValue.plus(stepVal, ChronoUnit.SECONDS);
} else {
res.add(maxDateValue);
maxDateValue = maxDateValue.minus(Math.abs(stepVal.longValue()), ChronoUnit.SECONDS);
}
}
}
break;
case IType.POINT:
GamaPoint pointValue = Cast.asPoint(scope, var.getMinValue(scope));
GamaPoint maxPointValue = Cast.asPoint(scope, var.getMaxValue(scope));
GamaPoint increment = null;
Double stepV = null;

if (var.getStepValue(scope) != null) {
increment = GamaPointType.staticCast(scope, var.getStepValue(scope), true);

if (increment == null) {
Double d = GamaFloatType.staticCast(scope, var.getStepValue(scope), null, false);
if (d == null) {
Expand All @@ -432,16 +433,17 @@ private List<Object> getParameterSwip(final IScope scope, final Batch var) {
increment = new GamaPoint(d, d, d);
}
} else {
stepV = (increment.x + increment.y + increment.z)/3.0;
stepV = (increment.x + increment.y + increment.z) / 3.0;

}

} else {
increment = new GamaPoint((maxPointValue.x - pointValue.x)/10.0,(maxPointValue.y - pointValue.y)/10.0,(maxPointValue.z - pointValue.z)/10.0);

increment = new GamaPoint((maxPointValue.x - pointValue.x) / 10.0,
(maxPointValue.y - pointValue.y) / 10.0, (maxPointValue.z - pointValue.z) / 10.0);

}
while (pointValue.smallerThanOrEqualTo(maxPointValue)) {
if ((stepV == null) || (stepV> 0)) {
if (stepV == null || stepV > 0) {
res.add(pointValue);
pointValue = pointValue.plus(Cast.asPoint(scope, increment));
} else {
Expand All @@ -460,25 +462,28 @@ private List<Object> getParameterSwip(final IScope scope, final Batch var) {
floatcrement = (maxVarValue - varValue) / __default_step_factor;
}
while (varValue <= maxVarValue) {

if (var.getType().id() == IType.INT) {
if (floatcrement >= 0)
if (floatcrement >= 0) {
res.add((int) varValue);
else
} else {
res.add((int) maxVarValue);

}

} else if (var.getType().id() == IType.FLOAT) {
if (floatcrement >= 0)
if (floatcrement >= 0) {
res.add(varValue);
else
} else {
res.add(maxVarValue);
}
} else {
continue;
}
if (floatcrement >= 0)
if (floatcrement >= 0) {
varValue = varValue + floatcrement;
else
} else {
maxVarValue = maxVarValue + floatcrement;
}
}
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void explore(final IScope scope) {
currentExperiment.setSeeds(new Double[1]);
// TODO : why doesnt it take into account the value of 'keep_simulations:' ?
currentExperiment.setKeepSimulations(false);
if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue()) {
if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
res_outputs = currentExperiment.launchSimulationsWithSolution(sets);
} else {
res_outputs = GamaMapFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void explore(final IScope scope) {
currentExperiment.setSeeds(new Double[1]);
// TODO : why doesnt it take into account the value of 'keep_simulations:' ?
currentExperiment.setKeepSimulations(false);
if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue()) {
if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
res_outputs = currentExperiment.launchSimulationsWithSolution(solutions);
} else {
res_outputs = GamaMapFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void explore(final IScope scope) {
currentExperiment.setSeeds(new Double[1]);
// TODO : why doesnt it take into account the value of 'keep_simulations:' ?
currentExperiment.setKeepSimulations(false);
if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue()) {
if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
res_outputs = currentExperiment.launchSimulationsWithSolution(solutions);
} else {
res_outputs = GamaMapFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* StochanalysisExploration.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation
* platform (v.1.9.0).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
Expand Down Expand Up @@ -173,21 +173,20 @@ public void explore(final IScope scope) throws GamaRuntimeException {
case IKeyword.FACTORIAL -> {
List<ParametersSet> ps = null;
if (hasFacet(Exploration.SAMPLE_FACTORIAL)) {
@SuppressWarnings("unchecked")
int[] factors = Cast.asList(scope, getFacet(Exploration.SAMPLE_FACTORIAL).value(scope))
.stream().mapToInt(o -> Integer.valueOf(o.toString()))
.toArray();
@SuppressWarnings ("unchecked") int[] factors =
Cast.asList(scope, getFacet(Exploration.SAMPLE_FACTORIAL).value(scope)).stream()
.mapToInt(o -> Integer.parseInt(o.toString())).toArray();
ps = RandomSampling.FactorialUniformSampling(scope, factors, params);
} else {
ps = RandomSampling.FactorialUniformSampling(scope, sample_size, params);
}

yield ps;
}
default -> RandomSampling.UniformSampling(scope, sample_size, params);
};

if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue()) {
if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
res_outputs = currentExperiment.launchSimulationsWithSolution(sets);
} else {
res_outputs = GamaMapFactory.create();
Expand Down Expand Up @@ -236,7 +235,7 @@ public void explore(final IScope scope) throws GamaRuntimeException {

@Override
public List<ParametersSet> buildParameterSets(final IScope scope, final List<ParametersSet> sets, final int index) {

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* HillClimbing.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.9.0).
* HillClimbing.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.9.0).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/

package msi.gama.kernel.batch.optimization;
Expand Down Expand Up @@ -100,44 +100,48 @@ public class HillClimbing extends ALocalSearchAlgorithm {

/** The Constant ITER_MAX. */
protected static final String ITER_MAX = "iter_max";

/** The stopping criterion. */
StoppingCriterion stoppingCriterion = null;

/** The max it. */
int maxIt;

/**
* Instantiates a new hill climbing.
*
* @param species the species
* @param species
* the species
*/
public HillClimbing(final IDescription species) {
super(species);
initParams();

}

/**
* Keep sol.
*
* @param neighborSol the neighbor sol
* @param neighborFitness the neighbor fitness
* @param neighborSol
* the neighbor sol
* @param neighborFitness
* the neighbor fitness
* @return true, if successful
*/
public boolean keepSol(ParametersSet neighborSol, Double neighborFitness ) {
public boolean keepSol(final ParametersSet neighborSol, final Double neighborFitness) {
if (isMaximize() && neighborFitness.doubleValue() > getBestFitness()
|| !isMaximize() && neighborFitness.doubleValue() < getBestFitness()) {
setBestFitness(neighborFitness);
return true;
}
return false;
}

@Override
public ParametersSet findBestSolution(final IScope scope) throws GamaRuntimeException {
setBestSolution(this.solutionInit);
double currentFitness = (Double) currentExperiment.launchSimulationsWithSolution(getBestSolution()).get(IKeyword.FITNESS).get(0);
double currentFitness = (Double) currentExperiment.launchSimulationsWithSolution(getBestSolution())
.get(IKeyword.FITNESS).get(0);
initializeTestedSolutions();
testedSolutions.put(getBestSolution(), currentFitness);
int nbIt = 0;
Expand All @@ -146,38 +150,31 @@ public ParametersSet findBestSolution(final IScope scope) throws GamaRuntimeExce
endingCritParams.put("Iteration", Integer.valueOf(nbIt));
while (stoppingCriterion == null || !stoppingCriterion.stopSearchProcess(endingCritParams)) {
final List<ParametersSet> neighbors = neighborhood.neighbor(scope, getBestSolution());
if (neighbors.isEmpty()) {
break;
}
if (neighbors.isEmpty()) { break; }
setBestFitness(currentFitness);
ParametersSet bestNeighbor = null;

if (GamaExecutorService.CONCURRENCY_SIMULATIONS_ALL.getValue() && ! currentExperiment.getParametersToExplore().isEmpty()) {
Map<ParametersSet,Double> result = testSolutions(neighbors);

if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)
&& !currentExperiment.getParametersToExplore().isEmpty()) {
Map<ParametersSet, Double> result = testSolutions(neighbors);
if (result.containsKey(bestSolution)) { bestNeighbor = bestSolution; }
} else {
for (final ParametersSet neighborSol : neighbors) {
if (neighborSol == null) {
continue;
}
if (neighborSol == null) { continue; }
Double neighborFitness = testedSolutions.get(neighborSol);
if (neighborFitness == null) {
neighborFitness = (Double) currentExperiment.launchSimulationsWithSolution(neighborSol).get(IKeyword.FITNESS).get(0);
neighborFitness = (Double) currentExperiment.launchSimulationsWithSolution(neighborSol)
.get(IKeyword.FITNESS).get(0);
}
testedSolutions.put(neighborSol, neighborFitness);
if (neighborSol.equals(bestSolution)) { bestNeighbor = neighborSol;}
if (neighborSol.equals(bestSolution)) { bestNeighbor = neighborSol; }

}
}



if (bestNeighbor != null) {
setBestSolution(bestNeighbor);
currentFitness = getBestFitness();
} else {
break;
}

if (bestNeighbor == null) { break; }
setBestSolution(bestNeighbor);
currentFitness = getBestFitness();
nbIt++;
endingCritParams.put("Iteration", Integer.valueOf(nbIt));
}
Expand All @@ -190,7 +187,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

0 comments on commit ba1f1ee

Please sign in to comment.