当前位置: 首页 > 知识库问答 >
问题:

RepastJava入门

劳和歌
2023-03-14

我是Repast的新用户,请帮我修复这个错误:我有一个运行时错误,如下所示。当我评论addEdge命令时,程序运行良好。

请注意,我的代码与教程完全相同。

我的错误如下:

FATAL [Thread-7] 14:34:36,287 repast.simphony.ui.GUIScheduleRunner - RunTimeException when running the schedule
Current tick (14.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.lang.Thread.run(Unknown Source)
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 jzombies.Zombie.infect(Zombie.java:106)
	at jzombies.Zombie.step(Zombie.java:60)
	... 10 more

以下是我的课程:

僵尸:

package jzombies;

import java.util.ArrayList;
import java.util.List;

import repast.simphony.context.Context;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.graph.Network;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.ContextUtils;
import repast.simphony.util.SimUtilities;

/**
 * @author gamal91
 *
 */
public class Zombie {

	private ContinuousSpace<Object> space;
	private Grid<Object> grid;
	private boolean moved;

	public Zombie(ContinuousSpace<Object> space, Grid<Object> grid) {
		this.space = space;
		this.grid = grid;
	}

	// (Annotation) To iterate each time step.
	@ScheduledMethod  (start = 1 ,interval = 1)
	public void step() {
		// Get the grid location of this Zombie.
		GridPoint pt = grid.getLocation(this);

		GridCellNgh<Human> nghCreator = new GridCellNgh<Human>(grid, pt, Human.class, 1, 1);

		List<GridCell<Human>> gridCells = nghCreator.getNeighborhood(true);

		SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

		GridPoint pointWithMostHumans = null;
		int maxCount = -1;

		for (GridCell<Human> cell : gridCells) {
			if (cell.size() > maxCount) {
				pointWithMostHumans = cell.getPoint();
				maxCount = cell.size();
			}
		}
		moveTowards(pointWithMostHumans);
		infect();
	}

	public void moveTowards(GridPoint pt) {

		if (!pt.equals(grid.getLocation(this))) {
			NdPoint myPoint = space.getLocation(this);
			NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());

			double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, otherPoint);

			// You may delete the zero and see what will happen
			space.moveByVector(this, 1, angle, 0);

			myPoint = space.getLocation(this);
			grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

			moved = true;
		}
	}

	@SuppressWarnings("unchecked")
	public void infect() {
		GridPoint pt = grid.getLocation(this);
		List<Object> humans = new ArrayList<Object>();
		for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
			if (obj instanceof Human) {
				humans.add(obj);
			}
		}

		if (humans.size() > 0) {
			int index = RandomHelper.nextIntFromTo(0, humans.size() - 1);
			Object obj = humans.get(index);
			NdPoint spacePt = space.getLocation(obj);
			Context<Object> context = ContextUtils.getContext(obj);
			context.remove(obj);
			Zombie zombie = new Zombie(space, grid);
			context.add(zombie);
			
			space.moveTo(zombie, spacePt.getX(), spacePt.getY());
			grid.moveTo(zombie, pt.getX(), pt.getY());
			
			Network<Object> net = (Network<Object>) context.getProjection("infection netwrok");
			net.addEdge(this, zombie);
		}
	}
}

人类班

package jzombies;

import java.util.List;

import repast.simphony.engine.watcher.Watch;
import repast.simphony.engine.watcher.WatcherTriggerSchedule;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.SimUtilities;

/**
 * @author gamal91
 *
 */
public class Human {
	private ContinuousSpace<Object> space;
	private Grid<Object> grid;
	private int energy, startingEnergy;

	public Human(ContinuousSpace<Object> space, Grid<Object> grid, int energy) {
		this.space = space;
		this.grid = grid;
		this.energy = startingEnergy = energy;
	}

	
	@Watch (watcheeClassName = "jzombies.Zombie" , 
			watcheeFieldNames = "moved",
			query = "within_moore 1",
			whenToTrigger = WatcherTriggerSchedule.IMMEDIATE)
			
			
	public void run() {
		GridPoint pt = grid.getLocation(this);

		GridCellNgh<Zombie> nghCreator = new GridCellNgh<Zombie>(grid, pt, Zombie.class, 1, 1);
		List<GridCell<Zombie>> gridCells = nghCreator.getNeighborhood(true);

		SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

		GridPoint pointWithLeastZombies = null;
		int minCount = Integer.MAX_VALUE;

		for (GridCell<Zombie> cell : gridCells) {
			if (cell.size() < minCount) {
				pointWithLeastZombies = cell.getPoint();
				minCount = cell.size();
			}
		}

		if (energy > 0) {
			moveTowards(pointWithLeastZombies);
		} else {
			energy = startingEnergy;
		}
	}

