-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHillClimbing.cpp
323 lines (276 loc) · 8.78 KB
/
HillClimbing.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
#include<bits/stdc++.h>
#include<ctime>
#include "WriteCSV.h"
using namespace std;
#ifndef Head_Queen
#define Head_Queen
vector<int>start;
#define OFF 0 //Representing 0 as stop
#define ON 1 // Representing 1 as go_on
class N_Queen
{
public:
N_Queen(int, int);
virtual ~N_Queen();
int find_x();
int find_y();
void select_x(int);
void select_y(int);
int walking_up(int); // taking upward one step
int walking_down(int); // taking one step downward
protected:
private:
int xval; // xval on the screen_board
int yval; // yval on the screen_board
};
#endif // H_Queen
#ifndef Head_state
#define Head_state
class States
{
public:
States();
States(int);
virtual ~States();
vector<N_Queen> find_queens();
void append_queen(N_Queen); // add queens_arrray on the screen_board
vector<States> possible_sates(); // possible states for this state
States best_possible_states(); // finding best possible state for this state
int attacking_count(); // number of possible attks between 2 queens_arrray
int get_n();
void print_board(); // print the screen_board
protected:
private:
vector<N_Queen> queens_arrray; // list of on the screen_board
int no; // screen_board dimension_size
};
#endif // Head_state
States::States() {
// constructor
}
/* sets dimension_size of screen_board */
States::States(int dimension_size) {
no = dimension_size;
start.clear();
for(int i = 0; i < no; i++) {
int number = rand() % no;
start.push_back(number);
N_Queen q(number, i); // setting value for random rows and different columns
this->append_queen(q);
}
}
/* calling the destructor */
States::~States() {
}
/* Getter method for n */
int States::get_n() {
return no;
}
/* Adding a queen to the list of queens */
void States::append_queen(N_Queen q) {
if((int)queens_arrray.size() < no) {
queens_arrray.push_back(q); // pushing the queen in the list
}
}
/* Getting all possible states from this state by making each of the queen on the screen_board
// manipulating its x_value, but with fixed */
vector<States> States::possible_sates() {
vector<States> pos_staes;
int x_already, y_already;
for(int i = 0; i < no; i++) {
// position initially
x_already = queens_arrray[i].find_x();
y_already = queens_arrray[i].find_y();
while(queens_arrray[i].walking_up(no) != OFF)
pos_staes.push_back(*this);
queens_arrray[i].select_x(x_already);
queens_arrray[i].select_y(y_already);
while(queens_arrray[i].walking_down(no) != OFF)
pos_staes.push_back(*this);
queens_arrray[i].select_x(x_already);
queens_arrray[i].select_y(y_already);
}
return pos_staes;
}
/* possible number attacks counting */
int States::attacking_count() {
// counting number of attacks in rows
int attack_row = 0;
int row_queen[no];
for(int i = 0; i < no; i++)
row_queen[i] = 0;
// counting number of attacks in diagonals
int diagonally_attacks = 0;
int queens_by_diagonal[2*no-1], queens_by_diagonal_2[2*no-1];
for(int i = 0; i < 2*no-1; i++) {
queens_by_diagonal[i] = 0;
queens_by_diagonal_2[i] = 0;
}
// counting number of queens
int xval, yval;
for(int i = 0; i < no; i++) {
xval = queens_arrray[i].find_x();
yval = queens_arrray[i].find_y();
++row_queen[xval]; // for each row
++queens_by_diagonal[xval+yval]; // antidiagonal direction
++queens_by_diagonal_2[no-1+yval-xval]; // in each primary diagonal direction
}
// row_wise number of attks
for(int i = 0; i < no; i++)
if(row_queen[i] > 1)
attack_row += row_queen[i] - 1;
// Diagonally number of attckks
for(int i = 0; i < 2*no-1; i++) {
if(queens_by_diagonal[i] > 1)
diagonally_attacks += queens_by_diagonal[i] - 1;
if(queens_by_diagonal_2[i] > 1)
diagonally_attacks += queens_by_diagonal_2[i] - 1;
}
return diagonally_attacks + attack_row;
}
States States::best_possible_states() {
vector<States> pos_staes = possible_sates();
int number_of_possible_states = pos_staes.size();
vector<int> best_possible;
int minimum_index; // index for best possible successor
int attacks_min = no*no;
int attks;
for(int i = 0; i < number_of_possible_states; i++) {
attks = pos_staes[i].attacking_count();
if(attks < attacks_min) { // trying to find a new minimum
attacks_min = attks;
best_possible.clear(); // restart the vector
best_possible.push_back(i); // adding the new minimum value
}
else if(attks == attacks_min)
best_possible.push_back(i); // adds to list of best possible states
}
//initializing random number
minimum_index = rand() % (int)best_possible.size();
return pos_staes[best_possible[minimum_index]];
}
/* Printing the screen_board */
void States::print_board() {
char screen_board[no][no];
// initializing with underscores
for(int i = 0; i < no; i++)
for(int j = 0; j < no; j++)
screen_board[i][j] = '_';
// putting on screen_board the queens
for(int i = 0; i < no; i++)
screen_board[queens_arrray[i].find_x()][queens_arrray[i].find_y()] = 'Q';
// now prining :::
for(int i = 0; i < no; i++)
for(int j = 0; j < no; j++)
cout << screen_board[i][j] << (j == no-1 ? "\n" : " ");
}
/* Constructor function */
N_Queen::N_Queen(int coord_x, int coord_y) {
xval = coord_x;
yval = coord_y;
}
/* Destructor function */
N_Queen::~N_Queen() {
}
/* Getter method funcion for x_value */
int N_Queen::find_x() {
return xval;
}
/* Getter method function for y_value */
int N_Queen::find_y() {
return yval;
}
/* Setter method function for x_value */
void N_Queen::select_x(int coord_x) {
xval = coord_x;
}
/* Setter method function for y_value */
void N_Queen::select_y(int coord_y) {
yval = coord_y;
}
int N_Queen::walking_up(int no) {
if(xval > 0) {
--xval; // down step
return ON;
}
return OFF;
}
int N_Queen::walking_down(int no) {
if(xval < no-1) {
++xval; // up step
return ON;
}
return OFF;
}
/* Finally Hill_climb algo for finding the min number of attacks */
States Hill_climb(States now, int dimension_size) {
States further;
int no_of_iteration = 1; // iteration counting
while(true) { // find the solution unless an error occur
further = now.best_possible_states();
cout<< "Solving" << dimension_size << "-Queens" << " interation count: " << no_of_iteration << "\n";
if(now.attacking_count() <= further.attacking_count()) {
return now;
}
now = further;
++no_of_iteration;
}
// error case
return States(-1);
}
void Solve(int dimension_size, map<int, double>&mapp){
clock_t startTime = clock();
States initial_state(dimension_size);
States solution = Hill_climb(initial_state, dimension_size);
// if in the case not able to find the solution ::
// reset the inital stage and trying again
while(solution.attacking_count() > 0) {
cout<< "Trying Again.................................................\n";
States initial_state(dimension_size);
solution = Hill_climb(initial_state, dimension_size);
}
clock_t stopTime = clock();
double Runtime = (float) (stopTime - startTime) / 1000000 ;
cout << "Runtime: " << Runtime << " seconds" << endl;
cout << "\nInitial States of Queens:\n\n";
for(int i=0;i<dimension_size;i++){
for(int j=0;j<dimension_size;j++){
if(start[i]==j){
cout << 'Q' << " ";
}
else{
cout << "_" << " ";
}
}
cout << "\n";
}
cout << "\nFinal Output:\n" << endl;
if(solution.get_n() == -1){
mapp.clear();
// double zero = 0;
mapp.insert(make_pair(dimension_size, -1));
cout << "Could not find the solution" << endl;
WriteCSV("HillClimbing.csv", mapp);
}
else {
mapp.clear();
mapp.insert(make_pair(dimension_size, Runtime));
WriteCSV("HillClimbing.csv", mapp);
solution.print_board();
}
}
int main() {
map<int, double> mapp;
int dimension_size;
for(int i = 0; i < 40; i++){
cout << "Enter the Dimension Size: \n";
cin >> dimension_size;
if(dimension_size > 40){
cout << "values above 40 take too long with hill Climbing";
return 0;
}
Solve(dimension_size, mapp);
}
// WriteCSV("HillClimbing.csv", mapp);
return 0;
}