-
Notifications
You must be signed in to change notification settings - Fork 0
/
hillclimber.c
175 lines (143 loc) · 3.23 KB
/
hillclimber.c
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
#include "hillclimber.h"
void produce_random_solution(
struct Board *board,
struct Num_Coordinates *numbers)
{
int row, col;
int random_num;
for (row = 0; row < board->rows; row++)
{
for (col = 0; col < board->cols; col++)
{
if (board->grid[row][col] == FREE)
{
do {
random_num = rand() % numbers->count;
} while (numbers->coordinates[random_num].row != UNKNOWN);
numbers->coordinates[random_num].row = row;
numbers->coordinates[random_num].col = col;
board->grid[row][col] = TAKEN;
}
}
}
}
void update_points(
struct Num_Coordinates *solution,
int *points,
int i,
int end)
{
while (i < end)
{
if (distance(&solution->coordinates[i], &solution->coordinates[i + 1]) == 1)
{
points[i] = 1;
}
i++;
}
}
void climb_hills(
struct Board *board,
struct Num_Coordinates *initial,
int *points,
int *iterations)
{
produce_random_solution(board, initial);
update_points(initial, points, 0, initial->count);
process_deriviate_solutions(board, initial, points, iterations);
}
void process_deriviate_solutions(
struct Board *board,
struct Num_Coordinates *solution,
int *points,
int *iterations)
{
int i, j, best_i, best_j;
int best_change, current_change;
do
{
(*iterations)++;
best_i = NONE;
best_j = NONE;
i = best_change = 0;
while (i < solution->count)
{
i = find_number_to_swap(board, solution, i);
j = i + 1;
while (j < solution->count)
{
j = find_number_to_swap(board, solution, j);
if (j < solution->count)
{
current_change = assess_deriviate(solution, points, i, j);
if (current_change > best_change)
{
best_i = i;
best_j = j;
best_change = current_change;
}
}
j++;
}
i++;
}
if (best_change > 0)
{
swap_numbers(solution, best_i, best_j);
update_points(solution, points, best_i - 1, best_i + 1);
update_points(solution, points, best_j - 1, best_j + 1);
}
} while (best_change > 0);
}
int assess_deriviate(
const struct Num_Coordinates *solution,
const int *points,
const int i,
const int j)
{
int after_points = 0;
int initial_points = points[i - 1] + points[i] + points[j - 1] + points[j];
if (distance(&solution->coordinates[i - 1], &solution->coordinates[j]) == 1)
{
after_points++;
}
if (distance(&solution->coordinates[j], &solution->coordinates[i + 1]) == 1)
{
after_points++;
}
if (distance(&solution->coordinates[j - 1], &solution->coordinates[i]) == 1)
{
after_points++;
}
if (distance(&solution->coordinates[i], &solution->coordinates[j + 1]) == 1)
{
after_points++;
}
return after_points - initial_points;
}
int find_number_to_swap(
const struct Board *board,
const struct Num_Coordinates *solution,
int i)
{
while (
i < solution->count &&
board->grid[solution->coordinates[i].row][solution->coordinates[i].col] != TAKEN)
{
i++;
}
return i;
}
void swap_numbers(
struct Num_Coordinates *solution,
const int i,
const int j)
{
struct Coordinate temp;
temp.row = solution->coordinates[i].row;
temp.col = solution->coordinates[i].col;
solution->coordinates[i].row = solution->coordinates[j].row;
solution->coordinates[i].col = solution->coordinates[j].col;
solution->coordinates[j].row = temp.row;
solution->coordinates[j].col = temp.col;
}