【问题标题】:Repast: how to count the total number of agents satisfying the specific conditionRepast:如何计算满足特定条件的代理总数
【发布时间】:2019-08-21 22:32:38
【问题描述】:

每个代理都有一个私有布尔变量“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();
        }
    }

【问题讨论】:

    标签: agent-based-modeling repast-simphony


    【解决方案1】:

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

    Repast: how to get a particular agent set based on the specific conditions?

    您还可以在上下文中使用查询方法,向它传递一个谓词,如果高兴,谓词返回 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 应该确保所有代理行为都会在幸福计数被轮询之前发生。

    【讨论】:

    • 您好,我实际上想知道如何安排在每种代理方法之外的全局操作方法/过程。像这个一样,我有两种类型的代理(男孩和女孩)。我想计算快乐的总人数,这种方法不需要遍历每个男孩和女孩代理,而是在所有代理都采取行动后立即在全局级别执行。
    • 感谢它的工作。但这在我看来不是模块化的。是否有更结构化的方式来使用 method() 来安排全局行为?最终,我想要一个结构,其中包含一些用于特定代理的方法()和一些用于全局行为的方法,并且所有这些都遵循特定的执行顺序。如何做到这一点?谢谢
    • 我已经有一段时间没有这样做了,但我认为它应该可以工作。通过扩展 DefaultContext 创建自定义上下文,并在您的 ContextBuilder 中忽略传入的 Context,创建、填充并返回您的。使用自定义上下文中方法的调度注释来实现全局行为。您还可以将上面的“快乐”代码移动到实现 IAction 的单独类中,并像上面一样手动安排。
    • 您介意我提出一个专门针对此问题的新问题吗?这对我构建基于代理的模型非常重要。
    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2022-06-22
    • 2016-05-09
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    相关资源
    最近更新 更多