Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: population routing requires vehicles now #192

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
}
}
}
Loading