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
|
||||
from coderack import Coderack
|
||||
from randomness import Randomness
|
||||
from slipnet import Slipnet
|
||||
from temperature import Temperature
|
||||
from workspace import Workspace
|
||||
|
||||
|
||||
class Context(object):
|
||||
def __init__(self):
|
||||
self.coderack = None
|
||||
self.random = None
|
||||
self.slipnet = None
|
||||
self.temperature = None
|
||||
self.workspace = None
|
||||
def __init__(self, rng_seed=None):
|
||||
self.coderack = Coderack(self)
|
||||
self.random = Randomness(rng_seed)
|
||||
self.slipnet = Slipnet()
|
||||
self.temperature = Temperature()
|
||||
self.workspace = Workspace(self)
|
||||
|
||||
def mainLoop(self, lastUpdate):
|
||||
currentTime = self.coderack.codeletsRun
|
||||
@ -45,7 +50,7 @@ class Context(object):
|
||||
answers[answer]['timesum'] += finalTime
|
||||
|
||||
def run(self, initial, modified, target, iterations):
|
||||
self.workspace.setStrings(initial, modified, target)
|
||||
self.workspace.resetWithStrings(initial, modified, target)
|
||||
answers = {}
|
||||
for i in xrange(iterations):
|
||||
self.runTrial(answers)
|
||||
@ -53,6 +58,3 @@ class Context(object):
|
||||
d['avgtemp'] = d.pop('tempsum') / d['count']
|
||||
d['avgtime'] = d.pop('timesum') / d['count']
|
||||
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 sys
|
||||
|
||||
import copycat
|
||||
|
||||
from context import Context
|
||||
|
||||
def main(program, args):
|
||||
"""Run the program"""
|
||||
@ -18,6 +17,7 @@ def main(program, args):
|
||||
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'])
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
import unittest
|
||||
|
||||
import copycat
|
||||
from context import Context
|
||||
|
||||
def pnormaldist(p):
|
||||
table = {
|
||||
@ -66,7 +66,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 = 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.assertProbabilitiesLookRoughlyLike(actual, expected)
|
||||
|
||||
|
||||
@ -16,10 +16,7 @@ def __adjustUnhappiness(values):
|
||||
|
||||
class Workspace(object):
|
||||
def __init__(self, ctx):
|
||||
#logging.debug('workspace.__init__()')
|
||||
self.ctx = ctx
|
||||
self.setStrings('', '', '')
|
||||
self.reset()
|
||||
self.totalUnhappiness = 0.0
|
||||
self.intraStringUnhappiness = 0.0
|
||||
self.interStringUnhappiness = 0.0
|
||||
@ -28,13 +25,13 @@ class Workspace(object):
|
||||
return '<Workspace trying %s:%s::%s:?>' % (
|
||||
self.initialString, self.modifiedString, self.targetString)
|
||||
|
||||
def setStrings(self, initial, modified, target):
|
||||
def resetWithStrings(self, initial, modified, target):
|
||||
self.targetString = target
|
||||
self.initialString = initial
|
||||
self.modifiedString = modified
|
||||
self.reset()
|
||||
|
||||
def reset(self):
|
||||
#logging.debug('workspace.reset()')
|
||||
self.foundAnswer = False
|
||||
self.changedObject = None
|
||||
self.objects = []
|
||||
|
||||
Reference in New Issue
Block a user