diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/vrpagent/VrpAgentQueryHelper.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/vrpagent/VrpAgentQueryHelper.java index 64e15ad5036..ddb8d2e6f08 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/vrpagent/VrpAgentQueryHelper.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/vrpagent/VrpAgentQueryHelper.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.TransportMode; import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Leg; @@ -170,12 +171,12 @@ public void setIterationCreated(int iteration) { } @Override - public String getPlanId() { + public Id getPlanId() { throw new UnsupportedOperationException(); } @Override - public void setPlanId(String planId) { + public void setPlanId(Id planId) { throw new UnsupportedOperationException(); } diff --git a/contribs/locationchoice/src/main/java/org/matsim/contrib/locationchoice/zzunused/LCPlan.java b/contribs/locationchoice/src/main/java/org/matsim/contrib/locationchoice/zzunused/LCPlan.java index b92cf6ebe7f..f9a167cde9a 100644 --- a/contribs/locationchoice/src/main/java/org/matsim/contrib/locationchoice/zzunused/LCPlan.java +++ b/contribs/locationchoice/src/main/java/org/matsim/contrib/locationchoice/zzunused/LCPlan.java @@ -286,13 +286,13 @@ else if (planElement instanceof Leg) { } @Override - public void setPlanId(String planId) { + public void setPlanId(Id planId) { throw new UnsupportedOperationException(); } @Override - public String getPlanId() { + public Id getPlanId() { throw new UnsupportedOperationException(); } diff --git a/contribs/pseudosimulation/src/main/java/org/matsim/contrib/pseudosimulation/distributed/plans/PlanGenome.java b/contribs/pseudosimulation/src/main/java/org/matsim/contrib/pseudosimulation/distributed/plans/PlanGenome.java index 63f9bf8d8d9..34f725fc92a 100644 --- a/contribs/pseudosimulation/src/main/java/org/matsim/contrib/pseudosimulation/distributed/plans/PlanGenome.java +++ b/contribs/pseudosimulation/src/main/java/org/matsim/contrib/pseudosimulation/distributed/plans/PlanGenome.java @@ -221,10 +221,10 @@ public void setType(final String type) { } @Override - public String getPlanId() { return null; } + public Id getPlanId() { return null; } @Override - public void setPlanId(String planId) { /* nothing to do here */ } + public void setPlanId(Id planId) { /* nothing to do here */ } @Override public int getIterationCreated() { return -1; } diff --git a/contribs/socnetsim/src/main/java/org/matsim/contrib/socnetsim/framework/population/PlanWithCachedJointPlan.java b/contribs/socnetsim/src/main/java/org/matsim/contrib/socnetsim/framework/population/PlanWithCachedJointPlan.java index 00e96f82d89..ce912ec1682 100644 --- a/contribs/socnetsim/src/main/java/org/matsim/contrib/socnetsim/framework/population/PlanWithCachedJointPlan.java +++ b/contribs/socnetsim/src/main/java/org/matsim/contrib/socnetsim/framework/population/PlanWithCachedJointPlan.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Leg; import org.matsim.api.core.v01.population.Person; @@ -174,10 +175,10 @@ public void setType(String type) { } @Override - public String getPlanId() { return null; } + public Id getPlanId() { return null; } @Override - public void setPlanId(String planId) { /* nothing to do here */ } + public void setPlanId(Id planId) { /* nothing to do here */ } @Override public int getIterationCreated() { return -1; } diff --git a/matsim/src/main/java/org/matsim/api/core/v01/population/Plan.java b/matsim/src/main/java/org/matsim/api/core/v01/population/Plan.java index 556f1e80d8a..85e7775e9b9 100644 --- a/matsim/src/main/java/org/matsim/api/core/v01/population/Plan.java +++ b/matsim/src/main/java/org/matsim/api/core/v01/population/Plan.java @@ -23,6 +23,7 @@ import java.util.List; import org.matsim.api.core.v01.Customizable; +import org.matsim.api.core.v01.Id; import org.matsim.core.api.internal.MatsimPopulationObject; import org.matsim.utils.objectattributes.attributable.Attributable; @@ -50,9 +51,9 @@ public interface Plan extends MatsimPopulationObject, Customizable, BasicPlan, A public abstract void setType(final String type); - public abstract void setPlanId(final String planId); + public abstract void setPlanId(final Id planId); - public abstract String getPlanId(); + public abstract Id getPlanId(); public abstract int getIterationCreated(); diff --git a/matsim/src/main/java/org/matsim/core/config/groups/PlanInheritanceConfigGroup.java b/matsim/src/main/java/org/matsim/core/config/groups/PlanInheritanceConfigGroup.java index 922f2e37eb0..b889df2fa15 100644 --- a/matsim/src/main/java/org/matsim/core/config/groups/PlanInheritanceConfigGroup.java +++ b/matsim/src/main/java/org/matsim/core/config/groups/PlanInheritanceConfigGroup.java @@ -47,7 +47,7 @@ public Map getComments() { @StringSetter( ENABLED ) - public void setWriteLinkStatsInterval(boolean enabled) { + public void setEnabled(boolean enabled) { this.enabled = enabled; } diff --git a/matsim/src/main/java/org/matsim/core/population/PlanImpl.java b/matsim/src/main/java/org/matsim/core/population/PlanImpl.java index 08047673bfd..4d7f0fd0363 100644 --- a/matsim/src/main/java/org/matsim/core/population/PlanImpl.java +++ b/matsim/src/main/java/org/matsim/core/population/PlanImpl.java @@ -27,6 +27,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.matsim.api.core.v01.Customizable; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Leg; import org.matsim.api.core.v01.population.Person; @@ -124,12 +125,12 @@ public void setType(final String type) { } @Override - public String getPlanId() { - return (String) this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID); + public Id getPlanId() { + return (Id) this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID); } @Override - public void setPlanId(String planId) { + public void setPlanId(Id planId) { this.getAttributes().putAttribute(PlanInheritanceModule.PLAN_ID, planId); } diff --git a/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java b/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java index ed49e66ed22..adac6f947a2 100644 --- a/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java +++ b/matsim/src/main/java/org/matsim/core/population/PopulationUtils.java @@ -383,12 +383,12 @@ public void setType(String type) { } @Override - public String getPlanId() { + public Id getPlanId() { return this.delegate.getPlanId(); } @Override - public void setPlanId(String planId) { + public void setPlanId(Id planId) { throw new UnsupportedOperationException(); } diff --git a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceModule.java b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceModule.java index e8b2b30fbe5..80ef946b263 100644 --- a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceModule.java +++ b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceModule.java @@ -33,6 +33,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.population.Person; import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.groups.ControlerConfigGroup.CompressionType; @@ -59,7 +60,7 @@ *
  • Book-keeping, i.e. setting additional plan attributes not stored in the plan itself *
  • Calculation default stats like the distribution of mutators among (selected) plans * - * @author neuma + * @author neuma, alex94263 */ @Singleton public class PlanInheritanceModule extends AbstractModule implements StartupListener, BeforeMobsimListener, ShutdownListener { @@ -74,7 +75,7 @@ public class PlanInheritanceModule extends AbstractModule implements StartupList public static final String FILENAME_PLAN_INHERITANCE_RECORDS = "planInheritanceRecords"; long numberOfPlanInheritanceRecordsCreated = 0; - Map planId2planInheritanceRecords = new ConcurrentHashMap<>(); + Map, PlanInheritanceRecord> planId2planInheritanceRecords = new ConcurrentHashMap<>(); PlanInheritanceRecordWriter planInheritanceRecordWriter; private ArrayList strategies; @@ -95,7 +96,7 @@ public void notifyStartup(StartupEvent event) { // reset all plan attributes that might be present from a previously performed matsim run for (Person person : event.getServices().getScenario().getPopulation().getPersons().values()) { for (Plan plan : person.getPlans()) { - plan.setPlanId(NONE); + plan.setPlanId(Id.create(NONE, String.class)); plan.setPlanMutator(INITIAL_PLAN); plan.setIterationCreated(0); } @@ -156,8 +157,8 @@ private BufferedWriter initializeDistributionWriter(ArrayList strategies public void notifyBeforeMobsim(BeforeMobsimEvent event) { // check the plans of the population and all currently stored plan records - do the actual book-keeping - Set activePlanIds = new HashSet<>(); - Set selectedPlanIds = new HashSet<>(); + Set> activePlanIds = new HashSet<>(); + Set> selectedPlanIds = new HashSet<>(); for (Person person : event.getServices().getScenario().getPopulation().getPersons().values()) { for (Plan plan : person.getPlans()) { @@ -172,18 +173,18 @@ public void notifyBeforeMobsim(BeforeMobsimEvent event) { // it's a new plan created in this iteration - create a new record PlanInheritanceRecord planInheritanceRecord = new PlanInheritanceRecord(); - planInheritanceRecord.agentId = person.getId().toString(); - planInheritanceRecord.planId = Long.toString(++this.numberOfPlanInheritanceRecordsCreated, 36); - planInheritanceRecord.ancestorId = plan.getPlanId(); - plan.setPlanId(planInheritanceRecord.planId); - planInheritanceRecord.iterationCreated = plan.getIterationCreated(); - planInheritanceRecord.mutatedBy = plan.getPlanMutator(); + planInheritanceRecord.setAgentId(person.getId()); + planInheritanceRecord.setPlanId( Id.create(Long.toString(++this.numberOfPlanInheritanceRecordsCreated, 36), String.class) ); + planInheritanceRecord.setAncestorId(plan.getPlanId()); + plan.setPlanId(planInheritanceRecord.getPlanId()); + planInheritanceRecord.setIterationCreated(plan.getIterationCreated()); + planInheritanceRecord.setMutatedBy(plan.getPlanMutator()); - this.planId2planInheritanceRecords.put(planInheritanceRecord.planId, planInheritanceRecord); + this.planId2planInheritanceRecords.put(planInheritanceRecord.getPlanId(), planInheritanceRecord); } if (PersonUtils.isSelected(plan)) { - this.planId2planInheritanceRecords.get(plan.getPlanId()).iterationsSelected.add(event.getIteration()); + this.planId2planInheritanceRecords.get(plan.getPlanId()).getIterationsSelected().add(event.getIteration()); selectedPlanIds.add(plan.getPlanId()); } @@ -191,16 +192,16 @@ public void notifyBeforeMobsim(BeforeMobsimEvent event) { } } - List deletedPlans = new ArrayList<>(); - for (String planId : this.planId2planInheritanceRecords.keySet()) { + List> deletedPlans = new ArrayList<>(); + for (Id planId : this.planId2planInheritanceRecords.keySet()) { if (!activePlanIds.contains(planId)) { deletedPlans.add(planId); } } - for (String deletedPlanId : deletedPlans) { + for (Id deletedPlanId : deletedPlans) { PlanInheritanceRecord deletedPlanInheritanceRecord = this.planId2planInheritanceRecords.remove(deletedPlanId); - deletedPlanInheritanceRecord.iterationRemoved = event.getIteration(); + deletedPlanInheritanceRecord.setIterationRemoved(event.getIteration()); this.planInheritanceRecordWriter.write(deletedPlanInheritanceRecord); } @@ -213,13 +214,13 @@ public void notifyBeforeMobsim(BeforeMobsimEvent event) { /** * Updates the default plan stats - namely the distribution of plan mutators based on the given plan ids. */ - private void calculateAndWriteDistribution(int currentIteration, ArrayList strategies, Map planId2planInheritanceRecords, Set planIds, BufferedWriter writer) { + private void calculateAndWriteDistribution(int currentIteration, ArrayList strategies, Map, PlanInheritanceRecord> planId2planInheritanceRecords, Set> planIds, BufferedWriter writer) { Map strategy2count = new HashMap<>(); for (String strategyName : strategies) { strategy2count.put(strategyName, new AtomicLong(0)); } - for (String planId : planIds) { - String mutatedBy = planId2planInheritanceRecords.get(planId).mutatedBy; + for (Id planId : planIds) { + String mutatedBy = planId2planInheritanceRecords.get(planId).getMutatedBy(); strategy2count.get(mutatedBy).incrementAndGet(); } long sum = strategy2count.values().stream().mapToLong(count -> count.get()).sum(); diff --git a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecord.java b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecord.java index 90dc463d98f..8a3e54280ac 100644 --- a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecord.java +++ b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecord.java @@ -23,47 +23,106 @@ import java.util.ArrayList; import java.util.List; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.population.Person; + /** * Data container storing the data of a single plan. * - * @author neuma + * @author neuma, alex94263 */ public class PlanInheritanceRecord { /** * Id of the person that plan record belongs to. */ - String agentId; + private Id agentId; /** * The globally unique plan id. */ - String planId; + private Id planId; /** * Id of the plan that this plan had been copied from before mutating. */ - String ancestorId; + private Id ancestorId; /** * The name of the strategy that altered this plan. */ - String mutatedBy; + private String mutatedBy; /** * Iteration in which this plan had been created. May be {@linkplain PlanInheritanceModule#INITIAL_PLAN} if the plan had been in the choice-set from the very beginning. */ - int iterationCreated; + private int iterationCreated; /** * Iteration in which the plan had been removed from the choice-set. */ - int iterationRemoved; + private int iterationRemoved; /** * Collection of iterations this plan had been the selected plan. * Initialize this with one since each plan is selected at least once. */ - List iterationsSelected = new ArrayList<>(1); + private List iterationsSelected = new ArrayList<>(1); + + public Id getPlanId() { + return planId; + } + + public void setPlanId(Id planId) { + this.planId = planId; + } + + public Id getAncestorId() { + return ancestorId; + } + + public void setAncestorId(Id ancestorId) { + this.ancestorId = ancestorId; + } + + public String getMutatedBy() { + return mutatedBy; + } + + public void setMutatedBy(String mutatedBy) { + this.mutatedBy = mutatedBy; + } + + public int getIterationCreated() { + return iterationCreated; + } + + public void setIterationCreated(int iterationCreated) { + this.iterationCreated = iterationCreated; + } + + public int getIterationRemoved() { + return iterationRemoved; + } + + public void setIterationRemoved(int iterationRemoved) { + this.iterationRemoved = iterationRemoved; + } + + public List getIterationsSelected() { + return iterationsSelected; + } + + public void setIterationsSelected(List iterationsSelected) { + this.iterationsSelected = iterationsSelected; + } + + public Id getAgentId() { + return agentId; + } + + public void setAgentId(Id agentId) { + this.agentId = agentId; + } } diff --git a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordReader.java b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordReader.java new file mode 100644 index 00000000000..a6b90fbd44e --- /dev/null +++ b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordReader.java @@ -0,0 +1,95 @@ +package org.matsim.core.replanning.inheritance; + +import java.io.BufferedReader; + +/* *********************************************************************** * + * project: org.matsim.* + * ParallelPopulationReaderMatsimV6.java + * * + * *********************************************************************** * + * * + * copyright : (C) 2023 by the members listed in the COPYING, * + * LICENSE and WARRANTY file. * + * email : info at matsim dot org * + * * + * *********************************************************************** * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * See also COPYING, LICENSE and WARRANTY file * + * * + * *********************************************************************** */ + +import java.io.BufferedWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.matsim.api.core.v01.Id; +import org.matsim.core.utils.io.IOUtils; + +/** + * Writes {@linkplain PlanInheritanceRecord} to file in a fixed column sequence. + * + * @author alex94263 + */ +public class PlanInheritanceRecordReader { + + + private final String DELIMITER = "\t"; + private final BufferedReader reader; + + public PlanInheritanceRecordReader(String filename) { + this.reader = IOUtils.getBufferedReader(filename); + + + + } + public Map buildIdx(String[] header) { + Map lookup = new HashMap(); + for (int i=0; i read() { + List records = new ArrayList(); + try { + Map lookUp = buildIdx(reader.readLine().split(DELIMITER)); + String lineString = reader.readLine(); + while(lineString !=null) { + String[] line = lineString.split(DELIMITER); + PlanInheritanceRecord planInheritanceRecord = new PlanInheritanceRecord(); + planInheritanceRecord.setAgentId(Id.createPersonId(line[lookUp.get(PlanInheritanceRecordWriter.AGENT_ID)])); + planInheritanceRecord.setPlanId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.PLAN_ID)], String.class)); + planInheritanceRecord.setAncestorId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.ANCESTOR_ID)], String.class)); + planInheritanceRecord.setMutatedBy(line[lookUp.get(PlanInheritanceRecordWriter.MUTATED_BY)]); + planInheritanceRecord.setIterationCreated(Integer.parseInt(line[lookUp.get(PlanInheritanceRecordWriter.ITERATION_CREATED)])); + planInheritanceRecord.setIterationRemoved(Integer.parseInt(line[lookUp.get(PlanInheritanceRecordWriter.ITERATION_REMOVED)])); + String iterationsSelected = line[lookUp.get(PlanInheritanceRecordWriter.ITERATIONS_SELECTED)]; + planInheritanceRecord.setIterationsSelected(Arrays.asList(iterationsSelected.substring(1, iterationsSelected.length()-1).split(", ")).stream() + .map(Integer::parseInt) + .collect(Collectors.toList())); + records.add(planInheritanceRecord); + lineString = reader.readLine(); + } + return records; + + } catch (IOException e) { + throw new RuntimeException("Could not read the plan inheritance records!", e); + } + + + + } + + +} diff --git a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordWriter.java b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordWriter.java index 04c3729ac11..1400a915406 100644 --- a/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordWriter.java +++ b/matsim/src/main/java/org/matsim/core/replanning/inheritance/PlanInheritanceRecordWriter.java @@ -28,17 +28,17 @@ /** * Writes {@linkplain PlanInheritanceRecord} to file in a fixed column sequence. * - * @author neuma + * @author neuma, alex94263 */ public class PlanInheritanceRecordWriter { - public final String AGENT_ID = "agentId"; - public final String PLAN_ID = "planId"; - public final String ANCESTOR_ID = "ancestorId"; - public final String MUTATED_BY = "mutatedBy"; - public final String ITERATION_CREATED = "iterationCreated"; - public final String ITERATION_REMOVED = "iterationRemoved"; - public final String ITERATIONS_SELECTED = "iterationsSelected"; + public static final String AGENT_ID = "agentId"; + public static final String PLAN_ID = "planId"; + public static final String ANCESTOR_ID = "ancestorId"; + public static final String MUTATED_BY = "mutatedBy"; + public static final String ITERATION_CREATED = "iterationCreated"; + public static final String ITERATION_REMOVED = "iterationRemoved"; + public static final String ITERATIONS_SELECTED = "iterationsSelected"; private final Character DELIMITER = '\t'; private final BufferedWriter writer; @@ -65,13 +65,13 @@ public PlanInheritanceRecordWriter(String filename) { public void write(PlanInheritanceRecord planInheritanceRecord) { StringBuffer line = new StringBuffer(); - line.append(planInheritanceRecord.agentId); line.append(DELIMITER); - line.append(planInheritanceRecord.planId); line.append(DELIMITER); - line.append(planInheritanceRecord.ancestorId); line.append(DELIMITER); - line.append(planInheritanceRecord.mutatedBy); line.append(DELIMITER); - line.append(planInheritanceRecord.iterationCreated); line.append(DELIMITER); - line.append(planInheritanceRecord.iterationRemoved); line.append(DELIMITER); - line.append(planInheritanceRecord.iterationsSelected); + line.append(planInheritanceRecord.getAgentId()); line.append(DELIMITER); + line.append(planInheritanceRecord.getPlanId()); line.append(DELIMITER); + line.append(planInheritanceRecord.getAncestorId()); line.append(DELIMITER); + line.append(planInheritanceRecord.getMutatedBy()); line.append(DELIMITER); + line.append(planInheritanceRecord.getIterationCreated()); line.append(DELIMITER); + line.append(planInheritanceRecord.getIterationRemoved()); line.append(DELIMITER); + line.append(planInheritanceRecord.getIterationsSelected()); try { this.writer.write(line.toString()); diff --git a/matsim/src/test/java/org/matsim/core/replanning/planInheritance/PlanInheritanceTest.java b/matsim/src/test/java/org/matsim/core/replanning/planInheritance/PlanInheritanceTest.java new file mode 100644 index 00000000000..56f27301f11 --- /dev/null +++ b/matsim/src/test/java/org/matsim/core/replanning/planInheritance/PlanInheritanceTest.java @@ -0,0 +1,110 @@ +package org.matsim.core.replanning.planInheritance; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.Scenario; +import org.matsim.api.core.v01.population.Person; +import org.matsim.core.config.Config; +import org.matsim.core.controler.Controler; +import org.matsim.core.population.io.StreamingPopulationReader; +import org.matsim.core.replanning.inheritance.PlanInheritanceModule; +import org.matsim.core.replanning.inheritance.PlanInheritanceRecord; +import org.matsim.core.replanning.inheritance.PlanInheritanceRecordReader; +import org.matsim.core.scenario.ScenarioUtils; +import org.matsim.testcases.MatsimTestUtils; + + + +public class PlanInheritanceTest { + /** + * @author alex94263 + */ + + @Rule + public MatsimTestUtils util = new MatsimTestUtils(); + + @Test + public void testPlanInheritanceEnabled() throws IOException { + String outputDirectory = util.getOutputDirectory(); + + Config config = this.util.loadConfig("test/scenarios/equil/config_plans1.xml"); + config.controler().setLastIteration(10); + config.controler().setOutputDirectory(outputDirectory); + config.planInheritance().setEnabled(true); + Controler c = new Controler(config); + + c.run(); + File csv = new File(outputDirectory, "planInheritanceRecords.csv.gz"); + + assertThat(csv).exists(); + + + final Scenario scenario = ScenarioUtils.createScenario(config); + StreamingPopulationReader streamingPopulationReader = new StreamingPopulationReader(scenario); + streamingPopulationReader.readFile(util.getOutputDirectory()+"output_plans.xml.gz"); + for(Person p : scenario.getPopulation().getPersons().values()) { + + assert(p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.PLAN_ID)); + assert(p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.PLAN_MUTATOR)); + assert(p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.ITERATION_CREATED)); + } + + PlanInheritanceRecordReader reader = new PlanInheritanceRecordReader(outputDirectory+"planInheritanceRecords.csv.gz"); + List records = reader.read(); + assert(records.size()==2); + assert( ((PlanInheritanceRecord) records.get(0)).getAgentId().equals(Id.createPersonId("1"))); + assert( ((PlanInheritanceRecord) records.get(0)).getAncestorId().equals(Id.create("NONE",String.class))); + assert( ((PlanInheritanceRecord) records.get(0)).getMutatedBy().equals(PlanInheritanceModule.INITIAL_PLAN)); + assert( ((PlanInheritanceRecord) records.get(0)).getIterationCreated() == 0); + assert( ((PlanInheritanceRecord) records.get(0)).getIterationRemoved() == 0); + assert( ((PlanInheritanceRecord) records.get(0)).getPlanId().equals(Id.create("1",String.class))); + assert( ((PlanInheritanceRecord) records.get(0)).getIterationsSelected().equals(Arrays.asList(0, 1, 2, 3, 4, 6, 7, 8, 9, 10))); + + assert( ((PlanInheritanceRecord) records.get(1)).getAgentId().equals(Id.createPersonId("1"))); + assert( ((PlanInheritanceRecord) records.get(1)).getAncestorId().equals(Id.create("1",String.class))); + assert( ((PlanInheritanceRecord) records.get(1)).getMutatedBy().equals("RandomPlanSelector_ReRoute")); + assert( ((PlanInheritanceRecord) records.get(1)).getIterationCreated() == 5); + assert( ((PlanInheritanceRecord) records.get(1)).getIterationRemoved() == 0); + assert( ((PlanInheritanceRecord) records.get(1)).getPlanId().equals(Id.create("2",String.class))); + assert( ((PlanInheritanceRecord) records.get(1)).getIterationsSelected().equals(Arrays.asList(5))); + + + + } + + @Test + public void testPlanInheritanceDisabled() throws IOException { + String outputDirectory = util.getOutputDirectory(); + + Config config = this.util.loadConfig("test/scenarios/equil/config_plans1.xml"); + config.controler().setLastIteration(1); + config.controler().setOutputDirectory(outputDirectory); + Controler c = new Controler(config); + + c.run(); + + File csv = new File(outputDirectory, "planInheritanceRecords.csv.gz"); + + assertThat(csv).doesNotExist(); + + + final Scenario scenario = ScenarioUtils.createScenario(config); + StreamingPopulationReader streamingPopulationReader = new StreamingPopulationReader(scenario); + streamingPopulationReader.readFile(util.getOutputDirectory()+"output_plans.xml.gz"); + for(Person p : scenario.getPopulation().getPersons().values()) { + + assert(!p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.PLAN_ID)); + assert(!p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.PLAN_MUTATOR)); + assert(!p.getAttributes().getAsMap().keySet().contains(PlanInheritanceModule.ITERATION_CREATED)); + } + + + } +}