Commit c3803ea5 authored by Sebastian Hörl's avatar Sebastian Hörl
Browse files

Add outside constraints for partial scenarios

parent 1b80e0d3
......@@ -20,11 +20,15 @@ import ch.ethz.matsim.discrete_mode_choice.modules.config.ModeAvailabilityConfig
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.SwissUtilityParameters.UtilitySet;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.cost.CostModel;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.cost.CostParameters;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.outside.OutsideConstraint;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.outside.SwissTourFinder;
public class SwissDiscreteModeChoiceModule extends AbstractDiscreteModeChoiceExtension {
public final static String COMMAND_LINE_PREFIX = "utility";
public static final String SWISS_ESTIMATOR_NAME = "Swiss";
public static final String SWISS_MODE_AVAILABILITY_NAME = "SwissModeAvailability";
public static final String SWISS_TOUR_FINDER_NAME = "SwissTourFinder";
public static final String OUTSIDE_CONSTRAINT_NAME = "OutsideConstraint";
private final UtilitySet utilitySet;
private final boolean useRouteChoice;
......@@ -45,6 +49,8 @@ public class SwissDiscreteModeChoiceModule extends AbstractDiscreteModeChoiceExt
install(new DiscreteModeChoiceModule());
bindTripEstimator(SWISS_ESTIMATOR_NAME).to(SwissUtilityEstimator.class);
bindModeAvailability(SWISS_MODE_AVAILABILITY_NAME).to(SwissModeAvailability.class);
bindTourFinder(SWISS_TOUR_FINDER_NAME).to(SwissTourFinder.class);
bindTripConstraintFactory(OUTSIDE_CONSTRAINT_NAME).to(OutsideConstraint.Factory.class);
}
@Provides
......@@ -89,4 +95,15 @@ public class SwissDiscreteModeChoiceModule extends AbstractDiscreteModeChoiceExt
ModeAvailabilityConfigGroup maConfig = dmcConfig.getCarModeAvailabilityConfig();
return new SwissModeAvailability(maConfig.getAvailableModes());
}
@Provides
public SwissTourFinder provideSwissTourFinder() {
return new SwissTourFinder();
}
@Provides
@Singleton
public OutsideConstraint.Factory provideOutsideConstraintFactory() {
return new OutsideConstraint.Factory();
}
}
......@@ -38,6 +38,12 @@ public class SwissModeAvailability extends CarModeAvailability {
modes.add(pravAvailability);
}
Boolean outside = (Boolean) person.getAttributes().getAttribute("outside");
if (outside != null && outside) {
modes.add("outside");
}
return modes;
}
}
......@@ -18,7 +18,6 @@ import ch.ethz.matsim.discrete_mode_choice.modules.DiscreteModeChoiceConfigurato
import ch.ethz.matsim.discrete_mode_choice.modules.EstimatorModule;
import ch.ethz.matsim.discrete_mode_choice.modules.ModelModule.ModelType;
import ch.ethz.matsim.discrete_mode_choice.modules.SelectorModule;
import ch.ethz.matsim.discrete_mode_choice.modules.TourFinderModule;
import ch.ethz.matsim.discrete_mode_choice.modules.config.DiscreteModeChoiceConfigGroup;
import ch.ethz.matsim.discrete_mode_choice.modules.config.VehicleTourConstraintConfigGroup.HomeType;
......@@ -38,7 +37,7 @@ public class SwissModeChoiceConfigurator {
dmcConfig.setTourEstimator(EstimatorModule.CUMULATIVE);
dmcConfig.setCachedModes(Arrays.asList("car", "bike", "pt", "walk"));
dmcConfig.setTourFinder(TourFinderModule.ACTIVITY_BASED);
dmcConfig.setTourFinder(SwissDiscreteModeChoiceModule.SWISS_TOUR_FINDER_NAME);
dmcConfig.getActivityTourFinderConfigGroup().setActivityType("home");
dmcConfig.setModeAvailability(SwissDiscreteModeChoiceModule.SWISS_MODE_AVAILABILITY_NAME);
......@@ -46,7 +45,8 @@ public class SwissModeChoiceConfigurator {
dmcConfig.setTourConstraints(
Arrays.asList(ConstraintModule.VEHICLE_CONTINUITY, ConstraintModule.FROM_TRIP_BASED));
dmcConfig.setTripConstraints(Arrays.asList(ConstraintModule.TRANSIT_WALK));
dmcConfig.setTripConstraints(
Arrays.asList(ConstraintModule.TRANSIT_WALK, SwissDiscreteModeChoiceModule.OUTSIDE_CONSTRAINT_NAME));
dmcConfig.getVehicleTourConstraintConfig().setHomeType(HomeType.USE_ACTIVITY_TYPE);
dmcConfig.getVehicleTourConstraintConfig().setRestrictedModes(Arrays.asList("car", "bike"));
......
......@@ -115,6 +115,8 @@ public abstract class BaseUtilityEstimator extends AbstractTripRouterEstimator {
case SharedAVModule.AV_POOL:
case SharedAVModule.AV_TAXI:
return estimateSharedAvTrip(personVariables, trip, routedTrip);
case "outside":
return 0.0;
default:
throw new IllegalStateException("Unknown mode: " + mode);
}
......
package ch.ethz.matsim.projects.astra_2018_002.mode_choice.outside;
import java.util.Collection;
import java.util.List;
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.constraints.AbstractTripConstraint;
import ch.ethz.matsim.discrete_mode_choice.model.trip_based.TripConstraint;
import ch.ethz.matsim.discrete_mode_choice.model.trip_based.TripConstraintFactory;
public class OutsideConstraint extends AbstractTripConstraint {
public static final String OUTSIDE_MODE = "outside";
@Override
public boolean validateBeforeEstimation(DiscreteModeChoiceTrip trip, String mode, List<String> previousModes) {
if (trip.getInitialMode().equals(OUTSIDE_MODE)) {
if (!mode.equals(OUTSIDE_MODE)) {
return false;
}
}
if (mode.equals(OUTSIDE_MODE)) {
if (!trip.getInitialMode().equals(OUTSIDE_MODE)) {
return false;
}
}
return true;
}
static public class Factory implements TripConstraintFactory {
@Override
public TripConstraint createConstraint(Person person, List<DiscreteModeChoiceTrip> planTrips,
Collection<String> availableModes) {
return new OutsideConstraint();
}
}
}
package ch.ethz.matsim.projects.astra_2018_002.mode_choice.outside;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import ch.ethz.matsim.discrete_mode_choice.components.tour_finder.TourFinder;
import ch.ethz.matsim.discrete_mode_choice.model.DiscreteModeChoiceTrip;
public class SwissTourFinder implements TourFinder {
private final String HOME_ACTIVITY_TYPE = "home";
private final String OUTSIDE_ACTIVITY_TYPE = "outside";
@Override
public List<List<DiscreteModeChoiceTrip>> findTours(List<DiscreteModeChoiceTrip> trips) {
Set<Integer> relevantActivityIndices = new HashSet<>();
for (int index = 0; index < trips.size(); index++) {
DiscreteModeChoiceTrip trip = trips.get(index);
if (trip.getOriginActivity().getType().equals(HOME_ACTIVITY_TYPE)
|| trip.getOriginActivity().getType().equals(OUTSIDE_ACTIVITY_TYPE)) {
relevantActivityIndices.add(index);
}
if (trip.getDestinationActivity().getType().equals(HOME_ACTIVITY_TYPE)
|| trip.getDestinationActivity().getType().equals(OUTSIDE_ACTIVITY_TYPE)) {
relevantActivityIndices.add(index + 1);
}
}
List<Integer> orderedActivityIndices = new ArrayList<>(relevantActivityIndices);
Collections.sort(orderedActivityIndices);
List<List<DiscreteModeChoiceTrip>> tours = new LinkedList<>();
int currentActivityIndex = 0;
while (orderedActivityIndices.size() > 0) {
int nextActivityIndex = orderedActivityIndices.remove(0);
tours.add(trips.subList(currentActivityIndex, nextActivityIndex));
currentActivityIndex = nextActivityIndex;
}
if (currentActivityIndex != trips.size()) {
tours.add(trips.subList(currentActivityIndex, trips.size()));
}
tours = tours.stream().filter(t -> t.size() > 0).collect(Collectors.toList());
return tours;
}
}
Markdown is supported
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