-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.cpp
326 lines (316 loc) · 10.2 KB
/
Sudoku.cpp
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
#include <algorithm>
#include <iostream>
#include <cmath>
#include <random>
#include <chrono>
#include "Sudoku.hpp"
using namespace std;
Sudoku::Sudoku(int size, const char* file_name, bool isDebug) {
_debugMode = isDebug;
_seed = isDebug ? 0 : chrono::system_clock::now().time_since_epoch().count();
srand(_seed);
ifstream ifs(file_name);
data.reserve((unsigned int)size);
solved.reserve((unsigned int)size);
if(ifs.is_open()) {
int v;
for (int i = 0; i < size; ++i) {
vector<int> vi, vs;
vi.reserve((unsigned int)size);
vs.reserve((unsigned int)size);
for (int j = 0; j < (unsigned int)size; ++j) {
ifs >> v;
vi.emplace_back(v);
vs.emplace_back(v);
}
data.emplace_back(vi);
solved.emplace_back(vs);
}
ifs.close();
} else{
cout << "Can't open file "<<file_name<<endl;
exit(1);
}
//init solution
solution.reserve((unsigned)size);
solutionidx.reserve((unsigned)size);
for (int i = 0; i < size; ++i)
__get_avial_inSQ(i);
}
// returns available numbers in the nth square
void Sudoku::__get_avial_inSQ(int sq){
auto size = (int)data.size();
auto sqLen = (int)sqrt(size);
vector<int> all_nums; //could use bitset
vector<int> idx;
all_nums.reserve((unsigned)size);
int sqrow = (sq / sqLen) * sqLen;
int sqcol = (sq * sqLen) % size;
for (int i = 0; i < size; ++i) all_nums.emplace_back(i+1);
for (int i = 0; i < size; ++i){
int i_idx = i/sqLen + sqrow, j_idx = i%sqLen + sqcol;
if(data[i_idx][j_idx])
all_nums[data[i_idx][j_idx] - 1] = 0;
else
idx.emplace_back(i);
}
solutionidx.emplace_back(idx);
vector<int> nums;
for (int i = 0; i < size; ++i)
if(all_nums[i])
nums.emplace_back(all_nums[i]);
solution.emplace_back(nums);
}
int Sudoku::__get_conflicts(){
int conflicts = 0;
auto size = (int)data.size();
//check each row/col
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if(data[i][j] == 0){
for (int k = 0; k < size; ++k) {
if(solved[i][j] == data[i][k]) conflicts += 100; //hard conflict
else if(k != j && solved[i][j] == solved[i][k]) conflicts += 1;
}
}
if(data[j][i] == 0){
for (int k = 0; k < size; ++k) {
if(solved[j][i] == data[k][i]) conflicts += 100; //hard conflict
else if(k != j && solved[j][i] == solved[k][i]) conflicts += 1;
}
}
}
}
return conflicts;
}
void Sudoku::__update_solution(){
//create solution
auto size = (int)data.size();
auto sqLen = (int)sqrt(size);
for (int i = 0; i < size; ++i) {
int sqrow = (i / sqLen) * sqLen;
int sqcol = (i * sqLen) % size;
int cnt = 0;
for (int j = 0; j < size; ++j) {
int i_idx = j/sqLen + sqrow, j_idx = j%sqLen + sqcol;
if(!data[i_idx][j_idx]) solved[i_idx][j_idx] = solution[i][cnt++];
}
}
}
int Sudoku::evalSolution() {
__update_solution();
return __get_conflicts();
}
void Sudoku::printSolution() {
cout<< "conflictos: "<< evalSolution()<<endl;
auto size = solved.size();
for (int i = 0; i < size; ++i){
for (int j = 0; j < size; ++j){
cout << solved[i][j] << " ";
}
cout << endl;
}
}
int Sudoku::__get_conflicts_by_elem(int sq, int item, int val){
int conflicts = 0;
auto size = (int)solution.size();
auto sqLen = (int)sqrt(size);
int sqrow = (sq / sqLen) * sqLen; //row and col of first element
int sqcol = (sq * sqLen) % size;
int i_idx = (item / sqLen) + sqrow, j_idx = (item % sqLen) + sqcol;
for (int i = 0; i < size; ++i) {
if(i < sqcol || i >= sqcol + sqLen){
if(data[i_idx][i] == val)
conflicts += 100;
else if(solved[i_idx][i] == val)
conflicts += 2; //should be 2...
}
if(i < sqrow || i >= sqrow + sqLen) {
if (data[i][j_idx] == val)
conflicts += 100;
else if (solved[i][j_idx] == val)
conflicts += 2; //should be 2...
}
}
return conflicts;
}
vector<vector< int> > Sudoku::initdpTable(unsigned size){
vector<vector< int> > dptable;
dptable.reserve(size);
for (int j = 0; j < size ; ++j) {
vector< int> tmp;
tmp.reserve(size);
for (int k = 0; k < size ; ++k) {
tmp.push_back(-1);
}
dptable.push_back(tmp);
}
return dptable;
}
void Sudoku::__calc_neighbours_allperm_sq(int sq, int mask, int w, int item, int max_w, vector<int> &s_n){
auto size = (unsigned)solution[sq].size();
for (int i = 0; i < size; ++i) {
int v = 1 << i;
if(v & mask) continue;
if(dptable[item][i] == -1){
int val = solution[sq][i];
int idx = solutionidx[sq][item];
dptable[item][i] = __get_conflicts_by_elem(sq, idx, val);
}
int n_mask = v | mask;
int new_w = w + dptable[item][i];
s_n[item] = solution[sq][i];
int mx = (1<<size)- 1;
if(n_mask < mx && new_w < max_w)
__calc_neighbours_allperm_sq(sq, n_mask, new_w, item + 1, max_w, s_n);
else{
if (new_w < max_w){
neighbours.emplace_back(SolutionNeighbour(sq, s_n, max_w - new_w ));
}
}
}
}
void Sudoku::__calc_neighbours_allperm(){
neighbours.clear();
auto size = (unsigned)solution.size();
for (int i = 0; i < size; ++i) {
auto v_size = (unsigned)solution[i].size();
dptable = initdpTable(v_size);
int max_w = 0;
for (int j = 0; j < v_size; ++j) {
dptable[j][j] = __get_conflicts_by_elem(i, solutionidx[i][j], solution[i][j]);
max_w += dptable[j][j];
}
vector<int> s_n(solution[i]);
__calc_neighbours_allperm_sq(i, 0, 0, 0, max_w, s_n);
dptable.clear();
}
}
void Sudoku::__calc_neighbours(bool addBad){
neighbours.clear();
auto size = (int)solution.size();
for (int i = 0; i < size; ++i) {
auto v_size = (unsigned)solution[i].size();
dptable.clear();
dptable = initdpTable(v_size);
for (int j = 0; j < v_size - 1; ++j) {
for (int k = j+1; k < v_size; ++k) {
//can be improved with dynamic programming, we check items n times, only 1 is actually needed
if(dptable[j][j] == -1){
dptable[j][j] = __get_conflicts_by_elem(i, solutionidx[i][j], solution[i][j]);
}
if(dptable[j][k] == -1){
dptable[j][k] = __get_conflicts_by_elem(i, solutionidx[i][j], solution[i][k]);
}
if(dptable[k][j] == -1){
dptable[k][j] = __get_conflicts_by_elem(i, solutionidx[i][k], solution[i][j]);
}
if(dptable[k][k] == -1){
dptable[k][k] = __get_conflicts_by_elem(i, solutionidx[i][k], solution[i][k]);
}
int conf_o1 = dptable[j][j];
int conf_o2 = dptable[k][k];
int conf = dptable[j][k];
int conf2 = dptable[k][j];
if(!addBad && (conf_o1 + conf_o2) <= (conf + conf2)) continue; //dont add bad neighbouts
vector<int> s_n(solution[i]);
s_n[j] = solution[i][k]; s_n[k] = solution[i][j]; //swap
neighbours.emplace_back(SolutionNeighbour(i, s_n, (conf_o1 + conf_o2) - (conf + conf2) ));
}
}
}
if(!addBad) return; //don't shuffle if no 'bad neighbours' where added
auto rng = std::default_random_engine(_seed);
shuffle(begin(neighbours), end(neighbours), rng);
}
void Sudoku::randomSolution(){
auto size = (int)solution.size();
auto rng = std::default_random_engine(_seed);
for (int i = 0; i < size; ++i) {
shuffle(begin(solution[i]), end(solution[i]), rng);
}
}
//should make both use the same code...
void Sudoku::localSearch(){
conflicts = evalSolution();
int iter = 0;
do {
iter++;
__calc_neighbours();
auto size_neighbours = neighbours.size();
if(size_neighbours == 0) break; //no improvement
auto i = rand() % size_neighbours; //look into using c++11 random lib
int sq = neighbours[i].get_square();
vector<int> tmp = solution[sq];
solution[sq] = neighbours[i].get_neighbour();
int dif = neighbours[i].get_eval();
__update_solution();
conflicts -= dif;
} while(iter < 10000);
// cout<<"Iteraciones de busqueda local: " << iter << endl;
}
void Sudoku::extendedLocalSearch(){
conflicts = evalSolution();
int iter = 0;
do {
iter++;
__calc_neighbours_allperm();
auto size_neighbours = neighbours.size();
if(size_neighbours == 0) break; //no improvement
auto i = rand() % size_neighbours; //look into using c++11 random lib
int sq = neighbours[i].get_square();
vector<int> tmp = solution[sq];
solution[sq] = neighbours[i].get_neighbour();
int dif = neighbours[i].get_eval();
__update_solution();
int nev = evalSolution();
conflicts -= dif;
} while(iter < 10000);
// cout<<"Iteraciones de busqueda local: " << iter << endl;
}
void Sudoku::simmulatedAnnealing(int total_time, double intial_temp, ofstream &out){
const clock_t begin_time = clock();
int last_written = 1000 * double(clock()) / CLOCKS_PER_SEC;
conflicts = evalSolution();
double temp = intial_temp;
int time = total_time;
out << conflicts << "\t" << temp << endl;
while (time && conflicts > 0){
time = total_time - 1000 * int(double( clock () - begin_time ) / CLOCKS_PER_SEC);
time = time > 0 ? time : 0;
// temp = pow(double(time)/double(total_time), 2) * intial_temp; //will cuadratically go to 0
temp = (double(time)/double(total_time)) * intial_temp; //will linearly go to 0
__calc_neighbours(true);
auto size_neighbours = neighbours.size();
for (int i = 0; i < size_neighbours; ++i){
int dif = neighbours[i].get_eval();
if(dif <= 0 && exp((dif - 1) / temp) < ((double) rand() / (RAND_MAX))) continue;
int sq = neighbours[i].get_square();
solution[sq] = neighbours[i].get_neighbour();
__update_solution();
conflicts -= dif;
int time_since = 1000 * int(double( clock () ) / CLOCKS_PER_SEC);
if (time_since - last_written >= 1000*60*2 - 50){ //every two minutes
last_written = 1000 * double(clock()) / CLOCKS_PER_SEC;
out << conflicts << "\t" << temp << endl;
}
break;
}
}
out << conflicts << "\t" << temp << endl;
out << "TIME:\t" << 1000 * int(double( clock () - begin_time ) / CLOCKS_PER_SEC)<<endl;
}
int SolutionNeighbour::get_square(){
return square;
}
vector<int> SolutionNeighbour::get_neighbour(){
return neighbour;
}
int SolutionNeighbour::get_eval() {
return eval;
}
SolutionNeighbour::SolutionNeighbour(int sq, vector<int> &n, int e){
square = sq;
neighbour = n;
eval = e;
}