-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (113 loc) 路 3.03 KB
/
main.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
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
/**
* @author :Franck GUTMANN et Julien Von Der Mark.
* @description : Probl猫me du pont du singe.
**/
const { readFile, runGame, generatePermutations } = require("./monkeys");
// Strategies :
function initialOrderStrategy(game) {
return {
...game,
strategyName: "initialOrderStrategy",
};
}
function increasingWeightStrategy(game) {
game.monkeys.sort((a, b) => a.poids - b.poids);
return {
...game,
strategyName: "increasingWeightStrategy",
};
}
function decreasingWeightStrategy(game) {
game.monkeys.sort((a, b) => b.poids - a.poids);
return {
...game,
strategyName: "decreasingWeightStrategy",
};
}
function increasingSpeedStrategy(game) {
game.monkeys.sort((a, b) => a.vitesse - b.vitesse);
return {
...game,
strategyName: "increasingSpeedStrategy",
};
}
function decreasingSpeedStrategy(game) {
game.monkeys.sort((a, b) => b.vitesse - a.vitesse);
return {
...game,
strategyName: "decreasingSpeedStrategy",
};
}
// Fonction de tri par rapport au rapport poids/vitesse
function ratioWeightWithSpeed(game) {
game.monkeys.sort((a, b) => (a.poids / a.vitesse) - (b.poids / b.vitesse));
return {
...game,
strategyName: "ratioWeightWithSpeed",
};
}
// Jeux
const games = [
"./jeu1.txt",
"./jeu2.txt",
"./jeu3.txt",
"./jeu4.txt",
"./jeu5.txt",
"./jeu6.txt",
]
// fonctions f(game): game
const strategies = [
initialOrderStrategy,
decreasingSpeedStrategy,
increasingWeightStrategy,
decreasingWeightStrategy,
increasingSpeedStrategy,
ratioWeightWithSpeed
]
const bruteForce = () => {
const path = "./jeu2.txt";
const game = readFile(path);
const startTime = Date.now();
let bestTime = Infinity;
const runPermuteTest = (permute) => {
const monkeys = permute.map((index) => game.monkeys[index]);
return runGame({
...game,
monkeys,
strategyName: "bruteForce",
});
}
const permute = (arr, m = []) => {
if (arr.length === 0) {
const result = runPermuteTest(m);
if (result.traveltime < bestTime) {
bestTime = result.traveltime;
}
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(Array.from(Array(game.n).keys()));
const endTime = Date.now();
console.log({
gameName: path,
strategyName: "bruteForce",
travelTime: bestTime,
time: endTime - startTime,
});
}
// Programme principal
games.forEach(path => {
const game = readFile(path);
// Classic strategies
strategies.forEach(strategy => {
const gameWithStrategy = strategy(JSON.parse(JSON.stringify(game)));
const result = runGame(gameWithStrategy);
console.log(result);
});
});
bruteForce();