Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert frequencies into trips for transit routes that are specified … #888

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/main/scala/beam/router/r5/NetworkCoordinator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import java.nio.file.Files.exists
import java.nio.file.Paths

import beam.sim.config.BeamConfig
import com.conveyal.r5.transit.TransportNetwork
import com.conveyal.r5.transit.{TransportNetwork, TripSchedule}
import com.typesafe.scalalogging.LazyLogging
import org.matsim.api.core.v01.network.{Network, NetworkWriter}
import org.matsim.core.network.NetworkUtils
import org.matsim.core.network.io.MatsimNetworkReader

import scala.collection.JavaConverters._

class NetworkCoordinator(beamConfig: BeamConfig) extends LazyLogging {


var transportNetwork: TransportNetwork = _
var network: Network = _

Expand Down Expand Up @@ -49,4 +52,36 @@ class NetworkCoordinator(beamConfig: BeamConfig) extends LazyLogging {
}
}

def convertFrequenciesToTrips() = {
transportNetwork.transitLayer.tripPatterns.asScala.foreach { tp =>
if (tp.hasFrequencies) {
val toAdd: Vector[TripSchedule] = tp.tripSchedules.asScala.toVector.flatMap { ts =>
val tripStartTimes = ts.startTimes(0).until(ts.endTimes(0)).by(ts.headwaySeconds(0)).toVector
tripStartTimes.zipWithIndex.map { case (startTime, ind) =>
val tsNew = ts.clone().asInstanceOf[TripSchedule]
val newTripId = s"${tsNew.tripId}-$ind"
val newArrivals = new Array[Int](ts.arrivals.size)
val newDepartures = new Array[Int](ts.arrivals.size)
for (i <- 0.until(tsNew.arrivals.length)) {
newArrivals(i) = tsNew.arrivals(i) + startTime
newDepartures(i) = tsNew.departures(i) + startTime
}
tsNew.arrivals = newArrivals
tsNew.departures = newDepartures
tsNew.tripId = newTripId
tsNew.frequencyEntryIds = null
tsNew.headwaySeconds = null
tsNew.startTimes = null
tsNew.endTimes = null
tsNew
}
}
tp.tripSchedules.clear()
toAdd.foreach(tp.tripSchedules.add(_))
tp.hasFrequencies = false
}
}
transportNetwork.transitLayer.hasFrequencies = false
}

}
7 changes: 4 additions & 3 deletions src/main/scala/beam/sim/BeamHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ trait BeamHelper extends LazyLogging {
}

def runBeamWithConfig(config: TypesafeConfig): (Config, String) = {
val (matsimConfig, outputDir, beamServices) = setupBeamWithConfig(config)
val (matsimConfig, outputDir, beamServices, networkCoordinator) = setupBeamWithConfig(config)
networkCoordinator.convertFrequenciesToTrips()

run(beamServices)
(matsimConfig, outputDir)
}

def setupBeamWithConfig(config: TypesafeConfig): (Config, String, BeamServices) = {
def setupBeamWithConfig(config: TypesafeConfig): (Config, String, BeamServices, NetworkCoordinator) = {
val beamConfig = BeamConfig(config)
level = beamConfig.beam.metrics.level
runName = beamConfig.beam.agentsim.simulationName
Expand Down Expand Up @@ -395,7 +396,7 @@ trait BeamHelper extends LazyLogging {

samplePopulation(scenario, beamConfig, matsimConfig, beamServices)

(matsimConfig, outputDirectory, beamServices)
(matsimConfig, outputDirectory, beamServices, networkCoordinator)
}

def run(beamServices: BeamServices){
Expand Down
4 changes: 2 additions & 2 deletions test/input/beamville/physsim-network.xml
Git LFS file not shown
4 changes: 2 additions & 2 deletions test/input/beamville/r5/network.dat
Git LFS file not shown
4 changes: 2 additions & 2 deletions test/input/beamville/r5/tolls.dat
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/agency.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/calendar.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/frequencies.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/routes.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/stop_times.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/stops.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/input/beamville/r5/train-freq/trips.txt
Git LFS file not shown
4 changes: 2 additions & 2 deletions test/input/beamville/r5/train.zip
Git LFS file not shown