Teach Context to be self-sufficient. NFC.
You can now create and run a Copycat instance by just saying
`Context().run('abc', 'abd', 'efg', 100)`!
This commit is contained in:
@ -1,13 +1,18 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from coderack import Coderack
|
||||||
|
from randomness import Randomness
|
||||||
|
from slipnet import Slipnet
|
||||||
|
from temperature import Temperature
|
||||||
|
from workspace import Workspace
|
||||||
|
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
def __init__(self):
|
def __init__(self, rng_seed=None):
|
||||||
self.coderack = None
|
self.coderack = Coderack(self)
|
||||||
self.random = None
|
self.random = Randomness(rng_seed)
|
||||||
self.slipnet = None
|
self.slipnet = Slipnet()
|
||||||
self.temperature = None
|
self.temperature = Temperature()
|
||||||
self.workspace = None
|
self.workspace = Workspace(self)
|
||||||
|
|
||||||
def mainLoop(self, lastUpdate):
|
def mainLoop(self, lastUpdate):
|
||||||
currentTime = self.coderack.codeletsRun
|
currentTime = self.coderack.codeletsRun
|
||||||
@ -45,7 +50,7 @@ class Context(object):
|
|||||||
answers[answer]['timesum'] += finalTime
|
answers[answer]['timesum'] += finalTime
|
||||||
|
|
||||||
def run(self, initial, modified, target, iterations):
|
def run(self, initial, modified, target, iterations):
|
||||||
self.workspace.setStrings(initial, modified, target)
|
self.workspace.resetWithStrings(initial, modified, target)
|
||||||
answers = {}
|
answers = {}
|
||||||
for i in xrange(iterations):
|
for i in xrange(iterations):
|
||||||
self.runTrial(answers)
|
self.runTrial(answers)
|
||||||
@ -53,6 +58,3 @@ class Context(object):
|
|||||||
d['avgtemp'] = d.pop('tempsum') / d['count']
|
d['avgtemp'] = d.pop('tempsum') / d['count']
|
||||||
d['avgtime'] = d.pop('timesum') / d['count']
|
d['avgtime'] = d.pop('timesum') / d['count']
|
||||||
return answers
|
return answers
|
||||||
|
|
||||||
|
|
||||||
context = Context()
|
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
from coderack import Coderack
|
|
||||||
from randomness import Randomness
|
|
||||||
from slipnet import Slipnet
|
|
||||||
from temperature import Temperature
|
|
||||||
from workspace import Workspace
|
|
||||||
|
|
||||||
from context import context
|
|
||||||
|
|
||||||
|
|
||||||
context.coderack = Coderack(context)
|
|
||||||
context.random = Randomness(42)
|
|
||||||
context.slipnet = Slipnet()
|
|
||||||
context.temperature = Temperature()
|
|
||||||
context.workspace = Workspace(context)
|
|
||||||
|
|
||||||
|
|
||||||
def run(initial, modified, target, iterations):
|
|
||||||
return context.run(initial, modified, target, iterations)
|
|
||||||
@ -3,8 +3,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import copycat
|
from context import Context
|
||||||
|
|
||||||
|
|
||||||
def main(program, args):
|
def main(program, args):
|
||||||
"""Run the program"""
|
"""Run the program"""
|
||||||
@ -18,6 +17,7 @@ def main(program, args):
|
|||||||
else:
|
else:
|
||||||
initial, modified, target = args
|
initial, modified, target = args
|
||||||
iterations = 1
|
iterations = 1
|
||||||
|
copycat = Context(rng_seed=42)
|
||||||
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'])
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import copycat
|
from context import Context
|
||||||
|
|
||||||
def pnormaldist(p):
|
def pnormaldist(p):
|
||||||
table = {
|
table = {
|
||||||
@ -66,7 +66,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 = copycat.run(initial, modified, target, iterations)
|
actual = Context().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)
|
||||||
|
|
||||||
|
|||||||
@ -16,10 +16,7 @@ def __adjustUnhappiness(values):
|
|||||||
|
|
||||||
class Workspace(object):
|
class Workspace(object):
|
||||||
def __init__(self, ctx):
|
def __init__(self, ctx):
|
||||||
#logging.debug('workspace.__init__()')
|
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
self.setStrings('', '', '')
|
|
||||||
self.reset()
|
|
||||||
self.totalUnhappiness = 0.0
|
self.totalUnhappiness = 0.0
|
||||||
self.intraStringUnhappiness = 0.0
|
self.intraStringUnhappiness = 0.0
|
||||||
self.interStringUnhappiness = 0.0
|
self.interStringUnhappiness = 0.0
|
||||||
@ -28,13 +25,13 @@ class Workspace(object):
|
|||||||
return '<Workspace trying %s:%s::%s:?>' % (
|
return '<Workspace trying %s:%s::%s:?>' % (
|
||||||
self.initialString, self.modifiedString, self.targetString)
|
self.initialString, self.modifiedString, self.targetString)
|
||||||
|
|
||||||
def setStrings(self, initial, modified, target):
|
def resetWithStrings(self, initial, modified, target):
|
||||||
self.targetString = target
|
self.targetString = target
|
||||||
self.initialString = initial
|
self.initialString = initial
|
||||||
self.modifiedString = modified
|
self.modifiedString = modified
|
||||||
|
self.reset()
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
#logging.debug('workspace.reset()')
|
|
||||||
self.foundAnswer = False
|
self.foundAnswer = False
|
||||||
self.changedObject = None
|
self.changedObject = None
|
||||||
self.objects = []
|
self.objects = []
|
||||||
|
|||||||
Reference in New Issue
Block a user