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

been a while since we did a fmt #922

Merged
merged 3 commits into from
Nov 13, 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
11 changes: 7 additions & 4 deletions src/main/scala/beam/agentsim/agents/Population.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ class Population(
.asInstanceOf[Double]
)

val householdVehicles: Map[Id[BeamVehicle], BeamVehicle] = JavaConverters.collectionAsScalaIterable(household.getVehicleIds).map{ vid=>
val bvid = BeamVehicle.createId(vid)
bvid->beamServices.privateVehicles(bvid)
}.toMap
val householdVehicles: Map[Id[BeamVehicle], BeamVehicle] = JavaConverters
.collectionAsScalaIterable(household.getVehicleIds)
.map { vid =>
val bvid = BeamVehicle.createId(vid)
bvid -> beamServices.privateVehicles(bvid)
}
.toMap
householdVehicles.foreach(x => beamServices.vehicles.update(x._1, x._2))
val householdActor = context.actorOf(
HouseholdActor.props(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import org.matsim.api.core.v01.population.Person
*/
class ModeChoiceDriveIfAvailable(val beamServices: BeamServices) extends ModeChoiceCalculator {

def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
val containsDriveAlt = alternatives.zipWithIndex.collect {
case (trip, idx) if trip.tripClassifier == CAR => idx
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class ModeChoiceLCCM(
var expectedMaximumUtility: Double = Double.NaN
var classMembershipDistribution: Map[String, Double] = Map()

override def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
override def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
choose(alternatives, attributesOfIndividual, Mandatory)
}

Expand All @@ -66,19 +69,19 @@ class ModeChoiceLCCM(
}

val attribIndivData: AlternativeAttributes = {
val theParams: Map[String, Double] = Map(
"income" -> attributesOfIndividual.householdAttributes.householdIncome,
"householdSize" -> attributesOfIndividual.householdAttributes.householdSize,
"male" -> (if (attributesOfIndividual.isMale) {
1.0
} else {
0.0
}),
"numCars" -> attributesOfIndividual.householdAttributes.numCars,
"numBikes" -> attributesOfIndividual.householdAttributes.numBikes
)
AlternativeAttributes("dummy", theParams)
}
val theParams: Map[String, Double] = Map(
"income" -> attributesOfIndividual.householdAttributes.householdIncome,
"householdSize" -> attributesOfIndividual.householdAttributes.householdSize,
"male" -> (if (attributesOfIndividual.isMale) {
1.0
} else {
0.0
}),
"numCars" -> attributesOfIndividual.householdAttributes.numCars,
"numBikes" -> attributesOfIndividual.householdAttributes.numBikes
)
AlternativeAttributes("dummy", theParams)
}

val classMembershipInputData =
lccm.classMembershipModels.head._2.alternativeParams.keySet.map { theClassName =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ import scala.util.Random
* BEAM
*/
class ModeChoiceMultinomialLogit(val beamServices: BeamServices, val model: MultinomialLogit)
extends ModeChoiceCalculator with LazyLogging {
extends ModeChoiceCalculator
with LazyLogging {

var expectedMaximumUtility: Double = 0.0

override def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
override def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
if (alternatives.isEmpty) {
None
} else {
val modeCostTimeTransfers = altsToModeCostTimeTransfers(alternatives, attributesOfIndividual)

val bestInGroup =
modeCostTimeTransfers groupBy (_.mode) map {
case (_, group) => group minBy timeAndCost
}
modeCostTimeTransfers groupBy (_.mode) map {
case (_, group) => group minBy timeAndCost
}

val inputData = bestInGroup.map { mct =>
val theParams: Map[String, Double] =
Expand Down Expand Up @@ -69,7 +73,10 @@ class ModeChoiceMultinomialLogit(val beamServices: BeamServices, val model: Mult
mct.scaledTime + mct.cost
}

def altsToModeCostTimeTransfers(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): IndexedSeq[ModeCostTimeTransfer] = {
def altsToModeCostTimeTransfers(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): IndexedSeq[ModeCostTimeTransfer] = {
val walkTripStartTime = alternatives
.find(_.tripClassifier == WALK)
.map(_.legs.head.beamLeg.startTime)
Expand All @@ -84,7 +91,7 @@ class ModeChoiceMultinomialLogit(val beamServices: BeamServices, val model: Mult
val totalCost: Double = mode match {
case TRANSIT | WALK_TRANSIT | DRIVE_TRANSIT =>
(altAndIdx._1.costEstimate + transitFareDefaults(altAndIdx._2)) * beamServices.beamConfig.beam.agentsim.tuning.transitPrice +
gasolineCostDefaults(altAndIdx._2)
gasolineCostDefaults(altAndIdx._2)
case RIDE_HAIL =>
(altAndIdx._1.costEstimate + rideHailDefaults(altAndIdx._2)) * beamServices.beamConfig.beam.agentsim.tuning.rideHailPrice
case RIDE_HAIL_TRANSIT =>
Expand All @@ -103,11 +110,21 @@ class ModeChoiceMultinomialLogit(val beamServices: BeamServices, val model: Mult
case _ =>
altAndIdx._1.costEstimate
}
val subsidy = beamServices.modeSubsidies.getSubsidy(mode, attributesOfIndividual.age, attributesOfIndividual.income.map(x => x.toInt))
val subsidy = beamServices.modeSubsidies.getSubsidy(
mode,
attributesOfIndividual.age,
attributesOfIndividual.income.map(x => x.toInt)
)
val subsidisedCost =
Math.max(0, totalCost.toDouble - subsidy)

if (totalCost < subsidy) logger.warn("Mode subsidy is even higher then the cost, setting cost to zero. Mode: {}, Cost: {}, Subsidy: {}", mode, totalCost, subsidy)
if (totalCost < subsidy)
logger.warn(
"Mode subsidy is even higher then the cost, setting cost to zero. Mode: {}, Cost: {}, Subsidy: {}",
mode,
totalCost,
subsidy
)

val numTransfers = mode match {
case TRANSIT | WALK_TRANSIT | DRIVE_TRANSIT | RIDE_HAIL_TRANSIT =>
Expand Down Expand Up @@ -158,7 +175,7 @@ class ModeChoiceMultinomialLogit(val beamServices: BeamServices, val model: Mult
val variables =
Map(
"transfer" -> numTransfers.toDouble,
"cost" -> (cost + scaleTimeByVot(time, Option(mode)))
"cost" -> (cost + scaleTimeByVot(time, Option(mode)))
)
model.getUtilityOfAlternative(AlternativeAttributes(mode.value, variables))
}
Expand Down Expand Up @@ -205,11 +222,11 @@ object ModeChoiceMultinomialLogit {
}

case class ModeCostTimeTransfer(
mode: BeamMode,
cost: Double,
scaledTime: Double,
numTransfers: Int,
index: Int = -1
)
mode: BeamMode,
cost: Double,
scaledTime: Double,
numTransfers: Int,
index: Int = -1
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import org.matsim.api.core.v01.population.Person
*/
class ModeChoiceRideHailIfAvailable(val beamServices: BeamServices) extends ModeChoiceCalculator {

override def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
override def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
val containsRideHailAlt = alternatives.zipWithIndex.collect {
case (trip, idx) if trip.tripClassifier == RIDE_HAIL => idx
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class ModeChoiceTransitIfAvailable(val beamServices: BeamServices) extends ModeC
override def clone(): ModeChoiceCalculator =
new ModeChoiceTransitIfAvailable(beamServices)

override def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
override def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
val containsTransitAlt = alternatives.zipWithIndex.collect {
case (trip, idx) if trip.tripClassifier.isTransit => idx
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import beam.sim.population.AttributesOfIndividual
*/
class ModeChoiceUniformRandom(val beamServices: BeamServices) extends ModeChoiceCalculator {

override def apply(alternatives: IndexedSeq[EmbodiedBeamTrip], attributesOfIndividual: AttributesOfIndividual): Option[EmbodiedBeamTrip] = {
override def apply(
alternatives: IndexedSeq[EmbodiedBeamTrip],
attributesOfIndividual: AttributesOfIndividual
): Option[EmbodiedBeamTrip] = {
if (alternatives.nonEmpty) {
Some(alternatives(chooseRandomAlternativeIndex(alternatives)))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ case class ModeSubsidy(modeSubsidies: Map[BeamMode, List[Subsidy]]) {
.filter(
s =>
(age.fold(true)(s.age.hasOrEmpty) && income.fold(false)(s.income.hasOrEmpty)) ||
(age.fold(false)(s.age.hasOrEmpty) && income.fold(true)(s.income.hasOrEmpty))
(age.fold(false)(s.age.hasOrEmpty) && income.fold(true)(s.income.hasOrEmpty))
)
.map(_.amount)
.sum
}


}

object ModeSubsidy {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/beam/agentsim/agents/choice/mode/Range.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ object Range {
val bounds = pattern.split(":")
val lowerBound = Try(
bounds(0).substring(1).toInt
+ (if (bounds(0).startsWith("(")) 1 else 0)
+ (if (bounds(0).startsWith("(")) 1 else 0)
).getOrElse(0)
val upperBound = Try(
bounds(1).substring(0, bounds(1).length - 1).toInt
- (if (bounds(1).endsWith(")")) 1 else 0)
- (if (bounds(1).endsWith(")")) 1 else 0)
).getOrElse(Int.MaxValue)
Range(lowerBound, upperBound)
}
Expand Down
77 changes: 37 additions & 40 deletions src/main/scala/beam/agentsim/agents/household/HouseholdActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ object HouseholdActor {
}

def props(
beamServices: BeamServices,
modeChoiceCalculator: AttributesOfIndividual => ModeChoiceCalculator,
schedulerRef: ActorRef,
transportNetwork: TransportNetwork,
router: ActorRef,
rideHailManager: ActorRef,
parkingManager: ActorRef,
eventsManager: EventsManager,
population: org.matsim.api.core.v01.population.Population,
householdId: Id[Household],
matSimHousehold: Household,
houseHoldVehicles: Map[Id[BeamVehicle], BeamVehicle],
homeCoord: Coord
): Props = {
beamServices: BeamServices,
modeChoiceCalculator: AttributesOfIndividual => ModeChoiceCalculator,
schedulerRef: ActorRef,
transportNetwork: TransportNetwork,
router: ActorRef,
rideHailManager: ActorRef,
parkingManager: ActorRef,
eventsManager: EventsManager,
population: org.matsim.api.core.v01.population.Population,
householdId: Id[Household],
matSimHousehold: Household,
houseHoldVehicles: Map[Id[BeamVehicle], BeamVehicle],
homeCoord: Coord
): Props = {
Props(
new HouseholdActor(
beamServices,
Expand Down Expand Up @@ -76,7 +76,6 @@ object HouseholdActor {

case class InitializeRideHailAgent(b: Id[Person])


/**
* Implementation of intra-household interaction in BEAM using actors.
*
Expand All @@ -92,28 +91,27 @@ object HouseholdActor {
* @see [[ChoosesMode]]
*/
class HouseholdActor(
beamServices: BeamServices,
modeChoiceCalculatorFactory: AttributesOfIndividual => ModeChoiceCalculator,
schedulerRef: ActorRef,
transportNetwork: TransportNetwork,
router: ActorRef,
rideHailManager: ActorRef,
parkingManager: ActorRef,
eventsManager: EventsManager,
val population: org.matsim.api.core.v01.population.Population,
id: Id[households.Household],
val household: Household,
vehicles: Map[Id[BeamVehicle], BeamVehicle],
homeCoord: Coord
) extends VehicleManager
with ActorLogging {
beamServices: BeamServices,
modeChoiceCalculatorFactory: AttributesOfIndividual => ModeChoiceCalculator,
schedulerRef: ActorRef,
transportNetwork: TransportNetwork,
router: ActorRef,
rideHailManager: ActorRef,
parkingManager: ActorRef,
eventsManager: EventsManager,
val population: org.matsim.api.core.v01.population.Population,
id: Id[households.Household],
val household: Household,
vehicles: Map[Id[BeamVehicle], BeamVehicle],
homeCoord: Coord
) extends VehicleManager
with ActorLogging {

import beam.agentsim.agents.memberships.Memberships.RankedGroup._

implicit val pop: org.matsim.api.core.v01.population.Population = population

household.members.foreach { person =>

val attributes = person.getCustomAttributes.get("beam-attributes").asInstanceOf[AttributesOfIndividual]

val modeChoiceCalculator = modeChoiceCalculatorFactory(attributes)
Expand Down Expand Up @@ -199,12 +197,12 @@ object HouseholdActor {
override def receive: Receive = {

case NotifyVehicleResourceIdle(
vId,
whenWhere,
_,
_,
_
) =>
vId,
whenWhere,
_,
_,
_
) =>
val vehId = vId.asInstanceOf[Id[BeamVehicle]]
_vehicleToStreetVehicle += (vehId -> StreetVehicle(vehId, whenWhere.get, CAR, asDriver = true))
log.debug("updated vehicle {} with location {}", vehId, whenWhere.get)
Expand Down Expand Up @@ -313,7 +311,7 @@ object HouseholdActor {
private def lookupAvailableVehicles(): Vector[StreetVehicle] =
Vector(
for {
availableVehicle <- _availableVehicles
availableVehicle <- _availableVehicles
availableStreetVehicle <- _vehicleToStreetVehicle.get(availableVehicle)
} yield availableStreetVehicle
).flatten
Expand Down Expand Up @@ -363,7 +361,7 @@ object HouseholdActor {
//Initialize all vehicles to have a stationary trajectory starting at time zero
val initialLocation = SpaceTime(homeCoord.getX, homeCoord.getY, 0)

for {veh <- _vehicles} yield {
for { veh <- _vehicles } yield {
//TODO following mode should match exhaustively
val mode = BeamVehicleType.getMode(vehicles(veh))

Expand All @@ -384,5 +382,4 @@ object HouseholdActor {
)
}


}
Loading