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

耗尽温柔 提交于 2019-12-11 02:31:00

问题


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...]

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


回答1:


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;
}

where "wealthType" is the name of the property.

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());
}

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());
}

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 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

for more info.

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

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

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"
}


来源:https://stackoverflow.com/questions/57351812/repast-how-to-get-a-particular-agent-set-based-on-the-specific-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!