Esempio n. 1
0
        def DistancePriorityPickup(c, meetingCount):
            '''
            param c: Taxi id.
            if success, taxi will have non 0 passengerid, path, i=0
            '''

            InfVeh = AKIVehTrackedGetInf(c)
            currentpos = str(InfVeh.idSection)
            steps = []
            # a tuple list storing (no.of steps needed to get passenger, passenger id )

            for d in range(1, len(Passengerlist)):
                if Passengerlist[d] == None:
                    continue
                if Passengerlist[d].assigned == 0:
                    path = na.dijkstra_path_length(G, currentpos, Passengerlist[d].origin)
                    steps.append((path, d))

            steps = sorted(steps, key=lambda x: (x[0], x[1]))
            # sorted by : firstly, smallest steps to get,
            # secondly passenger id (if steps equal, first come first serve)
            if len(steps) > 0:
                d = steps[0][1]
                Taxilist[c].passengerid = d
                Passengerlist[d].assigned = 1

                Taxilist[
                    c].origin = currentpos  # since after drop off, taxi's place is actually its destination. Hence new origin = its currentplace.
                Taxilist[c].destination = Passengerlist[Taxilist[c].passengerid].origin

                if Taxilist[c].origin == Taxilist[
                    c].destination:  # if the taxi happen to be on the same section as the passenger
                    Passengerlist[Taxilist[c].passengerid].pickuptime = Taxilist[c].stoptime
                    Taxilist[c].destination = Passengerlist[Taxilist[c].passengerid].destination
                    Taxilist[c].occupy = 1
                    meetingCount += 1

                    Passengerlist[Taxilist[c].passengerid].take = 1
                    Taxilist[c].path = na.dijkstra_path(G, str(Taxilist[c].origin), str(
                        Taxilist[c].destination))  # go to passenger's destination
                    Taxilist[c].i = 0
                else:
                    Taxilist[c].path = na.dijkstra_path(G, str(Taxilist[c].origin), str(
                        Taxilist[c].destination))  # go to passenger's origin
                    Taxilist[c].i = 0

            else:
                Taxilist[c].passengerid = 0
                Taxilist[c].path = []
                Taxilist[c].origin = 0
                Taxilist[c].destination = 0
                print(
                    '................................Currently no waiting passenger to assign for taxi :',
                    c)
def AAPIEnterVehicle(idveh, idsection):
    #print('Car enters with', idveh)
    import networkx.algorithms as na
    import traceback
    try:
        global CARS
        #and str(idsection) in secIDs
        if len(
                Taxilist
        ) <= CARS:  #only introduce tracked vehicle when it enters connected G.

            AKIVehSetAsTracked(idveh)
            InfVeh = AKIVehTrackedGetInf(idveh)

            if str(InfVeh.idSection) not in list(G.nodes):
                #print('Taxi coming from non SCP, have to throw it away')
                AKIVehSetAsNoTracked(idveh)
            else:
                #print ("Car", idveh, "is tracked")

                #occupy, passengerid, origin, destination, path, stoptime, firststop, stopping, occupied_distance, empty_distance, occupied_time, sectionlength, midsection, noofpath, i, droptime

                Taxi_current_pos = str(InfVeh.idSection)
                #print('Current IDsection,', Taxi_current_pos)
                steps = []
                # a tuple list storing (no.of steps needed to get passenger, passenger id )

                for d in range(0, numberofpassenger):
                    if Passengerlist[d].take == 0:

                        steps.append(
                            (na.dijkstra_path_length(G, Taxi_current_pos,
                                                     Passengerlist[d].origin),
                             d))

                steps = sorted(steps, key=lambda x: (x[0], x[1]))
                # sorted by : firstly, smallest steps to get,
                # secondly passenger id (if steps equal, first come first serve)
                if len(steps) > 0:
                    d = steps[0][1]
                    Taxilist[idveh] = Taxi(0, 0, 0, 0, 0, 0, True, False, 0, 0,
                                           0, 0, 0, 0, 0, 0)
                    Taxilist[idveh].passengerid = d
                    Passengerlist[d].take = 1
                    print(idveh, "got passenger", d)

                else:
                    AKIVehSetAsNoTracked(idveh)

    except Exception:
        traceback.print_exc()

    return 0
                        def DistancePriorityPickup():

                            steps = []
                            # a tuple list storing (no.of steps needed to get passenger, passenger id )

                            for d in range(0, numberofpassenger):
                                if Passengerlist[d].take == 0:
                                    steps.append((na.dijkstra_path_length(
                                        G, Taxilist[c].destination,
                                        Passengerlist[d].origin), d))

                            steps = sorted(steps, key=lambda x: (x[0], x[1]))
                            # sorted by : firstly, smallest steps to get,
                            # secondly passenger id (if steps equal, first come first serve)
                            if len(steps) > 0:
                                d = steps[0][1]
                                Taxilist[c].passengerid = d
                                Passengerlist[d].take = 1
