Fixes bugs with pausing
This commit is contained in:
@ -25,14 +25,15 @@ class Reporter(object):
|
||||
|
||||
|
||||
class Copycat(object):
|
||||
def __init__(self, rng_seed=None, reporter=None, showgui=True):
|
||||
def __init__(self, rng_seed=None, reporter=None, gui=False):
|
||||
self.coderack = Coderack(self)
|
||||
self.random = Randomness(rng_seed)
|
||||
self.slipnet = Slipnet()
|
||||
self.temperature = Temperature()
|
||||
self.workspace = Workspace(self)
|
||||
self.reporter = reporter or Reporter()
|
||||
self.gui = GUI('Copycat')
|
||||
if gui:
|
||||
self.gui = GUI('Copycat')
|
||||
self.lastUpdate = float('-inf')
|
||||
|
||||
def step(self):
|
||||
@ -52,15 +53,12 @@ class Copycat(object):
|
||||
def check_reset(self):
|
||||
if self.gui.app.primary.control.go:
|
||||
initial, modified, target = self.gui.app.primary.control.get_vars()
|
||||
self.reset_with_strings(initial, modified, target)
|
||||
self.gui.app.reset_with_strings(initial, modified, target)
|
||||
self.workspace.resetWithStrings(initial, modified, target)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def reset_with_strings(self, initial, modified, target):
|
||||
self.workspace.resetWithStrings(initial, modified, target)
|
||||
self.gui.app.reset_with_strings(initial, modified, target)
|
||||
|
||||
def mainLoop(self):
|
||||
currentTime = self.coderack.codeletsRun
|
||||
self.temperature.tryUnclamp(currentTime)
|
||||
@ -94,29 +92,28 @@ class Copycat(object):
|
||||
while True:
|
||||
if self.check_reset():
|
||||
answers = {}
|
||||
answer = self.runTrial()
|
||||
self.gui.refresh()
|
||||
self.gui.update(self)
|
||||
d = answers.setdefault(answer['answer'], {
|
||||
'count': 0,
|
||||
'sumtemp': 0,
|
||||
'sumtime': 0
|
||||
})
|
||||
d['count'] += 1
|
||||
d['sumtemp'] += answer['temp']
|
||||
d['sumtime'] += answer['time']
|
||||
self.gui.add_answers(answers)
|
||||
if not self.gui.paused():
|
||||
answer = self.runTrial()
|
||||
self.gui.update(self)
|
||||
d = answers.setdefault(answer['answer'], {
|
||||
'count': 0,
|
||||
'sumtemp': 0,
|
||||
'sumtime': 0
|
||||
})
|
||||
d['count'] += 1
|
||||
d['sumtemp'] += answer['temp']
|
||||
d['sumtime'] += answer['time']
|
||||
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)
|
||||
self.workspace.resetWithStrings(initial, modified, target)
|
||||
answers = {}
|
||||
for i in range(iterations):
|
||||
if self.check_reset():
|
||||
answers = {}
|
||||
answer = self.runTrial()
|
||||
d = answers.setdefault(answer['answer'], {
|
||||
'count': 0,
|
||||
@ -133,7 +130,6 @@ class Copycat(object):
|
||||
return answers
|
||||
|
||||
def run_forever(self, initial, modified, target):
|
||||
self.reset_with_strings(initial, modified, target)
|
||||
self.workspace.resetWithStrings(initial, modified, target)
|
||||
while True:
|
||||
self.check_reset()
|
||||
self.runTrial()
|
||||
|
||||
36
copycat/gui/gui.py
Executable file → Normal file
36
copycat/gui/gui.py
Executable file → Normal file
@ -30,25 +30,18 @@ class MainApplication(GridFrame):
|
||||
self.add(self.primary, 0, 0, xspan=2)
|
||||
self.create_widgets()
|
||||
GridFrame.configure(self)
|
||||
|
||||
#self.messages = []
|
||||
|
||||
#def log(self, message):
|
||||
# self.messages.append(message)
|
||||
|
||||
def create_widgets(self):
|
||||
self.slipList = List(self, 10)
|
||||
columns = 20
|
||||
self.slipList = List(self, columns)
|
||||
self.add(self.slipList, 0, 1)
|
||||
|
||||
self.codeletList = List(self, 10)
|
||||
self.codeletList = List(self, columns)
|
||||
self.add(self.codeletList, 1, 1)
|
||||
|
||||
self.objectList = List(self, 10)
|
||||
self.objectList = List(self, columns)
|
||||
self.add(self.objectList, 2, 1)
|
||||
|
||||
#self.logBox = List(self, 10)
|
||||
#self.add(self.logBox, 1, 0)
|
||||
|
||||
self.graph2 = Plot(self, 'Answer Distribution')
|
||||
self.add(self.graph2, 2, 0)
|
||||
|
||||
@ -62,14 +55,21 @@ class MainApplication(GridFrame):
|
||||
self.slipList.update(slipnodes, key=lambda s:s.activation,
|
||||
formatter=lambda s : '{}: {}'.format(s.name, round(s.activation, 2)))
|
||||
self.codeletList.update(codelets, key=lambda c:c.urgency, formatter= lambda s : '{}: {}'.format(s.name, round(s.urgency, 2)))
|
||||
self.objectList.update(objects, formatter=lambda s : '{}'.format(str(s.descriptions)))
|
||||
#self.logBox.update(list(reversed(self.messages))[:10])
|
||||
self.objectList.update(objects, formatter=lambda s : '{}'.format(', '.join(map(str, s.descriptions))))
|
||||
if len(objects) > 0:
|
||||
print('Descriptions:')
|
||||
for obj in objects:
|
||||
print(obj)
|
||||
for description in obj.descriptions:
|
||||
print(' {}:'.format(description))
|
||||
print(' {}'.format(description.descriptionType.name))
|
||||
print(' {}'.format(description.descriptor.name))
|
||||
|
||||
def reset_with_strings(self, initial, modified, target):
|
||||
self.primary.reset_with_strings(initial, modified, target)
|
||||
|
||||
class GUI(object):
|
||||
def __init__(self, title, updateInterval=.1):
|
||||
def __init__(self, title):
|
||||
self.root = tk.Tk()
|
||||
self.root.title(title)
|
||||
tk.Grid.rowconfigure(self.root, 0, weight=1)
|
||||
@ -79,9 +79,6 @@ class GUI(object):
|
||||
|
||||
configure_style(ttk.Style())
|
||||
|
||||
self.lastUpdated = time.time()
|
||||
self.updateInterval = updateInterval
|
||||
|
||||
def add_answers(self, answers):
|
||||
def modifier(status):
|
||||
with plt.style.context(('dark_background')):
|
||||
@ -92,7 +89,8 @@ class GUI(object):
|
||||
self.root.update_idletasks()
|
||||
self.root.update()
|
||||
|
||||
def paused(self):
|
||||
return self.app.primary.control.paused
|
||||
|
||||
def update(self, copycat):
|
||||
current = time.time()
|
||||
self.app.update(copycat)
|
||||
self.lastUpdated = current
|
||||
|
||||
@ -7,66 +7,23 @@ from tkinter import filedialog
|
||||
from .control import Control
|
||||
from .gridframe import GridFrame
|
||||
|
||||
font1Size = 32
|
||||
font2Size = 16
|
||||
font1 = ('Helvetica', str(font1Size))
|
||||
font2 = ('Helvetica', str(font2Size))
|
||||
|
||||
style = dict(background='black',
|
||||
foreground='white',
|
||||
font=font2)
|
||||
|
||||
def create_main_canvas(root, initial, final, new, guess):
|
||||
padding = 100
|
||||
|
||||
canvas = tk.Canvas(root, background='black')
|
||||
|
||||
def add_sequences(sequences, x, y):
|
||||
for sequence in sequences:
|
||||
x += padding
|
||||
if sequence is None:
|
||||
sequence = ''
|
||||
for char in sequence:
|
||||
canvas.create_text(x, y, text=char, anchor=tk.NW, font=font1, fill='white')
|
||||
x += font1Size
|
||||
return x, y
|
||||
|
||||
x = 0
|
||||
y = padding
|
||||
|
||||
add_sequences([initial, final], x, y)
|
||||
|
||||
x = 0
|
||||
y += padding
|
||||
|
||||
add_sequences([new, guess], x, y)
|
||||
|
||||
#canvas['height'] = str(int(canvas['height']) + padding)
|
||||
#canvas['width'] = str(int(canvas['width']) + padding)
|
||||
|
||||
return canvas
|
||||
from .workspacecanvas import WorkspaceCanvas
|
||||
|
||||
class Primary(GridFrame):
|
||||
|
||||
def __init__(self, parent, *args, **kwargs):
|
||||
GridFrame.__init__(self, parent, *args, **kwargs)
|
||||
|
||||
self.initial = ''
|
||||
self.modified = ''
|
||||
self.target = ''
|
||||
|
||||
self.canvas = create_main_canvas(self, self.initial, self.modified, self.target, '')
|
||||
self.canvas = WorkspaceCanvas(self)
|
||||
self.add(self.canvas, 0, 0, xspan=2)
|
||||
|
||||
self.control = Control(self)
|
||||
self.add(self.control, 0, 2)
|
||||
|
||||
GridFrame.configure(self)
|
||||
|
||||
def update(self, copycat):
|
||||
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)
|
||||
self.canvas.update(copycat)
|
||||
|
||||
def reset_with_strings(self, initial, modified, target):
|
||||
self.initial = initial
|
||||
self.modified = modified
|
||||
self.target = target
|
||||
self.canvas.reset_with_strings(initial, modified, target)
|
||||
|
||||
159787
copycat/gui/sys
Normal file
159787
copycat/gui/sys
Normal file
File diff suppressed because it is too large
Load Diff
3093
copycat/gui/time
Normal file
3093
copycat/gui/time
Normal file
File diff suppressed because it is too large
Load Diff
64
copycat/gui/workspacecanvas.py
Normal file
64
copycat/gui/workspacecanvas.py
Normal file
@ -0,0 +1,64 @@
|
||||
import tkinter as tk
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
from .gridframe import GridFrame
|
||||
|
||||
font1Size = 16
|
||||
font1 = ('Helvetica', font1Size)
|
||||
|
||||
class WorkspaceCanvas(GridFrame):
|
||||
|
||||
def __init__(self, parent, *args, **kwargs):
|
||||
GridFrame.__init__(self, parent, *args, **kwargs)
|
||||
|
||||
self.initial = ''
|
||||
self.modified = ''
|
||||
self.target = ''
|
||||
self.answer = ''
|
||||
|
||||
self.changed = False
|
||||
|
||||
self.canvas = tk.Canvas(self, background='black')
|
||||
self.add(self.canvas, 0, 0)
|
||||
GridFrame.configure(self)
|
||||
|
||||
def update(self, copycat):
|
||||
answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule()
|
||||
if answer != self.answer:
|
||||
self.changed = True
|
||||
|
||||
if self.changed:
|
||||
self.canvas.delete('all')
|
||||
self.add_text()
|
||||
|
||||
def add_text(self):
|
||||
padding = 100
|
||||
|
||||
def add_sequences(sequences, x, y):
|
||||
for sequence in sequences:
|
||||
x += padding
|
||||
if sequence is None:
|
||||
sequence = ''
|
||||
for char in sequence:
|
||||
self.canvas.create_text(x, y, text=char, anchor=tk.NW, font=font1, fill='white')
|
||||
x += font1Size
|
||||
return x, y
|
||||
|
||||
x = 0
|
||||
y = padding
|
||||
|
||||
add_sequences([self.initial, self.modified], x, y)
|
||||
|
||||
x = 0
|
||||
y += padding
|
||||
|
||||
add_sequences([self.target, self.answer], x, y)
|
||||
|
||||
def reset_with_strings(self, initial, modified, target):
|
||||
if initial != self.initial or \
|
||||
modified != self.modified or \
|
||||
target != self.target:
|
||||
self.changed = True
|
||||
self.initial = initial
|
||||
self.modified = modified
|
||||
self.target = target
|
||||
2
gui.py
2
gui.py
@ -17,7 +17,7 @@ def main():
|
||||
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 = Copycat(reporter=SimpleReporter(), rng_seed=options.seed, gui=True)
|
||||
copycat.runGUI()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user