Beispiel #1
0
def processRequest():

    global taxi
    recieved = request.args

    acoVal = {'currentLoc': taxi['currentLoc'],'newReq':{'src': [float(recieved['src_lat']),float(recieved['src_lon'])],'dest': [float(recieved['dest_lat']),float(recieved['dest_lon'])], "name":recieved["name"]},'passengers': taxi['passenger']}

    global broadcastDone

    if(taxi['noPassengers'] >= 4):
        if broadcastDone == 1:
        	broadcastDone = 0
        	return (json.dumps({'id':taxi['id'], 'port': 5000, 'cost':-1}))
        broadcastDone = 1
        print("Passing to my neighbor because i have 4 passengers")
        return(json.dumps(broadcastToAllPeers(acoVal['newReq'])))

    dataRecv = aco.acoData(acoVal)

    global cost 

    cost = dataRecv['cost']

    #return value to passenger
    retVal = {'id': taxi['id'], 'port':5002, 'cost': cost}


    if(cost != -1):
       global route 
       route = [taxi["currentLoc"]]+dataRecv['route']

    else:
        if broadcastDone == 1:
            broadcastDone = 0
            return(json.dumps(retVal))
        broadcastDone = 1
        print("Passing to my neighbor because of Passenger waiting Time is more")
        return(json.dumps(broadcastToAllPeers(acoVal['newReq'])))

    
    print("cost for taxi:",cost," and taxis current loc:",taxi["currentLoc"]) 

    global direction

    startPoint = route[0]
    direction = []
    names_src = []
    names_dst = []

    pass_array = taxi["passenger"]+[{'src': [float(recieved['src_lat']),float(recieved['src_lon'])],'dest': [float(recieved['dest_lat']),float(recieved['dest_lon'])],"name":recieved["name"]}]
    for i in route[1:]:
        path = giveRegionsPassed(startPoint,i)[:-1]
        res, name = check_src(i,names_src,pass_array)
        if(res):
           path[len(path)-1]['src'] = 1
           path[len(path)-1]['name'] = name
           names_src.append(name)
        res, name = check_dest(i,names_dst,pass_array)
        if(res):
           path[len(path)-1]['dest'] = 1
           path[len(path)-1]['name'] = name
           names_dst.append(name)
        direction += path
        startPoint = i
    

    #check if passenger waiting time of all other current passengers changes a lot then broadcast request to other peers
    if(check_pwtime_constraint() == False):
        if broadcastDone == 1:
        	broadcastDone = 0
        	return(json.dumps(retVal))
        broadcastDone = 1
        print("Passing to my neighbor because of Passenger Waiting Time of other customer(s)")
        return(json.dumps(broadcastToAllPeers(acoVal['newReq'])))

    if(check_dsttime_constraint()==False):
        if broadcastDone == 1:
        	broadcastDone = 0
        	return(json.dumps(retVal))
        broadcastDone = 1
        print("Passing to my neighbor because of Passenger Destination Time of other customer(s)")
        return(json.dumps(broadcastToAllPeers(acoVal['newReq'])))

    retVal = {'id': taxi['id'], 'port':5002, 'cost': cost}
    #print("Prev cost and now cost",prevcost,cost,abs(prevcost - cost))
    return(json.dumps(retVal))
