Пример #1
0
def handle_incoming_mp_multi_user_request(request, iteration=1):

    # For demo purpose, only send back one prediction result by commenting the timer for the next prediction
    # Timer(int(request[3])*60, handle_incoming_mp_multi_user_request, [request, iteration+1]).start()

    #err.log_error(err.INFO, "Using MP multi-user algorithm, iteration %d" % iteration)
    start_time = timeit.default_timer()

    current_time_in_min = sum(
        int(x) * 60**i for i, x in enumerate(reversed(request[1].split(':'))))
    next_time_in_min = current_time_in_min + (iteration * int(request[3]))
    next_time_formatted = '{:02d}:{:02d}'.format(*divmod(next_time_in_min, 60))

    if next_time_in_min > 1440 - int(request[3]):
        return

    result = mup.mp(int(request[0]), next_time_formatted, request[2],
                    int(request[3]))

    mu_predictions = []

    for k in result.keys():
        (user_id, current_cell_id, next_time) = k
        probabilities = result[k]
        predictions = []
        for p in probabilities:
            (cell_id, probability) = p
            predictions.append([cell_id, probability])
        mu_prediction = [user_id, current_cell_id, next_time, predictions]
        mu_predictions.append(mu_prediction)

    # Send answer
    send_mu_message(request[4], mu_predictions)
Пример #2
0
def handle_incoming_mp_multi_user_request(request, iteration=1):

    # For demo purpose, only send back one prediction result by commenting the timer for the next prediction
    # Timer(int(request[3])*60, handle_incoming_mp_multi_user_request, [request, iteration+1]).start()

    #err.log_error(err.INFO, "Using MP multi-user algorithm, iteration %d" % iteration)
    start_time = timeit.default_timer()

    current_time_in_min = sum(int(x) * 60 ** i for i,x in enumerate(reversed(request[1].split(':'))))
    next_time_in_min = current_time_in_min + (iteration * int(request[3]))
    next_time_formatted = '{:02d}:{:02d}'.format(*divmod(next_time_in_min, 60))

    if next_time_in_min > 1440 - int(request[3]):
        return;

    result = mup.mp(int(request[0]), next_time_formatted, request[2], int(request[3]))

    mu_predictions = []

    for k in result.keys():
        (user_id, current_cell_id, next_time) = k
        probabilities = result[k]
        predictions = []
        for p in probabilities:
            (cell_id, probability) = p
            predictions.append([cell_id, probability])
        mu_prediction = [user_id, current_cell_id, next_time, predictions]
        mu_predictions.append(mu_prediction)

    # Send answer
    send_mu_message(request[4], mu_predictions)
Пример #3
0
def handle_incoming_mp_multi_user_request(frontend, connection, msg, iteration=0):
    err.log_error(err.INFO, "Using MP multi-user algorithm, iteration %d" % iteration)
    start_time = timeit.default_timer()
    # do this every msg.future_time_delta minutes
    #result = mup.mp(msg.user_id, msg.current_time, msg.current_date, msg.future_time_delta)

    current_time_in_min = sum(int(x) * 60 ** i for i,x in enumerate(reversed(msg.current_time.split(':'))))
    next_time_in_min = current_time_in_min + (iteration * msg.future_time_delta)
    next_time_formatted = '{:02d}:{:02d}'.format(*divmod(next_time_in_min, 60))

    if next_time_in_min > 1440 - msg.future_time_delta:
        return;

    result = mup.mp(msg.user_id, next_time_formatted, msg.current_date, msg.future_time_delta)

    mu_predictions = []

    for k in result.keys():
        (user_id, current_cell_id, next_time) = k
        #err.log_error(err.INFO, "Processing result entry for %i %i %s " % (user_id, current_cell_id, next_time))
        probabilities = result[k]
        #user_entry = UserEntry.from_pykov_key(pykov_key)
        #prediction = data_objects.Prediction(user_entry.cell_id, probability)
	    #prediction_obj = mp.from_pykov_distribution_dict_to_predictions(probabilities)
        predictions = []
        for p in probabilities:
            (cell_id, probability) = p
            predictions.append(data_objects.Prediction(cell_id, probability))

        mu_prediction = data_objects.MultiUserPrediction(user_id, current_cell_id, next_time, predictions)
        mu_predictions.append(mu_prediction)

    answer_message = mo.MPMultiUserAnswer(mu_predictions)
    err.log_error(err.INFO, "answer_message before sending it:")
    pprint.pprint(answer_message)
    connection.add_message(answer_message)

    #self.mp_multi_user_loop_helper(frontend, connection, msg, iteration+1)
    err.log_error(err.INFO, "Execution time for this run (iteration %d) is : %d , Second" % (iteration, timeit.default_timer() - start_time))
    Timer(msg.future_time_delta, handle_incoming_mp_multi_user_request, (frontend, connection, msg, iteration+1)).start()