进餐:如何根据特定条件获得特定的代理集? [英] Repast: how to get a particular agent set based on the specific conditions?

查看:89
本文介绍了进餐:如何根据特定条件获得特定的代理集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用Netlogo,并且有一些非常好的内置方法,这些方法使我可以从总人口中筛选和控制所需的代理。 (请参阅: http://ccl.northwestern.edu/netlogo/docs/dictionary .html#agentsetgroup )。例如,我可以很容易地用简单的代码在模拟中命令不同类别的人员代理:

I am previously working with Netlogo and there are some very good built-in methods that allow me to filter and control the desired agents from the total population. (see: http://ccl.northwestern.edu/netlogo/docs/dictionary.html#agentsetgroup). For instance, I could very easily to command the different class of people agent in a simulation with simple codes like:

 ask peoples with [wealth_type = "rich"] [donate money...] 
 ask peoples with [wealth_type = "poor"] [get money from rich people...]

在Repast中,是否存在专门用于轻松控制代理集的方法列表?

In Repast, are there list of methods specifically built for easy controlling of agent set?

推荐答案

Repast Simphony Java中的等效项是使用查询。查询将断言应用于上下文中的每个代理,并在迭代器中返回评估为true的谓词。 PropertyEquals查询将代理的w / r属性评估为某个值(例如 wealth_type和 rich)。请注意,此处的属性是指Java属性,即吸气剂类型方法:

The equivalent in Repast Simphony Java is to use a Query. Queries apply a predicate to each agent in the Context and returns those that evaluate to true in an iterator. The PropertyEquals query evaluates an agent's property w/r to some value (e.g. "wealth_type" and "rich"). Note that "property" here refers to a Java property, i.e., a getter type method:

String getWealthType() {
     return wealthType;
}

其中 wealthType是属性的名称。

where "wealthType" is the name of the property.

例如,在JZombies示例模型中,我们可以查询能量等于5的人类。

As an example, in the JZombies example model, we can query Humans whose energy is equal to 5.

Query<Object> query = new PropertyEquals<Object>(context, "energy", 5);
for (Object o : query.query()) {
    Human h = (Human)o;
    System.out.println(h.getEnergy());
}

query()迭代器返回所有能量等于5的人类。

The query() iterator returns all the humans whose energy is equal to 5.

通过提供自己的谓词,在等效性测试中会变得更加复杂。例如,

You can get a bit more complicated in the equivalence test by providing your own predicate. For example,

PropertyEqualsPredicate<Integer, Integer> pep = (a, b) -> {
    return a * 2 == b;
};

Query<Object> query2 = new PropertyEquals<Object>(context, "energy", 8, pep);
for (Object o : query2.query()) {
    Human h = (Human)o;
    System.out.println(h.getEnergy());
}

在这里,我们正在检查能量* 2 == 8。在第一个参数中传递了代理的属性值,在第二个参数中传递了要与之比较的值。考虑到谓词返回的是布尔值,您还可以测试不等式(大于等)。

Here, we are checking if the energy * 2 == 8. The predicate is passed the agent's property value in the first parameter and the value to compare against in the second parameter. Given that the predicate returns a boolean, you could also test for inequality, greater than etc.

Simphony提供了多种查询。参见

Simphony has a variety of queries available. See,

https://repast.github.io/docs/api/repast_simphony/repast/simphony/query/package-summary.html
https://repast.github.io/docs/RepastReference/RepastReference.html#_repast_model_design_fundamental_concepts

了解更多信息。

您也可以使用Simphony的ReLogo方言来做到这一点:

You can also do this in Simphony's ReLogo dialect:

ask (turtles()){
    if (wealth_type == "rich") {
        donateMoney()
    }
    if (wealth_type == "poor") {
        getMoneyFromRichPeople()
    }
}

如果您只想收集richTurtles,可以这样做(其中 it是访问被迭代的单个海龟的默认方法结束于findAll):

If you want to just collect the richTurtles you can do (where "it" is the default method to access the individual turtle that is iterated over with findAll):

richTurtles = turtles().findAll{
    it.wealth_type == "rich"
}

或带有明确的闭包参数:

or with an explicit closure argument:

richTurtles = turtles().findAll{x->
    x.wealth_type == "rich"
}

这篇关于进餐:如何根据特定条件获得特定的代理集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