Skip to content

Commit

Permalink
Merge branch 'develop' into feat/model-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Oct 3, 2024
2 parents 18b3925 + 9d3a934 commit a08a8bf
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package org.eqasim.core.components.config;

import org.eqasim.core.simulation.EqasimConfigurator;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.ConfigWriter;

public class ConfigAdapter {
static public void run(String[] args, ConfigGroup[] modules, ConfigAdapterConsumer adapter)
static public void run(String[] args, EqasimConfigurator configurator, ConfigAdapterConsumer adapter)
throws ConfigurationException {
CommandLine cmd = new CommandLine.Builder(args) //
.requireOptions("input-path", "output-path", "prefix") //
.build();

Config config = ConfigUtils.loadConfig(cmd.getOptionStrict("input-path"), modules);
Config config = ConfigUtils.loadConfig(cmd.getOptionStrict("input-path"), configurator.getConfigGroups());
configurator.addOptionalConfigGroups(config);
adapter.accept(config, cmd.getOptionStrict("prefix"));

new ConfigWriter(config).write(cmd.getOptionStrict("output-path"));
}

public interface ConfigAdapterConsumer {
void accept(Config config, String prefix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.matsim.contribs.discrete_mode_choice.modules.DiscreteModeChoiceModule;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.population.io.PopulationWriter;
Expand Down Expand Up @@ -62,8 +61,7 @@ static public void main(String[] args) throws ConfigurationException, Interrupte
Injector injector = new InjectorBuilder(scenario) //
.addOverridingModules(configurator.getModules().stream()
.filter(module -> !(module instanceof AbstractEqasimExtension)) //
.filter(module -> !(module instanceof DiscreteModeChoiceModule)) //
.toList()) //
.filter(module -> !(module instanceof DiscreteModeChoiceModule)).toList()) //
.addOverridingModule(new PopulationRouterModule(numberOfThreads, batchSize, true, modes)) //
.addOverridingModule(new TimeInterpretationModule()).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.util.List;
import java.util.Map;

import org.eqasim.core.simulation.mode_choice.AbstractEqasimExtension;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.controler.TerminationCriterion;

Expand All @@ -18,12 +18,12 @@
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.MapBinder;

public class EqasimTerminationModule extends AbstractModule {
public class EqasimTerminationModule extends AbstractEqasimExtension {
private static final String TERMINATION_CSV_FILE = "eqasim_termination.csv";
private static final String TERMINATION_HTML_FILE = "eqasim_termination.html";

@Override
public void install() {
protected void installEqasimExtension() {
bind(TerminationCriterion.class).to(EqasimTerminationCriterion.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eqasim.core.analysis.DistanceUnit;
Expand Down Expand Up @@ -152,6 +154,7 @@ public static void main(String[] args) throws CommandLine.ConfigurationException
.allowOptions(CMD_SIMULATE_AFTER)
.allowOptions(CMD_SKIP_SCENARIO_CHECK)
.allowOptions(EQASIM_CONFIGURATOR_CLASS, MODE_CHOICE_CONFIGURATOR_CLASS)
.allowAnyOption(true)
.build();

// Loading the config
Expand Down Expand Up @@ -272,22 +275,28 @@ RecordedTravelTime provideRecordedTravelTime() {
try {
Class<?> runClass = Class.forName(cmd.getOptionStrict(CMD_SIMULATE_AFTER));
Method method = runClass.getMethod("main", String[].class);

String[] extraArgs = cmd.getAvailableOptions().stream()
.filter(argName -> argName.startsWith("config:"))
.filter(argName -> !argName.startsWith("config:standaloneModeChoice"))
.filter(argName -> !argName.equals("config:plans.inputPlansFile"))
.flatMap(argName -> Stream.of("--"+argName, cmd.getOption(argName).get()))
.toArray(String[]::new);

String[] baseArgs = new String[]{
"--config-path", cmd.getOptionStrict(CMD_CONFIG_PATH),
"--config:plans.inputPlansFile", Paths.get(outputDirectoryHierarchy.getOutputFilename("output_plans.xml.gz")).toAbsolutePath().toString(),
"--config:controler.outputDirectory", outputDirectoryHierarchy.getOutputFilename("sim"),
"--config:controler.lastIteration", "0"
};
String[] allArgs = ArrayUtils.addAll(baseArgs, extraArgs);

method.invoke(null, new Object[]{
new String[]{
"--config-path", cmd.getOptionStrict(CMD_CONFIG_PATH),
"--config:plans.inputPlansFile", Paths.get(outputDirectoryHierarchy.getOutputFilename("output_plans.xml.gz")).toAbsolutePath().toString(),
"--config:controler.outputDirectory", outputDirectoryHierarchy.getOutputFilename("sim"),
"--config:controler.lastIteration", "0"
}
allArgs
});

} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.eqasim.core.analysis.DefaultPersonAnalysisFilter;
import org.eqasim.core.analysis.PersonAnalysisFilter;
import org.eqasim.core.simulation.EqasimConfigurator;
import org.eqasim.core.simulation.modes.transit_with_abstract_access.TransitWithAbstractAbstractAccessModuleConfigGroup;
import org.eqasim.core.simulation.modes.transit_with_abstract_access.TransitWithAbstractAccessModule;
import org.eqasim.core.simulation.modes.transit_with_abstract_access.mode_choice.TransitWithAbstractAccessModeChoiceModule;
import org.eqasim.core.simulation.termination.EqasimTerminationModule;
import org.eqasim.core.simulation.vdf.VDFConfigGroup;
import org.eqasim.core.simulation.vdf.VDFModule;
Expand Down Expand Up @@ -31,6 +34,8 @@ public StandaloneModeChoiceConfigurator(Config config, CommandLine commandLine)
this.optionalModules = new LinkedHashMap<>();

this.registerOptionalModule(new VDFConfigGroup(), new VDFModule());
this.registerOptionalModule(TransitWithAbstractAbstractAccessModuleConfigGroup.GROUP_NAME, new TransitWithAbstractAccessModeChoiceModule());
this.registerOptionalModule(TransitWithAbstractAbstractAccessModuleConfigGroup.GROUP_NAME, new TransitWithAbstractAccessModule());
}

public Config getConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private static class TraversalExporter implements VehicleEntersTrafficEventHandl
private final IdMap<Person, LinkEnterEvent> enterEvents = new IdMap<>(Person.class);
private final Map<Id<Person>, Integer> tripIndex = new HashMap<>();
private final Map<Id<Person>, Integer> legIndex = new HashMap<>();
private final Map<Id<Vehicle>, String> legMode = new HashMap<>();
private final Set<String> modes;

TraversalExporter(BufferedWriter writer, ScenarioExtent extent, Network network, Set<String> modes) {
Expand All @@ -98,7 +99,7 @@ private static class TraversalExporter implements VehicleEntersTrafficEventHandl

try {
writer.write(String.join(";", Arrays.asList( //
"person_id", "vehicle_id", "link_id", "enter_time", "leave_time", "trip_index", "leg_index"))
"person_id", "vehicle_id", "link_id", "enter_time", "leave_time", "trip_index", "leg_index", "leg_mode"))
+ "\n");
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -108,13 +109,15 @@ private static class TraversalExporter implements VehicleEntersTrafficEventHandl
@Override
public void handleEvent(VehicleEntersTrafficEvent event) {
if (checkMode(event.getNetworkMode())) {
legMode.put(event.getVehicleId(), event.getNetworkMode());
drivers.put(event.getVehicleId(), event.getPersonId());
}
}

@Override
public void handleEvent(VehicleLeavesTrafficEvent event) {
if (drivers.remove(event.getVehicleId()) != null) {
legMode.remove(event.getVehicleId());
writeTraversal(event.getPersonId(), enterEvents.remove(event.getPersonId()), null);
}
}
Expand Down Expand Up @@ -167,6 +170,7 @@ private void writeTraversal(Id<Person> personId, LinkEnterEvent enterEvent, Link

int localTripIndex = tripIndex.getOrDefault(personId, 0);
int localLegIndex = legIndex.getOrDefault(personId, 0);
String localLegMode = legMode.get(personId);

try {
writer.write(String.join(";", new String[] { //
Expand All @@ -176,7 +180,8 @@ private void writeTraversal(Id<Person> personId, LinkEnterEvent enterEvent, Link
String.valueOf(enterTime), //
String.valueOf(leaveTime), //
String.valueOf(localTripIndex), //
String.valueOf(localLegIndex) //
String.valueOf(localLegIndex), //
String.valueOf(localLegMode) //
}) + "\n");
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
7 changes: 6 additions & 1 deletion core/src/test/java/org/eqasim/TestSimulationPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
import org.eqasim.core.simulation.vdf.utils.AdaptConfigForVDF;
import org.eqasim.core.standalone_mode_choice.RunStandaloneModeChoice;
import org.eqasim.core.standalone_mode_choice.StandaloneModeChoiceConfigurator;
import org.eqasim.core.tools.*;
import org.eqasim.core.tools.ExportActivitiesToShapefile;
import org.eqasim.core.tools.ExportNetworkRoutesToGeopackage;
import org.eqasim.core.tools.ExportNetworkToShapefile;
import org.eqasim.core.tools.ExportPopulationToCSV;
import org.eqasim.core.tools.ExportTransitLinesToShapefile;
import org.eqasim.core.tools.ExportTransitStopsToShapefile;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

public class RunAdaptConfig {
static public void main(String[] args) throws ConfigurationException {
IDFConfigurator configurator = new IDFConfigurator();
ConfigAdapter.run(args, configurator.getConfigGroups(), RunAdaptConfig::adaptConfiguration);
ConfigAdapter.run(args, new IDFConfigurator(), RunAdaptConfig::adaptConfiguration);
}

static public void adaptConfiguration(Config config, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class RunAdaptConfig {
protected final static List<String> ACTIVITY_TYPES = Arrays.asList("business");

static public void main(String[] args) throws ConfigurationException {
EqasimConfigurator configurator = new EqasimConfigurator();
ConfigAdapter.run(args, configurator.getConfigGroups(), RunAdaptConfig::adaptConfiguration);
ConfigAdapter.run(args, new EqasimConfigurator(), RunAdaptConfig::adaptConfiguration);
}

static public void adaptConfiguration(Config config, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class RunAdaptConfig {
protected final static List<String> ACTIVITY_TYPES = Arrays.asList("business");

static public void main(String[] args) throws ConfigurationException {
EqasimConfigurator configurator = new EqasimConfigurator();
ConfigAdapter.run(args, configurator.getConfigGroups(), RunAdaptConfig::adaptConfiguration);
ConfigAdapter.run(args, new EqasimConfigurator(), RunAdaptConfig::adaptConfiguration);
}

static public void adaptConfiguration(Config config, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

public class RunAdaptConfig {
static public void main(String[] args) throws ConfigurationException {
EqasimConfigurator configurator = new EqasimConfigurator();
ConfigAdapter.run(args, configurator.getConfigGroups(), RunAdaptConfig::adaptConfiguration);
ConfigAdapter.run(args, new EqasimConfigurator(), RunAdaptConfig::adaptConfiguration);
}

static public void adaptConfiguration(Config config, String prefix) {
Expand Down
46 changes: 30 additions & 16 deletions server/src/main/java/org/eqasim/server/RunServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -16,11 +17,15 @@
import org.eqasim.server.services.router.road.RoadRouterService;
import org.eqasim.server.services.router.transit.TransitRouterService;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.algorithms.NetworkCleaner;
import org.matsim.core.network.algorithms.TransportModeNetworkFilter;
import org.matsim.core.network.io.MatsimNetworkReader;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.pt.transitSchedule.api.TransitScheduleReader;
Expand All @@ -36,7 +41,7 @@ public static void main(String[] args)
throws ConfigurationException, JsonParseException, JsonMappingException, IOException {
CommandLine cmd = new CommandLine.Builder(args) //
.requireOptions("config-path", "port") //
.allowOptions("threads", "configuration-path") //
.allowOptions("threads", "configuration-path", "use-transit") //
.build();

int threads = cmd.getOption("threads").map(Integer::parseInt)
Expand Down Expand Up @@ -65,31 +70,40 @@ public static void main(String[] args)
new MatsimNetworkReader(scenario.getNetwork())
.readURL(ConfigGroup.getInputFileURL(config.getContext(), config.network().getInputFile()));

new TransitScheduleReader(scenario)
.readURL(ConfigGroup.getInputFileURL(config.getContext(), config.transit().getTransitScheduleFile()));
boolean useTransit = cmd.getOption("use-transit").map(Boolean::parseBoolean).orElse(true);
if (useTransit) {
new TransitScheduleReader(scenario).readURL(
ConfigGroup.getInputFileURL(config.getContext(), config.transit().getTransitScheduleFile()));
}

ExecutorService executor = Executors.newFixedThreadPool(threads);

RoadRouterService roadRouterService = RoadRouterService.create(config, scenario.getNetwork(),
configuration.walk, threads);
Network roadNetwork = NetworkUtils.createNetwork();
new TransportModeNetworkFilter(scenario.getNetwork()).filter(roadNetwork, Collections.singleton("car"));
new NetworkCleaner().run(roadNetwork);

RoadRouterService roadRouterService = RoadRouterService.create(config, roadNetwork, configuration.walk,
threads);
RoadRouterEndpoint roadRouterEndpoint = new RoadRouterEndpoint(executor, roadRouterService);
app.post("/router/road", roadRouterEndpoint::post);

RoadIsochroneService roadIsochroneService = RoadIsochroneService.create(config, scenario.getNetwork(),
RoadIsochroneService roadIsochroneService = RoadIsochroneService.create(config, roadNetwork,
configuration.walk);
RoadIsochroneEndpoint roadIsochroneEndpoint = new RoadIsochroneEndpoint(executor, roadIsochroneService);
app.post("/isochrone/road", roadIsochroneEndpoint::post);

TransitRouterService transitRouterService = TransitRouterService.create(config, scenario.getNetwork(),
scenario.getTransitSchedule(), configuration.transit, configuration.walk);
TransitRouterEndpoint transitRouterEndpoint = new TransitRouterEndpoint(executor, transitRouterService);
app.post("/router/transit", transitRouterEndpoint::post);

TransitIsochroneService transitIsochroneService = TransitIsochroneService.create(config,
scenario.getTransitSchedule(), configuration.transit, configuration.walk);
TransitIsochroneEndpoint transitIsochroneEndpoint = new TransitIsochroneEndpoint(executor,
transitIsochroneService);
app.post("/isochrone/transit", transitIsochroneEndpoint::post);
if (useTransit) {
TransitRouterService transitRouterService = TransitRouterService.create(config, scenario.getNetwork(),
scenario.getTransitSchedule(), configuration.transit, configuration.walk);
TransitRouterEndpoint transitRouterEndpoint = new TransitRouterEndpoint(executor, transitRouterService);
app.post("/router/transit", transitRouterEndpoint::post);

TransitIsochroneService transitIsochroneService = TransitIsochroneService.create(config,
scenario.getTransitSchedule(), configuration.transit, configuration.walk);
TransitIsochroneEndpoint transitIsochroneEndpoint = new TransitIsochroneEndpoint(executor,
transitIsochroneService);
app.post("/isochrone/transit", transitIsochroneEndpoint::post);
}

// Run API
int port = Integer.parseInt(cmd.getOptionStrict("port"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import org.eqasim.server.services.router.road.FreespeedSettings;
import org.eqasim.server.services.router.road.RoadRouterRequest;
import org.eqasim.server.services.router.road.RoadRouterResponse;
import org.eqasim.server.services.router.road.RoadRouterService;
Expand All @@ -25,11 +26,11 @@ public RoadRouterEndpoint(ExecutorService executor, RoadRouterService service) {
this.service = service;
}

private Collection<RoadRouterResponse> process(List<RoadRouterRequest> requests)
private Collection<RoadRouterResponse> process(List<RoadRouterRequest> requests, FreespeedSettings freespeed)
throws InterruptedException, ExecutionException {
List<Callable<RoadRouterResponse>> tasks = new LinkedList<>();
for (RoadRouterRequest request : requests) {
tasks.add(() -> service.processRequest(request));
tasks.add(() -> service.processRequest(request, freespeed));
}

List<RoadRouterResponse> response = new LinkedList<>();
Expand All @@ -44,14 +45,16 @@ public void post(Context ctx) throws JsonProcessingException, InterruptedExcepti
Request request = readRequest(ctx, Request.class);

if (request.request != null) {
writeResponse(ctx, process(Collections.singletonList(request.request)).iterator().next());
writeResponse(ctx,
process(Collections.singletonList(request.request), request.freespeed).iterator().next());
} else {
writeResponse(ctx, process(request.batch));
writeResponse(ctx, process(request.batch, request.freespeed));
}
}

static public class Request {
public RoadRouterRequest request = null;
public List<RoadRouterRequest> batch = new LinkedList<>();
public FreespeedSettings freespeed = null;
}
}
Loading

0 comments on commit a08a8bf

Please sign in to comment.