1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! This crate provides the necessary framework to simulate the iterated prisoner's dilemma in
//! cases where the history is not always entirely available.
//!
//! The mechanisms to provide one simulation are provided by the `simulate` function, and the full
//! details of how this works are provided by the associated documentation. If one is interested in
//! comparing many strategies against each other, the `tournament` function provides the necessary
//! interface.

pub use Choice::*;

/// Represents one of two possible choices that an actor can make.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Choice {
    Cooperate,
    Defect,
}

/// Represents a choice that the other player made in the last round.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Round {
    /// Contains the choice that the other player made
    Shown(Choice),
    /// Indicates that the information from the last round has been hidden by the revealing policy
    Hidden,
    /// Indicates that this is the first round of the game
    First,
}

/// Represents a playing strategy
///
/// This function will be invoked one time every time two players play the IPD. The closure that is
/// returned by the function will be repeatedly invoked with values of the `Round` enum; each
/// invocation of the closure corresponds to one round of gameplay. The closure is told the
/// other player's choice in the previous round, and asked to return the next round's choice.
///
/// A very simple strategy might be to always `Cooperate`. It could be written like this:
/// ```rust
/// use m_ipd::*;
/// 
/// fn always_cooperate() -> Box<dyn FnMut(Round) -> Choice> {
///     Box::new(|_| Cooperate)
/// }
/// 
/// let s: Strategy = always_cooperate;
/// ```
pub type Strategy = fn() -> Box<dyn FnMut(Round) -> Choice>;

/// Represents a policy that decides how history is revealed.
///
/// This type functions much like the `Strategy` type; the function is invoked once for each time
/// IPD is played. The returned closure is invoked once for each round of the game, with the
/// choices of the players; it should return true of false indicating whether the decisions from
/// that round should be revealed to the players.
pub type RevealPolicy = fn() -> Box<dyn FnMut(Choice, Choice) -> bool>;

// The standard reward function
fn reward(one: Choice, two: Choice) -> (u32, u32) {
    match (one, two) {
        (Cooperate, Cooperate) => (3, 3),
        (Cooperate, Defect) => (0, 5),
        (Defect, Cooperate) => (5, 0),
        (Defect, Defect) => (1, 1),
    }
}

// Simulates a match between two players.
fn simulate(
    one: Strategy,
    two: Strategy,
    policy: RevealPolicy,
) -> (u32, u32, u32) {
    let mut s1 = 0;
    let mut s2 = 0;
    let mut rounds = 0;
    for _ in 0..100 {
        let mut last = None;
        let mut one = one();
        let mut two = two();
        let mut policy = policy();

        while rand::random::<f32>() < 1f32 - 1f32 / 200f32 {
            let c1;
            let c2;

            if let Some((prev_one, prev_two, rev)) = last {
                c1 = one(if rev {
                    Round::Shown(prev_two)
                } else {
                    Round::Hidden
                });
                c2 = two(if rev {
                    Round::Shown(prev_one)
                } else {
                    Round::Hidden
                });
            } else {
                c1 = one(Round::First);
                c2 = two(Round::First);
            }

            let (r1, r2) = reward(c1, c2);
            s1 += r1;
            s2 += r2;
            rounds += 1;

            let show = policy(c1, c2);
            last = Some((c1, c2, show));
        }
    }

    (s1, s2, rounds)
}

/// Runs a tournament between a set of strategies.
///
/// This is a round-robin tournament; each strategy plays IPD against each other strategy 100
/// times. The output of the function is the average number of points each strategy earned per
/// round.
///
/// The reward function used is as follows:
///  - 1 point for mutual defection.
///  - 3 points for mutual cooperation.
///  - 5 points for successful betreyal.
pub fn tournament(
    strategies: &[Strategy],
    policy: RevealPolicy,
) -> Vec<f32> {
    let n = strategies.len();
    let mut points = vec![(0, 0); n];

    for i in 0..n - 1 {
        for j in i+1..n {
            let (p1, p2, rounds) = simulate(strategies[i], strategies[j], policy);
            points[i] = ((points[i].0 + p1), (points[i].1 + rounds));
            points[j] = ((points[j].0 + p2), (points[j].1 + rounds));
        }
    }
    points.into_iter().map(|(p, r)| p as f32 / r as f32).collect()
}

