Merge branch 'master' of https://github.com/Alex-Linhares/co.py.cat
This commit is contained in:
304866
copycat.log
304866
copycat.log
File diff suppressed because it is too large
Load Diff
@ -1010,7 +1010,7 @@ def correspondence_strength_tester(ctx, codelet):
|
|||||||
objectFromTarget.flipped_version())))
|
objectFromTarget.flipped_version())))
|
||||||
correspondence.updateStrength()
|
correspondence.updateStrength()
|
||||||
strength = correspondence.totalStrength
|
strength = correspondence.totalStrength
|
||||||
#TODO: use entropy
|
# TODO: use entropy
|
||||||
probability = temperature.getAdjustedProbability(strength / 100.0)
|
probability = temperature.getAdjustedProbability(strength / 100.0)
|
||||||
if random.coinFlip(probability):
|
if random.coinFlip(probability):
|
||||||
# activate some concepts
|
# activate some concepts
|
||||||
@ -1064,8 +1064,8 @@ def correspondence_builder(ctx, codelet):
|
|||||||
# if there is an incompatible bond then fight against it
|
# if there is an incompatible bond then fight against it
|
||||||
initial = correspondence.objectFromInitial
|
initial = correspondence.objectFromInitial
|
||||||
target = correspondence.objectFromTarget
|
target = correspondence.objectFromTarget
|
||||||
if (initial.leftmost or initial.rightmost and
|
if (initial.leftmost or initial.rightmost and
|
||||||
target.leftmost or target.rightmost):
|
target.leftmost or target.rightmost):
|
||||||
# search for the incompatible bond
|
# search for the incompatible bond
|
||||||
incompatibleBond = correspondence.getIncompatibleBond()
|
incompatibleBond = correspondence.getIncompatibleBond()
|
||||||
if incompatibleBond:
|
if incompatibleBond:
|
||||||
|
|||||||
@ -83,7 +83,7 @@ class Coderack(object):
|
|||||||
if 'correspondence' in codeletName:
|
if 'correspondence' in codeletName:
|
||||||
return workspace.interStringUnhappiness / 100.0
|
return workspace.interStringUnhappiness / 100.0
|
||||||
if 'description' in codeletName:
|
if 'description' in codeletName:
|
||||||
#TODO: use entropy
|
# TODO: use entropy
|
||||||
return (temperature.value() / 100.0) ** 2
|
return (temperature.value() / 100.0) ** 2
|
||||||
return workspace.intraStringUnhappiness / 100.0
|
return workspace.intraStringUnhappiness / 100.0
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ class Coderack(object):
|
|||||||
if codeletName == 'breaker':
|
if codeletName == 'breaker':
|
||||||
urgency = 1
|
urgency = 1
|
||||||
|
|
||||||
#TODO: use entropy
|
# TODO: use entropy
|
||||||
if temperature.value() < 25.0 and 'translator' in codeletName:
|
if temperature.value() < 25.0 and 'translator' in codeletName:
|
||||||
urgency = 5
|
urgency = 5
|
||||||
for _ in range(howMany):
|
for _ in range(howMany):
|
||||||
@ -291,7 +291,7 @@ class Coderack(object):
|
|||||||
temperature = self.ctx.temperature
|
temperature = self.ctx.temperature
|
||||||
assert self.codelets
|
assert self.codelets
|
||||||
|
|
||||||
#TODO: use entropy
|
# TODO: use entropy
|
||||||
scale = (100.0 - temperature.value() + 10.0) / 15.0
|
scale = (100.0 - temperature.value() + 10.0) / 15.0
|
||||||
chosen = random.weighted_choice(self.codelets, [codelet.urgency ** scale for codelet in self.codelets])
|
chosen = random.weighted_choice(self.codelets, [codelet.urgency ** scale for codelet in self.codelets])
|
||||||
self.removeCodelet(chosen)
|
self.removeCodelet(chosen)
|
||||||
|
|||||||
@ -27,11 +27,11 @@ def lower_bound_on_probability(hits, attempts, confidence=0.95):
|
|||||||
if attempts == 0:
|
if attempts == 0:
|
||||||
return 0
|
return 0
|
||||||
z = pnormaldist(confidence)
|
z = pnormaldist(confidence)
|
||||||
zsqr = z*z
|
zsqr = z * z
|
||||||
phat = 1.0 * hits / attempts
|
phat = 1.0 * hits / attempts
|
||||||
under_sqrt = (phat * (1 - phat) + zsqr / (4*attempts)) / attempts
|
under_sqrt = (phat * (1 - phat) + zsqr / (4 * attempts)) / attempts
|
||||||
denominator = (1 + zsqr / attempts)
|
denominator = (1 + zsqr / attempts)
|
||||||
return (phat + zsqr / (2*attempts) - z * (under_sqrt ** 0.5)) / denominator
|
return (phat + zsqr / (2 * attempts) - z * (under_sqrt ** 0.5)) / denominator
|
||||||
|
|
||||||
|
|
||||||
def upper_bound_on_probability(hits, attempts, confidence=0.95):
|
def upper_bound_on_probability(hits, attempts, confidence=0.95):
|
||||||
|
|||||||
@ -44,6 +44,16 @@ class Workspace(object):
|
|||||||
self.modified = WorkspaceString(self.ctx, self.modifiedString)
|
self.modified = WorkspaceString(self.ctx, self.modifiedString)
|
||||||
self.target = WorkspaceString(self.ctx, self.targetString)
|
self.target = WorkspaceString(self.ctx, self.targetString)
|
||||||
|
|
||||||
|
'''
|
||||||
|
# TODO: Initial part of refactoring in this method
|
||||||
|
def getAssessedUnhappiness(self, unhappiness):
|
||||||
|
o.Unhappiness = __adjustUnhappiness(
|
||||||
|
o.relativeImportance * o.Unhappiness
|
||||||
|
for o in self.objects)
|
||||||
|
pass
|
||||||
|
'''
|
||||||
|
|
||||||
|
# TODO: Extract method?
|
||||||
def assessUnhappiness(self):
|
def assessUnhappiness(self):
|
||||||
self.intraStringUnhappiness = __adjustUnhappiness(
|
self.intraStringUnhappiness = __adjustUnhappiness(
|
||||||
o.relativeImportance * o.intraStringUnhappiness
|
o.relativeImportance * o.intraStringUnhappiness
|
||||||
@ -55,6 +65,7 @@ class Workspace(object):
|
|||||||
o.relativeImportance * o.totalUnhappiness
|
o.relativeImportance * o.totalUnhappiness
|
||||||
for o in self.objects)
|
for o in self.objects)
|
||||||
|
|
||||||
|
# TODO: these 3 methods seem to be the same... are they? If so, Extract method.
|
||||||
def calculateIntraStringUnhappiness(self):
|
def calculateIntraStringUnhappiness(self):
|
||||||
value = sum(
|
value = sum(
|
||||||
o.relativeImportance * o.intraStringUnhappiness
|
o.relativeImportance * o.intraStringUnhappiness
|
||||||
@ -102,7 +113,7 @@ class Workspace(object):
|
|||||||
))
|
))
|
||||||
|
|
||||||
def numberOfUnrelatedObjects(self):
|
def numberOfUnrelatedObjects(self):
|
||||||
"""A list of all objects in the workspace with >= 1 open bond slots"""
|
"""Computes the number of all objects in the workspace with >= 1 open bond slots."""
|
||||||
objects = [o for o in self.objects
|
objects = [o for o in self.objects
|
||||||
if o.string == self.initial or o.string == self.target]
|
if o.string == self.initial or o.string == self.target]
|
||||||
objects = [o for o in objects if not o.spansString()]
|
objects = [o for o in objects if not o.spansString()]
|
||||||
@ -120,21 +131,21 @@ class Workspace(object):
|
|||||||
return len(objects)
|
return len(objects)
|
||||||
|
|
||||||
def numberOfUnreplacedObjects(self):
|
def numberOfUnreplacedObjects(self):
|
||||||
"""A list of all unreplaced objects in the initial string"""
|
"""A list of all unreplaced objects in the initial string."""
|
||||||
objects = [o for o in self.objects
|
objects = [o for o in self.objects
|
||||||
if o.string == self.initial and isinstance(o, Letter)]
|
if o.string == self.initial and isinstance(o, Letter)]
|
||||||
objects = [o for o in objects if not o.replacement]
|
objects = [o for o in objects if not o.replacement]
|
||||||
return len(objects)
|
return len(objects)
|
||||||
|
|
||||||
def numberOfUncorrespondingObjects(self):
|
def numberOfUncorrespondingObjects(self):
|
||||||
"""A list of all uncorresponded objects in the initial string"""
|
"""A list of all uncorresponded objects in the initial string."""
|
||||||
objects = [o for o in self.objects
|
objects = [o for o in self.objects
|
||||||
if o.string == self.initial or o.string == self.target]
|
if o.string == self.initial or o.string == self.target]
|
||||||
objects = [o for o in objects if not o.correspondence]
|
objects = [o for o in objects if not o.correspondence]
|
||||||
return len(objects)
|
return len(objects)
|
||||||
|
|
||||||
def numberOfBonds(self):
|
def numberOfBonds(self):
|
||||||
"""The number of bonds in the workspace"""
|
"""The number of bonds in the workspace."""
|
||||||
return sum(1 for o in self.structures if isinstance(o, Bond))
|
return sum(1 for o in self.structures if isinstance(o, Bond))
|
||||||
|
|
||||||
def correspondences(self):
|
def correspondences(self):
|
||||||
|
|||||||
@ -38,7 +38,7 @@ class WorkspaceString(object):
|
|||||||
return self.string[i]
|
return self.string[i]
|
||||||
|
|
||||||
def updateRelativeImportance(self):
|
def updateRelativeImportance(self):
|
||||||
"""Update the normalised importance of all objects in the string"""
|
"""Update the normalised importance of all objects in the string."""
|
||||||
total = sum(o.rawImportance for o in self.objects)
|
total = sum(o.rawImportance for o in self.objects)
|
||||||
if not total:
|
if not total:
|
||||||
for o in self.objects:
|
for o in self.objects:
|
||||||
|
|||||||
26
main.py
26
main.py
@ -1,15 +1,40 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Main Copycat program.
|
||||||
|
|
||||||
|
To run it, type at the terminal:
|
||||||
|
|
||||||
|
> python main.py abc abd ppqqrr --interations 10
|
||||||
|
|
||||||
|
The script takes three to five arguments. The first two are a pair of strings
|
||||||
|
with some change, for example "abc" and "abd". The third is a string which the
|
||||||
|
script should try to change analogously. The fourth (which defaults to "1") is
|
||||||
|
a number of iterations. One can also specify a defined seed falue for the
|
||||||
|
random number generator.
|
||||||
|
|
||||||
|
The above might produce output such as
|
||||||
|
|
||||||
|
ppqqss: 6 (avg time 869.0, avg temp 23.4)
|
||||||
|
ppqqrs: 4 (avg time 439.0, avg temp 37.3)
|
||||||
|
|
||||||
|
The first number indicates how many times Copycat chose that string as its
|
||||||
|
answer; higher means "more obvious". The last number indicates the average
|
||||||
|
final temperature of the workspace; lower means "more elegant".
|
||||||
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from copycat import Copycat, Reporter
|
from copycat import Copycat, Reporter
|
||||||
|
|
||||||
|
|
||||||
class SimpleReporter(Reporter):
|
class SimpleReporter(Reporter):
|
||||||
def report_answer(self, answer):
|
def report_answer(self, answer):
|
||||||
print('Answered %s (time %d, final temperature %.1f)' % (
|
print('Answered %s (time %d, final temperature %.1f)' % (
|
||||||
answer['answer'], answer['time'], answer['temp'],
|
answer['answer'], answer['time'], answer['temp'],
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig(level=logging.INFO, format='%(message)s', filename='./copycat.log', filemode='w')
|
logging.basicConfig(level=logging.INFO, format='%(message)s', filename='./copycat.log', filemode='w')
|
||||||
|
|
||||||
@ -27,5 +52,6 @@ def main():
|
|||||||
for answer, d in sorted(iter(answers.items()), key=lambda kv: kv[1]['avgtemp']):
|
for answer, d in sorted(iter(answers.items()), 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']))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user