Commit 2d1f0190 authored by clivings's avatar clivings
Browse files

Implemented (I think that is the right word) assigning and using private

AVs in the simulation. So now ownership of pravs (private AVs) is
assigned to agents and pravs can be choosen during mode choice and
routed on the network. 

But still to do are:
Flow Efficiency Calculator for pravs 
Utility parameters for pravs that are different from cars

And then for the 2030 Scenario:
creating networks with permissions for different pravs based on link and
prav type
parent 1608701a
......@@ -2,6 +2,7 @@ package ch.ethz.matsim.projects.astra_2018_002;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.corelisteners.PlansScoring;
import org.matsim.core.router.util.TravelTime;
public class ASTRAModule extends AbstractModule {
@Override
......
package ch.ethz.matsim.projects.astra_2018_002;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
......@@ -13,6 +18,7 @@ import ch.ethz.matsim.baseline_scenario.transit.BaselineTransitModule;
import ch.ethz.matsim.baseline_scenario.transit.routing.DefaultEnrichedTransitRoute;
import ch.ethz.matsim.baseline_scenario.transit.routing.DefaultEnrichedTransitRouteFactory;
import ch.ethz.matsim.projects.astra_2018_002.av.AVConfigurator;
import ch.ethz.matsim.projects.astra_2018_002.av.AssignPrivatAVs;
import ch.ethz.matsim.projects.astra_2018_002.flow_efficiency.FlowEfficiencyConfigurator;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.SwissDiscreteModeChoiceModule;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.SwissModeChoiceConfigurator;
......@@ -51,10 +57,21 @@ public class RunASTRA2018002 {
scenario.getPopulation().getFactory().getRouteFactories().setRouteFactory(DefaultEnrichedTransitRoute.class,
new DefaultEnrichedTransitRouteFactory());
ScenarioUtils.loadScenario(scenario);
// TODO: only until we actually assign allowed modes via the pipeline
for (Link link : scenario.getNetwork().getLinks().values()) {
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
if (allowedModes.contains("car")) {
allowedModes.addAll(Arrays.asList("prav3", "prav4", "prav5"));
}
link.setAllowedModes(allowedModes);
}
// Adjust scenario
SwissModeChoiceConfigurator.copyHousholdAttributes(scenario);
//AssignPrivatAVs.main(scenario);
AssignPrivatAVs.main(scenario);
FlowEfficiencyConfigurator.defineVehicleTypes(scenario);
// Set up controller
......
......@@ -16,11 +16,12 @@ import org.matsim.core.scenario.ScenarioUtils;
public class AssignPrivatAVs {
static public void main(String args[]) {
Config config = ConfigUtils.loadConfig(args[0]);
Scenario scenario = ScenarioUtils.loadScenario(config);
static public final String AV_AVAILABILITY_ATTRIBUTE = "prAvAvail";
static public void main(Scenario scenario) {
//Config config = ConfigUtils.loadConfig(args[0]);
//Scenario scenario = ScenarioUtils.loadScenario(config);
Random random = new Random(1);
final String AV_AVAILABILITY_ATTRIBUTE = "prAvAvail";
for (Person person : scenario.getPopulation().getPersons().values()) {
String carAvailability = (String) person.getAttributes().getAttribute("carAvail");
......@@ -43,46 +44,33 @@ public class AssignPrivatAVs {
if (r <= 0.4200) {//produces "true" 42.05% of the time
person.getAttributes().putAttribute(AV_AVAILABILITY_ATTRIBUTE,"car");
person.getAttributes().putAttribute("carAvail", "true");
person.getAttributes().putAttribute("carAvail", carAvailability);
} else if (r <= 0.66) {//produces "true" 65.91% of the time: because the first step took 42.05% of the agents already,
// then this step will only consider the next 23.86% of agents
person.getAttributes().putAttribute(AV_AVAILABILITY_ATTRIBUTE,"prav3");
person.getAttributes().putAttribute("carAvail", "false");
person.getAttributes().putAttribute("carAvail", "never");
} else if (r <= 0.82){
person.getAttributes().putAttribute(AV_AVAILABILITY_ATTRIBUTE,"prav4");
person.getAttributes().putAttribute("carAvail", "false");
person.getAttributes().putAttribute("carAvail", "never");
} else {
person.getAttributes().putAttribute(AV_AVAILABILITY_ATTRIBUTE,"prav5");
person.getAttributes().putAttribute("carAvail", "false");
person.getAttributes().putAttribute("carAvail", "never");
}
} else { // make sure that those agents who "never" have a car available also do not have an private av available,and to
// prevent "null" exceptions from Java, which occur when a variable, such as the attribute "pAvail
// prevent "null" exceptions from Java, which occur when a variable, such as the attribute "prAvAvail" don't have a value
person.getAttributes().putAttribute(AV_AVAILABILITY_ATTRIBUTE,"noPrav");
person.getAttributes().putAttribute("carAvail", "false");
person.getAttributes().putAttribute("carAvail", "never");
}
// should also set selected plan to choose AV in order to start the
// scenario with the percentages give by TP5 as a starting point....
// scenario with the percentages given by TP5 as a starting point....
/*for (Plan plan : person.getPlans()) {
for (PlanElement element : plan.getPlanElements()) {
if (element instanceof Leg) {
Leg leg = (Leg) element;
if (leg.getMode().equals("car")) {
//need to make sure Mode is not zero, otherwise java will throw an error
leg.setMode((String) person.getAttributes().getAttribute(AV_AVAILABILITY_ATTRIBUTE));
}
}
}
}*/
}
for (Person person : scenario.getPopulation().getPersons().values()) {
......@@ -92,8 +80,13 @@ public class AssignPrivatAVs {
Leg leg = (Leg) element;
if (leg.getMode().equals("car")) {
//need to make sure Mode is not zero, otherwise java will throw an error
leg.setMode((String) person.getAttributes().getAttribute(AV_AVAILABILITY_ATTRIBUTE));
String pravAvailability = (String) person.getAttributes().getAttribute(AV_AVAILABILITY_ATTRIBUTE);
if (pravAvailability.equals("noPrav")) {
leg.setMode("walk");
} else {
leg.setMode(pravAvailability);
}
}
}
}
......@@ -112,9 +105,9 @@ public class AssignPrivatAVs {
*/
Population population = scenario.getPopulation();
//Population population = scenario.getPopulation();
PopulationUtils.writePopulation(population, args[1]);
//PopulationUtils.writePopulation(population, args[1]);
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ import org.matsim.vehicles.Vehicle;
public interface FlowEfficiencyCalculator {
double calculateFlowEfficiency(Vehicle vehicle, Link link);
class Instance implements FlowEfficiencyCalculator {
class SingletonCalculator implements FlowEfficiencyCalculator {
private FlowEfficiencyCalculator delegate;
public void set(FlowEfficiencyCalculator delegate) {
......@@ -23,5 +23,5 @@ public interface FlowEfficiencyCalculator {
}
}
final static Instance INSTANCE = new Instance();
final static SingletonCalculator INSTANCE = new SingletonCalculator();
}
......@@ -11,6 +11,9 @@ public class FlowEfficiencyConfigurator {
static final public Id<VehicleType> CONVENTIONAL_VEHICLE_TYPE_ID = Id.create("car", VehicleType.class);
static final public Id<VehicleType> PRIVATE_AV_VEHICLE_TYPE_ID = Id.create("private_av", VehicleType.class);
static final public Id<VehicleType> SHARED_AV_VEHICLE_TYPE_ID = Id.create("shared_av", VehicleType.class);
static final public Id<VehicleType> PRAV3_ID = Id.create("prav3", VehicleType.class);
static final public Id<VehicleType> PRAV4_ID = Id.create("prav4", VehicleType.class);
static final public Id<VehicleType> PRAV5_ID = Id.create("prav5", VehicleType.class);
static public void configure(Config config) {
// Load vehicles by vehicle type
......@@ -40,5 +43,17 @@ public class FlowEfficiencyConfigurator {
VehicleType sharedAvVehicleType = VehicleUtils.getFactory() //
.createVehicleType(SHARED_AV_VEHICLE_TYPE_ID);
scenario.getVehicles().addVehicleType(sharedAvVehicleType);
VehicleType prav3VehicleType = VehicleUtils.getFactory() //
.createVehicleType(PRAV3_ID);
scenario.getVehicles().addVehicleType(prav3VehicleType);
VehicleType prav4VehicleType = VehicleUtils.getFactory() //
.createVehicleType(PRAV4_ID);
scenario.getVehicles().addVehicleType(prav4VehicleType);
VehicleType prav5VehicleType = VehicleUtils.getFactory() //
.createVehicleType(PRAV5_ID);
scenario.getVehicles().addVehicleType(prav5VehicleType);
}
}
package ch.ethz.matsim.projects.astra_2018_002.mode_choice;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.matsim.api.core.v01.TransportMode;
......@@ -9,6 +11,7 @@ import org.matsim.api.core.v01.population.Person;
import ch.ethz.matsim.discrete_mode_choice.model.DiscreteModeChoiceTrip;
import ch.ethz.matsim.discrete_mode_choice.model.mode_availability.CarModeAvailability;
import ch.ethz.matsim.projects.astra_2018_002.av.AssignPrivatAVs;
public class SwissModeAvailability extends CarModeAvailability {
public SwissModeAvailability(Collection<String> modes) {
......@@ -18,12 +21,24 @@ public class SwissModeAvailability extends CarModeAvailability {
@Override
public Collection<String> getAvailableModes(Person person, List<DiscreteModeChoiceTrip> trips) {
boolean bikeAvailability = !"FOR_NONE".equals((String) person.getAttributes().getAttribute("bikeAvailability"));
Set<String> modes = new HashSet<>();
if (!bikeAvailability) {
return super.getAvailableModes(person, trips).stream().filter(m -> !TransportMode.bike.equals(m))
.collect(Collectors.toSet());
modes.addAll(super.getAvailableModes(person, trips).stream().filter(m -> !TransportMode.bike.equals(m))
.collect(Collectors.toSet()));
} else {
modes.addAll(super.getAvailableModes(person, trips));
}
String pravAvailability = (String) person.getAttributes().getAttribute(AssignPrivatAVs.AV_AVAILABILITY_ATTRIBUTE);
if (!pravAvailability.equals("noPrav")) {
modes.add(pravAvailability);
}
System.err.println(modes);
return super.getAvailableModes(person, trips);
return modes;
}
}
......@@ -36,7 +36,7 @@ public class SwissModeChoiceConfigurator {
dmcConfig.setTripEstimator(SwissDiscreteModeChoiceModule.SWISS_ESTIMATOR_NAME);
dmcConfig.setTourEstimator(EstimatorModule.CUMULATIVE);
dmcConfig.setCachedModes(Arrays.asList("car", "bike", "pt", "walk"));
dmcConfig.setCachedModes(Arrays.asList("car", "bike", "pt", "walk", "prav3", "prav4", "prav5"));
dmcConfig.setTourFinder(TourFinderModule.ACTIVITY_BASED);
dmcConfig.getActivityTourFinderConfigGroup().setActivityType("home");
......@@ -49,7 +49,12 @@ public class SwissModeChoiceConfigurator {
dmcConfig.setTripConstraints(Arrays.asList(ConstraintModule.TRANSIT_WALK));
dmcConfig.getVehicleTourConstraintConfig().setHomeType(HomeType.USE_ACTIVITY_TYPE);
dmcConfig.getVehicleTourConstraintConfig().setRestrictedModes(Arrays.asList("car", "bike"));
dmcConfig.getVehicleTourConstraintConfig().setRestrictedModes(Arrays.asList("car", "bike", "prav3", "prav4", "prav5"));
config.plansCalcRoute().setNetworkModes(Arrays.asList("car","prav3", "prav4", "prav5"));
config.qsim().setMainModes(Arrays.asList("car","prav3", "prav4", "prav5"));
config.travelTimeCalculator().setAnalyzedModes("car,prav3,prav4,prav5");
config.travelTimeCalculator().setSeparateModes(false);
// These parameters are only used by SwissRailRaptor. We configure the
// parameters here in a way that SRR searches for the route with the shortest
......@@ -75,6 +80,15 @@ public class SwissModeChoiceConfigurator {
ModeRoutingParams walkParams = config.plansCalcRoute().getModeRoutingParams().get(TransportMode.walk);
walkParams.setBeelineDistanceFactor(1.3);
walkParams.setTeleportedModeSpeed(1.2); // 4.32 km/h
ModeParams prav3Params = new ModeParams("prav3");
config.planCalcScore().addModeParams(prav3Params);
ModeParams prav4Params = new ModeParams("prav4");
config.planCalcScore().addModeParams(prav4Params);
ModeParams prav5Params = new ModeParams("prav5");
config.planCalcScore().addModeParams(prav5Params);
}
static public void copyHousholdAttributes(Scenario scenario) {
......
......@@ -232,12 +232,13 @@ public class SwissUtilityEstimator extends BaseUtilityEstimator {
}
@Override
public double estimateCarTrip(DiscreteModeChoiceTrip trip, PersonVariables personVariables,
TripVariablesCar tripVariables) {
public double estimatePrivateVehicleTrip(DiscreteModeChoiceTrip trip, PersonVariables personVariables,
TripVariablesCar tripVariables, String mode) {
double utility = 0.0;
utility += parameters.car.alpha;
// TODO: Consider private AV VOT here.
utility += parameters.car.betaTravelTime //
* interaction(tripVariables.travelDistance_km, parameters.referenceDistance_km,
parameters.car.lambdaTravelTimeDistance) //
......
......@@ -84,7 +84,10 @@ public abstract class BaseUtilityEstimator extends AbstractTripRouterEstimator {
case TransportMode.bike:
return estimateBikeTrip(personVariables, trip, routedTrip);
case TransportMode.car:
return estimateCarTrip(personVariables, trip, routedTrip);
case "prav3":
case "prav4":
case "prav5":
return estimatePrivateVehicleTrip(personVariables, trip, routedTrip, mode);
case TransportMode.pt:
return estimatePtTrip(personVariables, trip, routedTrip);
case ASTRAAVModule.AV_POOL:
......@@ -232,11 +235,11 @@ public abstract class BaseUtilityEstimator extends AbstractTripRouterEstimator {
return estimatePtTrip(trip, personVariables, tripVariables);
}
abstract protected double estimateCarTrip(DiscreteModeChoiceTrip trip, PersonVariables personVariables,
TripVariablesCar tripVariables);
abstract protected double estimatePrivateVehicleTrip(DiscreteModeChoiceTrip trip, PersonVariables personVariables,
TripVariablesCar tripVariables, String mode);
private double estimateCarTrip(PersonVariables personVariables, DiscreteModeChoiceTrip trip,
List<? extends PlanElement> elements) {
private double estimatePrivateVehicleTrip(PersonVariables personVariables, DiscreteModeChoiceTrip trip,
List<? extends PlanElement> elements, String mode) {
TripVariablesCar tripVariables = new TripVariablesCar();
Leg leg = (Leg) elements.get(0);
......@@ -247,6 +250,7 @@ public abstract class BaseUtilityEstimator extends AbstractTripRouterEstimator {
/*
* TODO: Right now we don't define parking cost or parking search time
* TODO: Add different costs for private AVs here.
*/
tripVariables.parkingSearchTime_min = 0.0;
......@@ -258,7 +262,7 @@ public abstract class BaseUtilityEstimator extends AbstractTripRouterEstimator {
tripVariables.crowflyDistance_km = CoordUtils.calcEuclideanDistance(trip.getOriginActivity().getCoord(),
trip.getDestinationActivity().getCoord());
return estimateCarTrip(trip, personVariables, tripVariables);
return estimatePrivateVehicleTrip(trip, personVariables, tripVariables, mode);
}
abstract protected double estimateWalkTrip(DiscreteModeChoiceTrip trip, PersonVariables personVariables,
......
......@@ -54,7 +54,7 @@ public class TestUtility {
double ptUtility = estimator.estimatePtTrip(trip, personVariables, tripVariablesPt);
TripVariablesCar tripVariablesCar = new TripVariablesCar();
double carUtility = estimator.estimateCarTrip(trip, personVariables, tripVariablesCar);
double carUtility = estimator.estimatePrivateVehicleTrip(trip, personVariables, tripVariablesCar, "car");
System.out.println("Walk utility: " + walkUtility);
System.out.println("Bike utility: " + bikeUtility);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment