-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbatchsim.py
More file actions
executable file
·204 lines (176 loc) · 6.91 KB
/
Copy pathbatchsim.py
File metadata and controls
executable file
·204 lines (176 loc) · 6.91 KB
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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#Batch simulator for polychrome
#
#Created: CS Lee 4 Feb. 2012
from polychrome import *
from ui_simulator import *
from PyQt4 import QtCore, QtGui
from numpy import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
class SimulatorThread(QtCore.QThread):
update_signal = QtCore.pyqtSignal(str,int)
def __init__(self,parent=None):
QtCore.QThread.__init__(self,parent)
self.exiting = False
self.n_runs = 0 ;
self.players = None
self.scoring = None
self.results = None
def __del__(self):
self.exiting = True
self.wait()
def do_simulation(self,n,players,scoring):
self.n_runs = n
self.players = players
self.scoring = scoring
self.results = {}
self.start()
def run(self):
# prepare data structure to store results
self.results['scores'] = [list() for p in self.players]
n = 0
n_players = len(self.players)
progress = 0
while n < self.n_runs and not self.exiting:
# reset players
for p in self.players:
p.__init__(p.name)
self.update_signal.emit('\n>>>>>>> Starting Game #'+str(n+1)+'/'+str(self.n_runs)+' <<<<<<<\n',progress)
game = PolychromeGame(self.players,self.scoring)
game.play()
# save scores
scores = game.compute_scores()
for i in range(n_players):
self.results['scores'][i].append(scores[i])
progress = int(100*(n+1)/self.n_runs)
self.update_signal.emit(game.flush_log(),progress)
n += 1
class Simulator(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_Simulator()
self.game = None
self.players = []
self.player_types = []
self.scoring_schemes = [[0,1,3,6,10,15,21],[0,1,4,8,7,6,5]]
self.results = {}
self.populate_players()
self.thread = SimulatorThread()
self.setup_ui()
def setup_ui(self):
self.ui.setupUi(self)
# populate the UI combo boxes
player_names = [t.__name__ for t in self.player_types]
player_names.insert(0,'None')
self.boxes = [self.ui.cbo_player1,self.ui.cbo_player2,self.ui.cbo_player3,
self.ui.cbo_player4,self.ui.cbo_player5]
for box in self.boxes:
box.addItems(player_names)
if box is not self.ui.cbo_player1:
box.setEnabled(False)
# populate the scoring schemes
for s in self.scoring_schemes:
self.ui.cbo_scoring.addItem(str(s))
# set up the plotting canvas
self.ui.canvas
# connect UI elements
self.ui.btn_go.clicked.connect(self.do_simulation)
for box in self.boxes:
box.currentIndexChanged.connect(self.validate_checkbox)
self.ui.cbo_plots.activated.connect(self.do_results_plot)
self.thread.update_signal.connect(self.thread_update_slot)
self.thread.finished.connect(self.thread_finished_slot)
def populate_players(self):
"""
Inform the simulator of known Polychrome Player types
"""
self.player_types = PolychromePlayer.__subclasses__()
self.player_types.remove(HumanPlayer)
def do_simulation(self):
n_runs = self.ui.spin_n_games.value()
if not n_runs > 0:
QtGui.QMessageBox.warning(self,"Number of games must be at least 1")
return
self.log('#### Starting Batch Simulation ####')
# create the players
self.players = []
n = 0
for box in self.boxes:
n += 1
idx = box.currentIndex()
if idx > 0:
player_name = 'Player '+str(n)
player_class = self.player_types[idx-1]
self.players.append(player_class(player_name))
self.log('Players are: '+str([p.__class__.__name__ for p in self.players]))
n_players = len(self.players)
# get the scoring scheme
idx_scoring = self.ui.cbo_scoring.currentIndex()
scoring = self.scoring_schemes[idx_scoring]
self.log('Scoring is: '+str(scoring))
self.thread.do_simulation(n_runs,self.players,scoring)
# # prepare data structure to store results
# self.results['scores'] = [list() for p in self.players]
#
# for n in range(n_runs):
# # reset players
# for p in self.players:
# p.__init__(p.name)
# self.log('\n>>>>>>> Starting Game #'+str(n+1)+'/'+str(n_runs)+' <<<<<<<\n')
# game = PolychromeGame(self.players,scoring)
# game.play()
# # save scores
# scores = game.compute_scores()
# for i in range(n_players):
# self.results['scores'][i].append(scores[i])
# self.log(game.flush_log())
# self.ui.progress_bar.setValue(int(100*n+1/n_runs))
def do_results_plot(self,idx):
if idx == 0:
self.plot_win_percentage()
def plot_win_percentage(self):
""" Make a pie chart of winning frequency """
if len(self.results) == 0:
return
# count wins
n_players = len(self.players)
wins = [0]*n_players
scores = self.results['scores']
n_runs = len(scores[0])
for k in range(n_runs):
scores_k = [scores[j][k] for j in range(n_players)]
winner = scores_k.index(max(scores_k))
wins[winner] += 1
# print('wins= ',wins)
self.ui.canvas.plot(wins,plotmethod='pie')
def validate_checkbox(self,idx):
"""
If the selection is set to 'None', then make sure all subsequent boxes
are set to 'None' as well and disabled
"""
box = self.sender()
idx = self.boxes.index(box)
if box.currentText() == 'None':
for i in range(idx+1,5):
self.boxes[i].setCurrentIndex(0)
self.boxes[i].setEnabled(False)
else:
if idx < 4:
self.boxes[idx+1].setEnabled(True)
def thread_finished_slot(self):
self.results = self.thread.results
def thread_update_slot(self,logstring,progress_val):
self.log(logstring)
self.ui.progress_bar.setValue(progress_val)
def log(self,msg):
"""
append a string to the log
"""
self.ui.txt_log.append(msg)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main = Simulator()
main.show()
sys.exit(app.exec_())