2) Controler Listeners

For now, we can already run a simulation – now it is time to customize them. Simulation may be extended at any of the following points:

At any of those extension points, ControlerEvents and ControlerListeners are available, all of them are available in

org.matsim.core.controler.events.* or

org.matsim.core.controler.listener.*

Examples are IterationStartsEvent or IterationStartsListener.

A sample Listener class might look like this:

public class MyIterationListener1 implements IterationEndsListener {
   
    public void notifyIterationEnds(IterationEndsEvent event) {
        System.out.println("iteration " + event.getIteration()  + " / " + event.getControler().getLastIteration());
        //basic example - lists the Iteration that just ended.
        }

}

To include the Listener into your controler, you will need to add it:

controler.addControlerListener(new MyIterationListener1());

Of course, you can also combine EventsHandler with your ControlerListener:

private MyEventHandler2 eventHandler;
//will include a eventHandler from the last tutorial
public void notifyStartup(StartupEvent event) {
    this.eventHandler = new MyEventHandler2(event.getControler().getPopulation().getPersons().size());
    //gets the size of the population
    event.getControler().getEvents().addHandler(this.eventHandler);
    //adds the Handler
    }

Now, you could try to write a ControlerListener that calculates the average travel time for each iteration and writes it to a chart after the simulation has finished. You will need to implement StartupListener, IterationEndsListener, ShutdownListener for to do so could use MyEventHandler2 for calculating the average travel time. You will find the solution in MyControlerListener.java.