Skip to content

Commit

Permalink
implmented Plan as identifiable
Browse files Browse the repository at this point in the history
  • Loading branch information
awagner committed Oct 13, 2023
1 parent b6bada1 commit 78532cf
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public void setIterationCreated(int iteration) {
}

@Override
public Id<String> getPlanId() {
public Id<Plan> getId() {
throw new UnsupportedOperationException();
}

@Override
public void setPlanId(Id<String> planId) {
public void setPlanId(Id<Plan> planId) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ else if (planElement instanceof Leg) {
}

@Override
public void setPlanId(Id<String> planId) {
public void setPlanId(Id<Plan> planId) {
throw new UnsupportedOperationException();

}

@Override
public Id<String> getPlanId() {
public Id<Plan> getId() {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ public void setType(final String type) {
}

@Override
public Id<String> getPlanId() { return null; }
public Id<Plan> getId() { return null; }

@Override
public void setPlanId(Id<String> planId) { /* nothing to do here */ }
public void setPlanId(Id<Plan> planId) { /* nothing to do here */ }

@Override
public int getIterationCreated() { return -1; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ public void setType(String type) {
}

@Override
public Id<String> getPlanId() { return null; }
public Id<Plan> getId() { return null; }

@Override
public void setPlanId(Id<String> planId) { /* nothing to do here */ }
public void setPlanId(Id<Plan> planId) { /* nothing to do here */ }

@Override
public int getIterationCreated() { return -1; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.matsim.api.core.v01.Customizable;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.core.api.internal.MatsimPopulationObject;
import org.matsim.utils.objectattributes.attributable.Attributable;

Expand All @@ -35,7 +36,7 @@
* The only thing which is not "expected" in the same sense is the score.
*
*/
public interface Plan extends MatsimPopulationObject, Customizable, BasicPlan, Attributable {
public interface Plan extends MatsimPopulationObject, Customizable, BasicPlan, Attributable, Identifiable<Plan> {

public abstract List<PlanElement> getPlanElements();

Expand All @@ -51,9 +52,9 @@ public interface Plan extends MatsimPopulationObject, Customizable, BasicPlan, A

public abstract void setType(final String type);

public abstract void setPlanId(final Id<String> planId);
public abstract void setPlanId(Id<Plan> planId);

public abstract Id<String> getPlanId();
public abstract Id<Plan> getId();

public abstract int getIterationCreated();

Expand All @@ -72,5 +73,6 @@ public interface Plan extends MatsimPopulationObject, Customizable, BasicPlan, A
* you are using this method!.
*/
public abstract void setPerson(Person person);


}
16 changes: 12 additions & 4 deletions matsim/src/main/java/org/matsim/core/population/PlanImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.network.Link;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
Expand All @@ -40,6 +41,8 @@

/* deliberately package */ final class PlanImpl implements Plan {

private Id<Plan> id= null;

private ArrayList<PlanElement> actsLegs = new ArrayList<>();

private Double score = null;
Expand Down Expand Up @@ -125,13 +128,16 @@ public void setType(final String type) {
}

@Override
public Id<String> getPlanId() {
return (Id<String>) this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID);
public Id<Plan> getId() {
if(this.id!=null)
return this.id;
else return Id.create(this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID).toString(),Plan.class);
}

@Override
public void setPlanId(Id<String> planId) {
this.getAttributes().putAttribute(PlanInheritanceModule.PLAN_ID, planId);
public void setPlanId(Id<Plan> planId) {
this.getAttributes().putAttribute(PlanInheritanceModule.PLAN_ID, planId.toString());
this.id = planId;
}

@Override
Expand Down Expand Up @@ -196,6 +202,8 @@ public final Map<String, Object> getCustomAttributes() {
return this.customizableDelegate.getCustomAttributes();
}



// public final void setLocked() {
// for ( PlanElement pe : this.actsLegs ) {
// if ( pe instanceof ActivityImpl ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,12 @@ public void setType(String type) {
}

@Override
public Id<String> getPlanId() {
return this.delegate.getPlanId();
public Id<Plan> getId() {
return this.delegate.getId();
}

@Override
public void setPlanId(Id<String> planId) {
public void setPlanId(Id<Plan> planId) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void run(final HasPlansAndId<T, I> person) {
// set the working plan to a copy of the selected plan:
plan = person.createCopyOfSelectedPlanAndMakeSelected();

//planId is only set inside planInheritance -> if null planInheritance is disabled
if (plan instanceof Plan && ((Plan) plan).getPlanId() != null) {
//Id is only set inside planInheritance -> if null planInheritance is disabled
if (plan instanceof Plan && ((Plan) plan).getId() != null) {
// add plan inheritance flags
((Plan) plan).setIterationCreated(this.replanningContext.getIteration());
((Plan) plan).setPlanMutator(this.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.groups.ControlerConfigGroup.CompressionType;
import org.matsim.core.config.groups.PlanInheritanceConfigGroup;
import org.matsim.core.config.groups.StrategyConfigGroup.StrategySettings;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.events.BeforeMobsimEvent;
Expand All @@ -49,7 +48,6 @@
import org.matsim.core.population.PersonUtils;
import org.matsim.core.replanning.GenericPlanStrategy;
import org.matsim.core.replanning.StrategyManager;
import org.matsim.core.replanning.annealing.ReplanningAnnealerConfigGroup;
import org.matsim.core.utils.io.IOUtils;

import com.google.inject.Singleton;
Expand All @@ -75,7 +73,7 @@ public class PlanInheritanceModule extends AbstractModule implements StartupList
public static final String FILENAME_PLAN_INHERITANCE_RECORDS = "planInheritanceRecords";

long numberOfPlanInheritanceRecordsCreated = 0;
Map<Id<String>, PlanInheritanceRecord> planId2planInheritanceRecords = new ConcurrentHashMap<>();
Map<Id<Plan>, PlanInheritanceRecord> planId2planInheritanceRecords = new ConcurrentHashMap<>();

PlanInheritanceRecordWriter planInheritanceRecordWriter;
private ArrayList<String> strategies;
Expand All @@ -96,7 +94,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(Id.create(NONE, String.class));
plan.setPlanId(Id.create(NONE, Plan.class));
plan.setPlanMutator(INITIAL_PLAN);
plan.setIterationCreated(0);
}
Expand Down Expand Up @@ -157,8 +155,8 @@ private BufferedWriter initializeDistributionWriter(ArrayList<String> strategies
public void notifyBeforeMobsim(BeforeMobsimEvent event) {
// check the plans of the population and all currently stored plan records - do the actual book-keeping

Set<Id<String>> activePlanIds = new HashSet<>();
Set<Id<String>> selectedPlanIds = new HashSet<>();
Set<Id<Plan>> activePlanIds = new HashSet<>();
Set<Id<Plan>> selectedPlanIds = new HashSet<>();

for (Person person : event.getServices().getScenario().getPopulation().getPersons().values()) {
for (Plan plan : person.getPlans()) {
Expand All @@ -174,8 +172,8 @@ public void notifyBeforeMobsim(BeforeMobsimEvent event) {

PlanInheritanceRecord planInheritanceRecord = new PlanInheritanceRecord();
planInheritanceRecord.setAgentId(person.getId());
planInheritanceRecord.setPlanId( Id.create(Long.toString(++this.numberOfPlanInheritanceRecordsCreated, 36), String.class) );
planInheritanceRecord.setAncestorId(plan.getPlanId());
planInheritanceRecord.setPlanId(Id.create(Long.toString(++this.numberOfPlanInheritanceRecordsCreated, 36), Plan.class));
planInheritanceRecord.setAncestorId(plan.getId()); //works because new plan is copy of old selected and attributes are copied -> thus current attribute plan id is old selected plan id
plan.setPlanId(planInheritanceRecord.getPlanId());
planInheritanceRecord.setIterationCreated(plan.getIterationCreated());
planInheritanceRecord.setMutatedBy(plan.getPlanMutator());
Expand All @@ -184,22 +182,22 @@ public void notifyBeforeMobsim(BeforeMobsimEvent event) {
}

if (PersonUtils.isSelected(plan)) {
this.planId2planInheritanceRecords.get(plan.getPlanId()).getIterationsSelected().add(event.getIteration());
selectedPlanIds.add(plan.getPlanId());
this.planId2planInheritanceRecords.get(plan.getId()).getIterationsSelected().add(event.getIteration());
selectedPlanIds.add(plan.getId());
}

activePlanIds.add(plan.getPlanId());
activePlanIds.add(plan.getId());
}
}

List<Id<String>> deletedPlans = new ArrayList<>();
for (Id<String> planId : this.planId2planInheritanceRecords.keySet()) {
List<Id<Plan>> deletedPlans = new ArrayList<>();
for (Id<Plan> planId : this.planId2planInheritanceRecords.keySet()) {
if (!activePlanIds.contains(planId)) {
deletedPlans.add(planId);
}
}

for (Id<String> deletedPlanId : deletedPlans) {
for (Id<Plan> deletedPlanId : deletedPlans) {
PlanInheritanceRecord deletedPlanInheritanceRecord = this.planId2planInheritanceRecords.remove(deletedPlanId);
deletedPlanInheritanceRecord.setIterationRemoved(event.getIteration());
this.planInheritanceRecordWriter.write(deletedPlanInheritanceRecord);
Expand All @@ -214,12 +212,12 @@ 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<String> strategies, Map<Id<String>, PlanInheritanceRecord> planId2planInheritanceRecords, Set<Id<String>> planIds, BufferedWriter writer) {
private void calculateAndWriteDistribution(int currentIteration, ArrayList<String> strategies, Map<Id<Plan>, PlanInheritanceRecord> planId2planInheritanceRecords, Set<Id<Plan>> planIds, BufferedWriter writer) {
Map<String, AtomicLong> strategy2count = new HashMap<>();
for (String strategyName : strategies) {
strategy2count.put(strategyName, new AtomicLong(0));
}
for (Id<String> planId : planIds) {
for (Id<Plan> planId : planIds) {
String mutatedBy = planId2planInheritanceRecords.get(planId).getMutatedBy();
strategy2count.get(mutatedBy).incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;

/**
* Data container storing the data of a single plan.
Expand All @@ -41,12 +42,12 @@ public class PlanInheritanceRecord {
/**
* The globally unique plan id.
*/
private Id<String> planId;
private Id<Plan> planId;

/**
* Id of the plan that this plan had been copied from before mutating.
*/
private Id<String> ancestorId;
private Id<Plan> ancestorId;

/**
* The name of the strategy that altered this plan.
Expand All @@ -69,19 +70,19 @@ public class PlanInheritanceRecord {
*/
private List<Integer> iterationsSelected = new ArrayList<>(1);

public Id<String> getPlanId() {
public Id<Plan> getPlanId() {
return planId;
}

public void setPlanId(Id<String> planId) {
public void setPlanId(Id<Plan> planId) {
this.planId = planId;
}

public Id<String> getAncestorId() {
public Id<Plan> getAncestorId() {
return ancestorId;
}

public void setAncestorId(Id<String> ancestorId) {
public void setAncestorId(Id<Plan> ancestorId) {
this.ancestorId = ancestorId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.stream.Collectors;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.utils.io.IOUtils;

/**
Expand Down Expand Up @@ -69,8 +70,8 @@ public List<PlanInheritanceRecord> read() {
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.setPlanId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.PLAN_ID)], Plan.class));
planInheritanceRecord.setAncestorId(Id.create(line[lookUp.get(PlanInheritanceRecordWriter.ANCESTOR_ID)], Plan.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)]));
Expand Down
Loading

0 comments on commit 78532cf

Please sign in to comment.