Skip to content

How To Set Up a Basic Simulation

Sergeus edited this page Jun 17, 2012 · 5 revisions

Setting up a basic simulation primarily involves modifying two principle objects: the your simulation file in uk.ac.ic.kyoto.simulations and the runtime arguments to run your project.

Simulation Files

Modifying Your Simulation File

If you are working on an existing simulation, open its file and follow the steps starting in Environment Services.

Creating a New Simulation File

If you are creating a new type of simulation, or wish to use a new file for testing, then create a new class in uk.ac.ic.kyoto.simulations and inherited from InjectedSimulation. The easiest way to create a valid skeleton of simulation code is to go to uk.ac.ic.kyoto.simulations.Simulation and copy and paste that file into your new file. Eclipse will complain about the lines that don't match your new class name, so follow the prompts to fix those. Then work through the file using the following steps.

Environment Services

There are two main sections that you'll be changing in setting up a simulation. The first is the getModules() function. Within the first call to modules.add(), all of the environment services that are utilized in your simulation should be associated with the environment. Here's an example of a call to modules.add():

	modules.add(new AbstractEnvironmentModule()
		.addActionHandler(SubmitCarbonEmissionReportHandler.class)
		.addGlobalEnvironmentService(CarbonReportingService.class)
		.addParticipantEnvironmentService(Monitor.class)
		.addParticipantEnvironmentService(ParticipantCarbonReportingService.class)
		.addParticipantEnvironmentService(TimeService.class)
		.addParticipantEnvironmentService(Economy.class));		

Note that additional calls to addParticipantEnvironmentService are appended before the final ");".

Instantiating Agents

Still in Simulation.java, within the addToScenario() function, each agent that you wish to exist in your simulation must be instantiated. The Scenario object that you're adding the agents to is supplied as an argument to this function and is called "s". Each agent must then be instantiated within this function like you would any other java class. Here's an example of instantiating the Canada agent:

	AbstractParticipant p = new CanadaAgent(Random.randomUUID(),"CANADA","CAN",20000,10000,5000000,3,28000,50000,30000);

Note that regardless of which agent type you are instantiating, the variable MUST be an AbstractParticipant. Also, the constructor for each country is subject to change, and copying this code into a future simulation file may or may not run correctly.

After instantiating all of your agents, you must add each one to the scenario. Here's an example that adds the above Canada agent to the scenario:

	s.addParticipant(p);

Modifying Your Runtime Arguments

It's advisable that you use the debug run configuration within Eclipse to run your simulation. To view your runtime settings, click the little down arrow next to the bug icon on the top left of your screen and click "Debug Configurations...".

A window will pop up. On the left hand side, select RunSimulation within Java Application. You will be on the "Main" tab of the debug configurations. Make sure that "Stop in main" is UNTICKED.

Then move to the Arguments tab. In the "Program Arguments:" field, there will, by default, be a set of generic references to command line arguments. You will need to remove these and put in the following. At time of writing, to use the default simulation, the first argument should be: uk.ac.ic.kyoto.simulations.Simulation

This tells Presage2 where to find the simulation class. The minimum information you need to supply is how long (in ticks) you want the simulation to run for. This is determined by an injected variable called finishTime. You set this like so: finishTime=5

Using these two parameters, the arguments box should contain the following:

	uk.ac.ic.kyoto.simulations.Simulation finishTime=5

If you wish to use a different simulation file, alter the first argument. Note that you do NOT include the file extension in the arguments tab.

	uk.ac.ic.kyoto.simulations.YourSimFileName finishTime=5

Click Apply. Then debug, and watch as the console is flooded by output.

TroubleShooting

Exceptions

Exceptions are the easiest kind of bug to troubleshoot, since they are represented clearly in the console output. However, note that NOT ALL EXCEPTIONS ARE PRINTED IN RED.

Instantiation Issues

If your simulation output doesn't have confirmation of agent instantiation, then there is most likely an issue with your constructor call in your simulation file. Unfortunately, you're gonna have to compare that to the constructor in your agent (and make sure that one matches all of its superclass constructors). Here's an example of the Presage2 output confirming instantiation of the above Canada agent:

	1214 [main] DEBUG CANADA  - Created Participant CANADA, UUID: c58bcf98-9145-4ad5-905d-14e2435d6d6c

Action Handling Exceptions

If you are getting action handling exceptions, then make sure your action handlers are added correctly into your simulation file, during the call to getModules(). An example action handler addition is as follows.

	.addActionHandler(SubmitCarbonEmissionReportHandler.class)

EnvironmentService Exception

If environment services are generating exceptions, make sure your calls to addParticipantEnvironmentService() are in the correct place. (The brackets make it very easy to put in the wrong place.) If there are compile or runtime errors in the class itself, contact the author of the environment service.

If you are the environment service's author, then ensure your class extends EnvironmentService. Make sure your constructor looks like this:

@Inject
public TimeService(EnvironmentSharedStateAccess sharedState) {
	super(sharedState);
}

Replacing TimeService with the name of your service's class. Note that Eclipse will attempt to autofill a protected constructor. This constructor will get rid of compile errors, but it won't work in a simulation. You MUST use the injected constructor above.

Event-Related Exceptions

Either listening on or subscribing to events requires a few specific lines of code to work properly. You will receive a variety of event-related exceptions otherwise.

To listen on events you must include the following code EXACTLY:

EventBus eb;
@Inject
public void setEB(EventBus eb) {
	this.eb = eb;
	eb.subscribe(this);
}

Where eb is a field in your class of type EventBus. (The same name as the argument to the function, just to be confusing.)

Then you must have a function that will be executed when the event takes place. It should be formatted like so:

@EventListener
public void updateTickCounter (EndOfTimeCycle e) {
	// Your functionality here
}

You can call the function (in this case, updateTickCounter) anything you like. The argument must be an instance of the event you are listening for. (In this case, EndOfTimeCycle)

If you want to publish new events into the simulation, then you must include the injected EventBus setter above. When you want to publish an event, create a new instance of your class that extends the Event class, and call:

	eb.publish(yourEvent);