每个代理都有一个私有的布尔变量"Happy?“。如何计算Happy的代理数量?= True
在餐饮中有没有直接的方法?或者我已经遍历了所有的代理并逐个计算它们?
更新:
我已经尝试过全局调度方法:https://repast.github.io/docs/RepastReference/RepastReference.html#schedule-global
当我在ContextBuilder中使用@ScheduledMethods放入下面的代码时,它不工作。
grid.moveTo(this_girl, group_x,group_y);
}
}
return context;
}
@ScheduledMethod(start = 1, interval = 1, shuffle=true)
public void step () {
Context<Object> context = ContextUtils.getContext(this);
Query<Object> query = new PropertyEquals<Object>(context, "happy", true);
int end_count = 0;
System.out.println(end_count);
for (Object o : query.query()) {
if (o instanceof Boy) {
end_count ++;
}
if (o instanceof Girl) {
end_count ++;
}
}
System.out.println(end_count);
if (end_count == 70) {
RunEnvironment.getInstance().endRun();
}
}
}
如果我把上面的代码放在男孩代理或女孩代理的动作中,它就会起作用。
@ScheduledMethod(start = 1, interval = 1,shuffle=true)
public void step() {
relocation();
update_happiness();
endRun();
}
public void endRun( ) {
Context<Object> context = ContextUtils.getContext(this);
Query<Object> query = new PropertyEquals<Object>(context, "happy", true);
int end_count = 0;
System.out.println(end_count);
for (Object o : query.query()) {
if (o instanceof Boy) {
end_count ++;
}
if (o instanceof Girl) {
end_count ++;
}
}
System.out.println(end_count);
if (end_count == 70) {
RunEnvironment.getInstance().endRun();
}
}
发布于 2019-08-20 21:14:29
您可以使用查询来执行此操作--请参阅此问题的查询答案:
Repast: how to get a particular agent set based on the specific conditions?
您还可以在向其传递谓词的上下文中使用query方法,如果该谓词满足,则该谓词返回true。
在这两种情况下,您都需要一个用于私有布尔快乐字段的访问器方法--例如
public boolean isHappy() {
return happy;
}
同样,在这两种情况下,查询都会返回一个遍历所有代理的迭代数,其中happy为true,而不是一个集合,您可以在该集合中获取大小来获取计数。所以,你必须遍历它并递增一个计数器。
更新:
您当前的问题是日程安排。你不能轻易地在ConetextBuilder上调度一个方法,因为它实际上不是模型的一部分,而是用来初始化它的。调度所需内容的最简单方法是在ContextBuilder中显式地调度它,如下所示:
RunEnvironment.getInstance().getCurrentSchedule().schedule(ScheduleParameters.createRepeating(1, 1, ScheduleParameters.LAST_PRIORITY), () -> {
Query<Object> query = new PropertyEquals<Object>(context, "happy", true);
int end_count = 0;
System.out.println(end_count);
for (Object o : query.query()) {
if (o instanceof Boy) {
end_count++;
}
if (o instanceof Girl) {
end_count++;
}
}
System.out.println(end_count);
if (end_count == 70) {
RunEnvironment.getInstance().endRun();
}
});
LAST_PRIORITY应该确保所有代理行为都将在幸福计数被轮询之前发生。
https://stackoverflow.com/questions/57569645
复制相似问题