|
| 1 | +from axelrod.actions import Action, Actions |
| 2 | +from axelrod.player import Player |
| 3 | +from axelrod.random_ import random_choice |
| 4 | + |
| 5 | +from math import pi, sin |
| 6 | + |
| 7 | +C, D = Actions.C, Actions.D |
| 8 | + |
| 9 | + |
| 10 | +class SelfSteem(Player): |
| 11 | + """ |
| 12 | + This strategy is based on the feeling with the same name. |
| 13 | + It is modeled on the sine curve(f = sin( 2* pi * n / 10 )), which varies |
| 14 | + with the current iteration. |
| 15 | +
|
| 16 | + If f > 0.95, 'ego' of the algorithm is inflated; always defects. |
| 17 | + If 0.95 > abs(f) > 0.3, rational behavior; follows TitForTat algortithm. |
| 18 | + If 0.3 > f > -0.3; random behavior. |
| 19 | + If f < -0.95, algorithm is at rock bottom; always cooperates. |
| 20 | +
|
| 21 | + Futhermore, the algorithm implements a retaliation policy, if the opponent |
| 22 | + defects; the sin curve is shifted. But due to lack of further information, |
| 23 | + this implementation does not include a sin phase change. |
| 24 | + Names: |
| 25 | +
|
| 26 | + - SelfSteem: [Andre2013]_ |
| 27 | + """ |
| 28 | + |
| 29 | + name = 'SelfSteem' |
| 30 | + classifier = { |
| 31 | + 'memory_depth': float("inf"), |
| 32 | + 'stochastic': True, |
| 33 | + 'makes_use_of': set(), |
| 34 | + 'long_run_time': False, |
| 35 | + 'inspects_source': False, |
| 36 | + 'manipulates_source': False, |
| 37 | + 'manipulates_state': False |
| 38 | + } |
| 39 | + |
| 40 | + def strategy(self, opponent: Player) -> Action: |
| 41 | + turns_number = len(self.history) |
| 42 | + sine_value = sin(2 * pi * turns_number / 10) |
| 43 | + |
| 44 | + if sine_value > 0.95: |
| 45 | + return D |
| 46 | + |
| 47 | + if abs(sine_value) < 0.95 and abs(sine_value) > 0.3: |
| 48 | + return opponent.history[-1] |
| 49 | + |
| 50 | + if sine_value < 0.3 and sine_value > -0.3: |
| 51 | + return random_choice() |
| 52 | + |
| 53 | + return C |
0 commit comments