Skip to content

Commit

Permalink
switched from String to Id<> for planId, Junit Test, Record Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
awagner committed Oct 13, 2023
1 parent b733b66 commit b6bada1
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -170,12 +171,12 @@ public void setIterationCreated(int iteration) {
}

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

@Override
public void setPlanId(String planId) {
public void setPlanId(Id<String> 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(String planId) {
public void setPlanId(Id<String> planId) {
throw new UnsupportedOperationException();

}

@Override
public String getPlanId() {
public Id<String> getPlanId() {
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 String getPlanId() { return null; }
public Id<String> getPlanId() { return null; }

@Override
public void setPlanId(String planId) { /* nothing to do here */ }
public void setPlanId(Id<String> 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 @@ -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;
Expand Down Expand Up @@ -174,10 +175,10 @@ public void setType(String type) {
}

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

@Override
public void setPlanId(String planId) { /* nothing to do here */ }
public void setPlanId(Id<String> 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 @@ -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;

Expand Down Expand Up @@ -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<String> planId);

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

public abstract int getIterationCreated();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Map<String, String> getComments() {


@StringSetter( ENABLED )
public void setWriteLinkStatsInterval(boolean enabled) {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

Expand Down
7 changes: 4 additions & 3 deletions matsim/src/main/java/org/matsim/core/population/PlanImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,12 +125,12 @@ public void setType(final String type) {
}

@Override
public String getPlanId() {
return (String) this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID);
public Id<String> getPlanId() {
return (Id<String>) this.getAttributes().getAttribute(PlanInheritanceModule.PLAN_ID);
}

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

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 String getPlanId() {
public Id<String> getPlanId() {
return this.delegate.getPlanId();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,7 +60,7 @@
* <li> Book-keeping, i.e. setting additional plan attributes not stored in the plan itself
* <li> 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 {
Expand All @@ -74,7 +75,7 @@ public class PlanInheritanceModule extends AbstractModule implements StartupList
public static final String FILENAME_PLAN_INHERITANCE_RECORDS = "planInheritanceRecords";

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

PlanInheritanceRecordWriter planInheritanceRecordWriter;
private ArrayList<String> strategies;
Expand All @@ -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);
}
Expand Down Expand Up @@ -156,8 +157,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<String> activePlanIds = new HashSet<>();
Set<String> selectedPlanIds = new HashSet<>();
Set<Id<String>> activePlanIds = new HashSet<>();
Set<Id<String>> selectedPlanIds = new HashSet<>();

for (Person person : event.getServices().getScenario().getPopulation().getPersons().values()) {
for (Plan plan : person.getPlans()) {
Expand All @@ -172,35 +173,35 @@ 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());
}

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

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

for (String deletedPlanId : deletedPlans) {
for (Id<String> deletedPlanId : deletedPlans) {
PlanInheritanceRecord deletedPlanInheritanceRecord = this.planId2planInheritanceRecords.remove(deletedPlanId);
deletedPlanInheritanceRecord.iterationRemoved = event.getIteration();
deletedPlanInheritanceRecord.setIterationRemoved(event.getIteration());
this.planInheritanceRecordWriter.write(deletedPlanInheritanceRecord);
}

Expand All @@ -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<String> strategies, Map<String, PlanInheritanceRecord> planId2planInheritanceRecords, Set<String> planIds, BufferedWriter writer) {
private void calculateAndWriteDistribution(int currentIteration, ArrayList<String> strategies, Map<Id<String>, PlanInheritanceRecord> planId2planInheritanceRecords, Set<Id<String>> planIds, BufferedWriter writer) {
Map<String, AtomicLong> 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<String> planId : planIds) {
String mutatedBy = planId2planInheritanceRecords.get(planId).getMutatedBy();
strategy2count.get(mutatedBy).incrementAndGet();
}
long sum = strategy2count.values().stream().mapToLong(count -> count.get()).sum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> agentId;

/**
* The globally unique plan id.
*/
String planId;
private Id<String> planId;

/**
* Id of the plan that this plan had been copied from before mutating.
*/
String ancestorId;
private Id<String> 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<Integer> iterationsSelected = new ArrayList<>(1);
private List<Integer> iterationsSelected = new ArrayList<>(1);

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

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

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

public void setAncestorId(Id<String> 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<Integer> getIterationsSelected() {
return iterationsSelected;
}

public void setIterationsSelected(List<Integer> iterationsSelected) {
this.iterationsSelected = iterationsSelected;
}

public Id<Person> getAgentId() {
return agentId;
}

public void setAgentId(Id<Person> agentId) {
this.agentId = agentId;
}

}
Loading

0 comments on commit b6bada1

Please sign in to comment.