重新聚合数据集,但在Repast Simphony中为每个实例单独进行。

huangapple go评论79阅读模式
英文:

Repast aggregated dataset, but for each instance separately in Repast Simphony

问题

以下是翻译好的内容:

我正在使用Repast Simphony框架进行模拟。假设我有以下的类:

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();
    }

}

然后我创建了几个这个类的实例,将它们添加到上下文中并运行模拟:

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;
    }

}

是否有一种方法可以收集聚合数据,但是针对每个类的实例分别进行?我想要找出每个生成器生成的所有值的平均值,所以输出统计数据应该是以下格式:

名称,平均值
Gen1,0.458
Gen2,0.512
Gen3,0.463

如果我使用“Generator.getValue”方法创建新的聚合(平均)数据集并在每个时刻重复,我会得到一个大量的值的列表:

时刻,平均值
1,0.365
2,0.456
3,0.728
4,0.091
...

其中每个值都是均值,但是是指特定时刻来自所有生成器的值的均值,而不是一个生成器在所有时刻的值的均值。有没有办法在Repast Simphony中实现这一点?

编辑:当我想使用自定义数据源时,添加class Generator implements AggregateDataSource并添加方法:

@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; // 不是均值,仅用于测试的模拟值。
}

@Override
public void reset() {
    // 待办事项:重置方法
}

会出现错误:

重新聚合数据集,但在Repast Simphony中为每个实例单独进行。

英文:

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&lt;Object&gt; {

    @Override
    public Context build(Context&lt;Object&gt; context) {
        context.add(new Generator(&quot;Gen1&quot;));
        context.add(new Generator(&quot;Gen2&quot;));
        context.add(new Generator(&quot;Gen3&quot;));

        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&lt;?&gt; getDataType() {
	return Double.class;
}

@Override
public Class&lt;?&gt; getSourceType() {
	return Generator.class;
}

@Override
public Object get(Iterable&lt;?&gt; objs, int size) {
	return 7.0; // Not mean, only mock value for testing.
}

@Override
public void reset() {
	// TODO Auto-generated method stub
}

error appears:

重新聚合数据集,但在Repast Simphony中为每个实例单独进行。

答案1

得分: 1

我认为您可以通过定义一个自定义的聚合数据源来实现这一点。您可以通过“自定义数据源”选项卡添加一个,然后提供一个实现了AggregateDataSource接口的类。

get()方法中,您需要遍历所有的Generator对象,并按名称获取均值。您需要为每个生成器实现一个AggregateDataSource。如果您使用一些静态变量,可能可以编写代码,只需遍历一次并获取该时刻所有生成器的均值。不过,我建议您在实现功能之后再考虑这一点。

更新:

您应该为自定义数据源创建一个不同的类,以避免混淆。在get方法中,可迭代对象应允许您遍历所有Generator实例。另外,在需要提供完全限定的名称时,即包名+类名,例如x.y.MyCustomDataSource

英文:

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

huangapple
  • 本文由 发表于 2020年10月18日 00:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64404938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定