Repast Java入门

时间:2017-11-03 21:43:05

标签: repast-simphony

我是新的Repast用户,您能否帮我修复此错误: 我有一个运行时错误,如下所示。当我评论addEdge命令时,程序运行良好。 <<< addEdge(this,zombie); >>>

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

我的错误如下:



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
&#13;
&#13;
&#13;

以下是我的课程:

僵尸:

&#13;
&#13;
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);
		}
	}
}
&#13;
&#13;
&#13;

人类

&#13;
&#13;
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--;

		}
	}
}
&#13;
&#13;
&#13;

jZobies课程:

&#13;
&#13;
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;
	}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

看起来你在context.getProjection调用中拼错了“感染网络”。