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
|
from workspace import Workspace
|
||||||
|
|
||||||
|
|
||||||
class Context(object):
|
class CopycatReporter(object):
|
||||||
def __init__(self, rng_seed=None):
|
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.coderack = Coderack(self)
|
||||||
self.random = Randomness(rng_seed)
|
self.random = Randomness(rng_seed)
|
||||||
self.slipnet = Slipnet()
|
self.slipnet = Slipnet()
|
||||||
self.temperature = Temperature()
|
self.temperature = Temperature()
|
||||||
self.workspace = Workspace(self)
|
self.workspace = Workspace(self)
|
||||||
|
self.reporter = reporter or CopycatReporter()
|
||||||
|
|
||||||
def mainLoop(self, lastUpdate):
|
def mainLoop(self, lastUpdate):
|
||||||
currentTime = self.coderack.codeletsRun
|
currentTime = self.coderack.codeletsRun
|
||||||
@ -23,7 +35,10 @@ class Context(object):
|
|||||||
self.slipnet.update(self.random)
|
self.slipnet.update(self.random)
|
||||||
self.temperature.update(self.workspace.getUpdatedTemperature())
|
self.temperature.update(self.workspace.getUpdatedTemperature())
|
||||||
lastUpdate = currentTime
|
lastUpdate = currentTime
|
||||||
|
self.reporter.report_slipnet(self.slipnet)
|
||||||
self.coderack.chooseAndRunCodelet()
|
self.coderack.chooseAndRunCodelet()
|
||||||
|
self.reporter.report_coderack(self.coderack)
|
||||||
|
self.reporter.report_temperature(self.temperature)
|
||||||
return lastUpdate
|
return lastUpdate
|
||||||
|
|
||||||
def runTrial(self, answers):
|
def runTrial(self, answers):
|
||||||
@ -1,31 +1,26 @@
|
|||||||
"""Run the copycat program"""
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from context import Context
|
from copycat import Copycat
|
||||||
|
|
||||||
def main(program, args):
|
|
||||||
"""Run the program"""
|
if __name__ == '__main__':
|
||||||
logging.basicConfig(level=logging.WARN, format='%(message)s',
|
logging.basicConfig(level=logging.WARN, format='%(message)s', filename='./copycat.log', filemode='w')
|
||||||
filename='./copycat.log', filemode='w')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
args = sys.argv[1:]
|
||||||
if len(args) == 4:
|
if len(args) == 4:
|
||||||
initial, modified, target = args[:-1]
|
initial, modified, target = args[:-1]
|
||||||
iterations = int(args[-1])
|
iterations = int(args[-1])
|
||||||
else:
|
else:
|
||||||
initial, modified, target = args
|
initial, modified, target = args
|
||||||
iterations = 1
|
iterations = 1
|
||||||
copycat = Context(rng_seed=42)
|
except ValueError:
|
||||||
|
print >>sys.stderr, 'Usage: %s initial modified target [iterations]' % sys.argv[0]
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
copycat = Copycat()
|
||||||
answers = copycat.run(initial, modified, target, iterations)
|
answers = copycat.run(initial, modified, target, iterations)
|
||||||
|
|
||||||
for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']):
|
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'])
|
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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.exit(main(sys.argv[0], sys.argv[1:]))
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from context import Context
|
from copycat import Copycat
|
||||||
|
|
||||||
|
|
||||||
def pnormaldist(p):
|
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))
|
self.fail('No instances of expected key %s were produced! %r != %r' % (k, actual, expected))
|
||||||
|
|
||||||
def run_testcase(self, initial, modified, target, iterations, 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.assertEqual(sum(a['count'] for a in actual.values()), iterations)
|
||||||
self.assertProbabilitiesLookRoughlyLike(actual, expected)
|
self.assertProbabilitiesLookRoughlyLike(actual, expected)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user