diff --git a/copycat/copycat.py b/copycat/copycat.py index f2fee9c..51166d2 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -71,6 +71,13 @@ class Copycat(object): self.temperature.useAdj('original') #self.temperature.useAdj('entropy') #self.temperature.useAdj('inverse') # 100 weight + #self.temperature.useAdj('fifty_converge') + #self.temperature.useAdj('soft') + #self.temperature.useAdj('weighted_soft') + #self.temperature.useAdj('alt_fifty') + #self.temperature.useAdj('average_alt') + self.temperature.useAdj('best') + answers = {} for i in range(iterations): answer = self.runTrial() diff --git a/copycat/temperature.py b/copycat/temperature.py index 2a72e64..e62f0a8 100644 --- a/copycat/temperature.py +++ b/copycat/temperature.py @@ -24,18 +24,58 @@ def _entropy(temp, prob): f = (c + 1) * prob return -f * math.log2(f) -def _inverse_prob(temp, prob): +def _weighted(temp, prob, s, u): + weighted = (temp / 100) * s + ((100 - temp) / 100) * u + return weighted + +def _weighted_inverse(temp, prob): iprob = 1 - prob - return (temp / 100) * iprob + ((100 - temp) / 100) * prob + return _weighted(temp, prob, iprob, prob) + +def _fifty_converge(temp, prob): # Uses .5 instead of 1-prob + return _weighted(temp, prob, .5, prob) + +def _soft_curve(temp, prob): # Curves to the average of the (1-p) and .5 + return min(1, _weighted(temp, prob, (1.5-prob)/2, prob)) + +def _weighted_soft_curve(temp, prob): # Curves to the weighted average of the (1-p) and .5 + weight = 100 + gamma = .5 # convergance value + alpha = 1 # gamma weight + beta = 3 # iprob weight + curved = min(1, (temp / weight) * ((alpha * gamma + beta * (1 - prob)) / (alpha + beta)) + ((weight - temp) / weight) * prob) + return curved + +def _alt_fifty(temp, prob): + s = .5 + u = prob ** 2 if prob < .5 else math.sqrt(prob) + return _weighted(temp, prob, s, u) + +def _averaged_alt(temp, prob): + s = (1.5 - prob)/2 + 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 = 1.05 # power + u = prob ** r if prob < .5 else prob ** (1/r) + return _weighted(temp, prob, s, u) class Temperature(object): def __init__(self): self.reset() self.adjustmentType = 'inverse' self._adjustmentFormulas = { - 'original' : _original, - 'entropy' : _entropy, - 'inverse' : _inverse_prob} + 'original' : _original, + 'entropy' : _entropy, + 'inverse' : _weighted_inverse, + 'fifty_converge' : _fifty_converge, + 'soft' : _soft_curve, + 'weighted_soft' : _weighted_soft_curve, + 'alt_fifty' : _alt_fifty, + 'average_alt' : _averaged_alt, + 'best' : _working_best} def reset(self): self.actual_value = 100.0 diff --git a/tests.py b/tests.py index b194b9a..04f0418 100644 --- a/tests.py +++ b/tests.py @@ -70,16 +70,13 @@ class TestCopycat(unittest.TestCase): def test_abc_xyz(self): self.run_testcase('abc', 'abd', 'xyz', 100, - {'dyz': {'avgtemp': 26.143509984937367, 'avgtime': 9866.625, 'count': 8}, - 'wyz': {'avgtemp': 12.249539212574128, - 'avgtime': 9520.666666666666, - 'count': 18}, - 'xyd': {'avgtemp': 38.73402068486291, 'avgtime': 7439.225, 'count': 40}, - 'xyy': {'avgtemp': 24.614440709519627, 'avgtime': 3522.625, 'count': 8}, - 'xyz': {'avgtemp': 57.674822842028945, 'avgtime': 8315.2, 'count': 5}, - 'yyz': {'avgtemp': 26.874886217740315, - 'avgtime': 8493.142857142857, - 'count': 21}}) + {'dyz': {'avgtemp': 16.78130739435325, 'avgtime': 393.0, 'count': 1}, + 'wyz': {'avgtemp': 26.100450643627426, 'avgtime': 4040.0, 'count': 2}, + 'xyd': {'avgtemp': 21.310415433987586, + 'avgtime': 5592.277777777777, + 'count': 90}, + 'xyz': {'avgtemp': 23.798124933747882, 'avgtime': 3992.0, 'count': 1}, + 'yyz': {'avgtemp': 27.137975077133788, 'avgtime': 4018.5, 'count': 6}}) def test_ambiguous_case(self): self.run_testcase('abc', 'abd', 'ijkk', 100, @@ -99,15 +96,12 @@ class TestCopycat(unittest.TestCase): def test_mrrjjj(self): self.run_testcase('abc', 'abd', 'mrrjjj', 30, - {'drrjjj': {'avgtemp': 47.3961, 'avgtime': 1538.0, 'count': 1}, - 'mrrjjd': {'avgtemp': 70.5363, 'avgtime': 681.0, 'count': 1}, - 'mrrjjjj': {'avgtemp': 19.1294, 'avgtime': 2075.0, 'count': 1}, - 'mrrjjk': {'avgtemp': 48.0952, - 'avgtime': 2203.5714, - 'count': 14}, - 'mrrkkk': {'avgtemp': 43.6931, - 'avgtime': 2251.4615, - 'count': 13}}) + {'mrrjjd': {'avgtemp': 44.46354725386579, 'avgtime': 1262.0, 'count': 1}, + 'mrrjjjj': {'avgtemp': 17.50702440140412, 'avgtime': 1038.375, 'count': 8}, + 'mrrjjk': {'avgtemp': 55.189156978290264, + 'avgtime': 1170.6363636363637, + 'count': 11}, + 'mrrkkk': {'avgtemp': 43.709349775080746, 'avgtime': 1376.2, 'count': 10}}) ''' Below are examples of improvements that could be made to copycat.