如何构建一个像女性一样的物体?

时间:2015-02-25 02:13:33

标签: java repast-simphony

我有一个代理类可以完成任务:

public class Agent {


private Context<Object> context;
    private Geography<Object> geography;
    public int id;
    boolean female;

public Agent(Context<Object> context, Geography<Object> geography, int id, boolean female) {
    this.id = id;
    this.context = context;
    this.geography = geography;
    this.female = female;
}  

... setters getters
... do things methods

}

在上下文构建器类中,我的代理被添加到上下文(由纬度和经度坐标组成的地理空间)中,我想使我的代理女性的随机百分比(女性=真)。

for (int i = 0; i < 100; i++) {
        Agent agent = new Agent(context, geography, i, false);
        int id = i++;
        if(id > 50) {
            boolean female = true;  
        }
        context.add(agent);
        //specifies where to add the agent
        Coordinate coord = new Coordinate(-79.6976, 43.4763);
        Point geom = fac.createPoint(coord);
        geography.move(agent, geom);
    }

我相信上面的代码将最后50个代理构建为女性。我怎样才能让它们随机创建为女性?我改变了很多代理创建的代理。

3 个答案:

答案 0 :(得分:2)

使用您的代码,您始终可以创建一个MALE代理。

在创建Agent的实例

之前尝试评估它是否是女性
Agent agent = null;
boolean isFemale = false;
for (int i = 0; i < 100; i++) {
        int id = i++;
        if(id > 50) {
            isFemale = true;
        }
        agent = new Agent(context, geography, i, isFemale);
        context.add(agent);
        //specifies where to add the agent
        Coordinate coord = new Coordinate(-79.6976, 43.4763);
        Point geom = fac.createPoint(coord);
        geography.move(agent, geom);
    }

如果您想随机,请尝试使用随机实用程序:

        Random random = new Random();
        agent = new Agent(context, geography, i, random.nextBoolean());

希望这会有所帮助

答案 1 :(得分:0)

您可以在for循环外创建一个Random实例,并使用random.nextBoolean()作为agent()的boolean female属性的参数。

答案 2 :(得分:-1)

        Random random = new Random();

        for (int i=0; i < 100; i++)
        {
            boolean isFemale = (random.Next(2) % 2 == 1);
            ...
        }
相关问题