diff --git a/copycat/copycat.py b/copycat/copycat.py index 7a6e7c3..c899f6d 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -5,6 +5,9 @@ from .temperature import Temperature from .workspace import Workspace from .gui import GUI +from pprint import pprint + + class Reporter(object): """Do-nothing base class for defining new reporter types""" def report_answer(self, answer): @@ -88,6 +91,7 @@ class Copycat(object): self.gui.update(self) self.gui.refresh() answers = {} + self.temperature.useAdj('pbest') while True: if self.check_reset(): answers = {} @@ -108,26 +112,34 @@ class Copycat(object): for answer, d in answers.items(): d['avgtemp'] = d.pop('sumtemp') / d['count'] d['avgtime'] = d.pop('sumtime') / d['count'] + pprint(answers) + return answers def run(self, initial, modified, target, iterations): self.temperature.useAdj('best') self.gui.app.reset_with_strings(initial, modified, target) self.workspace.resetWithStrings(initial, modified, target) answers = {} - for i in range(iterations): - answer = self.runTrial() - d = answers.setdefault(answer['answer'], { - 'count': 0, - 'sumtemp': 0, # TODO: use entropy - 'sumtime': 0 - }) - d['count'] += 1 - d['sumtemp'] += answer['temp'] # TODO: use entropy - d['sumtime'] += answer['time'] + for formula in ['original', 'best', 'sbest', 'pbest']: + self.temperature.useAdj(formula) + answers = {} + for i in range(iterations): + answer = self.runTrial() + d = answers.setdefault(answer['answer'], { + 'count': 0, + 'sumtemp': 0, # TODO: use entropy + 'sumtime': 0 + }) + d['count'] += 1 + d['sumtemp'] += answer['temp'] # TODO: use entropy + d['sumtime'] += answer['time'] + + for answer, d in answers.items(): + d['avgtemp'] = d.pop('sumtemp') / d['count'] + d['avgtime'] = d.pop('sumtime') / d['count'] + print('The formula {} provided:'.format(formula)) + pprint(answers) - for answer, d in answers.items(): - d['avgtemp'] = d.pop('sumtemp') / d['count'] - d['avgtime'] = d.pop('sumtime') / d['count'] return answers def run_forever(self, initial, modified, target): diff --git a/copycat/gui/plot.py b/copycat/gui/plot.py index 8583ff7..02ab57c 100644 --- a/copycat/gui/plot.py +++ b/copycat/gui/plot.py @@ -2,6 +2,8 @@ import matplotlib.pyplot as plt; plt.rcdefaults() import numpy as np import matplotlib.pyplot as plt +plt.style.use('dark_background') + def plot_imbedded(answers, status): answers = sorted(answers.items(), key=lambda kv : kv[1]['count']) objects = [t[0] for t in answers] diff --git a/copycat/temperature.py b/copycat/temperature.py index e62f0a8..2e3781c 100644 --- a/copycat/temperature.py +++ b/copycat/temperature.py @@ -24,7 +24,7 @@ def _entropy(temp, prob): f = (c + 1) * prob return -f * math.log2(f) -def _weighted(temp, prob, s, u): +def _weighted(temp, prob, s, u, alpha=1, beta=1): weighted = (temp / 100) * s + ((100 - temp) / 100) * u return weighted @@ -56,12 +56,31 @@ def _averaged_alt(temp, prob): u = prob ** 2 if prob < .5 else math.sqrt(prob) return _weighted(temp, prob, s, u) + def _working_best(temp, prob): + s = .5 # convergence + r = 2 # power + u = prob ** r if prob < .5 else prob ** (1/r) + return _weighted(temp, prob, s, u) + +def _soft_best(temp, prob): s = .5 # convergence r = 1.05 # power u = prob ** r if prob < .5 else prob ** (1/r) return _weighted(temp, prob, s, u) +def _parameterized_best(temp, prob): + # (D$66/100)*($E$64*$B68 + $G$64*$F$64)/($E$64 + $G$64)+((100-D$66)/100)*IF($B68 > 0.5, $B68^(1/$H$64), $B68^$H$64) + # (T/100) * (alpha * p + beta * .5) / (alpha + beta) + ((100 - T)/100) * IF(p > .5, p^(1/2), p^2) + alpha = 5 + beta = 1 + s = .5 + s = (alpha * prob + beta * s) / (alpha + beta) + r = 1.05 + u = prob ** r if prob < .5 else prob ** (1/r) + return _weighted(temp, prob, s, u) + + class Temperature(object): def __init__(self): self.reset() @@ -75,7 +94,9 @@ class Temperature(object): 'weighted_soft' : _weighted_soft_curve, 'alt_fifty' : _alt_fifty, 'average_alt' : _averaged_alt, - 'best' : _working_best} + 'best' : _working_best, + 'sbest' : _soft_best, + 'pbest' : _parameterized_best} def reset(self): self.actual_value = 100.0 diff --git a/gui.py b/gui.py index 521a4d2..a3c72a5 100755 --- a/gui.py +++ b/gui.py @@ -4,6 +4,10 @@ import logging from copycat import Copycat, Reporter +import matplotlib.pyplot as plt + +plt.style.use('dark_background') + class SimpleReporter(Reporter): def report_answer(self, answer): print('Answered %s (time %d, final temperature %.1f)' % (