/
groupOrderingHillClimb_archive.py
242 lines (171 loc) · 6.78 KB
/
groupOrderingHillClimb_archive.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from mb import *
from multiprocessing import *
import csv
import copy
import math
import itertools
def random_ordering():
all_groups = ["Brown", "Light Blue", "Pink", "Orange",
"Red", "Yellow", "Green", "Dark Blue",
"Utility", "Railroad"]
shuffle(all_groups)
return tuple(all_groups)
def random_neighbor1(ordering):
index1 = randint(0, 9)
index2 = index1
while index2 == index1:
index2 = randint(0, 9)
list_ordering = list(ordering)
temp = list_ordering[index1]
list_ordering[index1] = list_ordering[index2]
list_ordering[index2] = temp
return tuple(list_ordering)
def all_short_random_neighbors1(ordering):
all_random_neighbors = []
for index1 in range(0, 10):
for index2 in range(index1, 10):
if index1 != index2:
list_ordering = list(ordering)
temp = list_ordering[index1]
list_ordering[index1] = list_ordering[index2]
list_ordering[index2] = temp
all_random_neighbors.append(tuple(list_ordering))
shuffle(all_random_neighbors)
return all_random_neighbors
def insertion_neighbors(ordering):
ordering = list(ordering)
all_random_neighbors = []
for i in range(10):
group = ordering[i]
short_ordering = copy.deepcopy(ordering)
short_ordering.remove(group)
for j in range(10):
if i != j:
new_ordering = copy.deepcopy(short_ordering)
new_ordering.insert(j, group)
all_random_neighbors.append(tuple(new_ordering))
shuffle(all_random_neighbors)
return all_random_neighbors
def all_short_random_neighbors2(ordering):
all_random_neighbors = []
for index in range(0, 9):
list_ordering = list(ordering)
temp = list_ordering[index]
list_ordering[index] = list_ordering[index + 1]
list_ordering[index + 1] = temp
all_random_neighbors.append(tuple(list_ordering))
return all_random_neighbors
'''def old_success_indicator(ordering, games_in_a_set=10000):
winner_matrix = [0, 0, 0]
for i in range(games_in_a_set):
# Play game.
player1 = Player(1, buying_threshold=500, group_ordering=ordering)
player2 = Player(2, buying_threshold=500, group_ordering=random_ordering())
game0 = Game([player1, player2], cutoff=1000, trading_enabled=True)
results = game0.play()
# Store length.
winner_matrix[results['winner']] += 1
return winner_matrix[1] / games_in_a_set'''
def play_set(ordering, number_of_games, results_q):
results_list = []
game0 = Game(cutoff=1000, trading_enabled=True)
for i in range(number_of_games):
# Play game.
player1 = Player(1, buying_threshold=500, group_ordering=ordering, step_threshold=True)
player2 = Player(2, buying_threshold=500, group_ordering=random_ordering(), step_threshold=True)
game0.new_players([player1, player2])
results = game0.play()
results_list.append(results['winner']) # Store the game's result.
results_q.put(results_list.count(1))
def progressive_success(ordering):
wins = 0
game0 = Game(cutoff=1000, trading_enabled=True)
counter = 0
interval_size = 10
base_games = 1000
p = 0
while (counter < base_games) or interval_size > 0.01:
counter += 1
# Play game.
player1 = Player(1, buying_threshold=500, group_ordering=ordering, step_threshold=True)
player2 = Player(2, buying_threshold=500, group_ordering=random_ordering(), step_threshold=True)
game0.new_players([player1, player2])
results = game0.play()
if results['winner'] == 1:
wins += 1
p = wins / counter
interval_size = 1.960 * math.sqrt((p * (1 - p)) / counter)
# print(counter, p, interval_size)
return p
def success_indicator(ordering, number_of_games=10000, procs=4):
results_q = Queue() # Queue for results.
proc_list = [Process(target=play_set, args=(ordering, int(number_of_games / procs), results_q))
for i in range(procs)] # List of processes.
for proc in proc_list: # Start all processes.
proc.start()
for proc in proc_list: # Wait for all processes to finish.
proc.join()
results_list = []
# Gather the results from each process in a list of counts.
while results_q.empty() == False:
results_list.append(results_q.get())
# Success rate = total count / total number of games
success_rate = float(sum(results_list)) / float(number_of_games)
return 100 * success_rate
def lookup_success(ordering, ordering_archive, success_archive):
length = len(ordering_archive)
for index in range(length):
if ordering_archive[index] == ordering:
print("-----used archive-----")
return success_archive[index]
success = success_indicator(ordering)
ordering_archive.append(ordering)
success_archive.append(success)
if length >= 50:
ordering_archive = []
success_archive = []
return success
def write_row(row):
with open('results/hillClimb_blah.csv', 'a', newline='') as csvfile:
output_file = csv.writer(csvfile, quotechar=',')
output_file.writerow(row)
def neighbors(ordering):
return all_short_random_neighbors1(ordering)
# return insertion_neighbors(ordering)
def success(ordering):
return progressive_success(ordering)
def hill_climb():
ordering_archive = []
success_archive = []
while True:
print('Start!')
write_row(["Start!"])
counter = 0
old_ordering = random_ordering()
old_success = success(old_ordering) # lookup_success(old_ordering, ordering_archive, success_archive)
print(old_ordering, old_success)
write_row([old_ordering, old_success])
all_neighbors = neighbors(old_ordering)
while counter < (len(all_neighbors) - 1):
new_ordering = all_neighbors[counter]
new_success = success(new_ordering)
counter += 1
if new_success > old_success:
old_ordering = new_ordering
old_success = new_success
print(old_ordering, old_success, counter)
write_row([old_ordering, old_success])
all_neighbors = neighbors(old_ordering)
counter = 0
print('No better neighbors.')
def brute_force():
groups = ["Brown", "Pink", "Red", "Yellow", "Green", "Dark Blue", "Utility"]
all_orderings = itertools.permutations(groups)
for addon in all_orderings:
ordering = ["Railroad", "Orange", "Light Blue"]
ordering.extend(addon)
print(ordering, success(ordering))
def main():
#hill_climb()
brute_force()
main()