pub mod strategies {
    use crate::*;

    pub mod axelrod {
        use crate::*;

        /// Copies the opponents last shown move.
        pub fn tit_for_tat() -> Box<dyn FnMut(Round) -> Choice> {
            let mut old = Cooperate;
    
            Box::new(move |c| match c {
                Round::Shown(n) => {
                    old = n;
                    n
                }
                _ => old,
            })
        }

        /// Copies the opponents last move if it was shown, cooperating otherwise.
        pub fn optimistic_tit_for_tat() -> Box<dyn FnMut(Round) -> Choice> {
            Box::new(move |c| match c {
                Round::Shown(o) => o,
                _ => Cooperate
            })
        }

        /// Punishes the other player by one turn more each time the other player
        /// engages in a streak of defections.
        pub fn punishing_tit_for_tat() -> Box<dyn FnMut(Round) -> Choice> {
            let mut reset = 0u32;
            let mut retaliate = 0;
            let mut last = Cooperate;

            Box::new(move |c| match c {
                Round::Shown(Cooperate) => {
                    if last == Defect {
                        reset = retaliate;
                        retaliate += 1;
                    }
                    last = Cooperate;

                    if reset > 0 {
                        reset -= 1;
                        Defect
                    } else {
                        Cooperate
                    }
                }
                Round::Shown(Defect) => Defect,
                Round::Hidden => {
                    if reset > 0 {
                        reset -= 1;
                        Defect
                    } else {
                        Cooperate
                    }
                }
                Round::First => Cooperate
            })
        }

        /// Cooperates unless one betrayer betrayed the other in the last shown round, in which
        /// case it only cooperates with low probability.
        ///
        /// Adapted from a strategy created by Grofman
        pub fn betray_defect() -> Box<dyn FnMut(Round) -> Choice> {
            let mut last = Cooperate;
            let mut betray = false;
            Box::new(move |c| match c {
                Round::Shown(o) => {
                    let n = if last == o {
                        betray = false;
                        Cooperate
                    } else {
                        betray = true;
                        if rand::random::<f32>() < 2f32/7f32 { Cooperate } else { Defect }
                    };
                    last = n;
                    n
                }
                Round::Hidden => {
                    let n = if betray {
                        if rand::random::<f32>() < 2f32/7f32 { Cooperate } else { Defect }
                    } else {
                        Cooperate
                    };
                    last = n;
                    n
                }
                Round::First => Cooperate
            })
        }

        /// Defects in streaks increasing by length 1.
        ///
        /// Original strategy by Shubik.
        pub fn defect_streaks() -> Box<dyn FnMut(Round) -> Choice> {
            let mut next = 0;
            let mut current = 0;
            Box::new(move |c| match c {
                Round::Shown(o) => {
                    if current > 0 {
                        current -= 1;
                        Defect
                    } else {
                        if o == Defect {
                            current = next;
                            next += 1;
                            Defect
                        } else {
                            Cooperate
                        }
                    }
                },
                Round::Hidden => {
                    if current > 0 {
                        current -= 1;
                        Defect
                    } else {
                        Cooperate
                    }
                }
                Round::First => Cooperate
            })
        }

        /// Cooperates until the opponent defects even once.
        ///
        /// Adapted from an original strategy by Friedman.
        pub fn grudge_holder() -> Box<dyn FnMut(Round) -> Choice> {
            let mut action = Cooperate;
    
            Box::new(move |c: Round| match c {
                Round::Shown(Defect) => {
                    action = Defect;
    
                    action
                }
                _ => action,
            })
        }

        /// Plays tit for tat but cooperates with decreasing probability each turn
        ///
        /// Adapted from an original strategy by Feld
        pub fn decreasing_tit_for_tat() -> Box<dyn FnMut(Round) -> Choice> {
            let mut last = Cooperate;
            let mut prob = 1f32;
            Box::new(move |c| {
                prob *= 0.99654; // 200th root of 0.5
                if let Round::Shown(c) = c {
                    last = c;
                }

                if last == Cooperate {
                    if rand::random::<f32>() < prob { Cooperate } else { Defect }
                } else {
                    Defect
                }
            })
        }

