git mv context.py -> copycat.py; and start work on a "reporter" API.
The goal here is to use `curses` to display the coderack, slipnet, and temperature in real time. A further goal would be a reporter that sent the data over websockets to a browser, at which point I could throw this thing up on Heroku and let people mess with it. (Not that that would be very entertaining, yet.)
This commit is contained in:
@ -5,13 +5,25 @@ from temperature import Temperature
|
||||
from workspace import Workspace
|
||||
|
||||
|
||||
class Context(object):
|
||||
def __init__(self, rng_seed=None):
|
||||
class CopycatReporter(object):
|
||||
def report_coderack(self, coderack):
|
||||
pass
|
||||
|
||||
def report_slipnet(self, slipnet):
|
||||
pass
|
||||
|
||||
def report_temperature(self, temperature):
|
||||
pass
|
||||
|
||||
|
||||
class Copycat(object):
|
||||
def __init__(self, rng_seed=None, reporter=None):
|
||||
self.coderack = Coderack(self)
|
||||
self.random = Randomness(rng_seed)
|
||||
self.slipnet = Slipnet()
|
||||
self.temperature = Temperature()
|
||||
self.workspace = Workspace(self)
|
||||
self.reporter = reporter or CopycatReporter()
|
||||
|
||||
def mainLoop(self, lastUpdate):
|
||||
currentTime = self.coderack.codeletsRun
|
||||
@ -23,7 +35,10 @@ class Context(object):
|
||||
self.slipnet.update(self.random)
|
||||
self.temperature.update(self.workspace.getUpdatedTemperature())
|
||||
lastUpdate = currentTime
|
||||
self.reporter.report_slipnet(self.slipnet)
|
||||
self.coderack.chooseAndRunCodelet()
|
||||
self.reporter.report_coderack(self.coderack)
|
||||
self.reporter.report_temperature(self.temperature)
|
||||
return lastUpdate
|
||||
|
||||
def runTrial(self, answers):
|
||||
@ -1,31 +1,26 @@
|
||||
"""Run the copycat program"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from context import Context
|
||||
from copycat import Copycat
|
||||
|
||||
def main(program, args):
|
||||
"""Run the program"""
|
||||
logging.basicConfig(level=logging.WARN, format='%(message)s',
|
||||
filename='./copycat.log', filemode='w')
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.WARN, format='%(message)s', filename='./copycat.log', filemode='w')
|
||||
|
||||
try:
|
||||
args = sys.argv[1:]
|
||||
if len(args) == 4:
|
||||
initial, modified, target = args[:-1]
|
||||
iterations = int(args[-1])
|
||||
else:
|
||||
initial, modified, target = args
|
||||
iterations = 1
|
||||
copycat = Context(rng_seed=42)
|
||||
answers = copycat.run(initial, modified, target, iterations)
|
||||
for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']):
|
||||
print '%s: %d (avg time %.1f, avg temp %.1f)' % (answer, d['count'], d['avgtime'], d['avgtemp'])
|
||||
return 0
|
||||
except ValueError:
|
||||
print >> sys.stderr, 'Usage: %s initial modified target [iterations]' % program
|
||||
return 1
|
||||
print >>sys.stderr, 'Usage: %s initial modified target [iterations]' % sys.argv[0]
|
||||
sys.exit(1)
|
||||
|
||||
copycat = Copycat()
|
||||
answers = copycat.run(initial, modified, target, iterations)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv[0], sys.argv[1:]))
|
||||
for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']):
|
||||
print '%s: %d (avg time %.1f, avg temp %.1f)' % (answer, d['count'], d['avgtime'], d['avgtemp'])
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import unittest
|
||||
|
||||
from context import Context
|
||||
from copycat import Copycat
|
||||
|
||||
|
||||
def pnormaldist(p):
|
||||
@ -67,7 +67,7 @@ class TestCopycat(unittest.TestCase):
|
||||
self.fail('No instances of expected key %s were produced! %r != %r' % (k, actual, expected))
|
||||
|
||||
def run_testcase(self, initial, modified, target, iterations, expected):
|
||||
actual = Context().run(initial, modified, target, iterations)
|
||||
actual = Copycat().run(initial, modified, target, iterations)
|
||||
self.assertEqual(sum(a['count'] for a in actual.values()), iterations)
|
||||
self.assertProbabilitiesLookRoughlyLike(actual, expected)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user