This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heur.c
214 lines (184 loc) · 4.01 KB
/
heur.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
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
#include "heur.h"
struct solution *
solution_new (size_t order)
{
struct solution *s = malloc(sizeof *s);
// The first node (0) is implicitly the first element
// hence the solution array is order-1 in size;
s->order = order-1;
s->data = malloc(order * sizeof *(s->data));
s->cost = WORSE_SOLUTION;
s->valid = false;
s->updated = false;
return s;
}
void
solution_init (struct solution * solution)
{
for(size_t i = 0; i < solution->order; i++)
solution->data[i] = i+1;
}
void
solution_free (struct solution *solution)
{
if(solution != NULL) {
if(solution->data != NULL) {
free(solution->data);
free(solution);
}
}
}
struct solution *
solution_new_shuffle(struct matrix *matrix)
{
struct solution *S = solution_new(matrix->order);
solution_init(S);
solution_shuffle(S, S->order);
solution_update(S, matrix);
return S;
}
void
solution_print (struct solution * solution)
{
printf("(");
printf("%c", solution->valid ? ' ':'!');
printf("%c", solution->updated ? ' ':'!');
printf("%10d):\t", solution->cost);
for(size_t i = 0; i < solution->order; i++)
printf("%d ", solution->data[i]);
printf("\n");
}
void
solution_path_print (struct solution * solution, struct matrix *matrix)
{
int a, b, c, i, cost;
i = 0;
cost = 0;
a = 0;
b = solution_get(solution, i++);
c = matrix_get(matrix, a, b);
cost += c;
fprintf(stdout,"Cost %d->%d = %d (%d)\n", a, b, c, cost);
fflush(stdout);
while(b != solution->order) {
a = b;
b = solution_get(solution, i++);
c = matrix_get(matrix, a, b);
cost += c;
fprintf(stdout,"Cost %d->%d = %d (%d)\n", a, b, c, cost);
fflush(stdout);
}
}
void
solution_shuffle (struct solution *solution, size_t n)
{
if (n > 1)
{
for (size_t i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = solution->data[j];
solution->data[j] = solution->data[i];
solution->data[i] = t;
}
}
solution->updated = false;
}
int
solution_get (struct solution *solution, size_t n)
{
return solution->data[n];
}
int
solution_cost (struct solution *solution)
{
return solution->cost;
}
void
solution_update (struct solution *solution, struct matrix *matrix)
{
int a, b, c, i, cost;
i = 0;
cost = 0;
a = 0;
b = solution_get(solution, i++);
c = matrix_get(matrix, a, b);
solution->valid = true;
if(c == 0)
solution->valid = false;
cost += c;
#ifdef DEBUG
printf("Cost %d->%d = %d (%d)\n", a, b, c, cost);
#endif
while(b != solution->order) {
a = b;
b = solution_get(solution, i++);
c = matrix_get(matrix, a, b);
if(c == 0) {
#ifdef DEBUG
printf("SOLUTION INVALID\n");
#endif
solution->valid = false;
}
cost += c;
#ifdef DEBUG
printf("Cost %d->%d = %d (%d)\n", a, b, c, cost);
#endif
}
solution->cost = cost;
solution->updated = true;
}
void
solution_swap (struct solution *solution,
size_t a,
size_t b)
{
#ifdef DEBUG
printf("Swap %d<->%d\n", a, b);
#endif
int t = solution->data[a];
solution->data[a] = solution->data[b];
solution->data[b] = t;
}
void
solution_copy (struct solution * dst,
struct solution * src)
{
dst->order = src->order;
dst->cost = src->cost;
dst->valid = src->valid;
dst->updated = src->updated;
for(size_t i = 0; i<dst->order; i++)
dst->data[i] = src->data[i];
}
struct solution *
solution_dup (struct solution * src)
{
struct solution *dst = solution_new(src->order);
solution_copy(dst, src);
return dst;
}
struct solution *
solution_new_like(struct solution *src)
{
struct solution *S = solution_new(src->order);
return S;
}
size_t
solution_pick_random_elem(struct solution *solution)
{
return rand() % solution->order;
}
void
solution_random_neighbor (struct solution *dst,
struct solution *src)
{
size_t pos_a = 0;
size_t pos_b = 0;
while(pos_a == pos_b) {
pos_a = solution_pick_random_elem(src);
pos_b = solution_pick_random_elem(src);
}
solution_copy(dst, src);
solution_swap(dst, pos_a, pos_b);
}