-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCspSolver.cpp
More file actions
169 lines (140 loc) · 5.26 KB
/
Copy pathCspSolver.cpp
File metadata and controls
169 lines (140 loc) · 5.26 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
//
// Created by Jonathan Hirsch on 3/21/15.
//
#include "CspSolver.h"
CspSolver::CspSolver(Rules rules) : constraints(rules)
{
}
PartialSolution *CspSolver::backtrack()
{
PartialSolution blankSol = PartialSolution();
PartialSolution result;
clock_t begin_time;
clock_t end_time;
begin_time = clock();
useVariableHeuristic = false;
useForwardChecking = false;
bool f = rBacktrack(blankSol, result);
end_time = clock();
//std::cout << ((float) end_time - begin_time) << "," << ((float) end_time - begin_time) / CLOCKS_PER_SEC;
std::cout << "Solution not using variable heuristic " << ((float) end_time - begin_time) << " ticks" << std::endl;
std::cout << "Solution not using variable heuristic " << ((float) end_time - begin_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
begin_time = clock();
useVariableHeuristic = true;
useForwardChecking = false;
f = rBacktrack(blankSol, result);
end_time = clock();
// std::cout << "," << ((float) end_time - begin_time) << "," << ((float) end_time - begin_time) / CLOCKS_PER_SEC;
std::cout << "Solution using variable heuristic " << ((float) end_time - begin_time) << " ticks" << std::endl;
std::cout << "Solution using variable heuristic " << ((float) end_time - begin_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
begin_time = clock();
useVariableHeuristic = false;
useForwardChecking = true;
f = rBacktrack(blankSol, result);
end_time = clock();
// std::cout << "," << ((float) end_time - begin_time) << "," << ((float) end_time - begin_time) / CLOCKS_PER_SEC;
std::cout << "Solution not using variable heuristic but using forward checking " << ((float) end_time - begin_time) << " ticks" << std::endl;
std::cout << "Solution not using variable heuristic but using forward checking " << ((float) end_time - begin_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
begin_time = clock();
useVariableHeuristic = true;
useForwardChecking = true;
f = rBacktrack(blankSol, result);
end_time = clock();
// std::cout << "," << ((float) end_time - begin_time) << "," << ((float) end_time - begin_time) / CLOCKS_PER_SEC << std::endl;
std::cout << "Solution using variable heuristic and using forward checking " << ((float) end_time - begin_time) << " ticks" << std::endl;
std::cout << "Solution using variable heuristic and using forward checking " << ((float) end_time - begin_time) / CLOCKS_PER_SEC << " seconds" << std::endl;
return new PartialSolution(result);
}
bool CspSolver::rBacktrack(PartialSolution &sol, PartialSolution &result)
{
int row, option;
chooseVariable(sol, row, option);
std::vector<int> cols = chooseCol(sol, row, option);
if (cols.size() == 0)
{
return NULL;
}
for (std::vector<int>::iterator i = cols.begin(); i != cols.end(); ++i)
{
int col = *i;
result = PartialSolution(sol);
result.chooseOptionForCell(row, col, option, useForwardChecking);
if (constraints.isSolValid(&result))
{
if (result.allDecided())
{
return true;
}
else
{
PartialSolution childResult;
if (rBacktrack(result, childResult))
{
result = PartialSolution(childResult);
return true;
}
}
}
}
return false;
}
bool compareRemainingValues(const std::vector<int> &first, const std::vector<int> &second)
{
if (first.at(2) == second.at(2))
{
// the one with more constraints
return first.at(3) >= second.at(3);
}
// the one with less options
return first.at(2) < second.at(2);
}
void CspSolver::chooseVariable(PartialSolution &sol, int &row, int &option)
{
if (!useVariableHeuristic)
{
// Naive implementation
for (row = 0; row < ARR_SIZE; ++row)
{
for (option = 0; option < ARR_SIZE; ++option)
{
if (!sol.isOptionDecided(row, option)) return;
}
}
}
else
{
std::list<std::vector<int> > variableRemainingOptions;
std::vector<int> temp;
for (int row = 0; row < ARR_SIZE; ++row)
{
for (int option = 0; option < ARR_SIZE; ++option)
{
if (!sol.isOptionDecided(row, option))
{
temp.push_back(row);
temp.push_back(option);
temp.push_back(sol.numOfCellsForOption(row, option));
temp.push_back(this->constraints.returnReleventConstraints(row, option).size());
variableRemainingOptions.push_back(temp);
temp.clear();
}
}
}
variableRemainingOptions.sort(compareRemainingValues);
temp = variableRemainingOptions.front();
row = temp.at(0);
option = temp.at(1);
}
}
std::vector<int> CspSolver::chooseCol(PartialSolution &sol, int row, int option)
{
std::vector<int> result = std::vector<int>();
for (int col = 0; col < ARR_SIZE; ++col)
{
if (sol.getOptionForCell(row, col, option))
{
result.push_back(col);
}
}
return result;
}