-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
139 lines (125 loc) · 4.02 KB
/
main.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
#include<iostream>
#include <ctime>
#include "src/Utils.h"
#include "src/Logger.h"
#include "src/Sudoku.h"
#include "src/SudokuGenerator.h"
#include "src/SimulatedAnnealing.h"
#include "src/GeneticAlgorithm.h"
#include "src/Backtrack.h"
using namespace std;
#define MAX_EMPTY_CELLS 160
#define ITERATION_PER_DIFFICULTY 100
void testGeneticAlgorithm()
{
// Parameters for Genetic algorithm
int population_size=10; //size of Gene pool
int stop=0; //number of generations to run until forced to stop; to go until completion set to 0
//!!!PLEASE use steps of 10% when adjusting the parameters below to avoid segmentation faults!!!
int elitism=10; //best percentage of candidates go to new gen unchanged; elitism=10 -> best 10%
int eligible=50; //best percentage of candidates get to mate for new gen; eligible=50 -> best 50%
float mutation=0.4; //chance of random gene for offspring instead of parents'; mutation=0.1 -> 10% chance
int mat[9][9] =
{
{0, -1, -1, -1, -1, -1, -1, -1, 1},
{-1, -1, 7, -1, -1, 8, -1, 2, 6},
{6, -1, -1, 4, 2, -1, -1, 7, -1},
{-1, 7, -1, -1, 6, 2, -1, 4, 3},
{-1, -1, 5, 3, -1, 1, 6, -1, -1},
{8, 6, -1, 7, 4, -1, -1, 0, -1},
{-1, 0, -1, -1, 7, 6, -1, -1, 8},
{2, 3, -1, 5, -1, -1, 7, -1, -1},
{7, -1, -1, -1, -1, -1, -1, -1, 0}
};
GeneticAlgorithm b(mat);
bool result = b.compute(population_size, elitism, eligible, mutation, stop);
cout << result << " , " << b.generation<< endl;
/*
Sudoku a = SudokuGenerator::generateGuarantee(9, 1, 20);
GeneticAlgorithm b = GeneticAlgorithm(a);
bool result = b.run();
cout << "R: " << result << endl;
a.destroy();
b.destroy();
*/
/*
for(int num_empty = 0; num_empty < MAX_EMPTY_CELLS; num_empty += 10)
{
Logger bullshit;
bullshit.open("./logs/genetic_algorithm_"+to_string(num_empty)+".txt");
for(int j = 0; j < ITERATION_PER_DIFFICULTY; j++)
{
Sudoku a = SudokuGenerator::generateGuarantee(16, 1, num_empty);
GeneticAlgorithm b = GeneticAlgorithm(a);
bool result = b.run();
cout << j << " : " << result << " , " << b.generation_count<< endl;
vector<double> shit;
shit.push_back(j);
shit.push_back(result);
shit.push_back(b.generation_count);
bullshit.log(shit);
b.destroy();
a.destroy();
}
bullshit.close();
}*/
}
void testSimulatedAnnealing()
{
for(int num_empty = 0; num_empty < MAX_EMPTY_CELLS; num_empty += 10)
{
Logger bullshit;
bullshit.open("./logs/local_search_"+to_string(num_empty)+".txt");
for(int j = 0; j < ITERATION_PER_DIFFICULTY; j++)
{
Sudoku a = SudokuGenerator::generateGuarantee(16, 1, num_empty);
SimulatedAnnealing b = SimulatedAnnealing(a);
b.alpha = 0.9995;
b.T = 4;
b.num_neighbors = 1000;
b.Tmin = 2;
bool result = b.run();
cout << j << " : " << result << " , " << b.total_iteration<< endl;
vector<double> shit;
shit.push_back(j);
shit.push_back(result);
shit.push_back(b.total_iteration);
bullshit.log(shit);
a.destroy();
}
bullshit.close();
}
}
void testBacktrack()
{
clock_t omg;
for(int num_empty = 160; num_empty < MAX_EMPTY_CELLS; num_empty += 10)
{
Logger bullshit;
bullshit.open_append("./logs/backtrack_"+to_string(num_empty)+".txt");
for(int j = 0; j < ITERATION_PER_DIFFICULTY; j++)
{
Sudoku a = SudokuGenerator::generateGuarantee(16, 1, num_empty);
omg = clock();
int result = Backtrack::run(a.size, a.board);
float goddamn = (float)(clock() - omg) / CLOCKS_PER_SEC;
cout << j << " : " << result << " , " << Backtrack::backtracks<< " , " << goddamn<< endl;
vector<double> shit;
shit.push_back(j);
shit.push_back(result);
shit.push_back(Backtrack::backtracks);
shit.push_back(goddamn);
bullshit.log(shit);
a.destroy();
}
bullshit.close();
}
}
int main()
{
//Sudoku::test();
testGeneticAlgorithm();
//testSimulatedAnnealing();
//testBacktrack();
return 0;
}