Repast Java:创建自定义边缘代理以计划特定操作



我有一个模型,在不同类型的其他代理(对象(之间有很多边(链接(。我想将这些边缘建模为代理,我可以在其中添加属性和计划操作。查看有关如何完成此工作的简单示例很有帮助吗?

更新: 我按照您的说明进行操作,并在运行模型时出现错误:

FATAL [Thread-2] 12:45:02,901 repast.simphony.ui.GUIScheduleRunner - RunTimeException when running the schedule
Current tick (1.0)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:72)
at repast.simphony.engine.controller.ScheduledMethodControllerAction$ScheduleMethodAllAction.execute(ScheduledMethodControllerAction.java:333)
at repast.simphony.engine.schedule.DefaultAction.execute(DefaultAction.java:38)
at repast.simphony.engine.schedule.ScheduleGroup.executeList(ScheduleGroup.java:205)
at repast.simphony.engine.schedule.ScheduleGroup.execute(ScheduleGroup.java:231)
at repast.simphony.engine.schedule.Schedule.execute(Schedule.java:352)
at repast.simphony.ui.GUIScheduleRunner$ScheduleLoopRunnable.run(GUIScheduleRunner.java:52)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
at jzombies.Zombie$$FastClassByCGLIB$$6141f31.invoke(<generated>)
at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53)
at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:69)
... 7 more
Caused by: java.lang.NullPointerException
at repast.simphony.query.PropertyGreaterThan.createPredicate(PropertyGreaterThan.java:72)
at repast.simphony.query.AbstractPropertyQuery.query(AbstractPropertyQuery.java:83)
at jzombies.Zombie.query_energy(Zombie.java:141)
at jzombies.Zombie.step(Zombie.java:67)
... 10 more

我认为它在僵尸中受到这种方法的影响:(但我不知道哪里出了问题,因为错误消息没有提供具体说明(

public void query_energy() {
//      Zombie this_zombie = new Zombie (space, grid, 9999);
Context<Object> context = ContextUtils.getContext(this);
Query<Object> query = new PropertyGreaterThan<Object>(context, "id", 2);
for (Object o : query.query()) {
Zombie h = (Zombie)o;
System.out.println("zombie id: " + h.getID());
}
}

下面是一个基于 JZombies_Demo 模型的示例。

首先创建一个自定义边缘类。

package jzombies;
import repast.simphony.space.graph.RepastEdge;
public class CustomEdge<T> extends RepastEdge<T> {
private double customProperty;
public double getCustomProperty() {
return customProperty;
}
public void setCustomProperty(double customProperty) {
this.customProperty = customProperty;
}
public CustomEdge(T source, T target, boolean directed, double weight) {
super(source, target, directed, weight);
}
public CustomEdge(T source, T target, boolean directed) {
super(source, target, directed);
}
public void customAction() {
// define custom action here
}
}

然后是 CustomEdgeCreator 类:

package jzombies;
import repast.simphony.space.graph.EdgeCreator;
public class CustomEdgeCreator<T> implements EdgeCreator<CustomEdge<T>, T> {
public Class<CustomEdge> getEdgeType() {
return CustomEdge.class;
}
public CustomEdge<T> createEdge(T source, T target, boolean isDirected, double weight) {
return new CustomEdge<T>(source, target, isDirected, weight);
}
}

然后,当您在 JZombiesBuilder 类中定义 NetworkBuilder 时,您提供了一个 CustomEdgeCreator 实例:

NetworkBuilder<Object> netBuilder = new NetworkBuilder<Object>(
"infection network", context, true).setEdgeCreator(new CustomEdgeCreator());
netBuilder.buildNetwork();

此时,无论何时向网络添加边缘,您都可以访问边缘实例并计划您定义的任何自定义操作,例如在 Zombie 类中:

Network<Object> net = (Network<Object>)context.getProjection("infection network");
CustomEdge edge = (CustomEdge)net.addEdge(this, zombie);
RunEnvironment.getInstance().getCurrentSchedule().schedule(
ScheduleParameters.createOneTime(RunEnvironment.getInstance().getCurrentSchedule().getTickCount() + 20), edge, "customAction");

我希望这有所帮助。

最新更新