	public void moveTowards(GridPoint pt) {
		if (!pt.equals(grid.getLocation(this))) {
			NdPoint myPoint = space.getLocation(this);
			NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
			double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, otherPoint);
			
			// Moves it two units along the calculated angel.
			space.moveByVector(this, 2, angle, 0);

			myPoint = space.getLocation(this);

			grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());
			energy--;

		}
	}
}

jZobies类:

package jzombies;

// import repast.simphony.engine.environment.RunEnvironment;
// import repast.simphony.context.DefaultContext;

import repast.simphony.context.Context;
import repast.simphony.context.space.continuous.ContinuousSpaceFactory;
import repast.simphony.context.space.continuous.ContinuousSpaceFactoryFinder;
import repast.simphony.context.space.graph.NetworkBuilder;
import repast.simphony.context.space.grid.GridFactory;
import repast.simphony.context.space.grid.GridFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.continuous.RandomCartesianAdder;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridBuilderParameters;
import repast.simphony.space.grid.SimpleGridAdder;
import repast.simphony.space.grid.WrapAroundBorders;

public class JZobieBuilder implements ContextBuilder<Object> {

	@Override
	public Context build(Context<Object> context) {

		// Should match the project name
		context.setId("jzombies");

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

		ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
		ContinuousSpace<Object> space = spaceFactory.createContinuousSpace("space", context,
				// Anything is added to this space will be randomly located.
				new RandomCartesianAdder<Object>(), new repast.simphony.space.continuous.WrapAroundBorders(), 50, 50);
		GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
		Grid<Object> grid = gridFactory.createGrid("grid", context, new GridBuilderParameters<Object>(
				// More than one obejct are allowed to occupy a grid point.
				new WrapAroundBorders(), new SimpleGridAdder<Object>(), true, 50, 50));

		int zombieCount = 20;
		for (int i = 0; i < zombieCount; i++) {
			context.add(new Zombie(space, grid));
		}

		int humanCount = 100;
		for (int i = 0; i < humanCount; i++) {
			int energy = RandomHelper.nextIntFromTo(4, 10);
			context.add(new Human(space, grid, energy));
		}

		for (Object obj : context) {
			NdPoint pt = space.getLocation(obj);
			grid.moveTo(obj, (int) pt.getX(), (int) pt.getY());
		}
		return context;
	}
}

共有1个答案

谭梓
2023-03-14

看起来你在上下文中拼错了“传染网络”。打个电话。

 类似资料:
  • 最初只创建一个自定义边缘的解决方案是:Repast Java:创建一个自定义边缘代理来安排特定的操作 1)。现在我需要创建一种以上的自定义边缘来充当唯一代理(例如,在我的模型中,我有路由代理、供应链接代理、关系链接代理)。我必须再次重复链接中描述的上述过程吗?(即添加另一个专用的CustomEdgeCreator类和具有不同名称的CustomEdge类),或者有更有效的方法吗? 2).给定Zomb

  • 我有一个模型,在不同种类的其他代理(对象)之间有很多边(链接)。我想将这些边建模为代理,在那里我可以添加属性并安排操作。看看如何做这项工作的简单例子很有帮助? 更新:我按照您的说明运行模型时出错: 我认为它受到僵尸中这种方法的影响:(但我不知道哪里错了,因为错误消息没有提供具体的说明)

  • quickstart 本教程假设你从头开始,没有Kafka和ZooKeeper历史数据。 quickstart_download 下载 0.10.0.0 的正式版本并解压。 > tar -xzf kafka_2.11-0.10.0.0.tgz > cd kafka_2.11-0.10.0.0 quickstart_startserver Kafka依赖ZooKeeper因此你首先启动一个ZooKe

  • 要开始创建Spring Cloud Stream应用程序,请访问Spring Initializr并创建一个名为“GreetingSource”的新Maven项目。在下拉菜单中选择Spring Boot {supported-spring-boot-version}。在“ 搜索依赖关系”文本框中键入Stream Rabbit或Stream Kafka,具体取决于您要使用的binder。 接下来,在

  • introduction Kafka是一个实现了分布式、分区、提交后复制的日志服务。它通过一套独特的设计提供了消息系统中间件的功能。 这是什么意思呢? 首先我们回顾几个基础的消息系统术语: Kafka将消息源放在称为topics的归类组维护 我们将发布消息到Kafka topic上的处理程序称之为producers 我们将订阅topic并处理消息源发布的信息的程序称之为consumers Kafk

  • 将本地Web服务器公开到互联网 ngrok允许您将本地计算机上运行的Web服务器公开到互联网。只是告诉ngrok你的web服务器正在监听什么端口。 如果你不知道你的web服务器正在监听什么端口,它可能是端口80,HTTP的默认值。 示例:将本地计算机的端口80上的Web服务器公开到互联网 ngrok http 80 当您启动ngrok时,它将在您的终端中显示一个UI,其中包含您的隧道的公共URL以