-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtournament_test.js
79 lines (70 loc) · 2.44 KB
/
tournament_test.js
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
const population = 20;
const liveDesignList = [];
const deadDesignList = [];
for (let i = 0; i < population * 2; i++) {
liveDesignList.push({
genID: i,
score: Math.random() * 10,
});
}
const liveDesignTournament = [];
const failThreshold = 20;
const tournament_size = 9;
for (let i = 0; i < liveDesignList.length; i++) {
liveDesignTournament.push({
genID: liveDesignList[i].genID,
score: liveDesignList[i].score,
rank: 0,
count: 0,
indices: {},
});
}
for (let i = 0; i < liveDesignList.length; i++) {
let failCount = 0;
while (liveDesignTournament[i].count < tournament_size) {
const randomIndex = Math.floor(Math.random() * liveDesignList.length);
if (randomIndex === i || liveDesignTournament[i].indices[randomIndex]) {
continue;
}
if (failCount < failThreshold && liveDesignTournament[randomIndex].count >= tournament_size) {
failCount += 1;
continue;
}
if (failCount >= failThreshold) {
console.log(" failed", failCount);
}
failCount = 0;
if (liveDesignTournament[i].score > liveDesignTournament[randomIndex].score) {
liveDesignTournament[i].rank += 1;
}
liveDesignTournament[i].count += 1;
liveDesignTournament[i].indices[randomIndex] = true;
if (liveDesignTournament[randomIndex].count < tournament_size) {
liveDesignTournament[randomIndex].count += 1;
liveDesignTournament[randomIndex].indices[i] = true;
if (liveDesignTournament[randomIndex].score > liveDesignTournament[i].score) {
liveDesignTournament[randomIndex].rank += 1;
}
}
}
}
const sortedTournament = liveDesignTournament.sort((a, b) => {
if (a.rank === b.rank) {
return a.score - b.score;
}
return a.rank - b.rank;
});
for (let i = 0; i < population; i++) {
for (let j = 0; j < liveDesignList.length; j++) {
if (sortedTournament[i].genID === liveDesignList[j].genID) {
deadDesignList.push(liveDesignList[j]);
liveDesignList.splice(j,1);
break;
}
}
}
sortedTournament.forEach((x) => console.log(" ", x.rank, '\t',x.score));
console.log('\n\n\nLIVE DESIGNS:')
liveDesignList.forEach((x) => console.log(" ", x.score));
console.log('\n\n\nDEAD DESIGNS:')
deadDesignList.forEach((x) => console.log(" ", x.score));