Repast:如何根据特定条件获取特定的代理集?
Repast: how to get a particular agent set based on the specific conditions?
我以前在使用 Netlogo,有一些非常好的内置方法可以让我从总人口中过滤和控制所需的代理。 (参见:http://ccl.northwestern.edu/netlogo/docs/dictionary.html#agentsetgroup)。例如,我可以很容易地在模拟中用简单的代码命令不同的 class 人代理:
ask peoples with [wealth_type = "rich"] [donate money...]
ask peoples with [wealth_type = "poor"] [get money from rich people...]
在Repast中,是否有专门为轻松控制代理集而构建的方法列表?
Repast Simphony Java 中的等效项是使用查询。查询将谓词应用于 Context 中的每个代理和 returns 那些在迭代器中评估为 true 的代理。 PropertyEquals 查询将代理的 属性 w/r 评估为某个值(例如 "wealth_type" 和 "rich")。注意这里的"property"指的是Java属性,即getter类型的方法:
String getWealthType() {
return wealthType;
}
其中 "wealthType" 是 属性 的名称。
举个例子,在JZombies示例模型中,我们可以查询能量等于5的Humans。
Query<Object> query = new PropertyEquals<Object>(context, "energy", 5);
for (Object o : query.query()) {
Human h = (Human)o;
System.out.println(h.getEnergy());
}
query()迭代器returns所有能量等于5的人。
您可以通过提供自己的谓词来使等效性测试变得更复杂一些。例如,
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。谓词在第一个参数中传递代理的 属性 值,在第二个参数中传递要比较的值。鉴于谓词 returns 是一个布尔值,您还可以测试不等式、大于等
Simphony 有多种查询可用。看,
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 方言中执行此操作:
ask (turtles()){
if (wealth_type == "rich") {
donateMoney()
}
if (wealth_type == "poor") {
getMoneyFromRichPeople()
}
}
如果你只想收集 richTurtles,你可以这样做(其中 "it" 是访问使用 findAll 迭代的单个海龟的默认方法):
richTurtles = turtles().findAll{
it.wealth_type == "rich"
}
或使用明确的闭包参数:
richTurtles = turtles().findAll{x->
x.wealth_type == "rich"
}
我以前在使用 Netlogo,有一些非常好的内置方法可以让我从总人口中过滤和控制所需的代理。 (参见:http://ccl.northwestern.edu/netlogo/docs/dictionary.html#agentsetgroup)。例如,我可以很容易地在模拟中用简单的代码命令不同的 class 人代理:
ask peoples with [wealth_type = "rich"] [donate money...]
ask peoples with [wealth_type = "poor"] [get money from rich people...]
在Repast中,是否有专门为轻松控制代理集而构建的方法列表?
Repast Simphony Java 中的等效项是使用查询。查询将谓词应用于 Context 中的每个代理和 returns 那些在迭代器中评估为 true 的代理。 PropertyEquals 查询将代理的 属性 w/r 评估为某个值(例如 "wealth_type" 和 "rich")。注意这里的"property"指的是Java属性,即getter类型的方法:
String getWealthType() {
return wealthType;
}
其中 "wealthType" 是 属性 的名称。
举个例子,在JZombies示例模型中,我们可以查询能量等于5的Humans。
Query<Object> query = new PropertyEquals<Object>(context, "energy", 5);
for (Object o : query.query()) {
Human h = (Human)o;
System.out.println(h.getEnergy());
}
query()迭代器returns所有能量等于5的人。
您可以通过提供自己的谓词来使等效性测试变得更复杂一些。例如,
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。谓词在第一个参数中传递代理的 属性 值,在第二个参数中传递要比较的值。鉴于谓词 returns 是一个布尔值,您还可以测试不等式、大于等
Simphony 有多种查询可用。看,
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 方言中执行此操作:
ask (turtles()){
if (wealth_type == "rich") {
donateMoney()
}
if (wealth_type == "poor") {
getMoneyFromRichPeople()
}
}
如果你只想收集 richTurtles,你可以这样做(其中 "it" 是访问使用 findAll 迭代的单个海龟的默认方法):
richTurtles = turtles().findAll{
it.wealth_type == "rich"
}
或使用明确的闭包参数:
richTurtles = turtles().findAll{x->
x.wealth_type == "rich"
}