Exemplo n.º 1
0
def shaping_int_events_vs_time(budget, n, mu, alpha, w, t0, tf, b, ell, base_activity, itr):
    g = lambda x: np.exp(-w * x)
    deg = np.zeros(n)
    for j in range(n):
        deg[j] = np.count_nonzero(alpha[j, :])

    graph = nx.from_numpy_matrix(alpha)
    pr = nx.pagerank(graph)
    weight = np.asanyarray(list(pr.values()))

    times = np.zeros([5, itr], dtype=object)
    users = np.zeros([5, itr], dtype=object)
    t_opt, u_opt = activity_shaping_int(b, budget, ell - base_activity, t0, tf, alpha, w)
    for i in range(itr):
        times[0, i], users[0, i] = generate_events(t0, tf, mu, alpha, lambda t: [u_opt[k] * (t < t_opt[k]) for k in range(n)], g=g)
        times[1, i], users[1, i] = generate_events(t0, tf, mu, alpha, lambda t: u_deg(t, tf, budget, deg), g=g)
        times[2, i], users[2, i] = generate_events(t0, tf, mu, alpha, lambda t: u_prk(t, tf, budget, weight), g=g)
        times[3, i], users[3, i] = generate_events(t0, tf, mu, alpha, lambda t: u_unf(t, tf, budget, n), g=g)
        times[4, i], users[4, i] = generate_events(t0, tf, mu, alpha)
    data = {'t_opt': t_opt, 'times': times, 'users': users, 'deg': deg, 'weight': weight, 'budget': budget, 'n': n,
            'mu': mu, 'alpha': alpha, 'w': w, 't0': t0, 'tf': tf, 'b': b, 'ell': ell, 'base_activity': base_activity, 
            'seed': RND_SEED}
    sio.savemat('./result/shaping_int_events_vs_time.mat', data)
    with open('./result/shaping_int_events_vs_time.pickle', 'wb') as f:
        pickle.dump(data, f)
    return
Exemplo n.º 2
0
def shaping_int_events_vs_budget(budget, n, mu, alpha, w, t0, tf, b, ell, base_activity, itr):
    g = lambda x: np.exp(-w*x)
    deg = np.zeros(n)
    for k in range(n):
        deg[k] = np.count_nonzero(alpha[k, :])

    graph = nx.from_numpy_matrix(alpha)
    pr = nx.pagerank(graph)
    weight = np.asanyarray(list(pr.values()))

    event_num = np.zeros([4, len(budget), n])
    t_opt = np.zeros((len(budget), n))
    u_opt = np.zeros((len(budget), n))
    for i in range(len(budget)):
        c = budget[i]
        t_opt[i, :], u_opt[i, :] = activity_shaping_int(b, c, ell - base_activity, t0, tf, alpha, w)
        # c_opt = np.dot(t_opt[i, :], u_opt[i, :])

        for k in range(itr):
            times_opt, users_opt = generate_events(t0, tf, mu, alpha, lambda t: [u_opt[i, k] * (t < t_opt[i, k]) for k in range(n)], g=g)
            times_deg, users_deg = generate_events(t0, tf, mu, alpha, lambda t: u_deg(t, tf, c, deg), g=g)
            times_prk, users_prk = generate_events(t0, tf, mu, alpha, lambda t: u_prk(t, tf, c, weight), g=g)
            times_unf, users_unf = generate_events(t0, tf, mu, alpha, lambda t: u_unf(t, tf, c, n), g=g)
            # times_unc, users_unc = generate_events(t0, tf, mu, alpha, g=g)

            event_num[0, i, :] += count_user_events(times_opt, users_opt, n, 0, tf)
            event_num[1, i, :] += count_user_events(times_deg, users_deg, n, 0, tf)
            event_num[2, i, :] += count_user_events(times_prk, users_prk, n, 0, tf)
            event_num[3, i, :] += count_user_events(times_unf, users_unf, n, 0, tf)
            # event_num[4, i, :] += count_user_events(times_unc, users_unc, n, 0, tf)
        event_num[:, i, :] = event_num[:, i, :] / itr

    obj = np.zeros((4, len(budget)))
    for i in range(4):
        for j in range(len(budget)):
            obj[i, j] = norm(event_num[i, j, :] - ell)**2

    data = {'event_num': event_num, 'obj': obj, 't_opt': t_opt, 'u_opt': u_opt, 'deg': deg, 'weight': weight,
            'budget': budget, 'n': n, 'mu': mu, 'alpha': alpha, 'w': w, 't0': t0, 'tf': tf, 'b': b, 'ell': ell,
            'base_activity': base_activity, 'seed': RND_SEED}
    sio.savemat('./result/shaping_int_events_vs_budget.mat', data)
    with open('./result/shaping_int_events_vs_budget.pickle', 'wb') as f:
        pickle.dump(data, f)
    return
Exemplo n.º 3
0
def max_events_vs_budget(budget, n, mu, alpha, w, t0, tf, b, d, itr):
    g = lambda x: np.exp(-w * x)
    deg = np.zeros(n)
    for j in range(n):
        deg[j] = np.count_nonzero(alpha[j, :])

    graph = nx.from_numpy_matrix(alpha)
    pr = nx.pagerank(graph)
    weight = np.asanyarray(list(pr.values()))

    event_num = np.zeros([4, len(budget), itr])
    terminal_event_num = np.zeros([4, len(budget), itr])
    t_opt = np.zeros((len(budget), n))
    for i in range(len(budget)):
        c = budget[i]
        t_opt[i, :] = activity_max(b, c, d, t0, tf, alpha, w)

        for j in range(itr):
            times_optimal, _ = generate_events(t0, tf, mu, alpha, lambda t: u_opt_inc(t, t_opt[i, :], n, b), g=g)
            times_deg, _ = generate_events(t0, tf, mu, alpha, lambda t: u_deg(t, tf, c, deg), g=g)
            times_prk, _ = generate_events(t0, tf, mu, alpha, lambda t: u_prk(t, tf, c, weight), g=g)
            times_uniform, _ = generate_events(t0, tf, mu, alpha, lambda t: u_unf(t, tf, c, n), g=g)
            # times_unc, _ = generate_events(t0, tf, mu, alpha, g=g)
            event_num[0, i, j] = len(times_optimal)
            event_num[1, i, j] = len(times_deg)
            event_num[2, i, j] = len(times_prk)
            event_num[3, i, j] = len(times_uniform)
            # event_num[4, i, j] = len(times_unc)
            terminal_event_num[0, i, j] = count_events(times_optimal, tf-1, tf)
            terminal_event_num[1, i, j] = count_events(times_deg, tf-1, tf)
            terminal_event_num[2, i, j] = count_events(times_prk, tf-1, tf)
            terminal_event_num[3, i, j] = count_events(times_uniform, tf-1, tf)
            # terminal_event_num[4, i, j] = count_events(times_unc, tf - 1, tf)
    obj = np.mean(terminal_event_num, 2)

    data = {'obj': obj, 'event_num': event_num, 'terminal_event_num': terminal_event_num, 't_opt': t_opt, 'deg': deg,
            'weight': weight, 'budget': budget, 'n': n, 'mu': mu, 'alpha': alpha, 'w': w, 't0': t0, 'tf': tf, 'b': b,
            'd': d, 'seed': RND_SEED}
    sio.savemat('./result/max_events_vs_budget.mat', data)
    with open('./result/max_events_vs_budget.pickle', 'wb') as f:
        pickle.dump(data, f)
    return