-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimm_conv_sol_marginal.py
159 lines (127 loc) · 4.1 KB
/
imm_conv_sol_marginal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import random
import evaluate
import json
from networkx.readwrite import json_graph
import time
import sys
import networkx as nx
num_k = 500
model = sys.argv[1]
base_path = sys.argv[2]
train_or_test = sys.argv[3]
graph_path = base_path + '/' + model + '/' + train_or_test + '/'
print(graph_path)
G_data = json.load(open(graph_path + "large_graph" + "-G.json"))
G = json_graph.node_link_graph(G_data)
total_nodes = len(G)
epsilon = 0.5
#epsilon = 0.5
print(os.getcwd())
print(graph_path)
#total_nodes = len(G.nodes())
#print("total nodes ", total_nodes)
dataset_path = "../../../../../" + graph_path +"edges.txt"
print(dataset_path)
int_selected_nodes = []
num_of_iterations_imm = 25
appeared_count = {}
max_appear_count = 0
print(os.getcwd())
class_map_file = graph_path + "large_graph" +"_num_k_"+str(num_k)+"-class_map.json"
class_map = {}
f2 = open(class_map_file, 'w')
dict_marginal_gain = {}
for iter in range(0, num_of_iterations_imm):
print(" imm iteratin for class map new #", iter)
out_file_path = graph_path +'/multi_iter/'+ "large_graph_ic_imm_sol_eps" + str(epsilon) + "_num_k_" + str(
num_k) +"_iter_"+str(iter)+"_dict_node_gain"
print(out_file_path)
solution_file_name = out_file_path + ".txt"
print(solution_file_name)
solution_file = open(solution_file_name, "r")
optimal_nodes_gain = solution_file.readlines()
for line in optimal_nodes_gain:
line=line.replace("\n","")
node,gain= line.split(" ")
node,gain = int(node), int(gain)
if node not in dict_marginal_gain:
dict_marginal_gain[node] = 0
dict_marginal_gain[node] += gain
pass
sum_of_marginal_gains = 0
for node, marg_gain in dict_marginal_gain.items():
print("node marg_gain ", node, marg_gain)
sum_of_marginal_gains+= marg_gain
print("sum of marginal gains ", sum_of_marginal_gains)
dict_marginal_gain_normalized = {}
for node, marg_gain in dict_marginal_gain.items():
dict_marginal_gain_normalized[node] = [marg_gain*1.0/sum_of_marginal_gains]
print("dict_marginal_gain_normalized" , dict_marginal_gain_normalized)
for node in range(0, total_nodes):
# if node in int_selected_nodes:
if node not in dict_marginal_gain_normalized:
dict_marginal_gain_normalized[str(node)] = [0]
classdata = json.dumps(dict_marginal_gain_normalized)
f2.write(classdata)
f2.close()
#int_selected_nodes = []
#
# for i in range(0, num_k):
# int_selected_nodes.append(int(optimal_nodes[i]))
# if int(optimal_nodes[i]) not in appeared_count:
# appeared_count[int(optimal_nodes[i])] = 0
#
# appeared_count[int(optimal_nodes[i])] += 1
#
# if appeared_count[int(optimal_nodes[i])] > max_appear_count:
# max_appear_count = appeared_count[int(optimal_nodes[i])]
#
# # print( appeared_count)
#
# appeared_count_normalized = {}
# for node_id, node_freq in appeared_count.items():
# appeared_count_normalized[node_id] = node_freq / (max_appear_count * 1.0)
# # print( appeared_count_normalized)
#
# os.chdir("../../../../../")
#
# # print(appeared_count_normalized)
# print("Writing to class map ")
# print(appeared_count_normalized)
#
# for node in range(0, total_nodes):
# # if node in int_selected_nodes:
# if node not in appeared_count_normalized:
# class_map[str(node)] = [0]
# else:
# class_map[str(node)] = [appeared_count_normalized[node]] # [1,0]
#
#
# classdata = json.dumps(class_map)
# f2.write(classdata)
# f2.close()
#
#
# # else:
# ## 3 class_map[str(node)] = # [0,1]
# # print(class_map)
#
# # num_mc_simulation = 100
#
# # spread = 0
# print(" loading graph")
# G_data = json.load(open("GraphSAGE-master/real_data/large_youtube/large_graph" + "-G.json"))
# G = json_graph.node_link_graph(G_data)
# print(" running mc simulation")
# for i in range(0, num_mc_simulation):
# UG_Copy = G.copy()
# temp_spread = evaluate.evaluate(UG_Copy, int_selected_nodes)
# print(" iter {} spread {}".format(i, temp_spread))
# spread = spread + temp_spread
# spread = spread * 1.0 / num_mc_simulation
# print('Spread = ', spread)
# reward_file_name = "GraphSAGE-master/real_data/large_youtube/large_graph"+"_reward_imm_eps"+ str(epsilon)
# reward_file = open(reward_file_name, 'w')
# reward_file.write(str(spread))
# reward_file.close()