Creates a GUI specific runnable

This commit is contained in:
LSaldyt
2017-10-25 10:21:07 -07:00
parent 3a6b2ac18f
commit 2cf1320672
5 changed files with 74 additions and 7 deletions

View File

@ -92,14 +92,41 @@ class Copycat(object):
self.reporter.report_answer(answer)
return answer
def runGUI(self):
while not self.check_reset():
self.gui.update(self)
self.gui.refresh()
answers = {}
while True:
if self.check_reset():
answers = {}
answer = self.runTrial()
if self.showgui:
self.gui.app.log('Answered: {}'.format(answer['answer']))
d = answers.setdefault(answer['answer'], {
'count': 0,
'sumtemp': 0,
'sumtime': 0
})
d['count'] += 1
d['sumtemp'] += answer['temp']
d['sumtime'] += answer['time']
if self.showgui:
self.gui.add_answers(answers)
for answer, d in answers.items():
d['avgtemp'] = d.pop('sumtemp') / d['count']
d['avgtime'] = d.pop('sumtime') / d['count']
def run(self, initial, modified, target, iterations):
self.reset_with_strings(initial, modified, target)
answers = {}
for i in range(iterations):
if self.check_reset():
answers = {}
answer = self.runTrial()
if self.showgui:
self.gui.app.log('Answered: {}'.format(answer['answer']))
d = answers.setdefault(answer['answer'], {
'count': 0,
'sumtemp': 0,

View File

@ -38,6 +38,11 @@ class MainApplication(GridFrame):
self.iterations = 0
self.messages = []
def log(self, message):
self.messages.append(message)
def create_widgets(self):
self.slipList = tk.Listbox(self, **style, bd=0)
@ -49,11 +54,8 @@ class MainApplication(GridFrame):
self.objectList = tk.Listbox(self, **style, bd=0)
self.add(self.objectList, 2, 1)
#l = ttk.Label(self, text='', **style, padding=30)
#self.add(l, 2, 1)
self.log = tk.Label(self, text='[Logging]', **style)
self.add(self.log, 1, 0)
self.logBox = tk.Label(self, text='', **style, bd=1)
self.add(self.logBox, 1, 0)
self.graph2 = Status()
sframe2 = StatusFrame(self, self.graph2, 'graph 2')
self.add(sframe2, 2, 0)
@ -85,6 +87,10 @@ class MainApplication(GridFrame):
listStr = str(o)
self.objectList.insert(tk.END, listStr)
self.logBox['text'] = '\n'.join(list(reversed(self.messages))[:10])
if len(self.messages) > 10:
self.logBox['text'] += '\n...'
def reset_with_strings(self, initial, modified, target):
self.primary.reset_with_strings(initial, modified, target)

View File

@ -61,7 +61,7 @@ class Primary(GridFrame):
self.add(self.control, 0, 2)
def update(self, copycat):
answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule()
answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule()
self.canvas = create_main_canvas(self, self.initial, self.modified, self.target, answer)
self.add(self.canvas, 0, 0, xspan=2)

View File

@ -19,6 +19,16 @@ class Workspace(object):
self.intraStringUnhappiness = 0.0
self.interStringUnhappiness = 0.0
# LSaldyt: default initializations for GUI entry
self.targetString = ''
self.initialString = ''
self.modifiedString = ''
self.finalAnswer = None
self.changedObject = None
self.objects = []
self.structures = []
self.rule = None
def __repr__(self):
return '<Workspace trying %s:%s::%s:?>' % (
self.initialString, self.modifiedString, self.targetString,

24
gui.py Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
import argparse
import logging
from copycat import Copycat, Reporter
class SimpleReporter(Reporter):
def report_answer(self, answer):
print('Answered %s (time %d, final temperature %.1f)' % (
answer['answer'], answer['time'], answer['temp'],
))
def main():
logging.basicConfig(level=logging.INFO, format='%(message)s', filename='./output/copycat.log', filemode='w')
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=None, help='Provide a deterministic seed for the RNG.')
options = parser.parse_args()
copycat = Copycat(reporter=SimpleReporter(), rng_seed=options.seed)
copycat.runGUI()
if __name__ == '__main__':
main()