Run multiple iterations. Print final temperatures. Reduce stdout spew.

This makes the output of the program more closely resemble that of the
original Copycat described in "FCCA" page 236:

> [T]he average final temperature of an answer can be thought of as
> the program's own assessment of that answer's quality, with lower
> temperatures meaning higher quality.

For example, running `python main.py abc abd ijk 100` produced the
following output:

    ijl: 98 (avg temp 16.0)
    jjk: 1 (avg temp 56.3)
    ijk: 1 (avg temp 57.9)

And for `python main.py abc abd ijkk 100`:

    ijkkk: 2 (avg temp 19.8)
    ijkl: 51 (avg temp 28.1)
    ijll: 46 (avg temp 28.9)
    djkk: 1 (avg temp 77.4)
This commit is contained in:
Arthur O'Dwyer
2017-04-14 11:59:52 -07:00
parent ed1d95896e
commit a2260cdaf6
3 changed files with 28 additions and 17 deletions

View File

@ -31,7 +31,7 @@ def __showWhichStringObjectIsFrom(structure):
whence = 'target'
if structure.string == workspace.initial:
whence = 'initial'
print 'object chosen = %s from %s string' % (structure, whence)
#print 'object chosen = %s from %s string' % (structure, whence)
def __getScoutSource(slipnode, relevanceMethod, typeName):

View File

@ -1,7 +1,4 @@
import logging
logging.basicConfig(level=logging.INFO, format='%(message)s',
filename='./copycat.log', filemode='w')
from workspace import workspace
from workspaceFormulas import workspaceFormulas
@ -33,9 +30,8 @@ def mainLoop(lastUpdate):
return result
def runTrial():
def runTrial(answers):
"""Run a trial of the copycat algorithm"""
answers = {}
slipnet.reset()
workspace.reset()
coderack.reset()
@ -46,13 +42,20 @@ def runTrial():
answer = workspace.rule.finalAnswer
else:
answer = None
print '%d: %s' % (coderack.codeletsRun, answer)
answers[answer] = answers.get(answer, 0) + 1
logging.debug('codelets used:')
for answer, count in answers.iteritems():
print '%s:%d' % (answer, count)
finalTemperature = temperature.value
print 'Answered %s (time %d, final temperature %.1f)' % (answer, coderack.codeletsRun, finalTemperature)
answers[answer] = answers.get(answer, {'count': 0, 'tempsum': 0})
answers[answer]['count'] += 1
answers[answer]['tempsum'] += finalTemperature
def run(initial, modified, target):
def run(initial, modified, target, iterations):
workspace.setStrings(initial, modified, target)
runTrial()
answers = {}
for i in xrange(iterations):
runTrial(answers)
for answer, d in answers.iteritems():
d['avgtemp'] = d['tempsum'] / d['count']
d.pop('tempsum')
for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']):
print '%s: %d (avg temp %.1f)' % (answer, d['count'], d['avgtemp'])

View File

@ -1,6 +1,6 @@
"""Run the copycat program"""
import logging
import sys
import copycat
@ -8,12 +8,20 @@ import copycat
def main(program, args):
"""Run the program"""
logging.basicConfig(level=logging.WARN, format='%(message)s',
filename='./copycat.log', filemode='w')
try:
initial, modified, target = args
copycat.run(initial, modified, target)
if len(args) == 4:
initial, modified, target = args[:-1]
iterations = int(args[-1])
else:
initial, modified, target = args
iterations = 1
copycat.run(initial, modified, target, iterations)
return 0
except ValueError:
print >> sys.stderr, 'Usage: %s initial modified target' % program
print >> sys.stderr, 'Usage: %s initial modified target [iterations]' % program
return 1