/
ExperimentParameter.java
463 lines (414 loc) · 14.9 KB
/
ExperimentParameter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*********************************************************************************************
*
*
* 'ExperimentParameter.java', in plugin 'msi.gama.core', is part of the source code of the
* GAMA modeling and simulation platform.
* (c) 2007-2014 UMI 209 UMMISCO IRD/UPMC & Partners
*
* Visit https://code.google.com/p/gama-platform/ for license information and developers contact.
*
*
**********************************************************************************************/
package msi.gama.kernel.experiment;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.util.StringUtils;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.example;
import msi.gama.precompiler.GamlAnnotations.facet;
import msi.gama.precompiler.GamlAnnotations.facets;
import msi.gama.precompiler.GamlAnnotations.inside;
import msi.gama.precompiler.GamlAnnotations.symbol;
import msi.gama.precompiler.GamlAnnotations.usage;
import msi.gama.precompiler.GamlAnnotations.validator;
import msi.gama.precompiler.IConcept;
import msi.gama.precompiler.ISymbolKind;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gaml.compilation.ISymbol;
import msi.gaml.compilation.Symbol;
import msi.gaml.descriptions.IDescription;
import msi.gaml.descriptions.ModelDescription;
import msi.gaml.descriptions.VariableDescription;
import msi.gaml.expressions.ConstantExpression;
import msi.gaml.expressions.IExpression;
import msi.gaml.operators.Cast;
import msi.gaml.operators.fastmaths.FastMath;
import msi.gaml.statements.IExecutable;
import msi.gaml.types.IType;
import msi.gaml.types.Types;
import msi.gaml.variables.IVariable;
import msi.gaml.variables.Variable;
@facets(value = {
@facet(name = IKeyword.NAME, type = IType.LABEL, optional = true, doc = @doc("The message displayed in the interface")),
@facet(name = IKeyword.TYPE, type = IType.TYPE_ID, optional = true, doc = @doc("the variable type")),
@facet(name = IKeyword.INIT, type = IType.NONE, optional = true, doc = @doc("the init value")),
@facet(name = IKeyword.MIN, type = IType.NONE, optional = true, doc = @doc("the minimum value")),
@facet(name = IKeyword.MAX, type = IType.NONE, optional = true, doc = @doc("the maximum value")),
@facet(name = IKeyword.CATEGORY, type = IType.LABEL, optional = true, doc = @doc("a category label, used to group parameters in the interface")),
@facet(name = IKeyword.VAR, type = IType.ID, optional = false, doc = @doc("the name of the variable (that should be declared in the global)")),
@facet(name = IKeyword.UNIT, type = IType.LABEL, optional = true, doc = @doc("the variable unit")),
@facet(name = IKeyword.ON_CHANGE, type = IType.NONE, optional = true, doc = @doc("Provides a block of statements that will be executed whenever the value of the parameter changes")),
@facet(name = "slider", type = IType.BOOL, optional = true, doc = @doc("Whether or not to display a slider for entering an int or float value. Default is true when max and min values are defined, false otherwise. If no max or min value is defined, setting this facet to true will have no effect")),
@facet(name = IKeyword.STEP, type = IType.FLOAT, optional = true, doc = @doc("the increment step (mainly used in batch mode to express the variation step between simulation)")),
@facet(name = IKeyword.AMONG, type = IType.LIST, optional = true, doc = @doc("the list of possible values")) }, omissible = IKeyword.NAME)
@symbol(name = { IKeyword.PARAMETER }, kind = ISymbolKind.PARAMETER, with_sequence = false, concept = {
IConcept.EXPERIMENT, IConcept.PARAMETER })
@inside(kinds = { ISymbolKind.EXPERIMENT })
@validator(Variable.VarValidator.class)
@doc(value = "The parameter statement specifies which global attributes (i) will change through the successive simulations (in batch experiments), (ii) can be modified by user via the interface (in gui experiments). In GUI experiments, parameters are displayed depending on their type.", usages = {
@usage(value = "In gui experiment, the general syntax is the following:", examples = {
@example(value = "parameter title var: global_var category: cat;", isExecutable = false) }),
@usage(value = "In batch experiment, the two following syntaxes can be used to describe the possible values of a parameter:", examples = {
@example(value = "parameter 'Value of toto:' var: toto among: [1, 3, 7, 15, 100]; ", isExecutable = false),
@example(value = "parameter 'Value of titi:' var: titi min: 1 max: 100 step: 2; ", isExecutable = false) }), })
@SuppressWarnings({ "rawtypes" })
public class ExperimentParameter extends Symbol implements IParameter.Batch {
static Object UNDEFINED = new Object();
private Object value = UNDEFINED;
// int order;
static int INDEX = 0;
Number minValue, maxValue, stepValue;
private List amongValue;
String varName, title, category, unitLabel;
IType type = Types.NO_TYPE;
boolean isEditable;
boolean canBeNull;
boolean isDefined = true;
final IExpression init, among, min, max, step, slider, onChange;
public ExperimentParameter(final IDescription sd) throws GamaRuntimeException {
super(sd);
final VariableDescription desc = (VariableDescription) sd;
setName(desc.getLitteral(IKeyword.VAR));
type = desc.getType();
title = sd.getName();
unitLabel = getLiteral(IKeyword.UNIT);
final ModelDescription wd = desc.getModelDescription();
final IDescription targetedGlobalVar = wd.getAttribute(varName);
if (type.equals(Types.NO_TYPE)) {
type = targetedGlobalVar.getType();
}
setCategory(desc.getLitteral(IKeyword.CATEGORY));
min = getFacet(IKeyword.MIN);
max = getFacet(IKeyword.MAX);
step = getFacet(IKeyword.STEP);
among = getFacet(IKeyword.AMONG);
onChange = getFacet(IKeyword.ON_CHANGE);
slider = getFacet("slider");
init = hasFacet(IKeyword.INIT) ? getFacet(IKeyword.INIT) : targetedGlobalVar.getFacetExpr(IKeyword.INIT);
// order = desc.getDefinitionOrder();
isEditable = true;
}
public ExperimentParameter(final IScope scope, final IParameter p) {
this(scope, p, p.getTitle(), p.getCategory(), p.getAmongValue(scope), false);
}
public ExperimentParameter(final IScope scope, final IParameter p, final String title, final String category,
final List among, final boolean canBeNull) {
this(scope, p, title, category, null, among, canBeNull);
}
public ExperimentParameter(final IScope scope, final IParameter p, final String title, final String category,
final String unit, final List among, final boolean canBeNull) {
super(null);
this.slider = null;
this.title = title;
this.canBeNull = canBeNull;
// this.order = p.getDefinitionOrder();
this.amongValue = among;
if (among != null) {
this.among = new ConstantExpression(among);
} else {
this.among = null;
}
this.minValue = p.getMinValue(scope);
if (minValue != null) {
this.min = new ConstantExpression(minValue);
} else {
min = null;
}
this.maxValue = p.getMaxValue(scope);
if (maxValue != null) {
this.max = new ConstantExpression(maxValue);
} else {
max = null;
}
this.stepValue = p.getStepValue(scope);
if (stepValue != null) {
this.step = new ConstantExpression(stepValue);
} else {
step = null;
}
onChange = null;
setName(p.getName());
setCategory(category);
setType(p.getType());
if (p instanceof IVariable && getType().getType().id() == IType.FILE) {
init = ((IVariable) p).getFacet(IKeyword.INIT);
} else {
init = null;
setValue(scope, p.getInitialValue(scope));
}
setEditable(p.isEditable());
}
@Override
public void setName(final String name2) {
varName = name2;
if (title == null) {
title = name2;
}
}
private void setType(final IType iType) {
type = iType;
}
@Override
public boolean isEditable() {
return isEditable;
}
@Override
public boolean isDefined() {
return isDefined;
}
@Override
public void setDefined(final boolean defined) {
isDefined = defined;
}
@Override
public void setEditable(final boolean editable) {
isEditable = editable;
}
// private static Object[] JunkResults = new Object[1];
public void setAndVerifyValue(final IScope scope, final Object val) {
Object newValue = val;
if (minValue != null) {
if (newValue instanceof Number) {
if (((Number) newValue).doubleValue() < minValue.doubleValue()) {
if (type.id() == IType.INT) {
newValue = minValue.intValue();
} else {
newValue = minValue.doubleValue();
}
}
}
}
if (maxValue != null) {
if (newValue instanceof Number) {
if (((Number) newValue).doubleValue() > maxValue.doubleValue()) {
if (type.id() == IType.INT) {
newValue = maxValue.intValue();
} else {
newValue = maxValue.doubleValue();
}
}
}
}
if (getAmongValue(scope) != null && !getAmongValue(scope).isEmpty()) {
if (!getAmongValue(scope).contains(newValue)) {
newValue = getAmongValue(scope).get(0);
}
}
if (value != UNDEFINED && onChange != null) {
// Already initialized, we call the on_change behavior
final IExecutable on_changer = scope.getAgent().getSpecies()
.getAction(Cast.asString(scope, onChange.value(scope)));
scope.getExperiment().executeAction(on_changer);
// scope.execute(on_changer, scope.getAgentScope(), null,
// JunkResults);
}
value = newValue;
}
@Override
public void setValue(final IScope scope, final Object val) {
if (val == UNDEFINED) {
if (getAmongValue(scope) != null) {
value = getAmongValue(scope).get(scope.getRandom().between(0, getAmongValue(scope).size() - 1));
} else if (type.id() == IType.INT || type.id() == IType.FLOAT) {
value = drawRandomValue(scope);
} else if (type.id() == IType.BOOL) {
value = scope.getRandom().between(1, 100) > 50;
} else {
value = null;
}
return;
}
setAndVerifyValue(scope, val);
}
@Override
public void reinitRandomly(final IScope scope) {
setValue(scope, UNDEFINED);
}
public void tryToInit(final IScope scope) {
if (value != UNDEFINED) {
return;
}
if (init == null) {
return;
}
setValue(scope, init.value(scope));
}
private Number drawRandomValue(final IScope scope) {
final double step = stepValue == null ? 1.0 : stepValue.doubleValue();
if (type.id() == IType.INT) {
final int min = minValue == null ? Integer.MIN_VALUE : minValue.intValue();
final int max = maxValue == null ? Integer.MAX_VALUE : maxValue.intValue();
return scope.getRandom().between(min, max, (int) step);
}
final double min = minValue == null ? Double.MIN_VALUE : minValue.doubleValue();
final double max = maxValue == null ? Double.MAX_VALUE : maxValue.doubleValue();
return scope.getRandom().between(min, max, step);
}
@Override
public Set<Object> neighborValues(final IScope scope) throws GamaRuntimeException {
final Set<Object> neighborValues = new HashSet<Object>();
if (getAmongValue(scope) != null && !getAmongValue(scope).isEmpty()) {
final int index = getAmongValue(scope).indexOf(this.value(scope));
if (index > 0) {
neighborValues.add(getAmongValue(scope).get(index - 1));
}
if (index < getAmongValue(scope).size() - 1) {
neighborValues.add(getAmongValue(scope).get(index + 1));
}
return neighborValues;
}
final double step = stepValue == null ? 1.0 : stepValue.doubleValue();
if (type.id() == IType.INT) {
final int min = minValue == null ? Integer.MIN_VALUE : minValue.intValue();
final int max = maxValue == null ? Integer.MAX_VALUE : maxValue.intValue();
final int val = Cast.as(value(scope), Integer.class, false);
if (val >= min + (int) step) {
neighborValues.add(val - (int) step);
}
if (val <= max - (int) step) {
neighborValues.add(val + (int) step);
}
} else if (type.id() == IType.FLOAT) {
final double min = minValue == null ? Double.MIN_VALUE : minValue.doubleValue();
final double max = maxValue == null ? Double.MAX_VALUE : maxValue.doubleValue();
final double removeZ = FastMath.max(100000.0, 1.0 / step);
final double val = Cast.asFloat(null, value(scope));
if (val >= min + step) {
final double valLow = FastMath.round((val - step) * removeZ) / removeZ;
neighborValues.add(valLow);
}
if (val <= max - step) {
final double valHigh = FastMath.round((val + step) * removeZ) / removeZ;
neighborValues.add(valHigh);
}
}
return neighborValues;
}
@Override
public String getTitle() {
return title;
}
@Override
public String getName() {
return varName;
}
@Override
public String getCategory() {
return category;
}
@Override
public void setCategory(final String cat) {
category = cat;
}
// @Override
// public Integer getDefinitionOrder() {
// return order;
// }
@Override
public Object value(final IScope scope) {
return getValue(scope);
}
@Override
public Object value() {
return GAMA.run(scope -> getValue(scope));
}
@Override
public Object getInitialValue(final IScope scope) {
return getValue(scope);
}
@Override
public Number getMinValue(final IScope scope) {
if (minValue == null && min != null) {
minValue = (Number) min.value(scope);
}
return minValue;
}
@Override
public Number getMaxValue(final IScope scope) {
if (maxValue == null && max != null) {
maxValue = (Number) max.value(scope);
}
return maxValue;
}
@Override
public List getAmongValue(final IScope scope) {
if (amongValue == null && among != null) {
amongValue = (List) among.value(scope);
}
return amongValue;
}
@Override
public Number getStepValue(final IScope scope) {
if (stepValue == null && step != null) {
stepValue = (Number) step.value(scope);
}
return stepValue;
}
@Override
public IType getType() {
return type;
}
@Override
public String serialize(final boolean includingBuiltIn) {
return GAMA.run(scope -> StringUtils.toGaml(getValue(scope), includingBuiltIn));
}
@Override
public void setChildren(final Iterable<? extends ISymbol> commands) {
}
@Override
public String toString() {
return "Parameter '" + title + "' targets var " + varName;
}
public boolean canBeNull() {
return canBeNull;
}
@Override
public boolean canBeExplored() {
return among != null || min != null && max != null && step != null;
}
@Override
public String getUnitLabel(final IScope scope) {
if (unitLabel == null && canBeExplored()) {
return computeExplorableLabel(scope);
}
return unitLabel;
}
private String computeExplorableLabel(final IScope scope) {
final List l = getAmongValue(scope);
if (l != null) {
return "among " + l;
}
final Number max = getMaxValue(scope);
final Number min = getMinValue(scope);
final Number step = getStepValue(scope);
return "between " + min + " and " + max + " every " + step;
}
@Override
public void setUnitLabel(final String label) {
unitLabel = label;
}
Object getValue(final IScope scope) {
tryToInit(scope);
return value;
}
@Override
public boolean acceptsSlider(final IScope scope) {
if (slider == null)
return true;
return Cast.asBool(scope, slider.value(scope));
}
}