コード例 #1
0
def point4(true_graph: Graph, budget, repetitions, simulations):
    # Copy the original graph
    graph = Graph(copy=true_graph)
    graph.adj_matrix = np.where(true_graph.adj_matrix > 0, 0.5, 0)

    x_list = []
    x2_list = []
    y_list = []
    y2_list = []

    total_error = 0.0

    # Main procedure
    for r in range(repetitions):
        print("Iteration: " + str(r + 1) + "/" + str(repetitions), end="")
        #epsilon = (1 - r / repetitions) ** 2
        seeds = choose_seeds_from_sampling(graph, budget, simulations)
        graph.influence_episode(seeds, true_graph.adj_matrix)

        # in this case where return only of the indices of the non-zero value
        indices = np.where(graph.adj_matrix > 0)

        error = get_total_error(graph, true_graph)
        total_error += error

        x_list.append(r)
        x2_list.append(r)
        y_list.append(total_error)
        y2_list.append(0)
        print("", end="\r")
    print("", end="")

    plt.plot(x_list,
             y_list,
             label='Bandit Approximation',
             color='tab:blue',
             linestyle='-')
    plt.plot(x2_list,
             y2_list,
             label='Ideal 0 Value',
             color='tab:orange',
             linestyle='--')
    plt.title("Unknown Activation Probabilities - Approximation Error")
    plt.ylabel("Approximation Error")
    plt.xlabel("Time")
    plt.legend()

    plt.show()
コード例 #2
0
def point7(graphs, prices, conv_rates, n_phases, k, budget, n_experiments, T,
           simulations):
    window_size = 2 * int((np.sqrt(T)))
    # init revenue and n_customer for each graph, expeeriment and day
    revenue = np.zeros([len(graphs), n_experiments, T])
    n_customers = np.zeros([len(graphs), n_experiments, T])

    phases_lens = np.zeros([len(graphs), n_phases], dtype=int)

    best_graphs_seeds = []
    for g in range(len(graphs)):
        seeds, _ = greedy_algorithm(graphs[g], budget, k)
        best_graphs_seeds.append(seeds)

    for exper in range(n_experiments):
        for g in range(len(graphs)):
            learner = SWTS_Learner(len(prices), prices, window_size, T)
            env = Non_Stationary_Environment(len(prices), conv_rates[g], T)
            # init the graph for point 4
            graph = Graph(copy=graphs[g])
            graph.adj_matrix = np.where(graphs[g].adj_matrix > 0, 0.5, 0)

            print(
                f'Experiment : {exper+1}/{n_experiments} Graph : {g+1}/{len(graphs)}'
            )
            for t in tqdm(range(T)):
                r = 0
                # every day the sellers make social influence
                seeds = choose_seeds_from_sampling(graph, budget, simulations)
                potential_customers = graph.influence_episode(
                    seeds, graphs[g].adj_matrix)
                best_potential_customers = graph.influence_episode(
                    best_graphs_seeds[g], graphs[g].adj_matrix, sampling=False)
                indeces = np.where(graph.adj_matrix > 0)

                curr_phase = int(t / (T / n_phases))
                phases_lens[g][curr_phase] += potential_customers

                # Retrieve for each of them alpha and beta, compute the deviation and update probability
                for i in range(len(indeces[0])):
                    x = indeces[0][i]
                    y = indeces[1][i]
                    alpha = graph.beta_parameters_matrix[x][y].a
                    beta = graph.beta_parameters_matrix[x][y].b
                    mu = alpha / (alpha + beta)
                    graph.adj_matrix[x][y] = mu

                n_customers[g][exper][t] = best_potential_customers

                for _ in range(potential_customers):
                    pulled_arm = learner.pull_arm()
                    reward = env.round(pulled_arm, t)
                    learner.update(pulled_arm, reward, t)
                    r += prices[pulled_arm] * reward

                # revenue of the day
                revenue[g, exper, t] = r

    # average over experiments
    avg_revenue = np.average(revenue, 1)
    avg_customers = np.average(n_customers, 1)

    # compute the true expected revenue
    true_expect_revenue = np.zeros([len(graphs), n_phases, len(prices)])
    for g, conv_rate in enumerate(conv_rates):
        for phase in range(n_phases):
            true_expect_revenue[g][phase] = conv_rate[phase] * prices

    time = range(T)

    for g in range(len(graphs)):
        opt_revenue = []
        actual_revenue = []
        regret = []

        for day in range(T):
            phase_size = T / n_phases
            curr_phase = int(day / phase_size)
            # compute the clairvoyant revenue
            avg_customers_per_graph = np.mean(avg_customers, 1)
            opt = np.max(true_expect_revenue[g]
                         [curr_phase]) * avg_customers_per_graph[g]
            # revenue of the algorithm
            actual = avg_revenue[g][day]
            # compute the instantaneous regret
            regret.append(opt - actual)
            opt_revenue.append(opt)
            actual_revenue.append(actual)
        # print the instantaneous revenue
        plt.figure(1)
        ax1 = plt.subplot(221)
        ax1.set_title(f'Graph {g}: Instantaneous Revenue')
        plt.plot(time, actual_revenue, label='TS_SW')
        plt.plot(time, opt_revenue, '--', label='clairvoyant')
        plt.ylabel('revenue')
        plt.xlabel('Time Horizon')
        plt.legend(loc="lower right")

        # print the cumulative revenue
        ax2 = plt.subplot(222)
        ax2.set_title(f'Graph {g}: Cumulative Revenue')
        plt.plot(time, np.cumsum(actual_revenue), label='TS_SW')
        plt.plot(time, np.cumsum(opt_revenue), '--', label='clairvoyant')
        plt.xlabel('Time Horizon')
        plt.ylabel('revenue')
        plt.legend(loc="lower right")

        # print the cumulative regret
        ax3 = plt.subplot(223)
        ax3.set_title(f'Graph {g}: Cumulative Regret')
        plt.plot(time, np.cumsum(regret), label='TS_SW')
        plt.legend(loc="lower right")
        plt.xlabel('Time Horizon')
        plt.ylabel('regret')
        # plt.savefig(f'results/point5 graph{g+1}')
        plt.show()