Skip to content

Commit

Permalink
Merge branch 'master' into fixedApproachDrtDropoffs
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel authored May 29, 2024
2 parents f92a4dd + 1c2cf78 commit ff8474b
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import jakarta.validation.constraints.NotNull;
import org.matsim.contrib.common.util.ReflectiveConfigGroupWithConfigurableParameterSets;
import org.matsim.contrib.common.zones.GridZoneSystem;
import org.matsim.contrib.common.zones.ZoneSystemParams;
import org.matsim.contrib.common.zones.systems.grid.GISFileZoneSystemParams;
import org.matsim.contrib.common.zones.systems.grid.h3.H3GridZoneSystemParams;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.core.config.ConfigGroup;

/**
* @author Michal Maciejewski (michalm)
Expand Down Expand Up @@ -65,8 +67,69 @@ private void initSingletonParameterSets() {
params -> zoneSystemParams = (H3GridZoneSystemParams)params);
}

@Override
public void handleAddUnknownParam(String paramName, String value) {
switch (paramName) {
case "zonesGeneration": {
if (getZoneSystemParams() == null) {
switch (value) {
case "ShapeFile": {
addParameterSet(createParameterSet(GISFileZoneSystemParams.SET_NAME));
break;
}
case "GridFromNetwork": {
addParameterSet(createParameterSet(SquareGridZoneSystemParams.SET_NAME));
break;
}
case "H3": {
addParameterSet(createParameterSet(H3GridZoneSystemParams.SET_NAME));
break;
}
default:
super.handleAddUnknownParam(paramName, value);
}
}
break;
}
case "cellSize": {
SquareGridZoneSystemParams squareGridParams;
if(getZoneSystemParams() == null) {
squareGridParams = (SquareGridZoneSystemParams) createParameterSet(SquareGridZoneSystemParams.SET_NAME);
addParameterSet(squareGridParams);
} else {
squareGridParams = (SquareGridZoneSystemParams) getZoneSystemParams();
}
squareGridParams.cellSize = Double.parseDouble(value);
break;
}
case "zonesShapeFile": {
GISFileZoneSystemParams gisFileParams;
if(getZoneSystemParams() == null) {
gisFileParams = (GISFileZoneSystemParams) createParameterSet(GISFileZoneSystemParams.SET_NAME);
addParameterSet(gisFileParams);
} else {
gisFileParams = (GISFileZoneSystemParams) getZoneSystemParams();
}
gisFileParams.zonesShapeFile = value;
break;
}
case "h3Resolution": {
H3GridZoneSystemParams h3GridParams;
if(getZoneSystemParams() == null) {
h3GridParams = (H3GridZoneSystemParams) createParameterSet(GISFileZoneSystemParams.SET_NAME);
addParameterSet(h3GridParams);
} else {
h3GridParams = (H3GridZoneSystemParams) getZoneSystemParams();
}
h3GridParams.h3Resolution = Integer.parseInt(value);
break;
}
default:
super.handleAddUnknownParam(paramName, value);
}
}

public ZoneSystemParams getZoneSystemParams() {
return zoneSystemParams;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.matsim.contrib.drt.analysis.zonal;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;

import java.net.URL;

/**
* @author nkuehnel / MOIA
*/
public class ReadOldConfigTest {

@Test
public void test() {
URL context = ExamplesUtils.getTestScenarioURL("kelheim");
Config config = ConfigUtils.loadConfig(IOUtils.extendUrl(context, "config-with-drt_old.xml"));
Assertions.assertThatNoException().isThrownBy(() -> ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class));
Assertions.assertThatNoException().isThrownBy(() -> ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ private void initSingletonParameterSets() {
params -> zoneSystemParams = (H3GridZoneSystemParams)params);
}

@Override
public void handleAddUnknownParam(String paramName, String value) {
if ("cellSize".equals(paramName)) {
SquareGridZoneSystemParams squareGridParams;
if(getZoneSystemParams() == null) {
squareGridParams = (SquareGridZoneSystemParams) createParameterSet(SquareGridZoneSystemParams.SET_NAME);
addParameterSet(squareGridParams);
} else {
squareGridParams = (SquareGridZoneSystemParams) getZoneSystemParams();
}
squareGridParams.cellSize = Double.parseDouble(value);
} else {
super.handleAddUnknownParam(paramName, value);
}
}

public ZoneSystemParams getZoneSystemParams() {
return zoneSystemParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static SpeedTarget calcTargetSpeed(double dist, double acceleration, double dece
}


assert FuzzyUtils.greaterEqualThan(allowedSpeed, currentSpeed) : "Current speed must be lower than allowed";
// assert FuzzyUtils.greaterEqualThan(allowedSpeed, currentSpeed) : "Current speed must be lower than allowed";
assert FuzzyUtils.greaterEqualThan(allowedSpeed, finalSpeed) : "Final speed must be smaller than target";

double timeAccel = (allowedSpeed - currentSpeed) / acceleration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,19 @@ private void enterLink(double time, UpdateEvent event) {
double stopTime = handleTransitStop(time, state);

assert stopTime >= 0 : "Stop time must be positive";
assert FuzzyUtils.equals(state.speed, 0) : "Speed must be 0 at pt stop, but was " + state.speed;
// assert FuzzyUtils.equals(state.speed, 0) : "Speed must be 0 at pt stop, but was " + state.speed;

// Same event is re-scheduled after stopping,
event.plannedTime = time + stopTime;
state.speed = 0;

return;
}

// Arrival at destination
if (!event.waitingForLink && state.isRouteAtEnd()) {

assert FuzzyUtils.equals(state.speed, 0) : "Speed must be 0 at end, but was " + state.speed;
// assert FuzzyUtils.equals(state.speed, 0) : "Speed must be 0 at end, but was " + state.speed;

// Free all reservations
for (RailLink link : state.route) {
Expand All @@ -379,6 +380,7 @@ private void enterLink(double time, UpdateEvent event) {
}
}

state.speed = 0;
state.driver.notifyArrivalOnLinkByNonNetworkMode(state.headLink);
state.driver.endLegAndComputeNextState(Math.ceil(time));

Expand Down Expand Up @@ -509,6 +511,18 @@ private void updatePosition(double time, UpdateEvent event) {

assert FuzzyUtils.greaterEqualThan(dist, 0) : "Travel distance must be positive, but was" + dist;

// This corrects inaccuracy happening when the train is at the end of the link
// Should very rarely be necessary
if (FuzzyUtils.greaterThan(state.headPosition + dist, resources.getLink(state.headLink).length)) {
// Dist will be dist to end of link
dist = resources.getLink(state.headLink).length - state.headPosition;
}
// In the same way also correct the speed
if (FuzzyUtils.greaterThan(state.speed, state.allowedMaxSpeed)) {
state.speed = state.allowedMaxSpeed;
state.acceleration = 0;
}

state.headPosition += dist;
state.tailPosition += dist;
state.approvedDist -= dist;
Expand All @@ -521,7 +535,6 @@ private void updatePosition(double time, UpdateEvent event) {
// this assertion may not hold depending on the network
// assert state.routeIdx <= 2 || FuzzyUtils.greaterEqualThan(state.tailPosition, 0) : "Illegal state update. Tail position should not be negative";

assert FuzzyUtils.lessEqualThan(state.headPosition, resources.getLink(state.headLink).length) : "Illegal state update. Head position must be smaller than link length";
assert FuzzyUtils.greaterEqualThan(state.headPosition, 0) : "Head position must be positive";
assert FuzzyUtils.lessEqualThan(state.speed, state.allowedMaxSpeed) : "Speed must be less equal than the allowed speed";
assert FuzzyUtils.greaterEqualThan(state.approvedDist, 0) : "Approved distance must be positive";
Expand Down Expand Up @@ -680,9 +693,10 @@ private void decideTargetSpeed(UpdateEvent event, TrainState state) {
state.train.acceleration(), state.train.deceleration(),
state.speed, state.allowedMaxSpeed, state.approvedSpeed);

assert FuzzyUtils.greaterEqualThan(target.decelDist(), 0) : "Decel dist is " + target.decelDist() + ", stopping is not possible";
// assert FuzzyUtils.greaterEqualThan(target.decelDist(), 0) : "Decel dist is " + target.decelDist() + ", stopping is not possible";

if (FuzzyUtils.equals(target.decelDist(), 0)) {
// Because of numerical inaccuracies, the decel dist might be slightly negative
if (FuzzyUtils.lessEqualThan(target.decelDist(), 0)) {
state.targetSpeed = state.approvedSpeed;
state.targetDecelDist = Double.POSITIVE_INFINITY;
stop = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package ch.sbb.matsim.contrib.railsim.qsimengine.resources;

import ch.sbb.matsim.contrib.railsim.qsimengine.TrainPosition;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.core.mobsim.framework.MobsimDriverAgent;

Expand All @@ -31,6 +33,8 @@
*/
final class FixedBlockResource implements RailResourceInternal {

private static final Logger log = LogManager.getLogger(FixedBlockResource.class);

private final Id<RailResource> id;

/**
Expand Down Expand Up @@ -178,8 +182,9 @@ public boolean release(RailLink link, MobsimDriverAgent driver) {
}
}

if (track == -1)
throw new AssertionError("Driver " + driver + " has not reserved the track.");
// This may happen in rare cases, but is not a problem
// if (track == -1)
// log.warn("Driver {} released {} multiple times.", driver, link.getLinkId());

boolean allFree = true;
for (MobsimDriverAgent[] others : tracks.values()) {
Expand Down
Loading

0 comments on commit ff8474b

Please sign in to comment.