Esempio n. 4
0
def AAPIEnterVehicle(idveh, idsection):
    #print('Car enters with', idveh)
    import networkx.algorithms as na
    import traceback
    try:
        global CARS, waitingTimeSteps, vacantTimeSteps, clock
        #and str(idsection) in secIDs

        if len(
                Taxilist
        ) <= CARS or clock % 120 == 0:  # and not CARcondition : #only introduce tracked vehicle when it enters connected G.

            AKIVehSetAsTracked(idveh)
            InfVeh = AKIVehTrackedGetInf(idveh)

            if str(InfVeh.idSection) not in list(G.nodes):
                #print('Taxi coming from non SCP, have to throw it away')
                AKIVehSetAsNoTracked(idveh)
            else:

                Taxi_current_pos = str(InfVeh.idSection)

                steps = []
                # a tuple list storing (no.of steps needed to get passenger, passenger id )

                for d in range(1, len(Passengerlist)):
                    if Passengerlist[d] == None:
                        continue
                    if Passengerlist[d].assigned == 0:
                        steps.append(
                            (na.dijkstra_path_length(G, Taxi_current_pos,
                                                     Passengerlist[d].origin),
                             d))

                #print(steps)
                if len(steps) == 0:
                    Taxilist[idveh] = Taxi(0, 0, 0, 0, 0, 0, True, False, 0, 0,
                                           0)
                    Taxilist[idveh].path = []
                    print(
                        idveh,
                        "initialized, does not have unassigned waiting passenger"
                    )

                else:
                    steps = sorted(steps, key=lambda x:
                                   (x[0], x[1]))  #distance priority pick up
                    # sorted by : firstly, smallest steps to get,
                    # secondly passenger id (if steps equal, first come first serve)
                    if len(steps) > 0:
                        d = steps[0][1]
                        Taxilist[idveh] = Taxi(0, 0, 0, 0, 0, 0, True, False,
                                               0, 0, 0)
                        Taxilist[idveh].passengerid = d
                        Passengerlist[d].assigned = 1
                        Taxilist[idveh].origin = str(InfVeh.idSection)
                        Taxilist[idveh].destination = str(
                            Passengerlist[Taxilist[idveh].passengerid].origin)
                        Taxilist[idveh].path = na.dijkstra_path(
                            G, Taxilist[idveh].origin,
                            Taxilist[idveh].destination)
                        #print(idveh, "is assigned to passenger", d, 'at section', Passengerlist[d].origin, 'with path', Taxilist[idveh].path)

            if idveh not in Taxilist.keys():
                AKIVehSetAsNoTracked(idveh)

    except Exception:
        traceback.print_exc()

    return 0
