20 package org.matsim.core.controler;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
31 import java.io.UncheckedIOException;
33 abstract class AbstractController {
37 private static final Logger log = LogManager.getLogger(AbstractController.class);
50 private Integer thisIteration = null;
52 protected AbstractController() {
57 log.info(
"Used Controler-Class: " + this.getClass().getCanonicalName());
58 this.controlerListenerManagerImpl = controlerListenerManager;
59 this.controlerListenerManagerImpl.setControler(matsimServices);
60 this.stopwatch = stopWatch;
63 private void resetRandomNumbers(
long seed,
int iteration) {
71 this.controlerIO = controlerIO;
75 protected final void run(
final Config config) {
77 MatsimRuntimeModifications.MyRunnable runnable =
new MatsimRuntimeModifications.MyRunnable() {
79 public void run()
throws MatsimRuntimeModifications.UnexpectedShutdownException {
88 public void shutdown(
boolean unexpected) {
92 MatsimRuntimeModifications.run(runnable);
97 protected abstract void loadCoreListeners();
99 protected abstract void runMobSim();
101 protected abstract void prepareForSim();
103 protected abstract void prepareForMobsim() ;
113 protected abstract boolean mayTerminateAfterIteration(
int iteration);
114 protected abstract boolean shouldTerminate(
int iteration);
116 private void doIterations(
Config config)
throws MatsimRuntimeModifications.UnexpectedShutdownException {
122 while (!doTerminate) {
123 boolean isLastIteration = mayTerminateAfterIteration(iteration);
124 iteration(config, iteration, isLastIteration);
125 doTerminate = isLastIteration && shouldTerminate(iteration);
130 final String MARKER =
"### ";
132 private void iteration(
final Config config,
final int iteration,
boolean isLastIteration)
throws MatsimRuntimeModifications.UnexpectedShutdownException {
133 this.thisIteration = iteration;
134 this.getStopwatch().beginIteration(iteration);
137 log.info(MARKER +
"ITERATION " + iteration +
" BEGINS");
138 this.getControlerIO().createIterationDirectory(iteration);
141 iterationStep(
"iterationStartsListeners",
new Runnable() {
149 iterationStep(
"replanning",
new Runnable() {
157 mobsim(config, iteration, isLastIteration);
159 iterationStep(
"scoring",
new Runnable() {
162 log.info(MARKER +
"ITERATION " + iteration +
" fires scoring event");
167 iterationStep(
"iterationEndsListeners",
new Runnable() {
170 log.info(MARKER +
"ITERATION " + iteration +
" fires iteration end event");
175 this.getStopwatch().endIteration();
177 this.getStopwatch().writeSeparatedFile(this.getControlerIO().getOutputFilename(
"stopwatch.csv"), config.
global().
getDefaultDelimiter());
178 }
catch (UncheckedIOException e) {
179 log.error(
"Could not write stopwatch file.", e);
184 log.info(MARKER +
"ITERATION " + iteration +
" ENDS");
188 private void mobsim(
final Config config,
final int iteration,
boolean isLastIteration)
throws MatsimRuntimeModifications.UnexpectedShutdownException {
195 iterationStep(
"beforeMobsimListeners",
new Runnable() {
202 iterationStep(
"prepareForMobsim",
new Runnable(){
210 iterationStep(
"mobsim",
new Runnable() {
218 catch ( Throwable t ) {
225 log.error(
"Mobsim did not complete normally! afterMobsimListeners will be called anyway." , t );
234 iterationStep(
"afterMobsimListeners",
new Runnable() {
237 log.info(MARKER +
"ITERATION " + iteration +
" fires after mobsim event");
244 private void iterationStep(String iterationStepName,
Runnable iterationStep)
throws MatsimRuntimeModifications.UnexpectedShutdownException {
245 this.getStopwatch().beginOperation(iterationStepName);
247 this.getStopwatch().endOperation(iterationStepName);
248 if (Thread.interrupted()) {
249 throw new MatsimRuntimeModifications.UnexpectedShutdownException();
273 public final Integer getIterationNumber() {
274 return this.thisIteration;
static Random getRandom()
static final String DIVIDER
void addControlerListener(final ControlerListener l)
void fireControlerBeforeMobsimEvent(final int iteration, boolean isLastIteration)
static final void checkConfigConsistencyAndWriteToLog(Config config, final String message)
void addCoreControlerListener(final ControlerListener l)
void fireControlerReplanningEvent(final int iteration, boolean isLastIteration)
int getCreateGraphsInterval()
void fireControlerScoringEvent(final int iteration, boolean isLastIteration)
void fireControlerIterationEndsEvent(final int iteration, boolean isLastIteration)
static void start(int interval_seconds)
static void closeOutputDirLogging()
void fireControlerIterationStartsEvent(final int iteration, boolean isLastIteration)
void fireControlerShutdownEvent(final boolean unexpected, int iteration)
final ControllerConfigGroup controller()
void fireControlerAfterMobsimEvent(final int iteration, boolean isLastIteration)
final GlobalConfigGroup global()
void fireControlerStartupEvent()
int getMemoryObserverInterval()
static final void initLogging(OutputDirectoryHierarchy outputDirectoryHierarchy)
String getDefaultDelimiter()