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 2 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
55 changes: 55 additions & 0 deletions src/main/scala/beam/sim/vehicles/FromFileVehiclesAdjustment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 {

var vehicles: Iterable[VehicleInfo] = List();
var vehicleTypesMap: Map[Id[BeamVehicleType], BeamVehicleType] = Map();

override def sampleVehicleTypesForHousehold(
numVehicles: Int,
vehicleCategory: VehicleCategory.VehicleCategory,
householdIncome: Double,
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution,
householdId: Id[Household] = Id.create("", classOf[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()
}

def readVechiclesFromFile() = {
readers.BeamCsvScenarioReader.readVehiclesFile(
beamScenario.beamConfig.beam.agentsim.agents.vehicles.vehiclesFilePath
)
}
}

object FromFileVehiclesAdjustment {

def apply(beamScenario: BeamScenario) = new FromFileVehiclesAdjustment(beamScenario) {
vehicles = readVechiclesFromFile()
vehicleTypesMap = beamScenario.vehicleTypes.map(i => i._1 -> i._2).toMap
Copy link
Collaborator

@nikolayilyin nikolayilyin Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put these rows as first rows (14-15) in the class and replace 'var' with 'val' to emphasize these values are not to be replaced.

}
}
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
Loading