def calcItineraryPenalty(self, plan, step): """ calculates penalty for a particular plan, at a particular time step (Stop[], int)""" penalty = 0 nPassengers = self.countPassengers currentPassengers = list(self.currentPassengers) runningTime = step for i in range(0, len(plan) - 1): o = plan[i] d = plan[i + 1] timeLapse = network.getTime(o, d) runningTime += int(timeLapse) # too early if d.serviceTime != None and runningTime < d.serviceTime: runningTime = d.serviceTime # running overtime if runningTime > self.end: return INFPENALTY # if pickup, update passengers if d.stopType == StopType.PICKUP: nPassengers += 1 currentPassengers.append(d.personID) # if dropoff, determine penalty to that passenger given dropoff # time elif d.stopType == StopType.DROPOFF: nPassengers -= 1 currentPassengers.remove(d.personID) penalty += peopleCollection.estimatePenalty(d.personID, runningTime) # if over capacity, return infinity if nPassengers > self.capacity: return INFPENALTY return penalty
def calcItineraryPenalty(self, plan, step): """ calculates penalty for a particular plan, at a particular time step (Stop[], int)""" penalty = 0 nPassengers = self.countPassengers currentPassengers = list(self.currentPassengers) runningTime = step for i in range(0, len(plan)-1): o = plan[i] d = plan[i+1] timeLapse = network.getTime(o,d) runningTime += int(timeLapse) # too early if d.serviceTime != None and runningTime < d.serviceTime: runningTime = d.serviceTime # running overtime if runningTime > self.end: return INFPENALTY # if pickup, update passengers if d.stopType == StopType.PICKUP: nPassengers += 1 currentPassengers.append(d.personID) # if dropoff, determine penalty to that passenger given dropoff # time elif d.stopType == StopType.DROPOFF: nPassengers -= 1 currentPassengers.remove(d.personID) penalty += peopleCollection.estimatePenalty(d.personID, runningTime) # if over capacity, return infinity if nPassengers > self.capacity: return INFPENALTY return penalty
def setDirectTime(self): """ set direct time based on network """ self.directTime = network.getTime(self.origin, self.destination)
def update(self, step): """ update plan at a time step (int)""" global peopleCollection # determine current position self.currentLink = network.getVehicleCurrentEdge(self.name) self.currentPos = network.getVehicleCurrentPosition(self.name) # distance travelled if self.lastLink != None: # has started moving # if has moved since last update, ignore if vehicle parked if ( not (self.lastLink == self.currentLink and self.lastPos == self.currentPos) or self.currentStatus != VehicleStatus.PARKED ): distMoved = network.getDistanceLong(self.lastLink, self.lastPos, self.currentLink, self.currentPos) if distMoved < LARGEDIST: # ignore large numbers self.totalDistance += distMoved # update passengers for p in self.currentPassengers: peopleCollection.incrementPersonDistance(p, distMoved) # update shared/deadheading if self.countPassengers >= 2: self.shared += distMoved if self.countPassengers == 0: self.deadheading += distMoved nextUpdate = self.getNextStop() # if at target and not depot; "at target" considered to be within # STOP_OFFSET of actual target, otherwise bus crowding delays passengers # unnecessarily while ( nextUpdate != None and nextUpdate.stopType != StopType.DEPOT and nextUpdate.link == self.currentLink and nextUpdate.pos - self.currentPos < STOP_OFFSET ): print step, self.name, nextUpdate.link, nextUpdate.pos, nextUpdate.stopType, self.currentLink, self.currentPos, nextUpdate.serviceTime # if target is a pickup if nextUpdate.stopType == StopType.PICKUP: peopleCollection.updatePersonPickup(nextUpdate.personID, step) self.countPassengers += 1 self.currentStatus = VehicleStatus.BOOKED self.currentPassengers.append(nextUpdate.personID) print self.name, self.countPassengers, " on board" nextUpdate = self.getNextStop(True) # else if target is a dropoff elif nextUpdate.stopType == StopType.DROPOFF: self.countPassengers -= 1 peopleCollection.updatePersonDropoff(nextUpdate.personID, step) self.totalPassengers += 1 self.currentPassengers.remove(nextUpdate.personID) self.occupancyTime += peopleCollection.getTravelTime(nextUpdate.personID) print self.name, self.countPassengers, " on board" nextUpdate = self.getNextStop(True) # update route if nextUpdate != None: if nextUpdate.stopType == StopType.DEPOT: # no future requests at this stage if self.operatingState == VehicleState.vsRunning: assert self.countPassengers == 0 currentStop = self.getCurrentPos() goHomeTime = self.end - network.getTime(currentStop, nextUpdate) if step < goHomeTime: # not time to go home yet if self.currentStatus != VehicleStatus.PARKED: self.currentStatus = VehicleStatus.PARKED print self.name, "waiting", goHomeTime self.parkedLink = self.currentLink self.parkedPos = self.currentPos + PARK_OFFSET # !! was 60+5*self.i # if many vehicles and few set stops, then parking # somewhere else reduces delay to other vehicles traci.vehicle.setStop( self.name, self.parkedLink, self.parkedPos, 0, (goHomeTime - step) * MILLISECONDS ) else: # raise from parking position and go home traci.vehicle.setStop(self.name, self.parkedLink, self.parkedPos, 0, 0) self.operatingState = VehicleState.vsGoingHome traci.vehicle.changeTarget(self.name, nextUpdate.link) self.currentStatus = VehicleStatus.BOOKED print self.name, "moving to depot" # elif self.operatingState == VehicleState.vsNotStarted: # waiting at start else: # update route for next pickup/dropoff traci.vehicle.changeTarget(self.name, nextUpdate.link) traci.vehicle.setStop(self.name, nextUpdate.link, nextUpdate.pos, 0, DWELLTIME) if self.currentStatus == VehicleStatus.PARKED: # raise from parking position if parked traci.vehicle.setStop(self.name, self.parkedLink, self.parkedPos, 0, 0) self.currentStatus = VehicleStatus.BOOKED # store last known link self.lastLink = self.currentLink self.lastPos = self.currentPos
def update(self, step): """ update plan at a time step (int)""" global peopleCollection # determine current position self.currentLink = network.getVehicleCurrentEdge(self.name) self.currentPos = network.getVehicleCurrentPosition(self.name) # distance travelled if self.lastLink != None: # has started moving # if has moved since last update, ignore if vehicle parked if not(self.lastLink == self.currentLink and \ self.lastPos == self.currentPos) or \ self.currentStatus != VehicleStatus.PARKED: distMoved = network.getDistanceLong(self.lastLink, self.lastPos, self.currentLink, self.currentPos) if distMoved < LARGEDIST: # ignore large numbers self.totalDistance += distMoved # update passengers for p in self.currentPassengers: peopleCollection.incrementPersonDistance(p, distMoved) # update shared/deadheading if self.countPassengers >= 2: self.shared += distMoved if self.countPassengers == 0: self.deadheading += distMoved nextUpdate = self.getNextStop() # if at target and not depot; "at target" considered to be within # STOP_OFFSET of actual target, otherwise bus crowding delays passengers # unnecessarily while nextUpdate != None and nextUpdate.stopType != StopType.DEPOT and \ nextUpdate.link == self.currentLink and \ nextUpdate.pos - self.currentPos < STOP_OFFSET: print step, self.name, nextUpdate.link, nextUpdate.pos, \ nextUpdate.stopType, self.currentLink, self.currentPos, \ nextUpdate.serviceTime # if target is a pickup if nextUpdate.stopType == StopType.PICKUP: peopleCollection.updatePersonPickup(nextUpdate.personID, step) self.countPassengers += 1 self.currentStatus = VehicleStatus.BOOKED self.currentPassengers.append(nextUpdate.personID) print self.name, self.countPassengers, " on board" nextUpdate = self.getNextStop(True) # else if target is a dropoff elif nextUpdate.stopType == StopType.DROPOFF: self.countPassengers -= 1 peopleCollection.updatePersonDropoff(nextUpdate.personID, step) self.totalPassengers += 1 self.currentPassengers.remove(nextUpdate.personID) self.occupancyTime += \ peopleCollection.getTravelTime(nextUpdate.personID) print self.name, self.countPassengers, " on board" nextUpdate = self.getNextStop(True) # update route if nextUpdate != None: if nextUpdate.stopType == StopType.DEPOT: # no future requests at this stage if self.operatingState == VehicleState.vsRunning: assert self.countPassengers == 0 currentStop = self.getCurrentPos() goHomeTime = self.end - network.getTime(currentStop, nextUpdate) if step < goHomeTime: # not time to go home yet if self.currentStatus != VehicleStatus.PARKED: self.currentStatus = VehicleStatus.PARKED print self.name, "waiting", goHomeTime self.parkedLink = self.currentLink self.parkedPos = self.currentPos + PARK_OFFSET # !! was 60+5*self.i # if many vehicles and few set stops, then parking # somewhere else reduces delay to other vehicles traci.vehicle.setStop(self.name, self.parkedLink, self.parkedPos, 0, (goHomeTime - step)*MILLISECONDS) else: # raise from parking position and go home traci.vehicle.setStop(self.name, self.parkedLink, self.parkedPos, 0, 0) self.operatingState = VehicleState.vsGoingHome traci.vehicle.changeTarget(self.name, nextUpdate.link) self.currentStatus = VehicleStatus.BOOKED print self.name, "moving to depot" #elif self.operatingState == VehicleState.vsNotStarted: # waiting at start else: # update route for next pickup/dropoff traci.vehicle.changeTarget(self.name, nextUpdate.link) traci.vehicle.setStop(self.name, nextUpdate.link, nextUpdate.pos, 0, DWELLTIME) if self.currentStatus == VehicleStatus.PARKED: # raise from parking position if parked traci.vehicle.setStop(self.name, self.parkedLink, self.parkedPos, 0, 0) self.currentStatus = VehicleStatus.BOOKED # store last known link self.lastLink = self.currentLink self.lastPos = self.currentPos