|
| 1 | +"""Memory One strategies. Note that there are Memory One strategies in other |
| 2 | +files, including titfortat.py and zero_determinant.py""" |
| 3 | + |
1 | 4 | from axelrod.action import Action
|
2 | 5 | from axelrod.player import Player
|
3 | 6 | from axelrod.random_ import random_choice
|
@@ -33,7 +36,8 @@ class MemoryOnePlayer(Player):
|
33 | 36 | 'manipulates_state': False
|
34 | 37 | }
|
35 | 38 |
|
36 |
| - def __init__(self, four_vector: type_four_vector = None, initial: Action = C) -> None: |
| 39 | + def __init__(self, four_vector: type_four_vector = None, |
| 40 | + initial: Action = C) -> None: |
37 | 41 | """
|
38 | 42 | Parameters
|
39 | 43 |
|
@@ -201,10 +205,10 @@ def __init__(self) -> None:
|
201 | 205 | class StochasticCooperator(MemoryOnePlayer):
|
202 | 206 | """Stochastic Cooperator.
|
203 | 207 |
|
204 |
| - Names: |
| 208 | + Names: |
205 | 209 |
|
206 |
| - - Stochastic Cooperator: [Adami2013]_ |
207 |
| - """ |
| 210 | + - Stochastic Cooperator: [Adami2013]_ |
| 211 | + """ |
208 | 212 |
|
209 | 213 | name = 'Stochastic Cooperator'
|
210 | 214 |
|
@@ -241,247 +245,11 @@ def __init__(self, ep: float = 0.05) -> None:
|
241 | 245 | """
|
242 | 246 |
|
243 | 247 | self.ep = ep
|
244 |
| - four_vector = (1.-ep, ep, ep, 1.-ep) |
| 248 | + four_vector = (1. - ep, ep, ep, 1. - ep) |
245 | 249 | super().__init__(four_vector)
|
246 | 250 | self.set_four_vector(four_vector)
|
247 | 251 |
|
248 | 252 |
|
249 |
| -class LRPlayer(MemoryOnePlayer): |
250 |
| - """Abstraction for Linear Relation players. These players enforce a linear |
251 |
| - difference in stationary payoffs s * (S_xy - l) = S_yx - l, with 0 <= l <= R. |
252 |
| - The parameter `s` is called the slope and the parameter `l` the |
253 |
| - baseline payoff. For extortionate strategies, the extortion factor is the |
254 |
| - inverse of the slope. |
255 |
| -
|
256 |
| - This parameterization is Equation 14 in |
257 |
| - http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0077886. |
258 |
| - See Figure 2 of the article for a more in-depth explanation. |
259 |
| -
|
260 |
| - Names: |
261 |
| -
|
262 |
| - - Linear Relation player: [Hilbe2013]_ |
263 |
| - """ |
264 |
| - |
265 |
| - name = 'LinearRelation' |
266 |
| - classifier = { |
267 |
| - 'memory_depth': 1, # Memory-one Four-Vector |
268 |
| - 'stochastic': True, |
269 |
| - 'makes_use_of': set(["game"]), |
270 |
| - 'long_run_time': False, |
271 |
| - 'inspects_source': False, |
272 |
| - 'manipulates_source': False, |
273 |
| - 'manipulates_state': False |
274 |
| - } |
275 |
| - |
276 |
| - |
277 |
| - def receive_match_attributes(self, phi: float = 0, s: float = None, l: float = None): |
278 |
| - """ |
279 |
| - Parameters |
280 |
| -
|
281 |
| - phi, s, l: floats |
282 |
| - Parameter used to compute the four-vector according to the |
283 |
| - parameterization of the strategies below. |
284 |
| - """ |
285 |
| - |
286 |
| - (R, P, S, T) = self.match_attributes["game"].RPST() |
287 |
| - if s is None: |
288 |
| - s = 1 |
289 |
| - if l is None: |
290 |
| - l = R |
291 |
| - |
292 |
| - # Check parameters |
293 |
| - s_min = - min((T-l) / (l-S), (l-S) / (T-l)) |
294 |
| - if (l < P) or (l > R) or (s > 1) or (s < s_min): |
295 |
| - raise ValueError |
296 |
| - |
297 |
| - p1 = 1 - phi * (1 - s) * (R - l) |
298 |
| - p2 = 1 - phi * (s * (l - S) + (T - l)) |
299 |
| - p3 = phi * ((l - S) + s * (T - l)) |
300 |
| - p4 = phi * (1 - s) * (l - P) |
301 |
| - |
302 |
| - four_vector = [p1, p2, p3, p4] |
303 |
| - self.set_four_vector(four_vector) |
304 |
| - |
305 |
| - |
306 |
| -class ZDExtort2(LRPlayer): |
307 |
| - """ |
308 |
| - An Extortionate Zero Determinant Strategy with l=P. |
309 |
| -
|
310 |
| - Names: |
311 |
| -
|
312 |
| - - Extort-2: [Stewart2012]_ |
313 |
| - """ |
314 |
| - |
315 |
| - name = 'ZD-Extort-2' |
316 |
| - |
317 |
| - def __init__(self, phi: float = 1/9, s: float = 0.5) -> None: |
318 |
| - """ |
319 |
| - Parameters |
320 |
| -
|
321 |
| - phi, s: floats |
322 |
| - Parameters passed through to LRPlayer to determine |
323 |
| - the four vector. |
324 |
| - """ |
325 |
| - self.phi = phi |
326 |
| - self.s = s |
327 |
| - super().__init__() |
328 |
| - |
329 |
| - def receive_match_attributes(self): |
330 |
| - (R, P, S, T) = self.match_attributes["game"].RPST() |
331 |
| - self.l = P |
332 |
| - super().receive_match_attributes( |
333 |
| - self.phi, self.s, self.l) |
334 |
| - |
335 |
| - |
336 |
| -class ZDExtort2v2(LRPlayer): |
337 |
| - """ |
338 |
| - An Extortionate Zero Determinant Strategy with l=1. |
339 |
| -
|
340 |
| -
|
341 |
| - Names: |
342 |
| -
|
343 |
| - - EXTORT2: [Kuhn2017]_ |
344 |
| - """ |
345 |
| - |
346 |
| - name = 'ZD-Extort-2 v2' |
347 |
| - |
348 |
| - def __init__(self, phi: float = 1/8, s: float = 0.5, l: float = 1) -> None: |
349 |
| - """ |
350 |
| - Parameters |
351 |
| -
|
352 |
| - phi, s: floats |
353 |
| - Parameters passed through to LRPlayer to determine |
354 |
| - the four vector. |
355 |
| - """ |
356 |
| - self.phi = phi |
357 |
| - self.s = s |
358 |
| - self.l = l |
359 |
| - super().__init__() |
360 |
| - |
361 |
| - def receive_match_attributes(self): |
362 |
| - super().receive_match_attributes( |
363 |
| - self.phi, self.s, self.l) |
364 |
| - |
365 |
| - |
366 |
| -class ZDExtort4(LRPlayer): |
367 |
| - """ |
368 |
| - An Extortionate Zero Determinant Strategy with l=1, s=1/4. TFT is the |
369 |
| - other extreme (with l=3, s=1) |
370 |
| -
|
371 |
| -
|
372 |
| - Names: |
373 |
| -
|
374 |
| - - Extort 4: Original name by Marc Harper |
375 |
| - """ |
376 |
| - |
377 |
| - name = 'ZD-Extort-4' |
378 |
| - |
379 |
| - def __init__(self, phi: float = 4/17, s: float = 0.25, l: float = 1) -> None: |
380 |
| - """ |
381 |
| - Parameters |
382 |
| -
|
383 |
| - phi, s: floats |
384 |
| - Parameters passed through to LRPlayer to determine |
385 |
| - the four vector. |
386 |
| - """ |
387 |
| - self.phi = phi |
388 |
| - self.s = s |
389 |
| - self.l = l |
390 |
| - super().__init__() |
391 |
| - |
392 |
| - def receive_match_attributes(self): |
393 |
| - super().receive_match_attributes( |
394 |
| - self.phi, self.s, self.l) |
395 |
| - |
396 |
| - |
397 |
| -class ZDGen2(LRPlayer): |
398 |
| - """ |
399 |
| - A Generous Zero Determinant Strategy with l=3. |
400 |
| -
|
401 |
| - Names: |
402 |
| -
|
403 |
| - - GEN2: [Kuhn2017]_ |
404 |
| - """ |
405 |
| - |
406 |
| - name = 'ZD-GEN-2' |
407 |
| - |
408 |
| - def __init__(self, phi: float = 1/8, s: float = 0.5, l: float = 3) -> None: |
409 |
| - """ |
410 |
| - Parameters |
411 |
| -
|
412 |
| - phi, s: floats |
413 |
| - Parameters passed through to LRPlayer to determine |
414 |
| - the four vector. |
415 |
| - """ |
416 |
| - self.phi = phi |
417 |
| - self.s = s |
418 |
| - self.l = l |
419 |
| - super().__init__() |
420 |
| - |
421 |
| - def receive_match_attributes(self): |
422 |
| - super().receive_match_attributes( |
423 |
| - self.phi, self.s, self.l) |
424 |
| - |
425 |
| - |
426 |
| -class ZDGTFT2(LRPlayer): |
427 |
| - """ |
428 |
| - A Generous Zero Determinant Strategy with l=R. |
429 |
| -
|
430 |
| - Names: |
431 |
| -
|
432 |
| - - ZDGTFT-2: [Stewart2012]_ |
433 |
| - """ |
434 |
| - |
435 |
| - name = 'ZD-GTFT-2' |
436 |
| - |
437 |
| - def __init__(self, phi: float = 0.25, s: float = 0.5) -> None: |
438 |
| - """ |
439 |
| - Parameters |
440 |
| -
|
441 |
| - phi, s: floats |
442 |
| - Parameters passed through to LRPlayer to determine |
443 |
| - the four vector. |
444 |
| - """ |
445 |
| - self.phi = phi |
446 |
| - self.s = s |
447 |
| - super().__init__() |
448 |
| - |
449 |
| - def receive_match_attributes(self): |
450 |
| - (R, P, S, T) = self.match_attributes["game"].RPST() |
451 |
| - self.l = R |
452 |
| - super().receive_match_attributes( |
453 |
| - self.phi, self.s, self.l) |
454 |
| - |
455 |
| - |
456 |
| -class ZDSet2(LRPlayer): |
457 |
| - """ |
458 |
| - A Generous Zero Determinant Strategy with l=2. |
459 |
| -
|
460 |
| - Names: |
461 |
| -
|
462 |
| - - SET2: [Kuhn2017]_ |
463 |
| - """ |
464 |
| - |
465 |
| - name = 'ZD-SET-2' |
466 |
| - |
467 |
| - def __init__(self, phi: float = 1/4, s: float = 0., l: float = 2) -> None: |
468 |
| - """ |
469 |
| - Parameters |
470 |
| -
|
471 |
| - phi, s: floats |
472 |
| - Parameters passed through to LRPlayer to determine |
473 |
| - the four vector. |
474 |
| - """ |
475 |
| - self.phi = phi |
476 |
| - self.s = s |
477 |
| - self.l = l |
478 |
| - super().__init__() |
479 |
| - |
480 |
| - def receive_match_attributes(self): |
481 |
| - super().receive_match_attributes( |
482 |
| - self.phi, self.s, self.l) |
483 |
| - |
484 |
| - |
485 | 253 | class SoftJoss(MemoryOnePlayer):
|
486 | 254 | """
|
487 | 255 | Defects with probability 0.9 when the opponent defects, otherwise
|
@@ -555,6 +323,7 @@ class ReactivePlayer(MemoryOnePlayer):
|
555 | 323 | - Reactive: [Nowak1989]_
|
556 | 324 | """
|
557 | 325 | name = "Reactive Player"
|
| 326 | + |
558 | 327 | def __init__(self, probabilities: Tuple[float, float]) -> None:
|
559 | 328 | four_vector = (*probabilities, *probabilities)
|
560 | 329 | super().__init__(four_vector)
|
|
0 commit comments