        /// Copies the other's cooperation with probability 90%, otherwise defects.
        ///
        /// Adapted from a strategy by Joss.
        pub fn random_tit_for_tat() -> Box<dyn FnMut(Round) -> Choice> {
            let mut old = Cooperate;
    
            Box::new(move |c| {
                if let Round::Shown(o) = c {
                    old = o;
                }

                if old == Cooperate {
                    if rand::random::<f32>() < 0.9 { Cooperate } else { Defect }
                } else {
                    Defect
                }
            })
        }

        /// Plays randomly with probability `p`.
        ///
        /// This must be used like `|| random(0.5)` instead of `random` since the `Strategy` type
        /// does not allow for taking a parameter.
        pub fn random(p: f32) -> Box<dyn FnMut(Round) -> Choice> {
            Box::new(move |_| {
                if rand::random::<f32>() < p {
                    Cooperate
                } else {
                    Defect
                }
            })
        }
    }

    /// Always cooperates
    pub fn always_cooperate() -> Box<dyn FnMut(Round) -> Choice> {
        Box::new(|_| Cooperate)
    }

    /// Always defects
    pub fn always_defect() -> Box<dyn FnMut(Round) -> Choice> {
        Box::new(|_| Defect)
    }

    /// Plays tit for tat, but does not defect until the other player has defected twice
    /// in a row.
    pub fn tit_for_two_tats() -> Box<dyn FnMut(Round) -> Choice> {
        let mut prev = Cooperate;
        let mut next = Cooperate;

        Box::new(move |c| {
            if let Round::Shown(c) = c {
                match c {
                    Cooperate => {
                        prev = Cooperate;
                        next = Cooperate;
                    }
                    Defect => {
                        next = prev;
                        prev = Defect;
                    }
                }
            }

            next
        })
    }

    /// Plays tit for tat, but if the last two rounds were both hidden, defects.
    pub fn opportunistic() -> Box<dyn FnMut(Round) -> Choice> {
        let mut last = Cooperate;
        let mut hidden = 0;

        Box::new(move |c| {
            match c {
                Round::Shown(o) => {
                    hidden = 0;
                    last = o;
                    last
                },
                Round::Hidden => {
                    hidden += 1;
                    if hidden > 2 { Defect } else { last }
                }
                Round::First => Cooperate
            }
        })
    }

    /// Plays the inverse of the other player's move.
    pub fn inverse() -> Box<dyn FnMut(Round) -> Choice> {
        let mut next = Cooperate;

        Box::new(move |c| match c {
            Round::Shown(n) => {
                next = match n { Cooperate => Defect, Defect => Cooperate };
                n
            }
            _ => next,
        })
    }

    /// Plays the other player's majority move.
    pub fn majority() -> Box<dyn FnMut(Round) -> Choice> {
        let mut coop = 0;
        let mut def = 0;
        Box::new(move |c| {
            if let Round::Shown(n) = c {
                if n == Cooperate {
                    coop += 1;
                } else {
                    def += 1;
                }
            }

            if coop > def { Cooperate } else { Defect }
        })
    }

    /// Plays the other player's minority move.
    pub fn minority() -> Box<dyn FnMut(Round) -> Choice> {
        let mut coop = 0;
        let mut def = 0;
        Box::new(move |c| {
            if let Round::Shown(n) = c {
                if n == Cooperate {
                    coop += 1;
                } else {
                    def += 1;
                }
            }

            if coop < def { Cooperate } else { Defect }
        })
    }
}

pub mod policies {
    use crate::*;

    pub fn always_show() -> Box<dyn FnMut(Choice, Choice) -> bool> {
        Box::new(|_, _| true)
    }

    pub fn never_show() -> Box<dyn FnMut(Choice, Choice) -> bool> {
        Box::new(|_, _| false)
    }

    pub fn random(reveal_chance: f32) -> Box<dyn FnMut(Choice, Choice) -> bool> {
        Box::new(move |_, _| rand::random::<f32>() < reveal_chance)
    }

    pub fn show_cooperate(init: f32) -> Box<dyn FnMut(Choice, Choice) -> bool> {
        let mut coop = init;
        let mut total = 2.0 * init;
        Box::new(move |c1, c2| {
            if c1 == Cooperate && c2 == Cooperate {
                coop += 1.0;
            }
            total += 1.0;

            rand::random::<f32>() < coop / total
        })
    }
}