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

Repast Java: Creating a custom edge agent to schedule specific actions

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

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

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$41f31.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

我觉得是受Zombie中这个方法的影响:(但是我不知道哪里错了,因为报错信息没有给出具体说明)

    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 模型的示例。

首先创建一个 CustomEdgeclass。

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

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 class 中定义 NetworkBuilder 时,您提供一个 CustomEdgeCreator 实例:

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

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

    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");

希望对您有所帮助。