MATSIM
PlansScoringImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansScoring.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.scoring;
22 
23 import com.google.inject.Inject;
24 import com.google.inject.Singleton;
36 
37 
46 @Singleton
47 final class PlansScoringImpl implements PlansScoring, ScoringListener, IterationEndsListener {
48 
49  @Inject private ScoringConfigGroup scoringConfigGroup;
50  @Inject private ControllerConfigGroup controllerConfigGroup;
51  @Inject private Population population;
52  @Inject private OutputDirectoryHierarchy controlerIO;
53  @Inject private ScoringFunctionsForPopulation scoringFunctionsForPopulation;
54  @Inject private ExperiencedPlansService experiencedPlansService;
55  @Inject private NewScoreAssigner newScoreAssigner;
56 
57  @Override
58  public void notifyScoring(final ScoringEvent event) {
59  scoringFunctionsForPopulation.finishScoringFunctions();
60  newScoreAssigner.assignNewScores(event.getIteration(), this.scoringFunctionsForPopulation, this.population);
61  }
62 
63  @Override
64  public void notifyIterationEnds(final IterationEndsEvent event) {
65  this.experiencedPlansService.finishIteration();
66  // (currently sets scores to experienced plans)
67 
68  if(scoringConfigGroup.isWriteExperiencedPlans()) {
69  final int writePlansInterval = controllerConfigGroup.getWritePlansInterval();
70  if (writePlansInterval > 0 && (event.getIteration() % writePlansInterval == 0 || event.isLastIteration())) {
71  this.experiencedPlansService.writeExperiencedPlans(controlerIO.getIterationFilename(event.getIteration(), "experienced_plans.xml.gz"));
72  this.scoringFunctionsForPopulation.writePartialScores(controlerIO.getIterationFilename(event.getIteration(), "experienced_plans_scores.txt.gz"));
73  }
74  }
75  if (scoringConfigGroup.isMemorizingExperiencedPlans() ) {
76  for ( Person person : this.population.getPersons().values() ) {
77  Plan experiencedPlan = this.experiencedPlansService.getExperiencedPlans().get( person.getId() ) ;
78  if ( experiencedPlan==null ) {
79  throw new RuntimeException("experienced plan is null; I don't think this should happen") ;
80  }
81  Plan selectedPlan = person.getSelectedPlan() ;
82  selectedPlan.getCustomAttributes().put(ScoringConfigGroup.EXPERIENCED_PLAN_KEY, experiencedPlan ) ;
83  }
84  }
85  }
86 
87 }