def AAPIManage(time, timeSta, timeTrans, acycle):
    #print('Current Manage step -------------------------------------------------------------------')
    import traceback, random
    import networkx.algorithms as na
    try:
        global clock, numberofpassenger, Passengerlist, Taxilist, CARS, clock2, accumulation, numbersinsection, numbersinjunction, astring, meeting, \
            meetingCount, waitNumber, vacant, waitAccumulation, vacantAccumulation
        global counter

        numberofpassenger = len(Passengerlist)

        wait = 0
        for s in range(0, numberofpassenger):
            if Passengerlist[s].take == 0:
                wait = wait + 1
        print('Number of passenger waiting: ', wait)

        vacant = 0
        for c in Taxilist:

            if Taxilist[c].occupy == 0:
                vacant += 1
        print('Number of vacant taxi: ', vacant)
        waitAccumulation += wait
        vacantAccumulation += vacant

        if clock == 10:  #introduces new passenger every 90 sec.

            start = random.choice(secIDs)
            while (True):
                end = random.choice(secIDs)
                if start != end:
                    break
            appeartime = AKIGetCurrentSimulationTime()  # get current time.
            Passengerlist.append(Passenger(start, end, 0, appeartime, 0, 0, 0))
            clock = 0

        else:
            clock = clock + 1
        #print clock

        if clock2 == 90:
            print('In this 900sec, there were ', waitAccumulation,
                  'passengers were waiting and there were ',
                  vacantAccumulation, 'Became vacant')
            meeting.append([
                meetingCount, wait, vacant, waitAccumulation,
                vacantAccumulation
            ])
            waitAccumulation = 0
            vacantAccumulation = 0
            meetingCount = 0
            clock2 = 0
        else:
            clock2 = clock2 + 1

        #print('We have ----------------------- Taxi.taxiCount:',Taxi.taxiCount)
        for c in Taxilist:

            if Taxilist[c].path == 0:
                '''
                Make this vacant taxi go to the passenger.
                '''
                InfVeh = AKIVehTrackedGetInf(c)
                '''
                AKIVehTrackedGetInf: Read the informaiton of a tracked vehicle.
                '''
                Taxilist[c].origin = str(InfVeh.idSection)
                Taxilist[c].destination = str(
                    Passengerlist[Taxilist[c].passengerid].origin)
                #print('Taxi origin:', Taxilist[c].origin)
                #print('Taxi destination:', Taxilist[c].destination)
                Taxilist[c].path = na.dijkstra_path(G, Taxilist[c].origin,
                                                    Taxilist[c].destination)

                Taxilist[c].currentPathLength = na.dijkstra_path_length(
                    G, Taxilist[c].origin, Taxilist[c].destination)

                Taxilist[c].vacant_distance.append(
                    Taxilist[c].currentPathLength)
            '''
            idSection = 32766 because it exceeded visible network. 
            '''

            #print('Taxi',c,' has Current Section ID', InfVeh.idSection, 'its next step is', weirdValue)

            AKIVehTrackedModifyNextSection(
                c, int(Taxilist[c].path[
                    Taxilist[c].i]))  #Vechicle id starting from 1 instead of 0

            #get the information of the vehicle again.
            InfVeh = AKIVehTrackedGetInf(c)

            if InfVeh.idSection == int(Taxilist[c].path[Taxilist[c].i]):

                #Taxilist[c].noofpath=len(Taxilist[c].path)  #length of all steps

                if Taxilist[c].i < len(
                        Taxilist[c].path) - 1:  #if number of steps < all steps

                    Taxilist[c].i = Taxilist[
                        c].i + 1  #move on to next step. This is incremented every for step.
                    AKIVehTrackedModifyNextSection(
                        c, int(Taxilist[c].path[
                            Taxilist[c].i]))  #move on to next step

            InfVeh = AKIVehTrackedGetInf(c)
            '''
            Passenger pick up.
            '''
            if InfVeh.idSection == int(Taxilist[c].destination):

                Taxilist[c].i = 0

                A2KSectionInf = AKIInfNetGetSectionANGInf(
                    int(Taxilist[c].destination))
                Taxilist[c].sectionlength = A2KSectionInf.length
                Taxilist[c].midsection = Taxilist[c].sectionlength * 0.5

                if Taxilist[c].midsection - 10 < InfVeh.distance2End < Taxilist[
                        c].midsection + 10:

                    Taxilist[
                        c].stopping = True  #stop it on the midsection, as if the passenger emerges at midsection.

                if Taxilist[c].stopping == True:

                    if Taxilist[
                            c].firststop == True:  #only record the first stopping time-- otherwise this value could be re-write up to 5sec.

                        Taxilist[c].stoptime = AKIGetCurrentSimulationTime(
                        )  #current time.
                        Taxilist[c].firststop = False
                    AKIVehTrackedModifySpeed(c, 0)  #temporary stop

                print(
                    'Taxi ', c, "has its passenger ", Taxilist[c].passengerid,
                    " now on trip to ",
                    Passengerlist[Taxilist[c].passengerid].destination
                )  # have taken the passenger, then send the passenger to his destination

                if time - Taxilist[c].stoptime > 5 and Taxilist[
                        c].stopping == True:  #if taxi restarts.
                    #current senario: taxi is stopped.
                    #Decide if it is a drop off or pick up senario.

                    Taxilist[c].stopping = False
                    Taxilist[
                        c].firststop = True  #change firststop back until next time the taxi stops.
                    counter = counter + 1

                    print('Taxi', c, 'should Passenger that has origin ',
                          Passengerlist[Taxilist[c].passengerid].origin,
                          'and destination',
                          Passengerlist[Taxilist[c].passengerid].destination)
                    '''
                    A searching and pickup senario: taxi reaching for passenger: taxi's destination = current passenger's origin.
                    A taxi sending passenger reaching its destination : taxi's destinations = current passenger's destination.
                    '''
                    Pickup = Taxilist[c].destination == Passengerlist[
                        Taxilist[c].passengerid].origin
                    Dropoff = Taxilist[c].destination == Passengerlist[
                        Taxilist[c].passengerid].destination

                    if Pickup:

                        Passengerlist[Taxilist[
                            c].passengerid].pickuptime = Taxilist[c].stoptime
                        #Taxilist[c].noofpath=len(Taxilist[c].path)

                        Taxilist[c].empty_distance = Taxilist[
                            c].currentPathLength
                        '''
                        vacant_time, a list. 
                        =: the taxi's drop off time of the LAST passenger) - (the taxi's pick up time of the NEXT passenger)
                        '''
                        Taxilist[c].vacant_time.append(
                            Passengerlist[Taxilist[c].passengerid].pickuptime -
                            Taxilist[c].droptime)
                        Taxilist[c].occupy = 1
                        Taxilist[c].origin = Passengerlist[
                            Taxilist[c].passengerid].origin
                        Taxilist[c].destination = Passengerlist[
                            Taxilist[c].passengerid].destination

                        Taxilist[c].path = na.dijkstra_path(
                            G, str(Taxilist[c].origin),
                            str(Taxilist[c].destination))

                        print(
                            'Taxi ', c, "has picked up",
                            Taxilist[c].passengerid, "on desination to",
                            Taxilist[c].destination
                        )  # have arrived the destination of the passenger and then go to take the next passenger
                        meetingCount += 1

                    elif Dropoff:

                        Passengerlist[
                            Taxilist[c].passengerid].dropofftime = Taxilist[
                                c].stoptime  #current time
                        Taxilist[c].droptime = Taxilist[c].stoptime  #
                        #Taxilist[c].noofpath=len(Taxilist[c].path)
                        Taxilist[c].occupied_distance = Taxilist[
                            c].occupied_distance + Taxilist[c].currentPathLength
                        Passengerlist[Taxilist[
                            c].passengerid].distance_travelled = Taxilist[
                                c].currentPathLength
                        Taxilist[c].occupy = 0  #becomes vacant

                        Taxilist[c].occupied_time = Taxilist[c].occupied_time + (
                            Passengerlist[Taxilist[c].passengerid].dropofftime
                            -
                            Passengerlist[Taxilist[c].passengerid].pickuptime)

                        #accummulate occupied time.

                        def SequentialPickup():
                            for d in range(0, numberofpassenger):
                                if Passengerlist[
                                        d].take == 0:  # subsequently take on the next waiting passenger on the given list.
                                    Taxilist[c].passengerid = d
                                    Passengerlist[d].take = 1
                                    break

                        def DistancePriorityPickup():

                            steps = []
                            # a tuple list storing (no.of steps needed to get passenger, passenger id )

                            for d in range(0, numberofpassenger):
                                if Passengerlist[d].take == 0:
                                    steps.append((na.dijkstra_path_length(
                                        G, Taxilist[c].destination,
                                        Passengerlist[d].origin), d))

                            steps = sorted(steps, key=lambda x: (x[0], x[1]))
                            # sorted by : firstly, smallest steps to get,
                            # secondly passenger id (if steps equal, first come first serve)
                            if len(steps) > 0:
                                d = steps[0][1]
                                Taxilist[c].passengerid = d
                                Passengerlist[d].take = 1

                        #SequentialPickup()
                        DistancePriorityPickup()
                        Taxilist[c].origin = Taxilist[
                            c].destination  #since after drop off, taxi's place is actually its destination. Hence new origin = its currentplace.
                        Taxilist[c].destination = Passengerlist[
                            Taxilist[c].passengerid].origin

                        if Taxilist[c].origin == Taxilist[
                                c].destination:  #if the taxi happen to be on the same section as the passenger
                            Passengerlist[
                                Taxilist[c].
                                passengerid].pickuptime = Taxilist[c].stoptime
                            Taxilist[c].vacant_time.append(0)
                            Taxilist[c].vacant_distance.append(0)
                            Taxilist[c].destination = Passengerlist[
                                Taxilist[c].passengerid].destination
                            Taxilist[c].path = na.dijkstra_path(
                                G, str(Taxilist[c].origin),
                                str(Taxilist[c].destination)
                            )  #go to passenger's destination
                        else:
                            Taxilist[c].path = na.dijkstra_path(
                                G, str(Taxilist[c].origin),
                                str(Taxilist[c].destination)
                            )  #go to passenger's origin
                            Taxilist[c].vacant_distance.append(
                                Taxilist[c].currentPathLength)

    except Exception:
        traceback.print_exc()

    return 0