diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/SampleNetwork.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/SampleNetwork.java index 37585a7cd94..82a7a278e32 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/SampleNetwork.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/SampleNetwork.java @@ -25,7 +25,7 @@ import org.matsim.core.network.NetworkUtils; import org.matsim.core.network.algorithms.MultimodalNetworkCleaner; import org.matsim.core.network.filter.NetworkFilterManager; -import org.matsim.core.router.FastDijkstraFactory; +import org.matsim.core.router.DijkstraFactory; import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility; import org.matsim.core.router.util.LeastCostPathCalculator; import org.matsim.core.router.util.TravelTime; @@ -293,7 +293,7 @@ private Network createCityNetwork(Network network) { private LeastCostPathCalculator createRandomizedRouter(Network network, TravelTime tt) { OnlyTimeDependentTravelDisutility util = new OnlyTimeDependentTravelDisutility(tt); - return new FastDijkstraFactory(false).createPathCalculator(network, util, tt); + return new DijkstraFactory(false).createPathCalculator(network, util, tt); } private static final class RandomizedTravelTime implements TravelTime { diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/ApplyNetworkParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/ApplyNetworkParams.java index c7ce72750f9..58a71fd52b5 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/ApplyNetworkParams.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/ApplyNetworkParams.java @@ -41,7 +41,7 @@ public class ApplyNetworkParams implements MATSimAppCommand { private final OutputOptions output = OutputOptions.ofCommand(ApplyNetworkParams.class); @CommandLine.Parameters(arity = "1..*", description = "Type of parameters to apply. Available: ${COMPLETION-CANDIDATES}") - private Set params; + private Set params; @CommandLine.Option(names = "--input-params", description = "Path to parameter json") private String inputParams; @@ -53,7 +53,7 @@ public class ApplyNetworkParams implements MATSimAppCommand { private double[] speedFactorBounds; private NetworkModel model; - private NetworkParamsOpt.Request paramsOpt; + private NetworkParams paramsOpt; private int warn = 0; @@ -88,8 +88,10 @@ public Integer call() throws Exception { mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); - try (BufferedReader in = IOUtils.getBufferedReader(inputParams)) { - paramsOpt = mapper.readValue(in, NetworkParamsOpt.Request.class); + if (inputParams != null) { + try (BufferedReader in = IOUtils.getBufferedReader(inputParams)) { + paramsOpt = mapper.readValue(in, NetworkParams.class); + } } Map, Feature> features = NetworkParamsOpt.readFeatures(input.getPath("features.csv"), network.getLinks().size()); @@ -115,7 +117,7 @@ private void applyChanges(Link link, String junctionType, Object2DoubleMap validationSet, Map, Feature> features, - double[] speedFactorBounds, Request request, String save) throws IOException { + double[] speedFactorBounds, NetworkParams request, String save) throws IOException { Map, double[]> attributes = new HashMap<>(); @@ -169,13 +169,13 @@ public Integer call() throws Exception { log.info("Model score:"); - Result r = applyAndEvaluateParams(network, model, validationSet, features, speedFactorBounds, new Request(0), save(getParamsName(null))); + Result r = applyAndEvaluateParams(network, model, validationSet, features, speedFactorBounds, new NetworkParams(0), save(getParamsName(null))); writeResult(csv, null, r); if (params != null) { log.info("Model with parameter score:"); r = applyAndEvaluateParams(network, model, validationSet, features, speedFactorBounds, - mapper.readValue(params.toFile(), Request.class), save(getParamsName(params))); + mapper.readValue(params.toFile(), NetworkParams.class), save(getParamsName(params))); writeResult(csv, params, r); } @@ -210,12 +210,12 @@ private void evalSpeedFactors(Path eval, String save) throws IOException { String networkName = FilenameUtils.getName(input.getNetworkPath()); - Request best = null; + NetworkParams best = null; double bestScore = Double.POSITIVE_INFINITY; double[] bounds = {0, 1}; for (int i = 25; i <= 100; i++) { - Request req = new Request(i / 100d); + NetworkParams req = new NetworkParams(i / 100d); Result res = applyAndEvaluateParams(network, model, validationSet, features, bounds, req, null); csv.printRecord(networkName, i / 100d, res.mae(), res.rmse()); if (best == null || res.mae() < bestScore) { diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/FreespeedOptServer.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/FreespeedOptServer.java index eb1adedbc19..fec68b7eabb 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/FreespeedOptServer.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/FreespeedOptServer.java @@ -23,7 +23,6 @@ import org.matsim.application.analysis.traffic.traveltime.SampleValidationRoutes; import org.matsim.application.options.InputOptions; import org.matsim.application.prepare.network.opt.NetworkParamsOpt.Feature; -import org.matsim.application.prepare.network.opt.NetworkParamsOpt.Request; import org.matsim.application.prepare.network.opt.NetworkParamsOpt.Result; import picocli.CommandLine; @@ -122,7 +121,7 @@ public Integer call() throws Exception { return 0; } - private Result applyAndEvaluateParams(Request request, String save) throws IOException { + private Result applyAndEvaluateParams(NetworkParams request, String save) throws IOException { return EvalFreespeedParams.applyAndEvaluateParams(network, model, validationSet, features, speedFactorBounds, request, save); } @@ -132,7 +131,7 @@ private final class Backend implements HttpRequestHandler, ExceptionListener { @Override public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws IOException { - Request req = mapper.readValue(request.getEntity().getContent(), Request.class); + NetworkParams req = mapper.readValue(request.getEntity().getContent(), NetworkParams.class); Result stats = applyAndEvaluateParams(req, null); diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/Parameter.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkAttribute.java similarity index 78% rename from contribs/application/src/main/java/org/matsim/application/prepare/network/opt/Parameter.java rename to contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkAttribute.java index 4b7cdc79c01..9454d17eba5 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/Parameter.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkAttribute.java @@ -3,7 +3,7 @@ /** * Enum of network parameters. */ -public enum Parameter { +public enum NetworkAttribute { freespeed, capacity } diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParams.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParams.java new file mode 100644 index 00000000000..4a72caee5be --- /dev/null +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParams.java @@ -0,0 +1,53 @@ +package org.matsim.application.prepare.network.opt; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Object containing parameters for a model. Can be used to serialize and deserialize parameters. + */ +final class NetworkParams { + + double f; + + @JsonIgnore + Map params = new HashMap<>(); + + /** + * Used by jackson + */ + public NetworkParams() { + } + + public NetworkParams(double f) { + this.f = f; + } + + @JsonAnyGetter + public double[] getParams(String type) { + return params.get(type); + } + + @JsonAnySetter + public void setParams(String type, double[] params) { + this.params.put(type, params); + } + + public boolean hasParams() { + return !params.isEmpty(); + } + + @Override + public String toString() { + if (f == 0) + return "Request{" + params.entrySet().stream() + .map(e -> e.getKey() + "=" + e.getValue().length).collect(Collectors.joining(",")) + '}'; + + return "Request{f=" + f + "}"; + } +} diff --git a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParamsOpt.java b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParamsOpt.java index efa169d24fb..debeb1adde3 100644 --- a/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParamsOpt.java +++ b/contribs/application/src/main/java/org/matsim/application/prepare/network/opt/NetworkParamsOpt.java @@ -1,8 +1,5 @@ package org.matsim.application.prepare.network.opt; -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; -import com.fasterxml.jackson.annotation.JsonIgnore; import it.unimi.dsi.fastutil.doubles.DoubleList; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2DoubleMap; @@ -18,7 +15,7 @@ import org.matsim.api.core.v01.network.Network; import org.matsim.api.core.v01.network.Node; import org.matsim.application.analysis.traffic.traveltime.SampleValidationRoutes; -import org.matsim.core.router.FastDijkstraFactory; +import org.matsim.core.router.DijkstraFactory; import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility; import org.matsim.core.router.util.LeastCostPathCalculator; import org.matsim.core.trafficmonitoring.FreeSpeedTravelTime; @@ -33,7 +30,6 @@ import java.util.List; import java.util.Map; import java.util.function.Function; -import java.util.stream.Collectors; /** * Private helper class with utility functions. @@ -120,7 +116,7 @@ static Object2DoubleMap readValidation(List< static Result evaluate(Network network, Object2DoubleMap validationSet, Map, Feature> features, Map, double[]> attributes, String save) throws IOException { FreeSpeedTravelTime tt = new FreeSpeedTravelTime(); OnlyTimeDependentTravelDisutility util = new OnlyTimeDependentTravelDisutility(tt); - LeastCostPathCalculator router = new FastDijkstraFactory(false).createPathCalculator(network, util, tt); + LeastCostPathCalculator router = new DijkstraFactory(false).createPathCalculator(network, util, tt); SummaryStatistics rmse = new SummaryStatistics(); SummaryStatistics mse = new SummaryStatistics(); @@ -174,50 +170,6 @@ static Result evaluate(Network network, Object2DoubleMap params = new HashMap<>(); - - /** - * Used by jackson - */ - public Request() { - } - - public Request(double f) { - this.f = f; - } - - @JsonAnyGetter - public double[] getParams(String type) { - return params.get(type); - } - - @JsonAnySetter - public void setParams(String type, double[] params) { - this.params.put(type, params); - } - - public boolean hasParams() { - return !params.isEmpty(); - } - - @Override - public String toString() { - if (f == 0) - return "Request{" + params.entrySet().stream() - .map(e -> e.getKey() + "=" + e.getValue().length).collect(Collectors.joining(",")) + '}'; - - return "Request{f=" + f + "}"; - } - } - record Feature(String junctionType, Object2DoubleMap features) { } diff --git a/contribs/application/src/test/java/org/matsim/application/prepare/network/opt/ApplyNetworkParamsTest.java b/contribs/application/src/test/java/org/matsim/application/prepare/network/opt/ApplyNetworkParamsTest.java new file mode 100644 index 00000000000..8f34a77dac6 --- /dev/null +++ b/contribs/application/src/test/java/org/matsim/application/prepare/network/opt/ApplyNetworkParamsTest.java @@ -0,0 +1,45 @@ +package org.matsim.application.prepare.network.opt; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.matsim.contrib.sumo.SumoNetworkConverter; +import org.matsim.testcases.MatsimTestUtils; + +import java.nio.file.Path; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class ApplyNetworkParamsTest { + + @RegisterExtension + MatsimTestUtils utils = new MatsimTestUtils(); + + @Test + void apply() throws Exception { + + Path networkPath = Path.of(utils.getPackageInputDirectory()).resolve("osm.net.xml"); + + Path output = Path.of(utils.getOutputDirectory()); + + SumoNetworkConverter converter = SumoNetworkConverter.newInstance(List.of(networkPath), + output.resolve("network.xml"), + "EPSG:4326", "EPSG:4326"); + + converter.call(); + + assertThat(output.resolve("network.xml")).exists(); + assertThat(output.resolve("network-ft.csv")).exists(); + + new ApplyNetworkParams().execute( + "capacity", "freespeed", + "--network", output.resolve("network.xml").toString(), + "--input-features", output.resolve("network-ft.csv").toString(), + "--output", output.resolve("network-opt.xml").toString(), + "--model", "org.matsim.application.prepare.network.opt.ref.GermanyNetworkParams" + ); + + assertThat(output.resolve("network-opt.xml")).exists(); + + } +} diff --git a/contribs/application/test/input/org/matsim/application/prepare/network/opt/osm.net.xml b/contribs/application/test/input/org/matsim/application/prepare/network/opt/osm.net.xml new file mode 100644 index 00000000000..021ff2e24cb --- /dev/null +++ b/contribs/application/test/input/org/matsim/application/prepare/network/opt/osm.net.xml @@ -0,0 +1,1759 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java index 4b9d84e6be0..bfe6adb3158 100644 --- a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java +++ b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java @@ -278,7 +278,8 @@ public void startElement(String uri, String localName, String qName, Attributes // aggregate edges split by sumo again String from = attributes.getValue("from"); - Junction j = junctions.get(edges.get(from).to); + Edge fromEdge = edges.get(from); + Junction j = fromEdge != null ? junctions.get(fromEdge.to) : null; Connection conn = new Connection(from, attributes.getValue("to"), Integer.parseInt(attributes.getValue("fromLane")),