Beispiel #2
0
def processRequest():

    global taxi
    global request_in_process
    global no_messages
    global request_queue

    request_queue.append({
        "timestamp":
        datetime.datetime.now().strftime("%H:%M:%S"),
        "type":
        "getRequest"
    })

    recieved = request.args
    if ((datetime.datetime.strptime(
            datetime.datetime.now().strftime("%H:%M:%S"), "%H:%M:%S") -
         (datetime.datetime.strptime(recieved["timestamp"], "%H:%M:%S"))
         ).total_seconds() > int(params["request_timers"])):
        return json.dumps({
            'id': taxi['id'],
            'port': int(taxi_id),
            'cost': -1,
            "message": "7.queued reaching taxi late"
        })

    broadcast_taxis = recieved["all_broadcasted_taxis"].split(" ")

    acoVal = {
        'currentLoc': taxi['currentLoc'],
        'newReq': {
            'src': [float(recieved['src_lat']),
                    float(recieved['src_lon'])],
            'dest': [float(recieved['dest_lat']),
                     float(recieved['dest_lon'])],
            "name": recieved["name"],
            "all_broadcasted_taxis": broadcast_taxis,
            'timestamp': recieved["timestamp"]
        },
        'passengers': taxi['passenger']
    }

    global cost
    '''
    check if taxi is waiting for someone other passenger's confirmation
    '''

    if (request_in_process["status"] == True):
        return (json.dumps({
            'id': taxi['id'],
            'port': int(taxi_id),
            'cost': -1,
            'message': "1.waiting for others pass confirmation"
        }))

    if (taxi['noPassengers'] >= 4):
        if 'first_hop' in request.args:
            return (json.dumps({
                'id': taxi['id'],
                'port': int(taxi_id),
                'cost': -1,
                "message": "2.capacity"
            }))
        else:
            print("Passing to my neighbor because I have 4 passengers")
            return (json.dumps(broadcastToAllPeers(acoVal['newReq'])))

    dataRecv = aco.acoData(acoVal, pheromone)

    #here cost is only total travel distance
    recv_cost = dataRecv['cost']

    #return value to passenger
    retVal = {
        'id': taxi['id'],
        'port': int(taxi_id),
        'cost': recv_cost,
        'detour_obj': 0,
        'time_obj': 0
    }

    if (recv_cost != -1):

        #use these values for setting taxi['total_dist'] and taxi['total_time'] after request confirms
        global direction_dist
        global direction_time

        direction_dist = recv_cost
        direction_time = dataRecv['cost_time']

        global detour_cost
        global tot_time_cost
        '''
       #computing detour time for the new request - use this when 
       #objective function for peak hours changes
       if(taxi['noPassengers'] == 0):
            detour_time = giveRegionsPassed(taxi['currentLoc'], dataRecv['route'][0], return_time = True)
       else:
            detour_time = abs(dataRecv['cost_time'] - taxi['total_time'])

       if detour_time != 0.0:
            cost = math.sqrt(detour_time**2+dataRecv['cost_time']**2)
       else:
            cost =  math.sqrt(0.001**2+dataRecv['cost_time']**2)

       print("Detour time for taxi",int(taxi['id']),"= ",detour_time)

       retVal["detour_obj"] =  detour_time
       retVal["time_obj"] = dataRecv['cost_time']
       '''

        #not sure if absolute is necessary
        if (taxi['noPassengers'] == 0):
            detour_distance = giveRegionsPassed(taxi['currentLoc'],
                                                dataRecv['route'][0],
                                                return_dist=True)
        else:
            detour_distance = abs(recv_cost - taxi['total_dist'])

        if detour_distance != 0.0:
            cost = math.sqrt(detour_distance**2 + dataRecv['cost_time']**2)
        else:
            cost = math.sqrt(0.001**2 + dataRecv['cost_time']**2)

        print("Detour distance for taxi", int(taxi['id']), "= ",
              detour_distance, " and time = ", dataRecv['cost_time'])

        retVal["detour_obj"] = detour_distance
        retVal["time_obj"] = dataRecv['cost_time']

        global route
        route = [taxi["currentLoc"]] + dataRecv['route']

    else:
        retVal["message"] = "3.Your PWT is more"
        return (json.dumps(retVal))

    print("cost for taxi", taxi['id'], ":", cost, " and taxis current loc:",
          taxi["currentLoc"])

    global direction

    startPoint = route[0]
    direction = []
    names_src = []
    names_dst = []

    pass_array = taxi["passenger"] + [{
        'src': [float(recieved['src_lat']),
                float(recieved['src_lon'])],
        'dest': [float(recieved['dest_lat']),
                 float(recieved['dest_lon'])],
        "name":
        recieved["name"],
        "picked":
        0
    }]
    for i in route[1:]:
        path = giveRegionsPassed(startPoint, i)[:-1]
        res, name = check_src(i, names_src, pass_array)
        if (res):
            path[len(path) - 1]['src'] = 1
            path[len(path) - 1]['name'] = name
            names_src.append(name)
        else:
            res, name = check_dest(i, names_dst, names_src, pass_array)
            if (res):
                path[len(path) - 1]['dest'] = 1
                path[len(path) - 1]['name'] = name
                names_dst.append(name)
        direction += path
        startPoint = i

    #return value to passenger
    retVal = {
        'id': taxi['id'],
        'port': int(taxi_id),
        'cost': cost,
        "detour_obj": detour_distance,
        "time_obj": dataRecv['cost_time']
    }

    #check if passenger waiting time of all other current passengers changes a lot then broadcast request to other peers
    if (check_pwtime_constraint() == False):
        retVal["message"] = "4.PWT of other passenger"
        retVal["cost"] = -1
        return (json.dumps(retVal))

    if (check_dsttime_constraint() == False):
        retVal["message"] = "5. DRT of other passenger"
        retVal["cost"] = -1
        return (json.dumps(retVal))

    if (taxi["noPassengers"] > 0):
        global t1
        if (t1.is_alive):
            t1.cancel()
    start_confirm_timer(recieved["name"])
    retVal["message"] = "6. success"
    return (json.dumps(retVal))