I am using Repast Simphony framework for simulation. Let's say that I have following class:
public class Generator {
private String name;
private Random random;
public Generator(String name) {
this.name = name;
this.random = new Random();
}
public double getValue() {
return random.nextDouble();
}
}
Then I create several instances of this class, add them to context and run simulation:
public class Builder implements ContextBuilder<Object> {
@Override
public Context build(Context<Object> context) {
context.add(new Generator("Gen1"));
context.add(new Generator("Gen2"));
context.add(new Generator("Gen3"));
return context;
}
}
Is there any way to collect aggregated data, but for each instance of class separately? I would like to find out mean of all generated values for each generator, so output statistics should be in following format:
Name,Mean
Gen1,0.458
Gen2,0.512
Gen3,0.463
If I create new aggregated (mean) Data Set with Method Data Source Generator.getValue
and repeat each tick, I get large list of values:
Tick,Mean
1,0.365
2,0.456
3,0.728
4,0.091
...
where each value is mean, but mean of values in specified tick from all generators, not mean of values in all ticks from one generator. Is there any way to do this using Repast Simphony?
EDIT: When I want to use Custom Data Sources, add class Generator implements AggregateDataSource
and add methods:
@Override
public String getId() {
return name;
}
@Override
public Class<?> getDataType() {
return Double.class;
}
@Override
public Class<?> getSourceType() {
return Generator.class;
}
@Override
public Object get(Iterable<?> objs, int size) {
return 7.0; // Not mean, only mock value for testing.
}
@Override
public void reset() {
// TODO Auto-generated method stub
}
error appears:
I think you might be able to do this by defining a custom aggregate datasource. You can add one via the Custom Data Sources tab, providing a class that implements AggregateDataSource.
https://repast.github.io/docs/api/repast_simphony/repast/simphony/data2/AggregateDataSource.html
In the get() method, you iterate through all the Generator objects and get the mean by name. You'd need an AggregateDataSource implementation for each generator. If you use some static variables, you can probably code it such that you only need to iterate through once and get the mean for all the generators for that tick. I'd leave that until you have it working though.
Update:
You should make a different class for the CustomDataSource to avoid confusion. The iterable in the get should allow you to iterate over all the instances of Generator. Also, when you need to supply the fully qualified name -- the package + the class name -- e.g. x.y.MyCustomDataSource