1

每个代理都有一个私有布尔变量“Happy?”。如何计算具有[快乐?=真]?

有没有直接吃的方法?或者我已经遍历所有代理并单独计算它们?

更新:

我试过全局调度方法: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();
        }
    }
4

1 回答 1

3

您可以为此使用查询 - 请参阅此问题的查询答案:

Repast:如何根据特定条件获取特定代理集?

您还可以在上下文中使用查询方法,向它传递一个谓词,如果高兴,谓词返回 true。

在这两种情况下,您都需要一个私有布尔快乐字段的访问器方法——例如

public boolean isHappy() {
   return happy;
}

同样在这两种情况下,查询返回一个可迭代的所有代理,其中快乐为真,而不是一个集合,您可以在其中获取大小来获取计数。所以,你必须遍历它并增加一个计数器。

更新:

您当前的问题是日程安排。您不能轻松地在 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 应该确保所有代理行为都会在幸福计数被轮询之前发生。

于 2019-08-20T13:14:29.100 回答