-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (62 loc) · 2.33 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
#include <iostream>
#include <vector>
#include <chrono>
#include "./src/genSudoSolver.h"
#include "./src/sudosolver.h"
using std::cout;
using std::endl;
using std::vector;
using std::cin;
using namespace std::chrono;
const int boardSize = 9; // 3x3 box grid
int main() {
vector<vector<int> > initialBoard(boardSize, vector<int>(boardSize, 0));
for(int i = 0; i < boardSize; ++i) { // Reads in initial board
for(int j = 0; j < boardSize; ++j) {
int tmp;
cin >> tmp;
initialBoard[i][j] = tmp; // There must be zeros for unfilled slots
}
}
// BACKTRACKING / GA
bool backtrack = false;
bool ga = true;
// PARAMETERS
const int populationSize = 25000; // Population size
int selectionRateChance = 25; // Percent chance
int randomRateChance = 25; // Percent chance
int numberChildren = 4; // Number of children per pair of parents
int mutationRate = 25; // Percent chance
int restartAfterNImprovement = 50; // Restart after N generations without improvement
int maxGenerationCap = 500; // Max generation cap
int multiple = populationSize / 100;
selectionRateChance *= multiple;
randomRateChance *= multiple;
int restartNumber = 0;
// The mutation rate is less strict than the selection rate and random selection rate
auto start = high_resolution_clock::now();
// GA
if(ga) {
while(!genSudoSolver::geneticSolver(populationSize, selectionRateChance, randomRateChance, numberChildren, mutationRate, restartAfterNImprovement, maxGenerationCap, initialBoard, boardSize, restartNumber)) {
++restartNumber;
}
}
// BACKTRACKING
if(backtrack) {
vector<int> allowedNums(boardSize, 0);
for(int i = 0; i < boardSize; ++i) {
allowedNums[i] = i + 1;
}
bool solvable = sudoSolver::solveSudoku(boardSize, initialBoard, allowedNums);
cout << endl;
for(int i = 0; i < boardSize; ++i) {
for(int j = 0; j < boardSize; ++j) {
cout << initialBoard[i][j] << " ";
}
cout << endl;
}
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<seconds>(end - start);
cout << "Time taken for solution: " << duration.count() << " seconds with " << restartNumber << " restarts." << endl;
}