def result(self, state, action, player): newState = [] T = [state[0] + 1] A, A1, A2, IA = list(state[1]), list(state[2]), list(state[3]), list(state[4]) A.append(action[1]) IA.remove(action[1]) if player == self.PLAYERS[0]: A1.append(action[1]) newState = Spreading(A, A1, A2, IA) else: A2.append(action[1]) newState = Spreading(A, A1, A2, IA) newState = T + newState return newState
def breadth_first_search(R, T, buf, M): global args K = args.seed_size TURNS = K + 1 SEED = [] fileName = "Path.csv" output = open(fileName, "w") cnt = R SEED = [] for x in range(7): SEED.append(buf[x][1]) output.write("{0},{1},{2}\n".format(x + 1, buf[x][1], buf[x][0])) Active = [] Active_1 = [] Active_2 = [] TREND = [] Inactive = list(setting.data.threshold.keys()) for i in range(K): Active.append(SEED[i]) Inactive.remove(SEED[i]) if i % 2 == 0: Active_1.append(SEED[i]) else: Active_2.append(SEED[i]) (Active, Active_1, Active_2, Inactive) = Spreading(Active, Active_1, Active_2, Inactive) if i % 2 == 1: TREND.append(len(Active) / len(setting.data.threshold)) output.write("{0}\n{1}\n{2}\n".format(len(Active), len(Active_1), len(Active_2))) output.write("MCT Construction time: {0}\n".format(T)) for t in TREND: output.write("{0},".format("{:.2%}".format(t))) output.write("\n\n") output.close() METHODS = ['MaxWeight', 'MajorityWeight', 'MaxPageRank', 'MaxLDAG'] INDEX = METHODS.index(M.__name__) output = open("Result.csv", "w") for i in range(4): STR = 'AI' output.write(M.__name__ + ' VS ' + METHODS[i] + '\n') outcome = AIVSSTUPID(M.__name__, METHODS[i], None) output.write('p1:{0},p2:{1}\n'.format(outcome[0], outcome[1])) output.write(METHODS[i] + ' VS ' + M.__name__ + '\n') outcome = AIVSSTUPID(METHODS[i], M.__name__, None) output.write('p1:{0},p2:{1}\n'.format(outcome[0], outcome[1])) output.write('AI VS ' + METHODS[i] + '\n') outcome1 = AIVSSTUPID(STR, METHODS[i], fileName) output.write('p1:{0},p2:{1}\n\n'.format(outcome1[0], outcome1[1])) output.write(METHODS[i] + ' VS ' + 'AI' + '\n') outcome2 = AIVSSTUPID(METHODS[i], STR, fileName) output.write('p1:{0},p2:{1}\n\n'.format(outcome2[0], outcome2[1])) output.close()
def resultofallmethods(): Result = [] METHODS = [MaxWeight, MajorityWeight, MaxPageRank, MaxLDAG] for M in METHODS: Active, Active1, Active2 = [], [], [] Inactive = [x for x in setting.data.threshold.keys()] seed = [] for i in range(10): X = M(list(Active), list(Active_1), list(Active_2), list(Inactive)) Active.append(X) Active_1.append(X) Inactive.remove(X) (Active, Active_1, Active_2, Inactive) = Spreading(Active, Active_1, Active_2, Inactive) Result.append(len(Active_1)) return Result
def simulation(self, METHOD): Actions = METHOD reward = 0.0 for A in Actions: tmp_turn = self.state[0] st = [self.state[0], list(self.state[1]), list(self.state[2]), list(self.state[3]), list(self.state[4])] p1 = self.player before,before1 = 0,0 if p1=='2': before = len(self.parent.state[2]) before1 = len(self.parent.state[3]) else: before = len(self.parent.state[3]) before1 = len(self.parent.state[2]) steps = 10 while not self.ccim.terminal(tmp_turn): if p1 == '1': #print("player1 selects--------------------") if self.player=='1': action = A(list(st[1]),list(st[2]),list(st[3]),list(st[4])) else: action = A(list(st[1]),list(st[2]),list(st[3]),list(st[4])) st[1].append(action) st[2].append(action) else: #print("player2 selects-------------------") if self.player=='2': action = A(list(st[1]),list(st[3]),list(st[2]),list(st[4])) else: action = A(list(st[1]),list(st[3]),list(st[2]),list(st[4])) st[1].append(action) st[3].append(action) tmp = Spreading(st[1], st[2], st[3], st[4]) tmp_turn += 1 st = [tmp_turn] st.extend(tmp) steps -= 1 if steps==0: break p1 = self.ccim.next_player(p1) Outcome = 0 now_round = 0 Outcome = self.ccim.outcome(st,p1) Outcome1 = self.ccim.outcome(st,self.ccim.next_player(p1)) now_round = tmp_turn - 1 reward += ((Outcome-before)) reward /= len(Actions) return reward
def gen_random(active, my_active, your_active, inactive, left_turns): gen_seed = [] for x in range(left_turns*2): seed = choice(inactive) print("Random seed: %s"%(seed)) gen_seed.append(seed) active.append(seed) inactive.remove(seed) if x%2 == 0: my_active.append(seed) else: your_active.append(seed) cnt = Spreading(list(active), list(my_active), list(your_active), list(inactive)) active = list(cnt[0]) my_active = list(cnt[1]) your_active = list(cnt[2]) inactive = list(cnt[3]) return Chromosome(gen_seed, Chromosome._update_fitness(my_active, your_active), list(active), list(my_active), list(your_active), list(inactive))
def _update_active_state(gene, active, my_active, your_active, inactive): tmp_active = list(active) tmp_my_active = list(my_active) tmp_your_active = list(your_active) tmp_inactive = list(inactive) for i in range(len(gene)): if gene[i] in tmp_active: continue tmp_active.append(gene[i]) tmp_inactive.remove(gene[i]) if i%2==0: tmp_my_active.append(gene[i]) else: tmp_your_active.append(gene[i]) cnt = Spreading(list(tmp_active), list(tmp_my_active), list(tmp_your_active), list(tmp_inactive)) tmp_active = list(cnt[0]) tmp_my_active = list(cnt[1]) tmp_your_active = list(cnt[2]) tmp_inactive = list(cnt[3]) return [Chromosome._update_fitness(my_active, your_active), tmp_active, tmp_my_active, tmp_your_active, tmp_inactive]
if x%2==0: Method.append(AI[x]) else: Method.append(p2) elif p2=="AI": for x in range(20): if x%2==1: Method.append(AI[x]) else: Method.append(p1) else: for x in range(20): if x%2==0: Method.append(p1) else: Method.append(p2) Active, Active_1, Active_2 = [], [], [] Inactive = [x for x in setting.data.threshold.keys()] seed = '1' for x in range(20): if x%2==0: seed = DIC[Method[x]](list(Active),list(Active_1),list(Active_2),list(Inactive)) if not seed: seed = MaxWeight(list(Active),list(Active_1),list(Active_2),list(Inactive)) Active.append(seed) Active_1.append(seed) Inactive.remove(seed) else: seed = DIC[Method[x]](list(Active),list(Active_2),list(Active_1),list(Inactive)) if not seed: seed = MaxWeight(list(Active),list(Active_2),list(Active_1),list(Inactive)) Active.append(seed) Active_2.append(seed) Inactive.remove(seed) (Active, Active_1, Active_2, Inactive) = Spreading(list(Active),list(Active_1),list(Active_2),list(Inactive)) return (len(Active_1),len(Active_2))