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

import beam.agentsim.agents.Population
import beam.agentsim.agents.vehicles.VehicleCategory.VehicleCategory
import beam.agentsim.agents.vehicles.{BeamVehicleType, VehicleCategory}
import beam.sim.BeamScenario
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.Coord

case class NoOpVehiclesAdjustment(beamScenario: BeamScenario) extends VehiclesAdjustment {
override def sampleVehicleTypesForHousehold(
numVehicles: Int,
vehicleCategory: VehicleCategory,
householdIncome: Double,
householdSize: Int,
householdPopulation: Population,
householdLocation: Coord,
realDistribution: UniformRealDistribution
): List[BeamVehicleType] = {
List()
}

override def sampleVehicleTypes(
numVehicles: Int,
vehicleCategory: VehicleCategory,
realDistribution: UniformRealDistribution
): List[BeamVehicleType] = {
List()
}
}
2 changes: 2 additions & 0 deletions src/main/scala/beam/sim/vehicles/VehiclesAdjustment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,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 => NoOpVehiclesAdjustment(beamScenario)
case INCOME_BASED_ADJUSTMENT => IncomeBasedVehiclesAdjustment(beamScenario)
case _ => UniformVehiclesAdjustment(beamScenario)
}
Expand Down
62 changes: 58 additions & 4 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,9 +21,10 @@ 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}
import scala.collection.{Iterable, mutable}
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.math.{max, min, round}
Expand Down Expand Up @@ -57,6 +61,14 @@ class UrbanSimScenarioLoader(
def loadScenario(): (Scenario, Boolean) = {
clear()

// for {
// vehicleInfo <- vehicles
// vehicle = buildBeamVehicle(beamScenario.vehicleTypes, vehicleInfo, rand.nextInt)
// } {
// beamScenario.privateVehicles.put(vehicle.id, vehicle)
// vehicleInfo.initialSoc.foreach(beamScenario.privateVehicleInitialSoc.put(vehicle.id, _))
// }

nikolayilyin marked this conversation as resolved.
Show resolved Hide resolved
val plansF = Future {
val plans = scenarioSource.getPlans
logger.info(s"Read ${plans.size} plans")
Expand Down Expand Up @@ -123,7 +135,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 @@ -168,7 +179,8 @@ class UrbanSimScenarioLoader(
.find(_.vehicleCategory == VehicleCategory.Bike)
.getOrElse(throw new RuntimeException("Bike not found in vehicle types."))

assignVehicles(households, householdIdToPersons, personId2Score).foreach { case (householdInfo, nVehicles) =>
assignVehicles(households, householdIdToPersons, personId2Score).foreach {
case (householdInfo, nVehicles) =>
val id = Id.create(householdInfo.householdId.id, classOf[Household])
val household = new HouseholdsFactoryImpl().createHousehold(id)
val coord = utmCoord(householdInfo.locationX, householdInfo.locationY)
Expand Down Expand Up @@ -218,6 +230,20 @@ class UrbanSimScenarioLoader(
beamScenario.privateVehicles.put(beamVehicle.id, beamVehicle)
vehicleCounter = vehicleCounter + 1
}
if ( beamScenario.beamConfig.beam.agentsim.agents.vehicles.vehicleAdjustmentMethod.equals("STATIC_FROM_FILE")) {
// populate private vehicles here
val vehicles = readers.BeamCsvScenarioReader.readVehiclesFile(beamScenario.beamConfig.beam.agentsim.agents.vehicles.vehiclesFilePath)
.filter(x => Id.create(x.householdId, classOf[Household]).equals(id));
for {
vehicleInfo <- vehicles
vehicle = buildBeamVehicle(beamScenario.vehicleTypes, vehicleInfo, rand.nextInt)
} {
beamScenario.privateVehicles.put(vehicle.id, vehicle)
vehicleIds.add(vehicle.id)
vehicleCounter = vehicleCounter + 1
}
}

nikolayilyin marked this conversation as resolved.
Show resolved Hide resolved
household.setVehicleIds(vehicleIds)
scenarioHouseholds.put(household.getId, household)
scenarioHouseholdAttributes.putAttribute(household.getId.toString, "homecoordx", coord.getX)
Expand All @@ -229,6 +255,34 @@ class UrbanSimScenarioLoader(
)
}


def buildBeamVehicle(
map: Map[Id[BeamVehicleType], BeamVehicleType],
info: VehicleInfo,
randomSeed: Int
): BeamVehicle = {
val matsimVehicleType: VehicleType =
VehicleUtils.getFactory.createVehicleType(Id.create(info.vehicleTypeId, classOf[VehicleType]))
val matsimVehicle: Vehicle =
VehicleUtils.getFactory.createVehicle(Id.createVehicleId(info.vehicleId), matsimVehicleType)

val beamVehicleId = Id.create(matsimVehicle.getId, classOf[BeamVehicle])
val beamVehicleTypeId = Id.create(info.vehicleTypeId, classOf[BeamVehicleType])

val beamVehicleType = map(beamVehicleTypeId)

val vehicleManagerId =
VehicleManager.createOrGetReservedFor(info.householdId, VehicleManager.TypeEnum.Household).managerId
val powerTrain = new Powertrain(beamVehicleType.primaryFuelConsumptionInJoulePerMeter)
new BeamVehicle(
beamVehicleId,
powerTrain,
beamVehicleType,
new AtomicReference(vehicleManagerId),
randomSeed = randomSeed
)
}
nikolayilyin marked this conversation as resolved.
Show resolved Hide resolved

private def getPersonScore(personTravelStats: PersonTravelStats): Double = {
val distanceExcludingLastTrip =
personTravelStats.tripStats.dropRight(1).map(x => geo.distUTMInMeters(x.origin, x.destination)).sum
Expand Down
Loading