1 package org.matsim.core.replanning.inheritance;
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.atomic.AtomicLong;
53 import com.google.inject.Singleton;
66 public static final String
PLAN_ID =
"planId";
71 public static final String
NONE =
"NONE";
75 long numberOfPlanInheritanceRecordsCreated = 0;
88 CompressionType compressionType =
event.getServices().getConfig().controller().getCompressionType();
96 for (
Plan plan : person.getPlans()) {
98 plan.setPlanMutator(INITIAL_PLAN);
99 plan.setIterationCreated(0);
108 Set<String> activeSubpopulations =
new HashSet<>();
110 activeSubpopulations.add(strategySetting.getSubpopulation());
113 Set<String> planStrategiesNames =
new HashSet<>();
114 for (String subpopulation : activeSubpopulations) {
116 planStrategiesNames.add(planStrategy.toString());
120 ArrayList<String> strategies =
new ArrayList<>(planStrategiesNames.size() + 1);
121 strategies.addAll(planStrategiesNames);
122 Collections.sort(strategies);
123 strategies.add(0, INITIAL_PLAN);
135 StringBuffer header =
new StringBuffer();
136 header.append(
"iteration"); header.append(DELIMITER);
137 for (
int i = 0; i < strategies.size(); i++) {
139 header.append(DELIMITER);
141 header.append(strategies.get(i));
145 planStrategyShareWriter.write(header.toString());
146 planStrategyShareWriter.newLine();
147 }
catch (IOException e) {
148 throw new RuntimeException(
"Could not initialize the plan strategy share writer!", e);
158 Set<Id<Plan>> activePlanIds =
new HashSet<>();
159 Set<Id<Plan>> selectedPlanIds =
new HashSet<>();
162 for (
Plan plan : person.getPlans()) {
164 if (plan.getPlanMutator() == null) {
166 plan.setPlanMutator(INITIAL_PLAN);
170 if (plan.getIterationCreated() ==
event.getIteration()) {
174 planInheritanceRecord.
setAgentId(person.getId());
175 planInheritanceRecord.
setPlanId(
Id.
create(Long.toString(++
this.numberOfPlanInheritanceRecordsCreated, 36),
Plan.class));
177 plan.setPlanId(planInheritanceRecord.
getPlanId());
179 planInheritanceRecord.
setMutatedBy(plan.getPlanMutator());
181 this.planId2planInheritanceRecords.put(planInheritanceRecord.
getPlanId(), planInheritanceRecord);
185 this.planId2planInheritanceRecords.get(plan.getId()).getIterationsSelected().add(event.
getIteration());
186 selectedPlanIds.add(plan.getId());
189 activePlanIds.add(plan.getId());
193 List<Id<Plan>> deletedPlans =
new ArrayList<>();
194 for (
Id<Plan> planId : this.planId2planInheritanceRecords.keySet()) {
195 if (!activePlanIds.contains(planId)) {
196 deletedPlans.add(planId);
200 for (
Id<Plan> deletedPlanId : deletedPlans) {
201 PlanInheritanceRecord deletedPlanInheritanceRecord = this.planId2planInheritanceRecords.remove(deletedPlanId);
203 this.planInheritanceRecordWriter.write(deletedPlanInheritanceRecord);
206 this.planInheritanceRecordWriter.flush();
216 Map<String, AtomicLong> strategy2count =
new HashMap<>();
217 for (String strategyName : strategies) {
218 strategy2count.put(strategyName,
new AtomicLong(0));
221 String mutatedBy = planId2planInheritanceRecords.get(planId).getMutatedBy();
222 strategy2count.get(mutatedBy).incrementAndGet();
224 long sum = strategy2count.values().stream().mapToLong(count -> count.get()).sum();
225 StringBuffer line =
new StringBuffer();
226 line.append(currentIteration);
227 line.append(DELIMITER);
228 for (
int i = 0; i < strategies.size(); i++) {
230 line.append(DELIMITER);
232 line.append(String.valueOf(strategy2count.get(strategies.get(i)).doubleValue() / sum));
235 writer.write(line.toString());
237 }
catch (IOException e) {
238 throw new RuntimeException(
"Could not initialize the plan strategy share writer!", e);
247 this.planInheritanceRecordWriter.write(planInheritanceRecord);
250 this.planInheritanceRecordWriter.flush();
251 this.planInheritanceRecordWriter.close();
254 this.selectedPlanStrategyShareWriter.flush();
255 this.selectedPlanStrategyShareWriter.close();
256 this.planStrategyShareWriter.flush();
257 this.planStrategyShareWriter.close();
258 }
catch (IOException e) {
262 this.planId2planInheritanceRecords.clear();
static final String FILENAME_PLAN_INHERITANCE_RECORDS
static final String INITIAL_PLAN
BufferedWriter planStrategyShareWriter
OutputDirectoryHierarchy getControlerIO()
BufferedWriter initializeDistributionWriter(ArrayList< String > strategies, String filename)
final List< GenericPlanStrategy< Plan, Person > > getStrategies(final String subpopulation)
void setPlanId(Id< Plan > planId)
void setAgentId(Id< Person > agentId)
Map< Id< Person >,? extends Person > getPersons()
void setMutatedBy(String mutatedBy)
ArrayList< String > strategies
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
static< T > Id< T > create(final long key, final Class< T > type)
final LinkedBindingBuilder< ControlerListener > addControlerListenerBinding()
void notifyBeforeMobsim(BeforeMobsimEvent event)
Population getPopulation()
static final String ITERATION_CREATED
void notifyShutdown(ShutdownEvent event)
void setIterationRemoved(int iterationRemoved)
final Character DELIMITER
ArrayList< String > getActiveStrategies(Collection< StrategySettings > strategySettings, StrategyManager strategyManager)
MatsimServices getServices()
void setIterationCreated(int iterationCreated)
void setAncestorId(Id< Plan > ancestorId)
void calculateAndWriteDistribution(int currentIteration, ArrayList< String > strategies, Map< Id< Plan >, PlanInheritanceRecord > planId2planInheritanceRecords, Set< Id< Plan >> planIds, BufferedWriter writer)
static final String PLAN_ID
void notifyStartup(StartupEvent event)
final ReplanningConfigGroup replanning()
final String getOutputFilename(final String filename)
BufferedWriter selectedPlanStrategyShareWriter
StrategySettings getStrategySettings(final Id< StrategySettings > index, final boolean createIfMissing)
static boolean isSelected(Plan plan)
static final String PLAN_MUTATOR