-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulatedAnnealing.cpp
More file actions
172 lines (142 loc) · 5.24 KB
/
Copy pathsimulatedAnnealing.cpp
File metadata and controls
172 lines (142 loc) · 5.24 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <numeric>
using namespace std;
using ll = long long;
using namespace chrono;
const int N = 50;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<vector<int>> opponent(N, vector<int>(N));
struct Individual {
double startTemp;
double endTemp;
double score;
Individual() {
startTemp = rng() % 9901 + 100; // 100 ~ 10000
endTemp = pow(10., -((rng() % 5) + 2.)); // 1e-2 ~ 1e-6
score = -1;
}
void mutate() {
if (uniform_real_distribution<>(0, 1)(rng) < 0.3) startTemp *= uniform_real_distribution<>(0.8, 1.2)(rng);
if (uniform_real_distribution<>(0, 1)(rng) < 0.3) endTemp *= uniform_real_distribution<>(0.8, 1.2)(rng);
}
};
// スコアを計算
double evaluate(const vector<int>& myDeck) {
double totalScore = 0.0;
for (int game = 0; game < N; ++game) {
double gameScore = 0.0;
for (int i = 0; i < N; ++i) {
if (myDeck[i] > opponent[game][i]) gameScore += 1.0;
else if (myDeck[i] == opponent[game][i]) gameScore += 0.5;
}
totalScore += gameScore;
}
return totalScore;
}
// 焼きなまし法
int simulatedAnnealing(Individual ind, double timeLimit) {
auto start = system_clock::now();
vector<int> currentDeck(N), bestDeck(N);
iota(currentDeck.begin(), currentDeck.end(), 1);
random_device rd;
mt19937 rng(rd());
shuffle(currentDeck.begin(), currentDeck.end(), rng);
double currentScore = evaluate(currentDeck);
double bestScore = currentScore;
bestDeck = currentDeck;
double startTemp = ind.startTemp, endTemp = ind.endTemp;
while (true) {
auto now = system_clock::now();
double elapsed = duration_cast<milliseconds>(now - start).count() / 1000.0;
if (elapsed > timeLimit) break;
double t = elapsed / timeLimit;
double temperature = startTemp * pow(endTemp / startTemp, t);
vector<int> nextDeck = currentDeck;
for (int i = 0; i < 1; ++i) {
int idx1 = uniform_int_distribution<int>(0, N - 1)(rng);
int idx2 = uniform_int_distribution<int>(0, N - 1)(rng);
swap(nextDeck[idx1], nextDeck[idx2]);
}
// int i = uniform_int_distribution<int>(0, N - 1)(rng);
// int j = uniform_int_distribution<int>(0, N - 1)(rng);
// swap(nextDeck[i], nextDeck[j]);
double nextScore = evaluate(nextDeck);
double diff = nextScore - currentScore;
if (diff >= 0 || exp(diff / temperature) > uniform_real_distribution<>(0.0, 1.0)(rng)) {
currentDeck = nextDeck;
currentScore = nextScore;
if (nextScore > bestScore) {
bestScore = nextScore;
bestDeck = nextDeck;
}
}
}
cerr << "-------------------------\n";
cerr << "startTemp: " << startTemp << ", endTemp: " << endTemp << endl;
cerr << "Best score: " << bestScore << endl;
for(int i = 0; i < N; ++i) {
cerr << bestDeck[i] << " ";
}
cerr << endl;
return bestScore;
}
int main() {
ifstream fin("deck.csv");
if (!fin) {
cerr << "deck.csv が開けませんでした。" << endl;
return 1;
}
for (int i = 0; i < N; ++i) {
string line;
getline(fin, line);
stringstream ss(line);
for (int j = 0; j < N; ++j) {
string val;
getline(ss, val, ',');
opponent[i][j] = stoi(val);
}
}
const int POP_SIZE = 20;
const int GENERATIONS = 30;
const int ELITE = 4;
vector<Individual> population(POP_SIZE);
for (auto& ind : population)
ind.score = simulatedAnnealing(ind, 2);
for (int gen = 0; gen < GENERATIONS; ++gen) {
sort(population.begin(), population.end(), [](const Individual& a, const Individual& b) {
return a.score > b.score;
});
cerr << "------------------------\n";
cerr << "Gen " << gen << " best: " << population[0].score << "\n";
cerr << "startTemp: " << population[0].startTemp << "\n";
cerr << "endTemp: " << population[0].endTemp << "\n";
cerr << "------------------------\n";
vector<Individual> newPop;
for (int i = 0; i < ELITE; ++i) newPop.push_back(population[i]);
while (newPop.size() < POP_SIZE) {
// トーナメント選択
Individual p1 = population[rng() % ELITE];
Individual p2 = population[rng() % ELITE];
Individual child;
child.startTemp = (p1.startTemp + p2.startTemp) / 2;
child.endTemp = sqrt(p1.endTemp * p2.endTemp);
child.mutate();
child.score = simulatedAnnealing(child, 2);
newPop.push_back(child);
}
population = newPop;
}
auto best = *max_element(population.begin(), population.end(),
[](const Individual& a, const Individual& b) { return a.score < b.score; });
cout << "Best parameters:\n";
cout << "startTemp = " << best.startTemp << "\n";
cout << "endTemp = " << best.endTemp << "\n";
cout << "score = " << best.score << "\n";
}