def run(i): with open ('data/json/comm' + str(i) + '.json', 'r') as fp: data = json.load(fp) network = data['trustMatrix'] opinion_attr = "sex" n = len(network) X = [0, 3, 6, 9, 15, 20, 30, 45, 60, 80, 100, 120, 150, 200, 300] Y = [] # FTPL payoff BR = [] # BR payoff file_X = open("FTPL_X.txt", "w") file_BR = open("FTPL_BR.txt", "w") file_Y = open("FTPL_Y.txt", "w") XAs, XBs = [], [] for x in X: print("BUDGET ", x) # file_X.write(str(x) + ', ') A = Candidate("A", x, 1, n) B = Candidate("B", 30, 0, n) e = Election(data, [A, B], 10, opinion_attr, rand=False) e.update_network() ftpl(e, 3, 0.1) ftpl_mean = e.calculate_mean() XAs.append(A.X) XBs.append(B.X) Y.append(ftpl_mean) file_Y.write(str(ftpl_mean)+ ', ') print("sum", sum(e.A.X)) print("FTPL MEAN", e.advertise()) print("theta", e.theta_T) print(sum(e.theta_T)) e.A.X = mov_oracle(e, A, np.zeros(n)) e.B.X = mov_oracle(e, B, np.zeros(n)) # print(A.X) br_mean = e.calculate_mean() BR.append(br_mean) print("BR MEAN", br_mean) file_BR.write(str(br_mean) + ', ') for file in [file_X, file_BR, file_Y]: file.flush() print("means: {}, {}".format(ftpl_mean, br_mean)) file.write("X: {}".format(X)) file.write("Y: {}".format(Y)) file.write("BR: {}".format(BR)) file.write("X_A: {}".format(XAs)) file.write("X_B: {}".format(XBs)) file.close() return X, np.array(Y)-np.array(BR)
def run(i): with open ('data/json/comm' + str(i) + '.json', 'r') as fp: data = json.load(fp) network = data['trustMatrix'] opinion_attr = "sex" n = len(network) A = Candidate("A", 20, 1, n) B = Candidate("B", 30, 0, n) e = Election(data, [A, B], 30, opinion_attr, rand=False) e.update_network() h = np.array(e.calculate_homophily(e.theta)) if h[0] < h[1]: mu = e.calculate_mean()/e.n else: mu = 1-e.calculate_mean()/e.n print(h, mu) return h, mu
def run(n, i): with open ('data/json/comm' + str(i) + '.json', 'r') as fp: data = json.load(fp) N = len(data['trustMatrix']) opinion_attr = "sex" A = Candidate("A", 20, 1, n) B = Candidate("B", 30, 0, n) e = Election(data, n, [A, B], 5, opinion_attr, rand=False) i, r, c_times, nc_times = iterated_best_response(e, 1e-2, 1e3) c, nc = 0, 0 if len(c_times): c = np.mean(c_times) if len(nc_times): nc = np.mean(nc_times) # print(e.A.X) # print(e.B.X) # print('A POV', e.calculate_pov_exact()) # print('iterations:{}, restarts:{}'.format( i, r)) # print('avg convex time:{}, avg nonconvex time{}'.format(np.mean(c_times), np.mean(nc_times))) return [i, r, c, nc]
def run(i): with open('json/comm' + str(i) + '.json', 'r') as fp: data = json.load(fp) opinion_attr = "sex" X = [0, 5, 10] # X = [0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 105, 110, 115, 118, 120] # X = [0, 3, 6, 9, 15, 20, 30, 45, 60, 80, 100, 120, 150, 200, 300] # X = np.linspace(0, 120, 13) n = 32 Y = [] # FTPL payoff BR = [] # BR payoff file_BR = open("data_fixed_sum_slurm/{}_br.txt".format(i), "w") file_Y = open("data_fixed_sum_slurm/{}_Y.txt".format(i), "w") A = Candidate("A", 0, 1, n) B = Candidate("B", 0, 0, n) e = Election(data, n, [A, B], 10, opinion_attr, rand=False) for x in X: print("BUDGET ", x) A.k = x B.k = 100 - x e.update_network() ftpl(e, 5, .1, x) ftpl_mean = e.calculate_mean() Y.append(ftpl_mean) file_Y.write(str(ftpl_mean) + ', ') A.X = mov_oracle(e, A, np.zeros(n)) B.X = mov_oracle(e, B, np.zeros(n)) br_mean = e.calculate_mean() BR.append(br_mean) file_BR.write(str(br_mean) + ', ') for file in [file_BR, file_Y]: file.flush() print("means: {}, {}".format(ftpl_mean, br_mean)) file_BR.close() file_Y.close() return X, np.array(Y) - np.array(BR)
def run(i): blockPrint() with open('data/json/comm' + str(i) + '.json', 'r') as fp: data = json.load(fp) opinion_attr = "sex" # X = [0, 5, 10] # X = [0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 105, 110, 115, 117, 118, 119, 119.5, 120] # X = [0, 3, 6, 9, 15, 20, 30, 45, 60, 80, 100, 120, 150, 200, 300] X = list(np.linspace(0, 300, 2)) n = 32 # file = open("data_utility/{}.txt".format(i), "w") A = Candidate("A", 0, 1, n) B = Candidate("B", 0, 0, n) e = Election(data, n, [A, B], 10, opinion_attr, rand=False) # if i == 1: # X = X[3:] res = {} for x in X: budget_dict = {} enablePrint() # print("BUDGET ", x) blockPrint() A.k = x B.k = max(X) - x # print("BUDGETS 1", A.k, B.k) e.update_network() i, r, c, nc = iterated_best_response(e, 1e-2, 20, 1e3) e.update_network() alt_pov = e.calculate_pov_exact() A.X, B.X = [0] * n, [0] * n A_X_SBR, _, _, _ = pov_oracle(e, A, 0, e.n, 20, [], []) A.X, B.X = [0] * n, [0] * n B_X_SBR, _, _, _ = pov_oracle(e, B, 0, e.n, 20, [], []) enablePrint() A.X = A_X_SBR B.X = B_X_SBR e.update_network() single_pov = e.calculate_pov_exact() budget_dict["i"] = i budget_dict["r"] = r budget_dict["c"] = np.mean(c) budget_dict["nc"] = np.mean(nc) budget_dict["abr"] = alt_pov budget_dict["sbr"] = single_pov res[x] = json.dumps(budget_dict) # file.flush() # enablePrint() # print("Completed in {} iters with {} restarts".format(i, r)) # print("ABR POV: {}, SBR POV: {}".format(alt_pov, single_pov)) # blockPrint() print("{}: ".format(i) + json.dumps(res) + ", ") # enablePrint() # file.close() A.X, B.X = [0] * n, [0] * n e.update_network() single_pov = e.calculate_pov_exact() print("default POV: {}".format(single_pov)) return X
# e2 = Election(network, [A, B], 20, theta) # e2.update_network() # print("max spend 1", ftpl_max_spend(e2, A, B, 1)) # print("ad", e2.advertise([.5, 2], p_a, np.zeros(2), p_b)) # print("ad", e2.advertise([2.5, 0], p_a, np.zeros(2), p_b)) n = 3 P = [[1, 0, 0], [0.3, 0.5, 0.2], [0.2, 0.1, 0.7]] #np.identity(n) network = {'trustMatrix': P} p_a = [1, 0, 0.8] p_b = [1, 0, 0.8] theta = [1, 0, 1] A = Candidate("A", 3, 1, n, p_a) B = Candidate("B", 4, 0, n, p_b) A.X = [1, 0, 0] B.X = [0.75, 0, 1.25] e3 = Election(network, [A, B], 3, theta=theta, rand=False) e3.update_network() # print(e3.calculate_homophily(e3.theta)) print(e3.theta) print(A.p) print(B.p) # print(e3.theta_0) # print(e3.theta_T) ftpl(e3, 5, 1e-3) # display(e3, A, B)
# A = Candidate("A", .5, 1, n, p_a) # B = Candidate("B", 0, 0, n, p_b) # e2 = Election(network, [A, B], 20, theta) # e2.update_network() def calc_pov(e): return (round(e3.calculate_mean(), 3)) # return (round(e3.calculate_pov_exact(), 3)) n = 3 network = {'trustMatrix': np.identity(n)} p_a = [0.5, 0.1, 1] p_b = [0.7, 0.4, 1] theta = [0.3, .2, 1] A = Candidate("A", 3, 1, n, p_a) B = Candidate("B", 3, 0, n, p_b) e3 = Election(network, [A, B], 0, "sex", theta=theta) # e3.A.X = [1,0,0] # e3.B.X = [0,0,1] # e3.update_network() # print(e3.theta_0) # print(e3.calculate_mean()) def test2(e): e.A.X = [2.0, 0.0, 1.0] e.B.X=[1.4285714285714286, 1.5714285714285714, 0.0] e.update_network() # print(e.theta_T) # print(e.calculate_pov_exact()) # random_allocate(e, e.A) iterated_best_response(e, 1e-4) print(e.A.X)