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

Fdariasm/#433 refactor vehicle types 4ci #532

Merged
merged 17 commits into from
Sep 12, 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
7 changes: 4 additions & 3 deletions src/main/java/beam/agentsim/events/PathTraversalEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package beam.agentsim.events;

import beam.agentsim.agents.vehicles.BeamVehicleType;
import beam.router.RoutingModel;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.Event;
Expand Down Expand Up @@ -51,9 +52,9 @@ public class PathTraversalEvent extends Event {
private final double endX;
private final double endY;

public PathTraversalEvent(double time, Id<Vehicle> vehicleId, VehicleType vehicleType, Integer numPass, RoutingModel.BeamLeg beamLeg, double fuelConsumed, double endLegFuelLevel) {
this(time, vehicleId, vehicleType.getDescription(), beamLeg.mode().value(), numPass, endLegFuelLevel,
vehicleType.getCapacity() != null ? vehicleType.getCapacity().getSeats() + vehicleType.getCapacity().getStandingRoom() : 0,
public PathTraversalEvent(double time, Id<Vehicle> vehicleId, BeamVehicleType vehicleType, Integer numPass, RoutingModel.BeamLeg beamLeg, double fuelConsumed, double endLegFuelLevel) {
this(time, vehicleId, vehicleType.vehicleCategory(), beamLeg.mode().value(), numPass, endLegFuelLevel,
(int)(vehicleType.seatingCapacity() + vehicleType.standingRoomCapacity()),
fuelConsumed,
beamLeg.travelPath().distanceInM(), beamLeg.travelPath().linkIds().mkString(","), beamLeg.startTime(), beamLeg.endTime(),
beamLeg.travelPath().startPoint().loc().getX(), beamLeg.travelPath().startPoint().loc().getY(), beamLeg.travelPath().endPoint().loc().getX(),
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/beam-template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ beam.agentsim.agents.modalBehaviors.mulitnomialLogit.params.ride_hail_intercept
beam.agentsim.agents.modalBehaviors.mulitnomialLogit.params.walk_intercept = "double | 0.0"
beam.agentsim.agents.modalBehaviors.mulitnomialLogit.params.bike_intercept = "double | 0.0"
beam.agentsim.agents.modalBehaviors.lccm.paramFile = ${beam.inputDirectory}"/lccm-long.csv"

#DrivingCostDefaults Params
beam.agentsim.agents.drivingCost.defaultLitersPerMeter = "double | 0.0001069"
beam.agentsim.agents.drivingCost.defaultPricePerGallon = "double | 3.115"
Expand Down Expand Up @@ -67,6 +68,11 @@ beam.agentsim.agents.rideHail.allocationManager.repositionLowWaitingTimes.demand
beam.agentsim.agents.rideHail.allocationManager.repositionLowWaitingTimes.produceDebugImages=true

beam.agentsim.agents.vehicles.bicycles.useBikes="boolean | false"
#BeamVehicles Params
beam.agentsim.agents.vehicles.beamFuelTypesFile = ${beam.inputDirectory}"/beamFuelTypes.csv"
beam.agentsim.agents.vehicles.beamVehicleTypesFile = ${beam.inputDirectory}"/vehicleTypes.csv"
beam.agentsim.agents.vehicles.beamVehiclesFile = ${beam.inputDirectory}"/vehicles.csv"


beam.agentsim.agents.rideHail.initialLocation.name="HOME"
beam.agentsim.agents.rideHail.initialLocation.home.radiusInMeters="double | 10000"
Expand Down
157 changes: 79 additions & 78 deletions src/main/scala/beam/agentsim/agents/Population.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import java.util.concurrent.TimeUnit

import akka.actor.SupervisorStrategy.Stop
import akka.actor.{Actor, ActorLogging, ActorRef, Identify, OneForOneStrategy, Props, Terminated}
import akka.event.LoggingAdapter
import akka.pattern._
import akka.util.Timeout
import beam.agentsim
import beam.agentsim.agents.BeamAgent.Finish
import beam.agentsim.agents.Population.InitParkingVehicles
import beam.agentsim.agents.household.HouseholdActor
import beam.agentsim.agents.vehicles.{BeamVehicle, BicycleFactory}
import beam.agentsim.agents.vehicles.BeamVehicleType.{BicycleVehicle, CarVehicle}
import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain
import beam.agentsim.vehicleId2BeamVehicleId
import beam.agentsim.infrastructure.ParkingManager.{ParkingInquiry, ParkingInquiryResponse}
Expand All @@ -27,7 +27,7 @@ import org.matsim.households.Household
import org.matsim.vehicles.{Vehicle, Vehicles}

import scala.collection.JavaConverters._
import scala.collection.{mutable, JavaConverters}
import scala.collection.{JavaConverters, mutable}
import scala.concurrent.{Await, Future}
import scala.util.Try

Expand Down Expand Up @@ -89,73 +89,73 @@ class Population(

private def initHouseholds(iterId: Option[String] = None): Unit = {
import scala.concurrent.ExecutionContext.Implicits.global

// Have to wait for households to create people so they can send their first trigger to the scheduler
val houseHoldsInitialized =
Future.sequence(scenario.getHouseholds.getHouseholds.values().asScala.map { household =>
//TODO a good example where projection should accompany the data
if (scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordx") == null) {
log.error(
s"Cannot find homeCoordX for household ${household.getId} which will be interpreted at 0.0"
try {
// Have to wait for households to create people so they can send their first trigger to the scheduler
val houseHoldsInitialized =
Future.sequence(scenario.getHouseholds.getHouseholds.values().asScala.map { household =>
//TODO a good example where projection should accompany the data
if (scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordx") == null) {
log.error(
s"Cannot find homeCoordX for household ${household.getId} which will be interpreted at 0.0"
)
}
if (scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString.toLowerCase(), "homecoordy") == null) {
log.error(
s"Cannot find homeCoordY for household ${household.getId} which will be interpreted at 0.0"
)
}
val homeCoord = new Coord(
scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordx")
.asInstanceOf[Double],
scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordy")
.asInstanceOf[Double]
)
}
if (scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString.toLowerCase(), "homecoordy") == null) {
log.error(
s"Cannot find homeCoordY for household ${household.getId} which will be interpreted at 0.0"

val houseHoldVehicles: Map[Id[BeamVehicle], BeamVehicle] =
Population.getVehiclesFromHousehold(household, beamServices)

houseHoldVehicles.foreach(x => beamServices.vehicles.update(x._1, x._2))

val householdActor = context.actorOf(
HouseholdActor.props(
beamServices,
beamServices.modeChoiceCalculatorFactory,
scheduler,
transportNetwork,
router,
rideHailManager,
parkingManager,
eventsManager,
scenario.getPopulation,
household.getId,
household,
houseHoldVehicles,
homeCoord
),
household.getId.toString
)
}
val homeCoord = new Coord(
scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordx")
.asInstanceOf[Double],
scenario.getHouseholds.getHouseholdAttributes
.getAttribute(household.getId.toString, "homecoordy")
.asInstanceOf[Double]
)

var houseHoldVehicles: Map[Id[BeamVehicle], BeamVehicle] =
Population.getVehiclesFromHousehold(household, beamServices)

houseHoldVehicles.foreach(x => beamServices.vehicles.update(x._1, x._2))

val householdActor = context.actorOf(
HouseholdActor.props(
beamServices,
beamServices.modeChoiceCalculatorFactory,
scheduler,
transportNetwork,
router,
rideHailManager,
parkingManager,
eventsManager,
scenario.getPopulation,
household.getId,
household,
houseHoldVehicles,
homeCoord
),
household.getId.toString
)

houseHoldVehicles.values.foreach { veh =>
veh.manager = Some(householdActor)
}

houseHoldVehicles.foreach {
vehicle =>
val initParkingVehicle = context.actorOf(Props(new Actor with ActorLogging {
parkingManager ! ParkingInquiry(
Id.createPersonId("atHome"),
homeCoord,
homeCoord,
"home",
0,
NoNeed,
0,
0
) //TODO personSelectedPlan.getType is null
houseHoldVehicles.values.foreach { veh =>
veh.manager = Some(householdActor)
}

houseHoldVehicles.foreach {
vehicle =>
val initParkingVehicle = context.actorOf(Props(new Actor with ActorLogging {
parkingManager ! ParkingInquiry(
Id.createPersonId("atHome"),
homeCoord,
homeCoord,
"home",
0,
NoNeed,
0,
0
) //TODO personSelectedPlan.getType is null

def receive = {
case ParkingInquiryResponse(stall, _) =>
Expand All @@ -167,11 +167,16 @@ class Population(
initParkingVeh append initParkingVehicle
}

context.watch(householdActor)
householdActor ? Identify(0)
})
Await.result(houseHoldsInitialized, timeout.duration)
log.info(s"Initialized ${scenario.getHouseholds.getHouseholds.size} households")
context.watch(householdActor)
householdActor ? Identify(0)
})
Await.result(houseHoldsInitialized, timeout.duration)
log.info(s"Initialized ${scenario.getHouseholds.getHouseholds.size} households")
} catch {
case e: Exception =>
log.error(e, "Error initializing houseHolds")
throw e
}
}

}
Expand All @@ -191,18 +196,14 @@ object Population {

// Add bikes
if (beamServices.beamConfig.beam.agentsim.agents.vehicles.bicycles.useBikes) {
val bikeFactory = new BicycleFactory(beamServices.matsimServices.getScenario)
val bikeFactory = new BicycleFactory(beamServices.matsimServices.getScenario, beamServices)
bikeFactory.bicyclePrepareForSim()
}
houseHoldVehicles
.map({ id =>
makeHouseholdVehicle(
beamServices.matsimServices.getScenario.getVehicles,
id,
defaultVehicleRange,
refuelRateLimitInWatts
) match {
makeHouseholdVehicle(beamServices.privateVehicles, id) match {
case Right(vehicle) => vehicleId2BeamVehicleId(id) -> vehicle
case Left(e) => throw e
}
})
.toMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ object DrivingCostDefaults {
)

val vehicle = beamServices.vehicles(neededLeg.beamVehicleId)
val litersPerMeter =
if (vehicle == null || vehicle.getType == null || vehicle.getType.getEngineInformation == null) {
drivingCostConfig.defaultLitersPerMeter
} else {
vehicle.getType.getEngineInformation.getGasConsumption
}
val cost = legs.view

val distance = legs.view
.map(_.beamLeg.travelPath.distanceInM)
.sum * litersPerMeter / LITERS_PER_GALLON * drivingCostConfig.defaultPricePerGallon // 3.78 liters per gallon and 3.115 $/gal in CA: http://www.californiagasprices.com/Prices_Nationally.aspx
.sum

val cost = if(null != vehicle && null != vehicle.beamVehicleType && null != vehicle.beamVehicleType.primaryFuelType && null != vehicle.beamVehicleType.primaryFuelConsumptionInJoule) {
(distance * vehicle.beamVehicleType.primaryFuelConsumptionInJoule * vehicle.beamVehicleType.primaryFuelType.priceInDollarsPerMJoule) / 1000000
} else {
0 //TODO
}
BigDecimal(cost)
case _ =>
BigDecimal(0)
Expand Down
Loading