-
Notifications
You must be signed in to change notification settings - Fork 57
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
Reads from csv file, skips vehicleAdjustment(No-Op), generates beam v… #3603
Open
alvinlee001
wants to merge
5
commits into
develop
Choose a base branch
from
alvin/#3179-Ability-to-load-vehicles-from-file-urbansim
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68b36ec
Reads from csv file, skips vehicleAdjustment(No-Op), generates beam v…
alvinlee001 e93a268
move logic into vehiclesAdjustment, keep other files clean
alvinlee001 9d3f605
code cleanup
alvinlee001 228c001
Integration Test draft for FromFileVehicleAdjustment
alvinlee001 0e919a6
clean up test
alvinlee001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
47 changes: 47 additions & 0 deletions
47
src/main/scala/beam/sim/vehicles/FromFileVehiclesAdjustment.scala
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,47 @@ | ||
package beam.sim.vehicles | ||
|
||
import beam.agentsim.agents.Population | ||
import beam.agentsim.agents.vehicles.{BeamVehicleType, VehicleCategory} | ||
import beam.sim.{BeamScenario, BeamServices} | ||
import beam.utils.csv.readers | ||
import beam.utils.scenario.VehicleInfo | ||
import org.apache.commons.math3.distribution.UniformRealDistribution | ||
import org.matsim.api.core.v01.{Coord, Id} | ||
import org.matsim.households.Household | ||
|
||
case class FromFileVehiclesAdjustment(beamScenario: BeamScenario) extends VehiclesAdjustment { | ||
|
||
val vehicles: Iterable[VehicleInfo] = readVehiclesFromFile(); | ||
val vehicleTypesMap: Map[Id[BeamVehicleType], BeamVehicleType] = beamScenario.vehicleTypes.map(i => i._1 -> i._2).toMap; | ||
|
||
override def sampleVehicleTypesForHousehold( | ||
numVehicles: Int, | ||
vehicleCategory: VehicleCategory.VehicleCategory, | ||
householdIncome: Double, | ||
householdSize: Int, | ||
householdPopulation: Population, | ||
householdLocation: Coord, | ||
realDistribution: UniformRealDistribution, | ||
householdId: Id[Household] | ||
): List[BeamVehicleType] = { | ||
vehicles | ||
.filter(x => Id.create(x.householdId, classOf[Household]).equals(householdId)) | ||
.map(x => Id.create(x.vehicleTypeId, classOf[BeamVehicleType])) | ||
.map(x => vehicleTypesMap(x)) | ||
.toList | ||
} | ||
|
||
override def sampleVehicleTypes( | ||
numVehicles: Int, | ||
vehicleCategory: VehicleCategory.VehicleCategory, | ||
realDistribution: UniformRealDistribution | ||
): List[BeamVehicleType] = { | ||
List() | ||
} | ||
|
||
private def readVehiclesFromFile() = { | ||
readers.BeamCsvScenarioReader.readVehiclesFile( | ||
beamScenario.beamConfig.beam.agentsim.agents.vehicles.vehiclesFilePath | ||
) | ||
} | ||
} |
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
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
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,15 +1,23 @@ | ||
package beam.utils.scenario | ||
|
||
import beam.agentsim.agents.vehicles.{BeamVehicle, BeamVehicleType} | ||
import beam.agentsim.agents.vehicles.VehicleCategory.Bike | ||
import beam.sim.BeamScenario | ||
import beam.sim.RunBeam.{buildUrbansimV2ScenarioSource, loadScenario} | ||
import beam.sim.common.GeoUtilsImpl | ||
import beam.sim.config.BeamConfig | ||
import beam.sim.config.{BeamConfig, MatSimBeamConfigBuilder} | ||
import beam.utils.TestConfigUtils.testConfig | ||
import org.matsim.core.scenario.MutableScenario | ||
import com.typesafe.config.ConfigFactory | ||
import org.matsim.api.core.v01.Id | ||
import org.matsim.core.scenario.{MutableScenario, ScenarioBuilder} | ||
import org.matsim.households.Household | ||
import org.mockito.Mockito.{mock, when} | ||
import org.scalatest.wordspec.AsyncWordSpec | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable` | ||
|
||
class UrbanSimScenarioLoaderTest extends AsyncWordSpec with Matchers with BeforeAndAfterEach { | ||
private val mutableScenario = mock(classOf[MutableScenario]) | ||
private val beamScenario = mock(classOf[BeamScenario]) | ||
|
@@ -90,11 +98,45 @@ class UrbanSimScenarioLoaderTest extends AsyncWordSpec with Matchers with Before | |
|
||
assertCarNumbers(res, "1" -> 2, "2" -> 2, "3" -> 2) | ||
} | ||
|
||
} | ||
|
||
"UrbanSimScenarioLoader with vehicleAdjustmentMethod = STATIC_FROM_FILE "should { | ||
"assign vehicles properly in case1" in { | ||
|
||
val matsimConfig = new MatSimBeamConfigBuilder(staticVehiclesConfig).buildMatSimConf() | ||
val emptyScenario = ScenarioBuilder(matsimConfig, beamScenario.network).build | ||
val staticVehicleBeamConfig = BeamConfig(staticVehiclesConfig) | ||
val staticGeoUtils = new GeoUtilsImpl(staticVehicleBeamConfig); | ||
when(beamScenario.beamConfig).thenReturn(staticVehicleBeamConfig); | ||
lazy val staticBeamScenario: BeamScenario = loadScenario(staticVehicleBeamConfig); | ||
val staticScenarioSource = buildUrbansimV2ScenarioSource(staticGeoUtils, staticVehicleBeamConfig); | ||
val urbanSimScenario = new UrbanSimScenarioLoader(emptyScenario, staticBeamScenario, staticScenarioSource, geoUtils) | ||
urbanSimScenario.loadScenario() | ||
|
||
val vehicleIds = emptyScenario.getHouseholds.getHouseholds.get(Id.create(2, classOf[Household])).getVehicleIds | ||
val vehicleMap = staticBeamScenario.privateVehicles.toMap | ||
val vehiclesBelongToSpecificHousehold = vehicleIds.map(x => Id.create(x, classOf[BeamVehicle])).flatMap(x => vehicleMap get x); | ||
vehiclesBelongToSpecificHousehold.count(x => x.beamVehicleType.id.compareTo( | ||
Id.create("Bicycle", classOf[BeamVehicleType]) | ||
) == 0) shouldBe 7 | ||
} | ||
|
||
} | ||
|
||
override protected def beforeEach(): Unit = | ||
idIter = Iterator.from(1) | ||
|
||
def staticVehiclesConfig: com.typesafe.config.Config = | ||
ConfigFactory | ||
.parseString(s""" | ||
|beam.agentsim.simulationName = "beamville-urbansimv2_static" | ||
|beam.agentsim.agents.vehicles.vehiclesFilePath = $${beam.inputDirectory}"/vehicles-test.csv" | ||
|beam.agentsim.agents.vehicles.vehicleAdjustmentMethod = "STATIC_FROM_FILE" | ||
""".stripMargin) | ||
.withFallback(testConfig("test/input/beamville/beam-urbansimv2.conf")) | ||
.resolve() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's put this config initialization inside the test, no need to create external variable for a test |
||
|
||
private def getConfig(fractionOfInitialVehicleFleet: Double = 1.0) = beamConfigBase.copy( | ||
matsim = beamConfigBase.matsim | ||
.copy( | ||
|
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,8 @@ | ||
include "../common/akka.conf" | ||
include "../common/metrics.conf" | ||
include "../common/matsim.conf" | ||
include "beam-urbansimv2.conf" | ||
|
||
beam.agentsim.simulationName = "beamville-urbansimv2_static" | ||
beam.agentsim.agents.vehicles.vehiclesFilePath = ${beam.inputDirectory}"/vehicles-test.csv" | ||
beam.agentsim.agents.vehicles.vehicleAdjustmentMethod = "STATIC_FROM_FILE" |
Git LFS file not shown
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use
equal
instead ofcompareTo