Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into production-sfbay-d…
Browse files Browse the repository at this point in the history
…evelop-debug
  • Loading branch information
REASY committed Sep 25, 2019
2 parents 2b2afea + 3291806 commit a2d1189
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void setupActorsAndRunPhysSim(int iterationNumber) {
log.info("JDEQSim Start");
startSegment("jdeqsim-execution", "jdeqsim");
if (beamConfig.beam().debug().debugEnabled()) {
log.info(DebugLib.gcAndGetMemoryLogMessage("Memory Use Before JDEQSim (after GC): "));
log.info(DebugLib.getMemoryLogMessage("Memory Use Before JDEQSim: "));
}

jdeqSimulation.run();
Expand All @@ -177,7 +177,7 @@ private void setupActorsAndRunPhysSim(int iterationNumber) {
}

if (beamConfig.beam().debug().debugEnabled()) {
log.info(DebugLib.gcAndGetMemoryLogMessage("Memory Use After JDEQSim (after GC): "));
log.info(DebugLib.getMemoryLogMessage("Memory Use After JDEQSim: "));
}

endSegment("jdeqsim-execution", "jdeqsim");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void handleEvent(Event event) {
int currentHour = (int) Math.floor(event.getTime() / 3600.0);
if (Math.abs(prevHour - currentHour) >= 1) {
if (debugMode) {
log.info(DebugLib.gcAndGetMemoryLogMessage("Hour " + currentHour + " completed. "));
log.info(DebugLib.getMemoryLogMessage("Hour " + currentHour + " completed. "));
} else {
log.info("Hour " + currentHour + " completed. ");
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/beam/utils/DebugLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static void emptyFunctionForSettingBreakPoint() {

}

public static String gcAndGetMemoryLogMessage(String message) {
System.gc();
public static String getMemoryLogMessage(String message) {
long jvmTotalMemoryInBytes = Runtime.getRuntime().totalMemory();
long jvmFreeMemoryInBytes = Runtime.getRuntime().freeMemory();
long jvmMemoryInUseInBytes = jvmTotalMemoryInBytes - jvmFreeMemoryInBytes;
Expand Down
20 changes: 9 additions & 11 deletions src/main/scala/beam/agentsim/agents/ridehail/RideHailAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,28 @@ object RideHailAgent {

case class ModifyPassengerScheduleAcks(acks: List[ModifyPassengerScheduleAck])

case class Interrupt(interruptId: Id[Interrupt], tick: Int)
case class Interrupt(interruptId: Int, tick: Int)

case object Resume

sealed trait InterruptReply {
val interruptId: Id[Interrupt]
val interruptId: Int
val vehicleId: Id[Vehicle]
val tick: Int
}

case class InterruptedWhileDriving(
interruptId: Id[Interrupt],
interruptId: Int,
vehicleId: Id[Vehicle],
tick: Int,
passengerSchedule: PassengerSchedule,
currentPassengerScheduleIndex: Int,
) extends InterruptReply

case class InterruptedWhileIdle(interruptId: Id[Interrupt], vehicleId: Id[Vehicle], tick: Int) extends InterruptReply
case class InterruptedWhileIdle(interruptId: Int, vehicleId: Id[Vehicle], tick: Int) extends InterruptReply

case class InterruptedWhileOffline(interruptId: Id[Interrupt], vehicleId: Id[Vehicle], tick: Int)
extends InterruptReply
case class InterruptedWhileWaitingToDrive(interruptId: Id[Interrupt], vehicleId: Id[Vehicle], tick: Int)
extends InterruptReply
case class InterruptedWhileOffline(interruptId: Int, vehicleId: Id[Vehicle], tick: Int) extends InterruptReply
case class InterruptedWhileWaitingToDrive(interruptId: Int, vehicleId: Id[Vehicle], tick: Int) extends InterruptReply

case object Idle extends BeamAgentState

Expand Down Expand Up @@ -311,7 +309,7 @@ class RideHailAgent(
Some(triggerId)
)
goto(Idle)
case ev @ Event(Interrupt(interruptId: Id[Interrupt], tick), _) =>
case ev @ Event(Interrupt(interruptId, tick), _) =>
log.debug("state(RideHailingAgent.Offline): {}", ev)
goto(OfflineInterrupted) replying InterruptedWhileOffline(interruptId, vehicle.id, latestObservedTick)
case ev @ Event(Resume, _) =>
Expand Down Expand Up @@ -386,7 +384,7 @@ class RideHailAgent(
}
rideHailManager ! NotifyVehicleOutOfService(vehicle.id)
goto(Offline) replying CompletionNotice(triggerId, newShiftToSchedule)
case ev @ Event(Interrupt(interruptId: Id[Interrupt], tick), _) =>
case ev @ Event(Interrupt(interruptId, tick), _) =>
log.debug("state(RideHailingAgent.Idle): {}", ev)
goto(IdleInterrupted) replying InterruptedWhileIdle(interruptId, vehicle.id, latestObservedTick)
case ev @ Event(
Expand Down Expand Up @@ -480,7 +478,7 @@ class RideHailAgent(
case ev @ Event(Resume, _) =>
log.debug("state(RideHailingAgent.IdleInterrupted): {}", ev)
goto(Idle)
case ev @ Event(Interrupt(interruptId: Id[Interrupt], tick), _) =>
case ev @ Event(Interrupt(interruptId, tick), _) =>
log.debug("state(RideHailingAgent.IdleInterrupted): {}", ev)
stay() replying InterruptedWhileIdle(interruptId, vehicle.id, latestObservedTick)
case ev @ Event(
Expand Down
28 changes: 6 additions & 22 deletions src/main/scala/beam/agentsim/agents/ridehail/RideHailManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package beam.agentsim.agents.ridehail

import java.awt.Color
import java.io.File
import java.lang.reflect.Method
import java.util
import java.util.concurrent.TimeUnit

import akka.actor.SupervisorStrategy.Stop
import akka.actor.{Actor, ActorLogging, ActorRef, Cancellable, OneForOneStrategy, Props, Stash, Terminated}
import akka.actor.{Actor, ActorLogging, ActorRef, OneForOneStrategy, Props, Stash, Terminated}
import akka.event.LoggingReceive
import akka.pattern._
import akka.util.Timeout
import beam.agentsim
import beam.agentsim.Resource._
import beam.agentsim.agents.BeamAgent.Finish
import beam.agentsim.agents.{Dropoff, InitializeTrigger, MobilityRequest, Pickup}
import beam.agentsim.agents.choice.logit.UtilityFunctionOperation
import beam.agentsim.agents.household.CAVSchedule.RouteOrEmbodyRequest
import beam.agentsim.agents.modalbehaviors.DrivesVehicle._
import beam.agentsim.agents.ridehail.RideHailAgent._
Expand All @@ -29,16 +28,9 @@ import beam.agentsim.agents.vehicles.AccessErrorCodes.{
import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain
import beam.agentsim.agents.vehicles.VehicleProtocol.StreetVehicle
import beam.agentsim.agents.vehicles.{PassengerSchedule, _}
import beam.agentsim.agents.{Dropoff, InitializeTrigger, MobilityRequest, Pickup}
import beam.agentsim.events.SpaceTime
import beam.agentsim.infrastructure.ZonalParkingManager.logger
import beam.agentsim.infrastructure.parking.{
ParkingMNL,
ParkingType,
ParkingZone,
ParkingZoneFileUtils,
ParkingZoneSearch
}
import beam.agentsim.infrastructure.taz.TAZ
import beam.agentsim.infrastructure.parking.ParkingMNL
import beam.agentsim.infrastructure.{ParkingInquiry, ParkingInquiryResponse, ParkingStall}
import beam.agentsim.scheduler.BeamAgentScheduler.{CompletionNotice, ScheduleTrigger}
import beam.agentsim.scheduler.Trigger
Expand All @@ -57,25 +49,21 @@ import beam.utils.logging.LogActorState
import beam.utils.matsim_conversion.ShapeUtils.QuadTreeBounds
import beam.utils.reflection.ReflectionUtils
import com.conveyal.r5.transit.TransportNetwork
import com.eaio.uuid.UUIDGen
import com.google.common.cache.{Cache, CacheBuilder}
import com.vividsolutions.jts.geom.Envelope
import org.apache.commons.math3.distribution.UniformRealDistribution
import org.matsim.api.core.v01.population.{Activity, Person}
import org.matsim.api.core.v01.{Coord, Id, Scenario}
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.vehicles.Vehicle

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.math.{max, min}
import scala.util.{Failure, Random, Success, Try}

import beam.agentsim.agents.choice.logit.{MultinomialLogit, UtilityFunctionOperation}
import beam.agentsim.infrastructure.parking.ParkingMNL.RemainingTripData
import beam.agentsim.infrastructure.parking.ParkingZoneSearch.ParkingAlternative
import scala.util.Random

object RideHailManager {
val INITIAL_RIDE_HAIL_LOCATION_HOME = "HOME"
Expand All @@ -84,10 +72,6 @@ object RideHailManager {
val INITIAL_RIDE_HAIL_LOCATION_ALL_AT_CENTER = "ALL_AT_CENTER"
val INITIAL_RIDE_HAIL_LOCATION_ALL_IN_CORNER = "ALL_IN_CORNER"

def nextRideHailInquiryId: Id[RideHailRequest] = {
Id.create(UUIDGen.createTime(UUIDGen.newTime()).toString, classOf[RideHailRequest])
}

sealed trait RideHailServiceStatus

case object NotifyIterationEnds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import beam.agentsim.agents.HasTickAndTrigger
import beam.agentsim.agents.modalbehaviors.DrivesVehicle.StopDriving
import beam.agentsim.agents.ridehail.RideHailAgent._
import beam.agentsim.agents.ridehail.RideHailManager.{BufferedRideHailRequestsTrigger, RideHailRepositioningTrigger}
import beam.agentsim.agents.ridehail.RideHailVehicleManager.RideHailAgentLocation
import beam.agentsim.agents.vehicles.PassengerSchedule
import beam.agentsim.events.SpaceTime
import beam.agentsim.scheduler.BeamAgentScheduler
import beam.agentsim.scheduler.BeamAgentScheduler.{CompletionNotice, ScheduleTrigger}
import beam.sim.config.BeamConfig
import beam.utils.DebugLib
import com.eaio.uuid.UUIDGen
import beam.utils.InterruptIdIdGenerator
import org.matsim.api.core.v01.Id
import org.matsim.vehicles.Vehicle

Expand All @@ -28,7 +25,7 @@ class RideHailModifyPassengerScheduleManager(
) extends HasTickAndTrigger {

private val interruptIdToModifyPassengerScheduleStatus =
mutable.Map[Id[Interrupt], RideHailModifyPassengerScheduleStatus]()
mutable.Map[Int, RideHailModifyPassengerScheduleStatus]()
private val vehicleIdToModifyPassengerScheduleStatus =
mutable.Map[Id[Vehicle], RideHailModifyPassengerScheduleStatus]()
private val interruptedVehicleIds = mutable.Set[Id[Vehicle]]() // For debug only
Expand Down Expand Up @@ -367,7 +364,7 @@ class RideHailModifyPassengerScheduleManager(
}
}
private def clearModifyStatusFromCacheWithInterruptId(
interruptId: Id[Interrupt]
interruptId: Int
): Unit = {
log.debug("remove interrupt from clearModifyStatusFromCacheWithInterruptId {}", interruptId)
interruptIdToModifyPassengerScheduleStatus.remove(interruptId).foreach { rideHailModifyPassengerScheduleStatus =>
Expand Down Expand Up @@ -435,7 +432,7 @@ case object Reposition extends InterruptOrigin
case object HoldForPlanning extends InterruptOrigin

case class RideHailModifyPassengerScheduleStatus(
interruptId: Id[Interrupt],
interruptId: Int,
vehicleId: Id[Vehicle],
modifyPassengerSchedule: ModifyPassengerSchedule,
interruptOrigin: InterruptOrigin,
Expand All @@ -448,8 +445,5 @@ case class RideHailModifyPassengerScheduleStatus(
case class ReduceAwaitingRepositioningAckMessagesByOne(vehicleId: Id[Vehicle])

object RideHailModifyPassengerScheduleManager {

def nextRideHailAgentInterruptId: Id[Interrupt] = {
Id.create(UUIDGen.createTime(UUIDGen.newTime()).toString, classOf[Interrupt])
}
def nextRideHailAgentInterruptId: Int = InterruptIdIdGenerator.nextId
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@ package beam.agentsim.agents.vehicles
import beam.agentsim.events.resources.ReservationErrorCode._
import beam.agentsim.events.resources._
import beam.agentsim.scheduler.BeamAgentScheduler.ScheduleTrigger
import beam.router.Modes.BeamMode
import beam.router.model.BeamLeg
import com.eaio.uuid.UUIDGen
import org.matsim.api.core.v01.Id

object Reservation {

def nextReservationId: Id[ReservationRequest] =
Id.create(UUIDGen.createTime(UUIDGen.newTime()).toString, classOf[ReservationRequest])
}
import beam.utils.ReservationRequestIdGenerator

case class ReservationRequest(
requestId: Id[ReservationRequest],
requestId: Int,
departFrom: BeamLeg,
arriveAt: BeamLeg,
passengerVehiclePersonId: PersonIdWithActorRef
Expand All @@ -29,7 +21,7 @@ object ReservationRequest {
passengerVehiclePersonId: PersonIdWithActorRef
): ReservationRequest =
ReservationRequest(
Reservation.nextReservationId,
ReservationRequestIdGenerator.nextId,
departFrom,
arriveAt,
passengerVehiclePersonId
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/sim/BeamMobsim.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BeamMobsim @Inject()(
validateVehicleTypes()

if (beamServices.beamConfig.beam.debug.debugEnabled)
logger.info(DebugLib.gcAndGetMemoryLogMessage("run.start (after GC): "))
logger.info(DebugLib.getMemoryLogMessage("run.start (after GC): "))
Metrics.iterationNumber = beamServices.matsimServices.getIterationNumber
eventsManager.initProcessing()

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/beam/sim/BeamSim.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class BeamSim @Inject()(
}

if (beamConfig.beam.debug.debugEnabled)
logger.info(DebugLib.gcAndGetMemoryLogMessage("notifyIterationEnds.start (after GC): "))
logger.info(DebugLib.getMemoryLogMessage("notifyIterationEnds.start (after GC): "))

rideHailUtilizationCollector.notifyIterationEnds(event)

Expand Down Expand Up @@ -273,7 +273,7 @@ class BeamSim @Inject()(
}

if (beamConfig.beam.debug.debugEnabled)
logger.info(DebugLib.gcAndGetMemoryLogMessage("notifyIterationEnds.end (after GC): "))
logger.info(DebugLib.getMemoryLogMessage("notifyIterationEnds.end (after GC): "))
stopMeasuringIteration()

val persons = scenario.getPopulation.getPersons.values().asScala
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/utils/DebugActorWithTimer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DebugActorWithTimer(val rideHailManager: ActorRef, val scheduler: ActorRef

def receive: PartialFunction[Any, Unit] = {
case Tick =>
log.info(DebugLib.gcAndGetMemoryLogMessage("Memory use after GC: "))
log.info(DebugLib.getMemoryLogMessage("Memory use after GC: "))
rideHailManager ! DebugRideHailManagerDuringExecution
scheduler ! Monitor
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/beam/utils/IdGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ object ParkingManagerIdGenerator extends IdGenerator {
id.getAndIncrement()
}
}

object InterruptIdIdGenerator extends IdGenerator {
private val id: AtomicInteger = new AtomicInteger(0)

def nextId: Int = {
id.getAndIncrement()
}
}

object ReservationRequestIdGenerator extends IdGenerator {
private val id: AtomicInteger = new AtomicInteger(0)

def nextId: Int = {
id.getAndIncrement()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PersonAndTransitDriverSpec
.withFallback(testConfig("test/input/beamville/beam.conf"))
.resolve()

lazy implicit val system: ActorSystem = ActorSystem("PersonWithVehicleSharingSpec", config)
lazy implicit val system: ActorSystem = ActorSystem("PersonAndTransitDriverSpec", config)

override def outputDirPath: String = TestConfigUtils.testOutputDir

Expand Down
10 changes: 5 additions & 5 deletions src/test/scala/beam/agentsim/agents/RideHailAgentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RideHailAgentSpec
.withFallback(testConfig("test/input/beamville/beam.conf"))
.resolve()

lazy implicit val system: ActorSystem = ActorSystem("PersonWithPersonalVehiclePlanSpec", config)
lazy implicit val system: ActorSystem = ActorSystem("RideHailAgentSpec", config)

override def outputDirPath: String = TestConfigUtils.testOutputDir

Expand Down Expand Up @@ -128,7 +128,7 @@ class RideHailAgentSpec
)
)
)
rideHailAgent ! Interrupt(Id.create("1", classOf[Interrupt]), 30000)
rideHailAgent ! Interrupt(1, 30000)
expectMsgType[InterruptedWhileIdle]
rideHailAgent ! ModifyPassengerSchedule(passengerSchedule, 30000)
rideHailAgent ! Resume
Expand Down Expand Up @@ -182,7 +182,7 @@ class RideHailAgentSpec
// Now I want to interrupt the agent, and it will say that for any point in time after 28800,
// I can tell it whatever I want. Even though it is already 30000 for me.

rideHailAgent ! Interrupt(Id.create("1", classOf[Interrupt]), 30000)
rideHailAgent ! Interrupt(1, 30000)
val interruptedAt = expectMsgType[InterruptedWhileDriving]
assert(interruptedAt.currentPassengerScheduleIndex == 0) // I know this agent hasn't picked up the passenger yet
assert(rideHailAgent.stateName == DrivingInterrupted)
Expand Down Expand Up @@ -258,7 +258,7 @@ class RideHailAgentSpec
// Now I want to interrupt the agent, and it will say that for any point in time after 28800,
// I can tell it whatever I want. Even though it is already 30000 for me.

rideHailAgent ! Interrupt(Id.create("1", classOf[Interrupt]), 30000)
rideHailAgent ! Interrupt(1, 30000)
val interruptedAt = expectMsgType[InterruptedWhileDriving]
assert(interruptedAt.currentPassengerScheduleIndex == 0) // I know this agent hasn't picked up the passenger yet
assert(rideHailAgent.stateName == DrivingInterrupted)
Expand Down Expand Up @@ -341,7 +341,7 @@ class RideHailAgentSpec
t
}

rideHailAgent ! Interrupt(Id.create("1", classOf[Interrupt]), 30000)
rideHailAgent ! Interrupt(1, 30000)
val interruptedAt = expectMsgType[InterruptedWhileDriving]
assert(interruptedAt.currentPassengerScheduleIndex == 1) // I know this agent has now picked up the passenger
assert(rideHailAgent.stateName == DrivingInterrupted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AgentsimWithMaximallyBadRouterSpec

def outputDirPath: String = basePath + "/" + testOutputDir + "bad-router-test"

lazy implicit val system: ActorSystem = ActorSystem("AgentSimWithBadRouterSpec", config)
lazy implicit val system: ActorSystem = ActorSystem("AgentsimWithMaximallyBadRouterSpec", config)

"The agentsim" must {
"not get stuck even if the router only throws exceptions" in {
Expand Down

0 comments on commit a2d1189

Please sign in to comment.