-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from arup-group/michael-refactoring
Michael refactoring
- Loading branch information
Showing
32 changed files
with
2,079 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ hs_err_pid* | |
replay_pid* | ||
|
||
target/ | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/com/arup/cml/kpi/DataModel.java → .../java/com/arup/cml/abm/kpi/DataModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.arup.cml.kpi; | ||
package com.arup.cml.abm.kpi; | ||
|
||
import tech.tablesaw.api.Table; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.arup.cml.abm.kpi; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface KpiCalculator { | ||
void linkEntered(String vehicleId, String linkId, double timestamp); | ||
void linkExited(String vehicleId, String linkId, double timestamp); | ||
void vehicleEntered(String vehicleId, String personId); | ||
void vehicleExited(String vehicleId, String personId); | ||
void recordVehicleMode(String vehicleId, String mode); | ||
void writeCongestionKpi(Path directory); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/arup/cml/abm/kpi/matsim/MatsimUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.arup.cml.abm.kpi.matsim; | ||
|
||
import org.matsim.api.core.v01.network.Network; | ||
import org.matsim.core.config.Config; | ||
import org.matsim.core.config.ConfigGroup; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.config.ReflectiveConfigGroup; | ||
import org.matsim.core.config.groups.*; | ||
import org.matsim.core.scenario.ScenarioUtils; | ||
import org.matsim.pt.config.TransitConfigGroup; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
|
||
public class MatsimUtils { | ||
private Path matsimOutputDir; | ||
private Network matsimNetwork; | ||
private Config matsimConfig; | ||
|
||
private final Set<String> necessaryConfigGroups = new HashSet<>(Arrays.asList( | ||
GlobalConfigGroup.GROUP_NAME, | ||
PlansConfigGroup.GROUP_NAME, | ||
FacilitiesConfigGroup.GROUP_NAME, | ||
HouseholdsConfigGroup.GROUP_NAME, | ||
TransitConfigGroup.GROUP_NAME, | ||
VehiclesConfigGroup.GROUP_NAME, | ||
NetworkConfigGroup.GROUP_NAME | ||
)); | ||
|
||
public MatsimUtils(Path matsimOutputDir, Path matsimConfigFile) { | ||
this.matsimOutputDir = matsimOutputDir; | ||
this.matsimConfig = getConfig(matsimConfigFile.toString()); | ||
this.matsimNetwork = ScenarioUtils.loadScenario(matsimConfig).getNetwork(); | ||
} | ||
|
||
private Config getConfig(String matsimInputConfig) { | ||
Config config = ConfigUtils.createConfig(); | ||
TreeMap<String, ConfigGroup> configuredModules = config.getModules(); | ||
for (ConfigGroup module : configuredModules.values().stream().toList()) { | ||
if (necessaryConfigGroups.contains(module.getName())) { | ||
System.out.println("Config group " + module + " is read as is"); | ||
} else { | ||
ReflectiveConfigGroup relaxedModule = | ||
new ReflectiveConfigGroup(module.getName(), true) {}; | ||
config.removeModule(module.getName()); | ||
config.addModule(relaxedModule); | ||
} | ||
} | ||
ConfigUtils.loadConfig(config, String.format(matsimInputConfig)); | ||
setOutputFilePaths(config); | ||
return config; | ||
} | ||
|
||
private void setOutputFilePaths(Config config) { | ||
TreeMap<String, ConfigGroup> modules = config.getModules(); | ||
modules.get("network") | ||
.addParam("inputNetworkFile", | ||
String.format("%s/output_network.xml.gz", this.matsimOutputDir)); | ||
modules.get("transit") | ||
.addParam("transitScheduleFile", | ||
String.format("%s/output_transitSchedule.xml.gz", this.matsimOutputDir)); | ||
modules.get("transit") | ||
.addParam("vehiclesFile", | ||
String.format("%s/output_transitVehicles.xml.gz", this.matsimOutputDir)); | ||
modules.get("plans") | ||
.addParam("inputPlansFile", | ||
String.format("%s/output_plans.xml.gz", this.matsimOutputDir)); | ||
modules.get("households") | ||
.addParam("inputFile", | ||
String.format("%s/output_households.xml.gz", this.matsimOutputDir)); | ||
modules.get("facilities") | ||
.addParam("inputFacilitiesFile", | ||
String.format("%s/output_facilities.xml.gz", this.matsimOutputDir)); | ||
modules.get("vehicles") | ||
.addParam("vehiclesFile", | ||
String.format("%s/output_vehicles.xml.gz", this.matsimOutputDir)); | ||
} | ||
|
||
public Network getMatsimNetwork() { | ||
return matsimNetwork; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/com/arup/cml/abm/kpi/matsim/handlers/MatsimLinkLogHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.arup.cml.abm.kpi.matsim.handlers; | ||
|
||
import com.arup.cml.abm.kpi.KpiCalculator; | ||
import org.matsim.api.core.v01.events.*; | ||
import org.matsim.api.core.v01.events.handler.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class MatsimLinkLogHandler implements | ||
VehicleEntersTrafficEventHandler, | ||
VehicleLeavesTrafficEventHandler, | ||
PersonEntersVehicleEventHandler, | ||
PersonLeavesVehicleEventHandler, | ||
LinkEnterEventHandler, | ||
LinkLeaveEventHandler { | ||
|
||
private KpiCalculator kpiCalculator; | ||
private final Map<String, AtomicInteger> eventCounts = new HashMap<>(); | ||
|
||
public MatsimLinkLogHandler(KpiCalculator kpiCalculator) { | ||
this.kpiCalculator = kpiCalculator; | ||
} | ||
|
||
@Override | ||
public void handleEvent(LinkEnterEvent event) { | ||
incrementEventCount(event); | ||
kpiCalculator.linkEntered( | ||
event.getVehicleId().toString(), | ||
event.getLinkId().toString(), | ||
event.getTime() | ||
); | ||
} | ||
|
||
@Override | ||
public void handleEvent(LinkLeaveEvent event) { | ||
incrementEventCount(event); | ||
kpiCalculator.linkExited( | ||
event.getVehicleId().toString(), | ||
event.getLinkId().toString(), | ||
event.getTime() | ||
); | ||
} | ||
|
||
@Override | ||
public void handleEvent(PersonEntersVehicleEvent event) { | ||
incrementEventCount(event); | ||
kpiCalculator.vehicleEntered( | ||
event.getVehicleId().toString(), | ||
event.getPersonId().toString() | ||
); | ||
} | ||
|
||
@Override | ||
public void handleEvent(PersonLeavesVehicleEvent event) { | ||
incrementEventCount(event); | ||
kpiCalculator.vehicleExited( | ||
event.getVehicleId().toString(), | ||
event.getPersonId().toString() | ||
); | ||
} | ||
|
||
@Override | ||
public void handleEvent(VehicleEntersTrafficEvent event) { | ||
incrementEventCount(event); | ||
// this is the earliest event we will see for this vehicle, and includes the | ||
// mode, which is missing from subsequent link events, so we must grab it now | ||
kpiCalculator.recordVehicleMode(event.getVehicleId().toString(), event.getNetworkMode()); | ||
kpiCalculator.linkEntered( | ||
event.getVehicleId().toString(), | ||
event.getLinkId().toString(), | ||
event.getTime() | ||
); | ||
} | ||
|
||
@Override | ||
public void handleEvent(VehicleLeavesTrafficEvent event) { | ||
incrementEventCount(event); | ||
kpiCalculator.linkExited( | ||
event.getVehicleId().toString(), | ||
event.getLinkId().toString(), | ||
event.getTime() | ||
); | ||
} | ||
|
||
public Map<String, AtomicInteger> getEventCounts() { | ||
return this.eventCounts; | ||
} | ||
|
||
private void incrementEventCount(Event e) { | ||
eventCounts.putIfAbsent(e.getEventType(), new AtomicInteger(0)); | ||
eventCounts.get(e.getEventType()).incrementAndGet(); | ||
} | ||
} |
Oops, something went wrong.