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

Reads from csv file, skips vehicleAdjustment(No-Op), generates beam v… #3603

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ object HouseholdActor {
household.getMemberIds.size(),
householdPopulation = null,
whenWhere.loc,
realDistribution
realDistribution,
household.getId
)
.headOption
.orElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import beam.agentsim.agents.vehicles.VehicleCategory.VehicleCategory
import beam.sim.{BeamScenario, BeamServices}
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.{Coord, Id}
import org.matsim.households.Household

case class DefaultVehiclesAdjustment(beamServices: BeamServices, beamScenario: BeamScenario)
extends VehiclesAdjustment {
Expand All @@ -28,7 +29,8 @@ case class DefaultVehiclesAdjustment(beamServices: BeamServices, beamScenario: B
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution
realDistribution: UniformRealDistribution,
householdId: Id[Household]
): List[BeamVehicleType] = {
if (vehicleCategory != VehicleCategory.Car) throw new NotImplementedError(vehicleCategory.toString)
List.fill(numVehicles)(vehicleTypesByCategory)
Expand Down
47 changes: 47 additions & 0 deletions src/main/scala/beam/sim/vehicles/FromFileVehiclesAdjustment.scala
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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import beam.agentsim.agents.vehicles.BeamVehicleType
import beam.agentsim.agents.vehicles.VehicleCategory.VehicleCategory
import beam.sim.BeamScenario
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.Coord
import org.matsim.api.core.v01.{Coord, Id}
import org.matsim.households.Household

case class IncomeBasedVehiclesAdjustment(beamScenario: BeamScenario) extends VehiclesAdjustment {

Expand All @@ -25,7 +26,8 @@ case class IncomeBasedVehiclesAdjustment(beamScenario: BeamScenario) extends Veh
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution
realDistribution: UniformRealDistribution,
householdId: Id[Household]
): List[BeamVehicleType] = {
val matchedGroups =
vehicleTypesAndProbabilityByCategoryAndGroup.keys.filter(x => isThisHouseholdInThisGroup(householdIncome, x))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import beam.agentsim.agents.vehicles.BeamVehicleType
import beam.agentsim.agents.vehicles.VehicleCategory.VehicleCategory
import beam.sim.{BeamScenario, BeamServices}
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.Coord
import org.matsim.api.core.v01.{Coord, Id}
import org.matsim.households.Household

case class UniformVehiclesAdjustment(beamScenario: BeamScenario) extends VehiclesAdjustment {

Expand All @@ -30,7 +31,8 @@ case class UniformVehiclesAdjustment(beamScenario: BeamScenario) extends Vehicle
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution
realDistribution: UniformRealDistribution,
householdId: Id[Household]
): List[BeamVehicleType] = {
val vehTypeWithProbability = vehicleTypesAndProbabilitiesByCategory(vehicleCategory, "Usage Not Set")
(1 to numVehicles).map { _ =>
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/beam/sim/vehicles/VehiclesAdjustment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import beam.agentsim.agents.vehicles.VehicleCategory.VehicleCategory
import beam.sim.BeamScenario
import beam.utils.logging.ExponentialLazyLogging
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.Coord
import org.matsim.api.core.v01.{Coord, Id}
import org.matsim.households.Household

trait VehiclesAdjustment extends ExponentialLazyLogging {

Expand All @@ -17,7 +18,8 @@ trait VehiclesAdjustment extends ExponentialLazyLogging {
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution
realDistribution: UniformRealDistribution,
householdId: Id[Household]
): List[BeamVehicleType]

def sampleVehicleTypes(
Expand All @@ -31,10 +33,12 @@ trait VehiclesAdjustment extends ExponentialLazyLogging {
object VehiclesAdjustment {
val UNIFORM_ADJUSTMENT = "UNIFORM"
val INCOME_BASED_ADJUSTMENT = "INCOME_BASED"
val STATIC_FROM_FILE = "STATIC_FROM_FILE"

def getVehicleAdjustment(beamScenario: BeamScenario): VehiclesAdjustment = {
beamScenario.beamConfig.beam.agentsim.agents.vehicles.vehicleAdjustmentMethod match {
case UNIFORM_ADJUSTMENT => UniformVehiclesAdjustment(beamScenario)
case STATIC_FROM_FILE => FromFileVehiclesAdjustment(beamScenario)
case INCOME_BASED_ADJUSTMENT => IncomeBasedVehiclesAdjustment(beamScenario)
case _ => UniformVehiclesAdjustment(beamScenario)
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/beam/utils/scenario/UrbanSimScenarioLoader.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package beam.utils.scenario

import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain
import beam.agentsim.agents.vehicles.{BeamVehicle, VehicleCategory}
import beam.agentsim.agents.vehicles.{BeamVehicle, BeamVehicleType, VehicleCategory, VehicleManager}
import beam.router.Modes.BeamMode
import beam.sim.BeamScenario
import beam.sim.common.GeoUtils
import beam.sim.vehicles.VehiclesAdjustment
import beam.sim.vehicles.VehiclesAdjustment.STATIC_FROM_FILE
import beam.utils.SequenceUtils
import beam.utils.csv.readers
import beam.utils.plan.sampling.AvailableModeUtils
import beam.utils.scenario.BeamScenarioLoader.buildBeamVehicle
import beam.utils.scenario.urbansim.HOVModeTransformer
import com.typesafe.scalalogging.LazyLogging
import org.apache.commons.math3.distribution.UniformRealDistribution
Expand All @@ -18,6 +21,7 @@ import org.matsim.core.scenario.MutableScenario
import org.matsim.households._
import org.matsim.vehicles.{Vehicle, VehicleType, VehicleUtils}

import java.util.concurrent.atomic.AtomicReference
import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer
import scala.collection.{mutable, Iterable}
Expand Down Expand Up @@ -123,7 +127,6 @@ class UrbanSimScenarioLoader(
scenario.getPopulation.getPersonAttributes.clear()
scenario.getHouseholds.getHouseholds.clear()
scenario.getHouseholds.getHouseholdAttributes.clear()

beamScenario.privateVehicles.clear()
beamScenario.privateVehicleInitialSoc.clear()
}
Expand Down Expand Up @@ -191,7 +194,8 @@ class UrbanSimScenarioLoader(
householdSize = household.getMemberIds.size,
householdPopulation = null,
householdLocation = coord,
realDistribution
realDistribution,
householdId = household.getId
)
.toBuffer

Expand Down
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])
Expand Down Expand Up @@ -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
Copy link
Collaborator

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 of compareTo

}

}

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(
Expand Down
8 changes: 8 additions & 0 deletions test/input/beamville/beam-urbansimv2-static.conf
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"
3 changes: 3 additions & 0 deletions test/input/beamville/vehicles-test.csv
Git LFS file not shown