コード例 #1
0
def addPassengers(index):
    global PASSENGERS
    global DIFFERENCE

    PASSENGERS = []

    fileName = FILELIST[index]

    fileDir = 'RAWDATA/TRIPRECORDSSIMULATION/' + fileName

    deployTime = datetime.datetime.now() - DIFFERENCE

    expiredTime = deployTime + datetime.timedelta(minutes=15)

    ALREADY_EXPIRED = deployTime

    for i, passenger in enumerate(PASSENGERS):
        if passenger != None:
            if (not passenger.expired and not passenger.pickedUp
                    and passenger.expiredTime <= ALREADY_EXPIRED):
                passenger.complete()
                PASSENGERS[i] = None
            elif passenger.pickedUp:
                PASSENGERS[i] = None

    with open(fileDir) as fp:
        rdr = csv.reader(fp)

        for line in rdr:
            startX = int(convX(float(line[2])))
            startY = int(convY(float(line[3])))

            endX = int(convX(float(line[4])))
            endY = int(convY(float(line[5])))

            startLocation = shpPt(startX, startY)
            dropoffLocation = shpPt(endX, endY)

            length = (
                datetime.datetime.strptime(str(line[1]), '%Y-%m-%d %H:%M:%S') -
                datetime.datetime.strptime(str(
                    line[0]), '%Y-%m-%d %H:%M:%S')).total_seconds()

            Fare = float(line[6])

            passenger = SimPassenger(startLocation, dropoffLocation,
                                     deployTime, expiredTime, Fare, length)

            PASSENGERS.append(passenger)
コード例 #2
0
def genTaxis(numTaxis):
    global TAXIS
    city = PyACO.PathFinder.genNYC()
    for i in range(numTaxis):
        pt = shpPt(-10, -10)

        while not city.contains(pt):
            x = (random.random() * 1000 + 0.5)
            y = (random.random() * 1000 + 0.5)

            pt = shpPt(x, y)

        taxi = SimTaxi(pt, getTripLength)

        TAXIS.append(taxi)
コード例 #3
0
 def getCentroidPointInPoly(poly, city):
     bounds = poly.bounds
     minX = (bounds[0])
     minY = (bounds[1])
     maxX = (bounds[2])
     maxY = (bounds[3])
     if (minX < city.bounds[0]):
         minX = city.bounds[0]
     if (minY < city.bounds[1]):
         minY = city.bounds[1]
     if (maxX > city.bounds[2]):
         maxX = city.bounds[2]
     if (maxY > city.bounds[3]):
         maxY = city.bounds[3]
     if (city.contains(poly.centroid)):
         return Graph.convertToTripClasses(poly.centroid)
     while True:
         randX = random.random() * (maxX - minX) + minX
         randY = random.random() * (maxY - minY) + minY
         pt = shpPt(randX, randY)
         if poly.contains(pt) and city.contains(pt):
             return Graph.convertToTripClasses(pt)
コード例 #4
0
 def convertToShapely(point):
     return shpPt(point.x, point.y)
