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

Railsim improvements #3657

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public void setPosition(TrainPosition position) {
@Override
public double getLinkTravelDisutility(Link link, double time, Person person, Vehicle vehicle) {
// only works with fixed block
return resources.hasCapacity(time, link.getId(), RailResourceManager.ANY_TRACK, position) ? 0 : 1;
int weight = resources.hasCapacity(time, link.getId(), RailResourceManager.ANY_TRACK, position) ? 0 : 1;

// Small offset in the weight prevents dead-locks in case there are loops within the station
return weight + 0.00001;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,46 @@
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.api.experimental.events.VehicleArrivesAtFacilityEvent;
import org.matsim.core.api.experimental.events.VehicleDepartsAtFacilityEvent;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.events.handler.EventHandler;
import org.matsim.core.gbl.MatsimRandom;
import org.matsim.core.mobsim.framework.listeners.MobsimListener;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.pt.transitSchedule.api.Departure;
import org.matsim.pt.transitSchedule.api.TransitLine;
import org.matsim.pt.transitSchedule.api.TransitRoute;
import org.matsim.pt.transitSchedule.api.TransitScheduleFactory;
import org.matsim.pt.transitSchedule.api.TransitStopFacility;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.testcases.utils.EventsCollector;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -317,6 +333,53 @@ void testMicroStationRerouting() {
// These arrive closer together, because both waited
assertTrainState(30974, 0, 0, 0, 400, filterTrainEvents(collector, "train3"));
assertTrainState(30982, 0, 0, 0, 400, filterTrainEvents(collector, "train4"));

// collect all arrivals and departures
Map<Id<Vehicle>, List<Id<TransitStopFacility>>> train2arrivalStops = new HashMap<>();
Map<Id<Vehicle>, List<Id<TransitStopFacility>>> train2departureStops = new HashMap<>();

for (Event event : collector.getEvents()) {
if (event.getEventType().equals(VehicleArrivesAtFacilityEvent.EVENT_TYPE)) {

VehicleArrivesAtFacilityEvent vehicleArrivesEvent = (VehicleArrivesAtFacilityEvent) event;

if (train2arrivalStops.get(vehicleArrivesEvent.getVehicleId()) == null) {

List<Id<TransitStopFacility>> stops = new ArrayList<>();
stops.add(vehicleArrivesEvent.getFacilityId());
train2arrivalStops.put(vehicleArrivesEvent.getVehicleId(), stops);
} else {
train2arrivalStops.get(vehicleArrivesEvent.getVehicleId()).add(vehicleArrivesEvent.getFacilityId());
}
}

if (event.getEventType().equals(VehicleDepartsAtFacilityEvent.EVENT_TYPE)) {

VehicleDepartsAtFacilityEvent vehicleDepartsEvent = (VehicleDepartsAtFacilityEvent) event;

if (train2departureStops.get(vehicleDepartsEvent.getVehicleId()) == null) {

List<Id<TransitStopFacility>> stops = new ArrayList<>();
stops.add(vehicleDepartsEvent.getFacilityId());
train2departureStops.put(vehicleDepartsEvent.getVehicleId(), stops);
} else {
train2departureStops.get(vehicleDepartsEvent.getVehicleId()).add(vehicleDepartsEvent.getFacilityId());
}
}
}

// test if all trains have the correct number of arrivals and departures
for (Id<Vehicle> vehicleId : train2arrivalStops.keySet()) {
Assertions.assertEquals(3, train2arrivalStops.get(vehicleId).size(), MatsimTestUtils.EPSILON, "Wrong number of arrival stops for train " + vehicleId);
}
for (Id<Vehicle> vehicleId : train2departureStops.keySet()) {
Assertions.assertEquals(3, train2departureStops.get(vehicleId).size(), MatsimTestUtils.EPSILON, "Wrong number of departure stops for train " + vehicleId);
}

// test if the trains have the correct arrival / departure stop facilities
Assertions.assertEquals("AB", train2arrivalStops.get(Id.createVehicleId("train3")).get(0).toString(), "Wrong stop facility. This is the start stop facility.");
Assertions.assertEquals("CD", train2arrivalStops.get(Id.createVehicleId("train3")).get(1).toString(), "Wrong stop facility. This is the rerouted stop Id. Train 3 is rerouted from CE to CD.");
Assertions.assertEquals("JK", train2arrivalStops.get(Id.createVehicleId("train3")).get(2).toString(), "Wrong stop facility. This is the final stop facility.");
}

@Test
Expand Down Expand Up @@ -344,6 +407,32 @@ void testMicroStationReroutingConcurrent() {

EventsCollector collector = runSimulation(new File(utils.getPackageInputDirectory(), "microStationRerouting"), filter);
}

@Test
void testMicroStationReroutingTwoDirections() {
// modifyScheduleAndRunSimulation(new File(utils.getPackageInputDirectory(), "microStationReroutingTwoDirections"), 300, 1800);

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> future = executor.submit(() -> {
modifyScheduleAndRunSimulation(
new File(utils.getPackageInputDirectory(), "microStationReroutingTwoDirections"),
300,
1800
);
});

try {
future.get(30, TimeUnit.SECONDS);
} catch (TimeoutException e) {
future.cancel(true);
fail("Simulation took too long and was aborted!");
} catch (ExecutionException | InterruptedException e) {
// other exceptions
fail("An error occurred during the simulation: " + e.getMessage());
} finally {
executor.shutdownNow();
}
}

@Test
void testScenarioKelheim() {
Expand Down Expand Up @@ -442,6 +531,50 @@ public void install() {

return collector;
}

private void modifyScheduleAndRunSimulation(File scenarioDir, int maxRndDelayStepSize, int maxMaxRndDelay) {
Random rnd = MatsimRandom.getRandom();
rnd.setSeed(1242);

for (double maxRndDelaySeconds = 0; maxRndDelaySeconds <= maxMaxRndDelay; maxRndDelaySeconds = maxRndDelaySeconds + maxRndDelayStepSize) {
Config config = ConfigUtils.loadConfig(new File(scenarioDir, "config.xml").toString());

config.controller().setOutputDirectory(utils.getOutputDirectory() + "maxRndDelay_" + maxRndDelaySeconds);
config.controller().setDumpDataAtEnd(true);
config.controller().setCreateGraphs(false);
config.controller().setLastIteration(0);

Scenario scenario = ScenarioUtils.loadScenario(config);

Controler controler = new Controler(scenario);
controler.addOverridingModule(new RailsimModule());
controler.configureQSimComponents(components -> new RailsimQSimModule().configure(components));

// modify scenario
for (TransitLine line : scenario.getTransitSchedule().getTransitLines().values()) {
for (TransitRoute route : line.getRoutes().values()) {
List<Departure> departuresToRemove = new ArrayList<>();
List<Departure> departuresToAdd = new ArrayList<>();
for (Departure departure : route.getDepartures().values()) {
departuresToRemove.add(departure);
double departureTime = departure.getDepartureTime() + rnd.nextDouble() * maxRndDelaySeconds;
Departure modifiedDeparture = scenario.getTransitSchedule().getFactory().createDeparture(departure.getId(), departureTime);
modifiedDeparture.setVehicleId(departure.getVehicleId());
departuresToAdd.add(modifiedDeparture);
}

for (Departure departureToRemove : departuresToRemove) {
route.removeDeparture(departureToRemove);
}
for (Departure departureToAdd : departuresToAdd) {
route.addDeparture(departureToAdd);
}
}
}

controler.run();
}
}

private double timeToAccelerate(double v0, double v, double a) {
return (v - v0) / a;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "http://www.matsim.org/files/dtd/config_v2.dtd">
<config>

<module name="global">
<param name="randomSeed" value="4711"/>
<param name="coordinateSystem" value="Atlantis"/>
</module>

<module name="controler">
<param name="runId" value="test"/>
<param name="firstIteration" value="0"/>
</module>

<module name="qsim">
<!-- "start/endTime" of MobSim (00:00:00 == take earliest activity time/ run as long as active vehicles exist) -->
<param name="startTime" value="00:00:00"/>
<param name="endTime" value="24:00:00"/>

<param name="snapshotperiod" value="00:00:00"/> <!-- 00:00:00 means NO snapshot writing -->
<param name="mainMode" value="car"/>

<!-- time in seconds. Time after which the frontmost vehicle on a link is called `stuck' if it does not move. -->
<param name="stuckTime" value="999999."/>
</module>

<module name="transit">
<param name="routingAlgorithmType" value="SwissRailRaptor"/>
<param name="transitScheduleFile" value="transitSchedule.xml"/>
<param name="useTransit" value="true"/>
<param name="usingTransitInMobsim" value="true"/>
<param name="vehiclesFile" value="transitVehicles.xml"/>
</module>

<module name="network">
<param name="inputNetworkFile" value="trainNetwork.xml"/>
</module>

</config>
Loading
Loading