Skip to content

Commit

Permalink
fix: population routing requires vehicles now (#192)
Browse files Browse the repository at this point in the history
* insert vehicles for pipeline routing

* add default vehicle type

* clear vehicles for defaults

* next try

* update changelog
  • Loading branch information
sebhoerl authored Feb 15, 2024
1 parent ffadf34 commit a8a73b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ included in the (note yet determined) next version number.

**Development version**

- fix: make compatible with downstream population pipelines
- Ensure outside activity id doesn't already exist
- Network-based (car) routing now generates access and egress walk legs
- Convert initial-routing only-walk legs to actual walk (instead of transit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package org.eqasim.core.scenario.routing;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eqasim.core.misc.InjectorBuilder;
import org.eqasim.core.simulation.EqasimConfigurator;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.QSimConfigGroup.VehiclesSource;
import org.matsim.core.population.io.PopulationWriter;
import org.matsim.core.population.routes.NetworkRoute;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.timing.TimeInterpretationModule;
import org.matsim.facilities.ActivityFacility;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleUtils;
import org.matsim.vehicles.Vehicles;
import org.matsim.vehicles.VehiclesFactory;

import com.google.inject.Injector;

Expand All @@ -34,6 +47,7 @@ static public void main(String[] args) throws ConfigurationException, Interrupte
.orElse(Runtime.getRuntime().availableProcessors());

Scenario scenario = ScenarioUtils.loadScenario(config);
insertVehicles(config, scenario);

if (scenario.getActivityFacilities() != null) {
for (ActivityFacility facility : scenario.getActivityFacilities().getFacilities().values()) {
Expand All @@ -54,12 +68,52 @@ static public void main(String[] args) throws ConfigurationException, Interrupte
Injector injector = new InjectorBuilder(scenario) //
.addOverridingModules(configurator.getModules()) //
.addOverridingModule(new PopulationRouterModule(numberOfThreads, batchSize, true, modes)) //
.addOverridingModule(new TimeInterpretationModule())
.build();
.addOverridingModule(new TimeInterpretationModule()).build();

PopulationRouter populationRouter = injector.getInstance(PopulationRouter.class);
populationRouter.run(scenario.getPopulation());

clearVehicles(config, scenario);
new PopulationWriter(scenario.getPopulation()).write(cmd.getOptionStrict("output-path"));
}

static private void insertVehicles(Config config, Scenario scenario) {
if (config.qsim().getVehiclesSource().equals(VehiclesSource.defaultVehicle)) {
Vehicles vehicles = scenario.getVehicles();
VehiclesFactory factory = vehicles.getFactory();

vehicles.addVehicleType(VehicleUtils.getDefaultVehicleType());

for (Person person : scenario.getPopulation().getPersons().values()) {
Map<String, Id<Vehicle>> personVehicles = new HashMap<>();

for (String mode : config.plansCalcRoute().getNetworkModes()) {
Vehicle vehicle = factory.createVehicle(Id.createVehicleId(person.getId().toString() + ":" + mode),
VehicleUtils.getDefaultVehicleType());
vehicles.addVehicle(vehicle);

personVehicles.put(mode, vehicle.getId());
}

VehicleUtils.insertVehicleIdsIntoAttributes(person, personVehicles);
}
}
}

static private void clearVehicles(Config config, Scenario scenario) {
if (config.qsim().getVehiclesSource().equals(VehiclesSource.defaultVehicle)) {
for (Person person : scenario.getPopulation().getPersons().values()) {
person.getAttributes().removeAttribute("vehicles");

for (Plan plan : person.getPlans()) {
for (Leg leg : TripStructureUtils.getLegs(plan)) {
if (leg.getRoute() instanceof NetworkRoute) {
NetworkRoute route = (NetworkRoute) leg.getRoute();
route.setVehicleId(null);
}
}
}
}
}
}
}

0 comments on commit a8a73b5

Please sign in to comment.