コード例 #5
0
    def move(self, simTime):

        if (not self.Vacant):
            if (simTime >= self.assignedEndTime):
                self.Vacant = True
                self.Assigned = False
                self.assignedEndTime = None
                self.assignedPassenger = None

        if (self.Vacant):

            if (self.Assigned):
                if (simTime >= self.assignedEndTime):
                    self.Assigned = False
                    self.assignedEndTime = None

            if (not self.Assigned):

                if 1 == 1:
                    polies = SimTaxi.PATH.passengerPolies
                    polyIndex = Dispatch.getPolyFromPointBare(
                        polies, self.location)

                    if (polyIndex != None):
                        poly = polies[polyIndex]

                        passengersList = SimTaxi.getPassengersInPoly(poly)
                    else:
                        try:
                            ptList = [
                                [self.location.x - 25, self.location.y + 25],
                                [self.location.x + 25, self.location.y + 25],
                                [self.location.x + 25, self.location.y - 25],
                                [self.location.x - 25, self.location.y - 25]
                            ]
                            poly = Polygon(ptList)
                            passengersList = SimTaxi.getPassengersInPoly(poly)
                        except IndexError:
                            passengersList = []

                    if (len(passengersList) > 0):

                        passenger = passengersList[0]

                        self.passengerPickedUp(passenger, simTime)

                    else:
                        try:
                            polies = SimTaxi.PATH.taxiPolies
                            polyIndex = Dispatch.getPolyFromPointBare(
                                polies, self.location)
                            poly = SimTaxi.PATH.passengerPolies[
                                SimTaxi.PATH.dispatch[polyIndex]]
                        except:
                            polyIndex = int(random.random() *
                                            len(SimTaxi.PATH.passengerPolies))
                            poly = SimTaxi.PATH.passengerPolies[polyIndex]

                        passengersList = SimTaxi.getPassengersInPoly(poly)

                        polyBounds = poly.bounds

                        minX = polyBounds[0]
                        minY = polyBounds[1]
                        maxX = polyBounds[2]
                        maxY = polyBounds[3]

                        polyPoint = PyACO.PathFinder.Graph.convertToShapely(
                            PyACO.PathFinder.Graph.getCentroidPointInPoly(
                                poly, SimTaxi.City))

                        travelLength = int(
                            self.tripLengthFinder(
                                shpPt(self.location.x, self.location.y),
                                polyPoint, SimTaxi.TRIPLENGTHS) * 60)

                        searchLength = int(
                            random.random() * (self.tripLengthFinder(
                                shpPt(minX, minY), shpPt(maxX, maxY),
                                SimTaxi.TRIPLENGTHS) * 2 /
                                               (len(passengersList) + 1) * 60))

                        SimTaxi.LOG.write(
                            str(simTime) + ',' + str(self.TaxiID) + ',' +
                            str(self.Vacant) + ',' +
                            str(travelLength + searchLength) + '\n')

                        endTime = simTime + datetime.timedelta(
                            seconds=(travelLength + searchLength))

                        self.Assigned = True
                        self.location = shpPt(poly.centroid.x, poly.centroid.y)
                        self.assignedEndTime = endTime

                else:
                    try:
                        polies = SimTaxi.PATH.taxiPolies
                        polyIndex = Dispatch.getPolyFromPointBare(
                            polies, self.location)
                        poly = SimTaxi.PATH.passengerPolies[
                            SimTaxi.PATH.dispatch[polyIndex]]
                    except:
                        polyIndex = int(random.random() *
                                        len(SimTaxi.PATH.passengerPolies))
                        poly = SimTaxi.PATH.passengerPolies[polyIndex]

                    passengersList = SimTaxi.getPassengersInPoly(poly)

                    polyBounds = poly.bounds

                    minX = polyBounds[0]
                    minY = polyBounds[1]
                    maxX = polyBounds[2]
                    maxY = polyBounds[3]

                    if (minX < 0):
                        minX = 0
                    if (minY < 0):
                        minY = 0

                    if (maxX > 999):
                        maxX = 999
                    if (maxY > 999):
                        maxY = 999

                    polyPoint = PyACO.PathFinder.Graph.convertToShapely(
                        PyACO.PathFinder.Graph.getCentroidPointInPoly(
                            poly, SimTaxi.City))

                    travelLength = int(
                        self.tripLengthFinder(
                            shpPt(self.location.x, self.location.y), polyPoint,
                            SimTaxi.TRIPLENGTHS) * 60)

                    searchLength = int(
                        self.tripLengthFinder(shpPt(minX, minY),
                                              shpPt(maxX, maxY),
                                              SimTaxi.TRIPLENGTHS) * 2 /
                        (len(passengersList) + 1) * 60)

                    SimTaxi.LOG.write(
                        str(simTime) + ',' + str(self.TaxiID) + ',' +
                        str(self.Vacant) + ',' +
                        str(travelLength + searchLength) + '\n')

                    endTime = simTime + datetime.timedelta(
                        seconds=(travelLength + searchLength))

                    self.Assigned = True
                    self.location = shpPt(poly.centroid.x, poly.centroid.y)
                    self.assignedEndTime = endTime
コード例 #6
0
    def moveGreedy(self, simTime):

        if (not self.Vacant):
            if (simTime >= self.assignedEndTime):
                self.Vacant = True
                self.Assigned = False
                self.assignedEndTime = None
                self.assignedPassenger = None

        if (self.Vacant):

            if (self.Assigned):
                if (simTime >= self.assignedEndTime):
                    self.Assigned = False
                    self.assignedEndTime = None

            if (not self.Assigned):

                ptList = [[self.location.x - 25, self.location.y + 25],
                          [self.location.x + 25, self.location.y + 25],
                          [self.location.x + 25, self.location.y - 25],
                          [self.location.x - 25, self.location.y - 25]]
                poly = Polygon(ptList)

                passengersList = SimTaxi.getPassengersInPoly(poly)

                if (len(passengersList) > 0):

                    passenger = passengersList[0]

                    self.passengerPickedUp(passenger, simTime)

                else:
                    newPointX = random.random() * 151 - 76 + self.location.x
                    newPointY = random.random() * 151 - 76 + self.location.y

                    if newPointX < 0:
                        newPointX = 0
                    elif newPointX > 999:
                        newPointX = 999

                    if newPointY < 0:
                        newPointY = 0
                    elif newPointY > 999:
                        newPointY = 999

                    newPtList = [[newPointX - 25, newPointY + 25],
                                 [newPointX + 25, newPointY + 25],
                                 [newPointX + 25, newPointY - 25],
                                 [newPointX - 25, newPointY - 25]]
                    newPoly = Polygon(newPtList)

                    polyPoint = shpPt(newPointX, newPointY)

                    travelLength = int(
                        self.tripLengthFinder(
                            shpPt(self.location.x, self.location.y), polyPoint,
                            SimTaxi.TRIPLENGTHS) * 60)

                    searchLength = int(
                        self.tripLengthFinder(
                            shpPt(newPointX - 25, newPointY - 25),
                            shpPt(newPointX + 25, newPointY + 25),
                            SimTaxi.TRIPLENGTHS) * 2 /
                        (len(passengersList) + 1) * 60)

                    SimTaxi.LOG.write(
                        str(simTime) + ',' + str(self.TaxiID) + ',' +
                        str(self.Vacant) + ',' +
                        str(travelLength + searchLength) + '\n')

                    endTime = simTime + datetime.timedelta(
                        seconds=(travelLength + searchLength))

                    self.Assigned = True
                    self.location = shpPt(poly.centroid.x, poly.centroid.y)
                    self.assignedEndTime = endTime