Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion copycat/codeletMethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 16 additions & 12 deletions copycat/copycat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -46,13 +42,21 @@ 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
finalTime = coderack.codeletsRun
print 'Answered %s (time %d, final temperature %.1f)' % (answer, finalTime, finalTemperature)
answers[answer] = answers.get(answer, {'count': 0, 'tempsum': 0, 'timesum': 0})
answers[answer]['count'] += 1
answers[answer]['tempsum'] += finalTemperature
answers[answer]['timesum'] += finalTime


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.pop('tempsum') / d['count']
d['avgtime'] = d.pop('timesum') / d['count']
return answers
18 changes: 14 additions & 4 deletions copycat/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
"""Run the copycat program"""


import logging
import sys

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
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' % program
print >> sys.stderr, 'Usage: %s initial modified target [iterations]' % program
return 1


Expand Down