我有一个包含6个盒子的列表(盒子清单),每个盒子每盒有1个对象(num_objects)。每个框都存在于地理环境中(位于特定的纬度和经度坐标处):
//iterate through each of the boxes in the list (6)
for (int i = 0; i < boxlist.size(); i++){
//get the first box... then second box... etc (call it target box)
Box targetbox = boxlist.get(i);
Context context = ContextUtils.getContext(targetbox);
Geography<Object> geography = (Geography)context.getProjection("Geography");
Geometry geom = geography.getGeometry(targetbox);
//get the coordinates of the target box
Coordinate coord = geom.getCoordinates()[0];
//for each of the 6 boxes, get the number of objects in the target box
double num = targetbox.getNum_objects();
// print the number of objects in each box (1)
System.out.println(num);
//create random utility
Random random = new Random();
// create the same number of BoxObjects as the num_objects in the box and place them in the geography at the same location as the box they are in (take them out of the box)
for (int j = 0; j < num; j++) {
boolean randomBoolean = random.nextBoolean();
boolean anotherBoolean = false;
BoxObject obj = new BoxObject(context, geography, randomBoolean, anotherBoolean);
context.add(obj);
// move to the object to the same coordinates of its box
geography.move(obj, new GeometryFactory().createPoint(coord));
}
}
我的循环正确地计算出盒子中有6个盒子,其中有1个对象应该创建6个对象,但是它创建了12个对象。我该如何解决这个问题?
注意:在实际模拟中,框可能有多个对象。
答案 0 :(得分:1)
我的问题是这段代码嵌套在Agent类中。我有两个Agent,它运行方法两次,每个代理一次(生成双重对象)。当我将此代码移动到主模型类时(从技术上讲,它与代理没有任何关系),它完美地运行。
答案 1 :(得分:0)
你有一个目标框,里面有一个对象。 Box targetbox = boxlist.get(i);
您还可以获取目标框中包含对象的上下文。 Context context = ContextUtils.getContext(targetbox);
然后将另一个对象添加到同一个上下文中。无论BoxObject做什么,当你传递上下文时,它应该从上下文中删除先前的对象,以便你只在结果中有新创建的对象。