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
use std::collections::VecDeque;
use super::single_elimination::SingleElimination;
use super::Tournament;
use crate::candidate::Candidate;
use crate::game::Game;
#[mockall_double::double]
use crate::rando::Rando;
pub struct DoubleElimination<G: Game<N, NSYMS> + Clone, const N: usize, const NSYMS: usize> {
pub game: G,
}
impl<G: Game<N, NSYMS> + Clone, const N: usize, const NSYMS: usize> DoubleElimination<G, N, NSYMS> {
pub const fn new(game: G) -> DoubleElimination<G, N, NSYMS> {
DoubleElimination { game }
}
}
impl<G: Game<N, NSYMS> + Clone, const N: usize, const NSYMS: usize> Tournament<N, NSYMS>
for DoubleElimination<G, N, NSYMS>
{
fn run(
&self,
population: &Vec<Candidate<N, NSYMS>>,
rng: &mut Rando,
score_weights: &Vec<f64>,
) -> (Candidate<N, NSYMS>, Vec<usize>) {
let mut remaining: VecDeque<usize> = VecDeque::with_capacity(population.len());
let mut rating = vec![1000usize; population.len()];
for i in 0..population.len() {
remaining.push_back(i);
}
rng.shuffle(remaining.make_contiguous());
SingleElimination::new(self.game.clone()).do_side(
population,
&mut remaining,
&mut rating,
200.0,
rng,
score_weights,
);
let winner = remaining.pop_front().unwrap();
for i in 0..population.len() {
if i != winner {
remaining.push_back(i);
}
}
rng.shuffle(remaining.make_contiguous());
SingleElimination::new(self.game.clone()).do_side(
population,
&mut remaining,
&mut rating,
180.0,
rng,
score_weights,
);
(population[winner].clone(), rating)
}
/*
let mut roundA: VecDeque<usize> = VecDeque::with_capacity(population.len());
let mut roundB: VecDeque<usize> = VecDeque::with_capacity(population.len());
let mut rating = vec![1000usize; population.len()];
let mut kA = 200.0;
let mut kB = 180.0;
for i in 0..population.len() {
roundA.push_back(i);
}
rng.shuffle(roundA.make_contiguous());
while roundA.len() >= 2 || roundB.len() >= 2 {
if roundA.len() >= 2 {
let left = roundA.pop_front().unwrap();
let right = roundA.pop_front().unwrap();
match self
.game
.run(&population[left], &population[right], rng, score_weights)
{
game::LeftRight::Left => {
roundA.push_back(left);
roundB.push_back(right);
(rating[left], rating[right]) = elo(kA, rating[left], rating[right]);
}
game::LeftRight::Right => {
roundA.push_back(right);
roundB.push_back(left);
(rating[right], rating[left]) = elo(kA, rating[right], rating[left]);
}
// the next round starts when there are only a power of two candidates remaining.
if roundA.len().count_ones() == 1 {
kA *= 0.9;
}
}
}
if roundB.len() >= 2 {
let left = roundB.pop_front().unwrap();
let right = roundB.pop_front().unwrap();
match self
.game
.run(&population[left], &population[right], rng, score_weights)
{
game::LeftRight::Left => {
roundB.push_back(left);
(rating[left], rating[right]) = elo(k, rating[left], rating[right]);
}
game::LeftRight::Right => {
roundB.push_back(right);
(rating[right], rating[left]) = elo(k, rating[right], rating[left]);
}
// the next round starts when there are only a power of two candidates remaining.
if roundA.len().count_ones() == 1 {
kA *= 0.9;
}
}
}
}
assert!(remaining.len() == 1);
(population[remaining.pop_front().unwrap()].clone(), rating)
} */
}
#[cfg(test)]
mod tests {
use super::*;
use crate::game;
use crate::gas::Gas;
#[test]
fn test_double_elimination_tournament() {
let gas = Gas::dut();
let pop = &vec![
Candidate::from_chromosone(&gas, [1, 0, 1, 0, 1]),
Candidate::from_chromosone(&gas, [0, 0, 1, 0, 1]),
];
let mut r = Rando::default();
r.expect_shuffle().times(2).return_const(());
let g = game::full::Full::new();
let t = DoubleElimination::new(g);
let (winner, weights) = t.run(&pop, &mut r, &vec![1.0; 9]);
assert_eq!(weights, [1100, 900]);
Candidate::assert_eq(&winner, &pop[0]);
}
#[test]
fn test_bye() {
let gas = Gas::dut();
let pop = &vec![
Candidate::from_chromosone(&gas, [1, 0, 1, 0, 1]),
Candidate::from_chromosone(&gas, [0, 0, 1, 0, 1]),
Candidate::from_chromosone(&gas, [0, 1, 2, 0, 1]),
];
let mut r = Rando::default();
r.expect_shuffle().times(2).return_const(());
let g = game::full::Full::new();
let t = DoubleElimination::new(g);
let (winner, weights) = t.run(&pop, &mut r, &vec![1.0; 9]);
assert_eq!(weights, [1053, 832, 1115]);
Candidate::assert_eq(&winner, &pop[2]);
}
}