Skip to content

Commit

Permalink
[BATCH] add raw result outputs for Sobol, need to extend to all batch…
Browse files Browse the repository at this point in the history
…, plus remove Set from exploration algo
  • Loading branch information
chapuisk committed Feb 10, 2022
1 parent 7d8d211 commit fe7d915
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Expand Up @@ -49,6 +49,7 @@
import msi.gama.util.GamaDate;
import msi.gama.util.GamaListFactory;
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IList;
import msi.gama.util.IMap;
import msi.gaml.compilation.ISymbol;
import msi.gaml.descriptions.IDescription;
Expand Down Expand Up @@ -196,6 +197,37 @@ public void explore(final IScope scope) throws GamaRuntimeException {
if (!parent.exists()) { parent.mkdirs(); }
if (f.exists()) f.delete();

IList<String> outVar = GamaListFactory.create(scope, Types.STRING, Cast.asList(scope, getOutputs().value(scope)));

// Header
String res = parameters.stream().map(p -> p.getName()).collect(Collectors.joining(","));
res += ","+outVar.stream(scope).joining(",");

// Inputs and outputs of simulations
for(ParametersSet ps : res_outputs.keySet()) {
res += "\n";
// Params
IList<String> params = GamaListFactory.create();
for(Batch b : parameters) { params.add(ps.get(b.getName()).toString()); }
String current_params = params.stream(scope).joining(",");
// Outputs
Map<String, List<Object>> ps_res = res_outputs.get(ps);

int rep_number = ps_res.values().stream().findAny().get().size();
// Each replications of a parameter set
for (int i = 0; i < rep_number; i++) {
final int idx = i;
res += current_params+",";
res += outVar.stream(scope).map(v -> ps_res.get(v).get(idx)).joining(",");
res += "\n";
}
}

try (FileWriter fw = new FileWriter(f, false)) {
fw.write(res);
} catch (IOException e) {
GamaRuntimeFileException.create(e, scope);
}
}

}
Expand Down Expand Up @@ -315,8 +347,7 @@ private ParametersSet addParameterValue(IScope scope, ParametersSet set, Batch v
set.put(var.getName(), sobolDecomposition>0.5?true:false);
return set;
default:
GamaRuntimeException.error("Trying to add a variable of unknown type "+var.getType().id()+" to a parameter set", scope);
return set;
throw GamaRuntimeException.error("Trying to add a variable of unknown type "+var.getType().id()+" to a parameter set", scope);
}
}

Expand All @@ -342,7 +373,7 @@ private void computeSobolIndexes(IScope scope) {
List<Map<String,Object>> res_rebuilt = rebuildSimulationResults(scope, res_outputs);

if (res_rebuilt.size() != this._sample || sample * (2 + this.parameters.size() * 2) != _sample) {
GamaRuntimeException.error("Sobol analysis carry out less simulation than expected: "+_sample, scope);
throw GamaRuntimeException.error("Sobol analysis carry out less simulation than expected: "+_sample, scope);
}

for (String v : outputVals) {
Expand Down
5 changes: 3 additions & 2 deletions msi.gama.core/src/msi/gama/kernel/experiment/BatchAgent.java
Expand Up @@ -310,8 +310,9 @@ public IMap<ParametersSet, Map<String, List<Object>>> launchSimulationsWithSolut

// The values present in the solution are passed to the parameters of
// the experiment
LinkedHashSet<ParametersSet> sols_u = new LinkedHashSet<>(sols);
for (ParametersSet sol : sols_u) {
// @Patrick What this set was for ?
// LinkedHashSet<ParametersSet> sols_u = new LinkedHashSet<>(sols);
for (ParametersSet sol : sols) {
for (int i = 0; i < getSeeds().length; i++) {
Map<String, Object> sim = new HashMap<>();
sim.put("parameters", sol);
Expand Down
Expand Up @@ -143,7 +143,7 @@ experiment Explicit type: batch repeat: 3 keep_seed: true until: ( time > 5000 )
experiment Sobol type: batch keep_seed:true until:( time > 5000 ) {
parameter 'Infection rate' var: infection_rate min:0.0 max:1.0;
parameter 'Probability of dying' var:dying_proba min:0.01 max:0.25;
method sobol outputs:["num_dead"] sample:10 report:"Results/sobol.txt";
method sobol outputs:["num_dead"] sample:100 report:"Results/sobol.txt" results:"Results/sobol_raw.csv";
}

// This experiment explores two parameters with a PSO strategy,
Expand Down

0 comments on commit fe7d915

Please sign in to comment.