diff --git a/src/main/java/beam/analysis/via/ExpectedMaxUtilityHeatMap.java b/src/main/java/beam/analysis/via/ExpectedMaxUtilityHeatMap.java index dc22c4c730f..9475306de57 100755 --- a/src/main/java/beam/analysis/via/ExpectedMaxUtilityHeatMap.java +++ b/src/main/java/beam/analysis/via/ExpectedMaxUtilityHeatMap.java @@ -1,6 +1,8 @@ package beam.analysis.via; import beam.agentsim.events.ModeChoiceEvent; +import beam.sim.BeamServices; +import beam.sim.config.BeamConfig; import beam.utils.NetworkHelper; import org.matsim.api.core.v01.events.Event; import org.matsim.api.core.v01.network.Link; @@ -15,18 +17,24 @@ public class ExpectedMaxUtilityHeatMap implements BasicEventHandler { private final String SEPERATOR = ","; - private final NetworkHelper networkHelper; private final OutputDirectoryHierarchy controlerIO; - private final int writeEventsInterval; private CSVWriter csvWriter; static final String fileBaseName = "expectedMaxUtilityHeatMap"; private BufferedWriter bufferedWriter; private boolean writeDataInThisIteration = false; + private BeamServices beamServices; - public ExpectedMaxUtilityHeatMap(EventsManager eventsManager, NetworkHelper networkHelper, OutputDirectoryHierarchy controlerIO, int writeEventsInterval) { - this.networkHelper = networkHelper; + private NetworkHelper networkHelper() { + return beamServices.networkHelper(); + } + + private int writeEventsInterval() { + return beamServices.beamConfig().beam().outputs().writeEventsInterval(); + } + + public ExpectedMaxUtilityHeatMap(EventsManager eventsManager, BeamServices beamServices, OutputDirectoryHierarchy controlerIO) { this.controlerIO = controlerIO; - this.writeEventsInterval = writeEventsInterval; + this.beamServices = beamServices; eventsManager.addHandler(this); } @@ -35,7 +43,7 @@ public void handleEvent(Event event) { if (writeDataInThisIteration && event instanceof ModeChoiceEvent) { ModeChoiceEvent modeChoiceEvent = (ModeChoiceEvent) event; int linkId = Integer.parseInt(modeChoiceEvent.location); - Link link = networkHelper.getLinkUnsafe(linkId); + Link link = networkHelper().getLinkUnsafe(linkId); if (link != null) { // TODO: fix this, so that location of mode choice event is always initialized try { @@ -61,6 +69,7 @@ public void reset(int iteration) { this.csvWriter.closeFile(); } + int writeEventsInterval = writeEventsInterval(); writeDataInThisIteration = writeEventsInterval > 0 && iteration % writeEventsInterval == 0; if (writeDataInThisIteration) { diff --git a/src/main/java/beam/matsim/CustomPlansDumpingImpl.java b/src/main/java/beam/matsim/CustomPlansDumpingImpl.java new file mode 100644 index 00000000000..eaeba8895e3 --- /dev/null +++ b/src/main/java/beam/matsim/CustomPlansDumpingImpl.java @@ -0,0 +1,76 @@ +package beam.matsim; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.apache.log4j.Logger; +import org.matsim.analysis.IterationStopWatch; +import org.matsim.api.core.v01.network.Network; +import org.matsim.api.core.v01.population.Population; +import org.matsim.api.core.v01.population.PopulationWriter; +import org.matsim.core.config.Config; +import org.matsim.core.config.groups.ControlerConfigGroup; +import org.matsim.core.controler.OutputDirectoryHierarchy; +import org.matsim.core.controler.corelisteners.PlansDumping; +import org.matsim.core.controler.events.BeforeMobsimEvent; +import org.matsim.core.controler.listener.BeforeMobsimListener; +import org.matsim.core.utils.geometry.CoordinateTransformation; +import org.matsim.core.utils.geometry.transformations.TransformationFactory; + +@Singleton +public class CustomPlansDumpingImpl implements PlansDumping, BeforeMobsimListener { + static final private Logger log = Logger.getLogger(CustomPlansDumpingImpl.class); + + @Inject + private Config config; + @Inject + private Network network; + @Inject + private Population population; + @Inject + private IterationStopWatch stopwatch; + @Inject + private OutputDirectoryHierarchy controlerIO; + @Inject + private ControlerConfigGroup controlerConfigGroup; + + private int writePlansInterval() { + return controlerConfigGroup.getWritePlansInterval(); + } + + private int writeMoreUntilIteration() { + return controlerConfigGroup.getWritePlansUntilIteration(); + } + + @Inject + CustomPlansDumpingImpl() { + } + + @Override + public void notifyBeforeMobsim(final BeforeMobsimEvent event) { + final boolean writingPlansAtAll = writePlansInterval() > 0; + final boolean regularWritePlans = writePlansInterval() > 0 && (event.getIteration() > 0 && event.getIteration() % writePlansInterval() == 0); + final boolean earlyIteration = event.getIteration() <= writeMoreUntilIteration(); + if (writingPlansAtAll && (regularWritePlans || earlyIteration)) { + stopwatch.beginOperation("dump all plans"); + log.info("dumping plans..."); + final String inputCRS = config.plans().getInputCRS(); + final String internalCRS = config.global().getCoordinateSystem(); + + if (inputCRS == null) { + new PopulationWriter(population, network).write(controlerIO.getIterationFilename(event.getIteration(), "plans.xml.gz")); + } else { + log.info("re-projecting population from " + internalCRS + " back to " + inputCRS + " for export"); + + final CoordinateTransformation transformation = + TransformationFactory.getCoordinateTransformation( + internalCRS, + inputCRS); + + new PopulationWriter(transformation, population, network).write(controlerIO.getIterationFilename(event.getIteration(), "plans.xml.gz")); + } + log.info("finished plans dump."); + stopwatch.endOperation("dump all plans"); + } + } + +} diff --git a/src/main/java/beam/matsim/MatsimConfigUpdater.java b/src/main/java/beam/matsim/MatsimConfigUpdater.java new file mode 100644 index 00000000000..12f88fafe50 --- /dev/null +++ b/src/main/java/beam/matsim/MatsimConfigUpdater.java @@ -0,0 +1,39 @@ +package beam.matsim; + +import beam.sim.BeamConfigChangesObservable; +import beam.sim.config.BeamConfig; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.matsim.core.config.groups.ControlerConfigGroup; +import org.matsim.core.config.groups.LinkStatsConfigGroup; +import scala.Tuple2; + +import java.util.Observable; +import java.util.Observer; + +@Singleton +public class MatsimConfigUpdater implements Observer { + private ControlerConfigGroup controlerConfigGroup; + private LinkStatsConfigGroup linkStatsConfigGroup; + + @Inject + MatsimConfigUpdater(BeamConfigChangesObservable beamConfigChangesObservable, LinkStatsConfigGroup linkStatsConfigGroup, ControlerConfigGroup controlerConfigGroup) { + this.controlerConfigGroup = controlerConfigGroup; + this.linkStatsConfigGroup = linkStatsConfigGroup; + + beamConfigChangesObservable.addObserver(this); + } + + @Override + public void update(Observable observable, Object o) { + if (o instanceof Tuple2) { + Tuple2 t = (Tuple2) o; + if (t._2 instanceof BeamConfig) { + BeamConfig beamConfig = (BeamConfig) t._2; + + controlerConfigGroup.setWritePlansInterval(beamConfig.beam().physsim().writePlansInterval()); + linkStatsConfigGroup.setWriteLinkStatsInterval(beamConfig.matsim().modules().linkStats().writeLinkStatsInterval()); + } + } + } +} diff --git a/src/main/resources/beam-template.conf b/src/main/resources/beam-template.conf index 71090bf4a61..a259ff18668 100755 --- a/src/main/resources/beam-template.conf +++ b/src/main/resources/beam-template.conf @@ -742,5 +742,9 @@ matsim.modules { } ] } + linkStats { + writeLinkStatsInterval = "int | 10" + averageLinkStatsOverIterations = "int | 5" + } } diff --git a/src/main/scala/beam/router/BeamSkimmer.scala b/src/main/scala/beam/router/BeamSkimmer.scala index 4dedfb2799a..07994f3d0c4 100644 --- a/src/main/scala/beam/router/BeamSkimmer.scala +++ b/src/main/scala/beam/router/BeamSkimmer.scala @@ -1,7 +1,6 @@ package beam.router import java.io.File -import java.util.concurrent.TimeUnit import beam.agentsim.agents.choice.mode.DrivingCost import beam.agentsim.agents.vehicles.{BeamVehicle, BeamVehicleType} @@ -22,13 +21,13 @@ import beam.router.Modes.BeamMode.{ } import beam.router.model.{BeamLeg, BeamPath, EmbodiedBeamTrip} import beam.sim.common.GeoUtils +import beam.sim.config.BeamConfig import beam.sim.vehiclesharing.VehicleManager -import beam.sim.{BeamScenario, BeamServices, BeamWarmStart} +import beam.sim.{BeamScenario, BeamServices} import beam.utils.{FileUtils, ProfilingUtils} import com.typesafe.scalalogging.LazyLogging import javax.inject.Inject import org.matsim.api.core.v01.{Coord, Id} -import org.matsim.core.config.groups.TravelTimeCalculatorConfigGroup import org.matsim.core.controler.events.IterationEndsEvent import org.matsim.core.utils.io.IOUtils import org.supercsv.io.CsvMapReader @@ -40,18 +39,21 @@ import scala.util.control.NonFatal //TODO to be validated against google api class BeamSkimmer @Inject()( + val beamServices: BeamServices, val beamScenario: BeamScenario, val geo: GeoUtils ) extends LazyLogging { import BeamSkimmer._ import beamScenario._ + def beamConfig: BeamConfig = beamServices.beamConfig + // The OD/Mode/Time Matrix private var previousSkims: BeamSkimmerADT = initialPreviousSkims() private var skims: BeamSkimmerADT = TrieMap() private def skimsFilePath: Option[String] = { - val filePath = beamScenario.beamConfig.beam.warmStart.skimsFilePath + val filePath = beamConfig.beam.warmStart.skimsFilePath if (new File(filePath).isFile) { Some(filePath) } else { @@ -385,7 +387,7 @@ class BeamSkimmer @Inject()( BeamSkimmer.excerptSkimsFileBaseName + ".csv.gz" ) val dummyId = Id.create( - beamScenario.beamConfig.beam.agentsim.agents.rideHail.initialization.procedural.vehicleTypeId, + beamConfig.beam.agentsim.agents.rideHail.initialization.procedural.vehicleTypeId, classOf[BeamVehicleType] ) val writer = IOUtils.getBufferedWriter(filePath) @@ -444,7 +446,7 @@ class BeamSkimmer @Inject()( val uniqueTimeBins = 0 to 23 val dummyId = Id.create( - beamScenario.beamConfig.beam.agentsim.agents.rideHail.initialization.procedural.vehicleTypeId, + beamConfig.beam.agentsim.agents.rideHail.initialization.procedural.vehicleTypeId, classOf[BeamVehicleType] ) @@ -513,7 +515,7 @@ class BeamSkimmer @Inject()( initialPreviousSkimsPlus() private var skimsPlus: TrieMap[BeamSkimmerPlusKey, Double] = TrieMap() private def skimsPlusFilePath: Option[String] = { - val filePath = beamScenario.beamConfig.beam.warmStart.skimsPlusFilePath + val filePath = beamConfig.beam.warmStart.skimsPlusFilePath if (new File(filePath).isFile) { Some(filePath) } else { @@ -561,7 +563,13 @@ class BeamSkimmer @Inject()( skimsPlus.put(key, skimsPlus.getOrElse(key, 0.0) + count.toDouble) } - def addValue(curBin: Int, tazId: Id[TAZ], vehicleManager: Id[VehicleManager], label: Label, value: Double) = { + def addValue( + curBin: Int, + tazId: Id[TAZ], + vehicleManager: Id[VehicleManager], + label: Label, + value: Double + ): Option[Double] = { if (curBin > trackSkimsPlusTS) trackSkimsPlusTS = curBin val key = (trackSkimsPlusTS, tazId, vehicleManager, label) skimsPlus.put(key, skimsPlus.getOrElse(key, 0.0) + value) diff --git a/src/main/scala/beam/router/TravelTimeObserved.scala b/src/main/scala/beam/router/TravelTimeObserved.scala index 7b030c5d8eb..bf876a5f86d 100644 --- a/src/main/scala/beam/router/TravelTimeObserved.scala +++ b/src/main/scala/beam/router/TravelTimeObserved.scala @@ -8,7 +8,7 @@ import beam.analysis.plots.{GraphUtils, GraphsStatsAgentSimEventsListener} import beam.router.Modes.BeamMode import beam.router.Modes.BeamMode.{CAR, WALK} import beam.router.model.{EmbodiedBeamLeg, EmbodiedBeamTrip} -import beam.sim.BeamScenario +import beam.sim.{BeamScenario, BeamServices} import beam.sim.common.GeoUtils import beam.utils.{FileUtils, GeoJsonReader, ProfilingUtils} import com.google.inject.Inject @@ -31,6 +31,7 @@ import org.supercsv.prefs.CsvPreference import scala.collection.mutable class TravelTimeObserved @Inject()( + val beamServices: BeamServices, val beamScenario: BeamScenario, val geo: GeoUtils ) extends LazyLogging { @@ -38,7 +39,7 @@ class TravelTimeObserved @Inject()( import beamScenario._ @volatile - private var skimmer: BeamSkimmer = new BeamSkimmer(beamScenario, geo) + private var skimmer: BeamSkimmer = new BeamSkimmer(beamServices, beamScenario, geo) private val observedTravelTimesOpt: Option[Map[PathCache, Float]] = { val zoneBoundariesFilePath = beamConfig.beam.calibration.roadNetwork.travelTimes.zoneBoundariesFilePath @@ -99,7 +100,7 @@ class TravelTimeObserved @Inject()( def notifyIterationEnds(event: IterationEndsEvent): Unit = { writeTravelTimeObservedVsSimulated(event) - skimmer = new BeamSkimmer(beamScenario, geo) + skimmer = new BeamSkimmer(beamServices, beamScenario, geo) } def writeTravelTimeObservedVsSimulated(event: IterationEndsEvent): Unit = { diff --git a/src/main/scala/beam/sim/BeamHelper.scala b/src/main/scala/beam/sim/BeamHelper.scala index ddc5e109ae1..f44643c5c67 100755 --- a/src/main/scala/beam/sim/BeamHelper.scala +++ b/src/main/scala/beam/sim/BeamHelper.scala @@ -12,6 +12,7 @@ import beam.agentsim.events.handling.BeamEventsHandling import beam.agentsim.infrastructure.taz.TAZTreeMap import beam.analysis.ActivityLocationPlotter import beam.analysis.plots.{GraphSurgePricing, RideHailRevenueAnalysis} +import beam.matsim.{CustomPlansDumpingImpl, MatsimConfigUpdater} import beam.replanning._ import beam.replanning.utilitybased.UtilityBasedModeChoice import beam.router._ @@ -44,7 +45,7 @@ import org.matsim.core.api.experimental.events.EventsManager import org.matsim.core.config.groups.TravelTimeCalculatorConfigGroup import org.matsim.core.config.{Config => MatsimConfig} import org.matsim.core.controler._ -import org.matsim.core.controler.corelisteners.{ControlerDefaultCoreListenersModule, EventsHandling} +import org.matsim.core.controler.corelisteners.{ControlerDefaultCoreListenersModule, EventsHandling, PlansDumping} import org.matsim.core.scenario.{MutableScenario, ScenarioBuilder, ScenarioByInstanceModule, ScenarioUtils} import org.matsim.core.trafficmonitoring.TravelTimeCalculator import org.matsim.households.Household @@ -153,7 +154,14 @@ trait BeamHelper extends LazyLogging { bind(classOf[BeamConfigHolder]) val beamConfigChangesObservable = new BeamConfigChangesObservable(beamConfig) + bind(classOf[MatsimConfigUpdater]).asEagerSingleton() + + bind(classOf[PlansDumping]).to(classOf[CustomPlansDumpingImpl]) + bind(classOf[BeamConfigChangesObservable]).toInstance(beamConfigChangesObservable) + + bind(classOf[TerminationCriterion]).to(classOf[CustomTerminateAtFixedIterationNumber]) + bind(classOf[PrepareForSim]).to(classOf[BeamPrepareForSim]) bind(classOf[RideHailSurgePricingManager]).asEagerSingleton() diff --git a/src/main/scala/beam/sim/BeamSim.scala b/src/main/scala/beam/sim/BeamSim.scala index 2460bb3cdf7..71d0b895026 100755 --- a/src/main/scala/beam/sim/BeamSim.scala +++ b/src/main/scala/beam/sim/BeamSim.scala @@ -145,9 +145,8 @@ class BeamSim @Inject()( modalityStyleStats = new ModalityStyleStats() expectedDisutilityHeatMapDataCollector = new ExpectedMaxUtilityHeatMap( eventsManager, - beamServices.networkHelper, - event.getServices.getControlerIO, - beamServices.beamConfig.beam.outputs.writeEventsInterval + beamServices, + event.getServices.getControlerIO ) tncIterationsStatsCollector = new RideHailIterationsStatsCollector( diff --git a/src/main/scala/beam/sim/CustomTerminateAtFixedIterationNumber.scala b/src/main/scala/beam/sim/CustomTerminateAtFixedIterationNumber.scala new file mode 100644 index 00000000000..70e47b74589 --- /dev/null +++ b/src/main/scala/beam/sim/CustomTerminateAtFixedIterationNumber.scala @@ -0,0 +1,24 @@ +package beam.sim + +import beam.sim.config.BeamConfigHolder +import javax.inject.Inject +import org.matsim.core.config.groups.ControlerConfigGroup +import org.matsim.core.controler.TerminationCriterion + +class CustomTerminateAtFixedIterationNumber extends TerminationCriterion { + private var lastIteration = 0 + private var beamConfigHolder: BeamConfigHolder = _ + + @Inject + def this(controlerConfigGroup: ControlerConfigGroup, beamConfigHolder: BeamConfigHolder) { + this() + this.beamConfigHolder = beamConfigHolder + this.lastIteration = controlerConfigGroup.getLastIteration + } + + override def continueIterations(iteration: Int): Boolean = { + lastIteration = beamConfigHolder.beamConfig.beam.agentsim.lastIteration + iteration <= lastIteration + } + +} diff --git a/src/main/scala/beam/sim/config/BeamConfig.scala b/src/main/scala/beam/sim/config/BeamConfig.scala index e04d7da19a4..93e2a55c075 100755 --- a/src/main/scala/beam/sim/config/BeamConfig.scala +++ b/src/main/scala/beam/sim/config/BeamConfig.scala @@ -2448,6 +2448,7 @@ object BeamConfig { counts: BeamConfig.Matsim.Modules.Counts, global: BeamConfig.Matsim.Modules.Global, households: BeamConfig.Matsim.Modules.Households, + linkStats: BeamConfig.Matsim.Modules.LinkStats, network: BeamConfig.Matsim.Modules.Network, parallelEventHandling: BeamConfig.Matsim.Modules.ParallelEventHandling, planCalcScore: BeamConfig.Matsim.Modules.PlanCalcScore, @@ -2551,6 +2552,23 @@ object BeamConfig { } } + case class LinkStats( + averageLinkStatsOverIterations: scala.Int, + writeLinkStatsInterval: scala.Int + ) + + object LinkStats { + + def apply(c: com.typesafe.config.Config): BeamConfig.Matsim.Modules.LinkStats = { + BeamConfig.Matsim.Modules.LinkStats( + averageLinkStatsOverIterations = + if (c.hasPathOrNull("averageLinkStatsOverIterations")) c.getInt("averageLinkStatsOverIterations") else 5, + writeLinkStatsInterval = + if (c.hasPathOrNull("writeLinkStatsInterval")) c.getInt("writeLinkStatsInterval") else 10 + ) + } + } + case class Network( inputNetworkFile: java.lang.String ) @@ -2831,6 +2849,10 @@ object BeamConfig { if (c.hasPathOrNull("households")) c.getConfig("households") else com.typesafe.config.ConfigFactory.parseString("households{}") ), + linkStats = BeamConfig.Matsim.Modules.LinkStats( + if (c.hasPathOrNull("linkStats")) c.getConfig("linkStats") + else com.typesafe.config.ConfigFactory.parseString("linkStats{}") + ), network = BeamConfig.Matsim.Modules.Network( if (c.hasPathOrNull("network")) c.getConfig("network") else com.typesafe.config.ConfigFactory.parseString("network{}") diff --git a/src/main/scala/beam/sim/config/BeamConfigHolder.scala b/src/main/scala/beam/sim/config/BeamConfigHolder.scala index 988f16e9e2c..1c4a561ebef 100644 --- a/src/main/scala/beam/sim/config/BeamConfigHolder.scala +++ b/src/main/scala/beam/sim/config/BeamConfigHolder.scala @@ -4,8 +4,10 @@ import java.util.{Observable, Observer} import beam.sim.BeamConfigChangesObservable import javax.inject.Inject -class BeamConfigHolder @Inject()(beamConfigChangesObservable: BeamConfigChangesObservable, config: BeamConfig) - extends Observer { +class BeamConfigHolder @Inject()( + beamConfigChangesObservable: BeamConfigChangesObservable, + config: BeamConfig +) extends Observer { var beamConfig: BeamConfig = config beamConfigChangesObservable.addObserver(this) diff --git a/src/test/scala/beam/agentsim/agents/PersonAgentSpec.scala b/src/test/scala/beam/agentsim/agents/PersonAgentSpec.scala index 3147a5d5219..d0d147589c1 100644 --- a/src/test/scala/beam/agentsim/agents/PersonAgentSpec.scala +++ b/src/test/scala/beam/agentsim/agents/PersonAgentSpec.scala @@ -115,9 +115,9 @@ class PersonAgentSpec parkingManager, services.tollCalculator, self, - beamSkimmer = new BeamSkimmer(beamScenario, services.geo), + beamSkimmer = new BeamSkimmer(services, beamScenario, services.geo), routeHistory = new RouteHistory(beamConfig), - travelTimeObserved = new TravelTimeObserved(beamScenario, services.geo), + travelTimeObserved = new TravelTimeObserved(services, beamScenario, services.geo), boundingBox = boundingBox ) ) @@ -180,8 +180,8 @@ class PersonAgentSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -379,8 +379,8 @@ class PersonAgentSpec homeCoord = new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -657,8 +657,8 @@ class PersonAgentSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) diff --git a/src/test/scala/beam/agentsim/agents/PersonAndTransitDriverSpec.scala b/src/test/scala/beam/agentsim/agents/PersonAndTransitDriverSpec.scala index 4a4ffd54f55..3ded199a8fa 100644 --- a/src/test/scala/beam/agentsim/agents/PersonAndTransitDriverSpec.scala +++ b/src/test/scala/beam/agentsim/agents/PersonAndTransitDriverSpec.scala @@ -345,8 +345,8 @@ class PersonAndTransitDriverSpec homeCoord = new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) diff --git a/src/test/scala/beam/agentsim/agents/PersonWithPersonalVehiclePlanSpec.scala b/src/test/scala/beam/agentsim/agents/PersonWithPersonalVehiclePlanSpec.scala index 63bbe24267c..f9014223241 100644 --- a/src/test/scala/beam/agentsim/agents/PersonWithPersonalVehiclePlanSpec.scala +++ b/src/test/scala/beam/agentsim/agents/PersonWithPersonalVehiclePlanSpec.scala @@ -123,8 +123,8 @@ class PersonWithPersonalVehiclePlanSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -344,8 +344,8 @@ class PersonWithPersonalVehiclePlanSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -489,8 +489,8 @@ class PersonWithPersonalVehiclePlanSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -584,8 +584,8 @@ class PersonWithPersonalVehiclePlanSpec new Coord(0.0, 0.0), Vector(), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) diff --git a/src/test/scala/beam/agentsim/agents/PersonWithVehicleSharingSpec.scala b/src/test/scala/beam/agentsim/agents/PersonWithVehicleSharingSpec.scala index bbe23722d9a..4a7333fb4ca 100644 --- a/src/test/scala/beam/agentsim/agents/PersonWithVehicleSharingSpec.scala +++ b/src/test/scala/beam/agentsim/agents/PersonWithVehicleSharingSpec.scala @@ -134,8 +134,8 @@ class PersonWithVehicleSharingSpec new Coord(0.0, 0.0), sharedVehicleFleets = Vector(mockSharedVehicleFleet.ref), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -278,8 +278,8 @@ class PersonWithVehicleSharingSpec new Coord(0.0, 0.0), sharedVehicleFleets = Vector(mockSharedVehicleFleet.ref), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) @@ -517,8 +517,8 @@ class PersonWithVehicleSharingSpec new Coord(0.0, 0.0), Vector(mockSharedVehicleFleet.ref), new RouteHistory(beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), boundingBox ) ) diff --git a/src/test/scala/beam/agentsim/agents/household/FastHouseholdCAVSchedulingSpec.scala b/src/test/scala/beam/agentsim/agents/household/FastHouseholdCAVSchedulingSpec.scala index 9a55d9b666d..564b2b3ce6b 100644 --- a/src/test/scala/beam/agentsim/agents/household/FastHouseholdCAVSchedulingSpec.scala +++ b/src/test/scala/beam/agentsim/agents/household/FastHouseholdCAVSchedulingSpec.scala @@ -6,16 +6,17 @@ import akka.testkit.{ImplicitSender, TestKit} import akka.util.Timeout import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain import beam.agentsim.agents.vehicles.{BeamVehicle, BeamVehicleType} -import beam.agentsim.agents.{Dropoff, Pickup} import beam.router.BeamSkimmer -import beam.sim.BeamHelper import beam.sim.common.GeoUtilsImpl -import beam.sim.config.BeamConfig +import beam.sim.config.{BeamConfig, MatSimBeamConfigBuilder} +import beam.sim.{BeamHelper, BeamServicesImpl} +import beam.utils.TestConfigUtils import beam.utils.TestConfigUtils.testConfig import com.typesafe.config.ConfigFactory import org.matsim.api.core.v01.population.{Activity, Person, Plan, Population} import org.matsim.api.core.v01.{Coord, Id} import org.matsim.core.config.ConfigUtils +import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting import org.matsim.core.population.PopulationUtils import org.matsim.core.population.io.PopulationReader import org.matsim.core.scenario.ScenarioUtils @@ -55,7 +56,15 @@ class FastHouseholdCAVSchedulingSpec private lazy val beamCfg = BeamConfig(system.settings.config) private lazy val beamScenario = loadScenario(beamCfg) - private lazy val skimmer: BeamSkimmer = new BeamSkimmer(beamScenario, new GeoUtilsImpl(beamCfg)) + private val matsimConfig = new MatSimBeamConfigBuilder(system.settings.config).buildMatSimConf() + matsimConfig.controler.setOutputDirectory(TestConfigUtils.testOutputDir) + matsimConfig.controler.setOverwriteFileSetting(OverwriteFileSetting.overwriteExistingFiles) + + private val scenario = buildScenarioFromMatsimConfig(matsimConfig, beamScenario) + private val injector = buildInjector(system.settings.config, beamCfg, scenario, beamScenario) + val services = new BeamServicesImpl(injector) + + private lazy val skimmer: BeamSkimmer = new BeamSkimmer(services, beamScenario, new GeoUtilsImpl(beamCfg)) describe("A Household CAV Scheduler") { it("generates two schedules") { diff --git a/src/test/scala/beam/integration/AgentsimWithMaximallyBadRouterSpec.scala b/src/test/scala/beam/integration/AgentsimWithMaximallyBadRouterSpec.scala index 2a84387493e..1b33f907ef9 100755 --- a/src/test/scala/beam/integration/AgentsimWithMaximallyBadRouterSpec.scala +++ b/src/test/scala/beam/integration/AgentsimWithMaximallyBadRouterSpec.scala @@ -56,8 +56,8 @@ class AgentsimWithMaximallyBadRouterSpec new RideHailSurgePricingManager(services), new RideHailIterationHistory(), new RouteHistory(services.beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), new GeoUtilsImpl(services.beamConfig), services.networkHelper ) diff --git a/src/test/scala/beam/integration/IntegrationSpecCommon.scala b/src/test/scala/beam/integration/IntegrationSpecCommon.scala index 8f5bf7dd50a..3b166e5d59a 100755 --- a/src/test/scala/beam/integration/IntegrationSpecCommon.scala +++ b/src/test/scala/beam/integration/IntegrationSpecCommon.scala @@ -4,8 +4,6 @@ import beam.utils.TestConfigUtils.testConfig import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory} trait IntegrationSpecCommon { - private val LAST_ITER_CONF_PATH = "matsim.modules.controler.lastIteration" - protected var totalIterations: Int = 1 val configFileName = "test/input/beamville/beam.conf" @@ -15,7 +13,8 @@ trait IntegrationSpecCommon { lazy val baseConfig: Config = testConfig(configFileName) .resolve() .withValue("beam.outputs.events.fileOutputFormats", ConfigValueFactory.fromAnyRef("xml")) - .withValue(LAST_ITER_CONF_PATH, ConfigValueFactory.fromAnyRef(totalIterations - 1)) + .withValue("matsim.modules.controler.lastIteration", ConfigValueFactory.fromAnyRef(totalIterations - 1)) + .withValue("beam.agentsim.lastIteration", ConfigValueFactory.fromAnyRef(totalIterations - 1)) .withFallback(configLocation) .resolve diff --git a/src/test/scala/beam/integration/ParkingSpec.scala b/src/test/scala/beam/integration/ParkingSpec.scala index ade74b5c262..b30a16893dc 100755 --- a/src/test/scala/beam/integration/ParkingSpec.scala +++ b/src/test/scala/beam/integration/ParkingSpec.scala @@ -78,6 +78,10 @@ class ParkingSpec extends WordSpecLike with BeforeAndAfterAll with Matchers with "beam.agentsim.events.ParkEvent:VERBOSE, beam.agentsim.events.LeavingParkingEvent:VERBOSE, org.matsim.api.core.v01.events.ActivityEndEvent:REGULAR, org.matsim.api.core.v01.events.ActivityStartEvent:REGULAR, org.matsim.api.core.v01.events.PersonEntersVehicleEvent:REGULAR, org.matsim.api.core.v01.events.PersonLeavesVehicleEvent:REGULAR, beam.agentsim.events.ModeChoiceEvent:VERBOSE, beam.agentsim.events.PathTraversalEvent:VERBOSE" ) ) + .withValue( + "beam.agentsim.lastIteration", + ConfigValueFactory.fromAnyRef(iterations) + ) .withValue( "matsim.modules.controler.lastIteration", ConfigValueFactory.fromAnyRef(iterations) diff --git a/src/test/scala/beam/integration/SingleModeSpec.scala b/src/test/scala/beam/integration/SingleModeSpec.scala index edb9fb46400..a3ea3763155 100755 --- a/src/test/scala/beam/integration/SingleModeSpec.scala +++ b/src/test/scala/beam/integration/SingleModeSpec.scala @@ -76,8 +76,8 @@ class SingleModeSpec new RideHailSurgePricingManager(services), new RideHailIterationHistory(), new RouteHistory(services.beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), new GeoUtilsImpl(services.beamConfig), services.networkHelper ) @@ -129,8 +129,8 @@ class SingleModeSpec new RideHailSurgePricingManager(services), new RideHailIterationHistory(), new RouteHistory(services.beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), new GeoUtilsImpl(services.beamConfig), services.networkHelper ) @@ -201,8 +201,8 @@ class SingleModeSpec new RideHailSurgePricingManager(services), new RideHailIterationHistory(), new RouteHistory(services.beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), new GeoUtilsImpl(services.beamConfig), services.networkHelper ) @@ -279,8 +279,8 @@ class SingleModeSpec new RideHailSurgePricingManager(services), new RideHailIterationHistory(), new RouteHistory(services.beamConfig), - new BeamSkimmer(beamScenario, services.geo), - new TravelTimeObserved(beamScenario, services.geo), + new BeamSkimmer(services, beamScenario, services.geo), + new TravelTimeObserved(services, beamScenario, services.geo), new GeoUtilsImpl(services.beamConfig), services.networkHelper ) diff --git a/src/test/scala/beam/integration/ridehail/RideHailTestHelper.scala b/src/test/scala/beam/integration/ridehail/RideHailTestHelper.scala index cf9fadaa51d..fba80814b6b 100644 --- a/src/test/scala/beam/integration/ridehail/RideHailTestHelper.scala +++ b/src/test/scala/beam/integration/ridehail/RideHailTestHelper.scala @@ -29,6 +29,8 @@ object RideHailTestHelper { ConfigValueFactory.fromAnyRef(0) ) .withValue("beam.debug.stuckAgentDetection.enabled", ConfigValueFactory.fromAnyRef(true)) + .withValue("matsim.modules.controler.lastIteration", ConfigValueFactory.fromAnyRef(0)) + .withValue("beam.agentsim.lastIteration", ConfigValueFactory.fromAnyRef(0)) .resolve() config @@ -38,7 +40,6 @@ object RideHailTestHelper { val configBuilder = new MatSimBeamConfigBuilder(config) val matsimConfig = configBuilder.buildMatSimConf() - matsimConfig.controler().setLastIteration(0) matsimConfig.planCalcScore().setMemorizingExperiencedPlans(true) matsimConfig } diff --git a/test/input/common/matsim.conf b/test/input/common/matsim.conf index 2d8d1e08b1c..448b9572f50 100644 --- a/test/input/common/matsim.conf +++ b/test/input/common/matsim.conf @@ -249,4 +249,8 @@ matsim.modules { } ] } + linkStats { + writeLinkStatsInterval = 10 + averageLinkStatsOverIterations = 5 + } }