-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_search.c
163 lines (129 loc) · 4.39 KB
/
local_search.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
#include "local_search.h"
#include "lib.h"
//ILS
int *apply_pertubations(int *solution, int size, MoveApplicator move_applicator, Move *moves, int num_moves, int nb_pertubations)
{
int *new = (int *)malloc(size * sizeof(int));
for (size_t i = 0; i < size; i++)
{
new[i] = solution[i];
}
srand(time(NULL));
for (size_t i = 0; i < nb_pertubations; i++)
{
int index = rand() % num_moves;
Move move = moves[index];
new = move_applicator(new, size, move);
}
return new;
}
int *iterated_local_search(Instance *instance, int *soltution, Algorithm algo, MoveApplicator move_applicator, int nb_pertubations, int max_eval)
{
int step = 0;
int *best_sol = soltution;
int *cur_sol = soltution;
int nb_moves;
Move *moves = generate_swap_moves(instance->dim, &nb_moves);
nombre_evaluations = 0;
while (nombre_evaluations < max_eval)
{
int *new_sol = descente(instance, cur_sol, algo);
if (cost_function(instance, new_sol) < cost_function(instance, best_sol))
{
best_sol = new_sol;
}
cur_sol = apply_pertubations(best_sol, instance->dim, move_applicator, moves, nb_moves, nb_pertubations);
step++; //log purpose
}
return best_sol;
}
int *iterated_local_search_best_improver_swap(Instance *instance, int *soltution, int nb_pertubations, int max_eval)
{
return iterated_local_search(instance, soltution, best_improver_swap, apply_move_swap, nb_pertubations, max_eval);
}
int *iterated_local_search_best_improver_2opt(Instance *instance, int *soltution, int nb_pertubations, int max_eval)
{
return iterated_local_search(instance, soltution, best_improver_2opt, apply_move_2opt, nb_pertubations, max_eval);
}
int *iterated_local_search_first_improver_swap(Instance *instance, int *soltution, int nb_pertubations, int max_eval)
{
return iterated_local_search(instance, soltution, first_improver_swap, apply_move_swap, nb_pertubations, max_eval);
}
int *iterated_local_search_first_improver_2opt(Instance *instance, int *soltution, int nb_pertubations, int max_eval)
{
return iterated_local_search(instance, soltution, first_improver_2opt, apply_move_2opt, nb_pertubations, max_eval);
}
//SW
Move *generate_n_moves(Move *moves, int nb_moves, int n)
{
int *indexes = malloc(nb_moves * sizeof(int));
int i, j, tmp;
Move *gen_moves = malloc(sizeof(Move) * n);
for (i = 0; i < nb_moves; i++)
{
indexes[i] = i;
}
// Melange Fisher-Yates
for (i = nb_moves - 1; i > 0; i--)
{
j = rand() % (i + 1);
tmp = indexes[i];
indexes[i] = indexes[j];
indexes[j] = tmp;
}
for (i = 0; i < n; i++)
{
gen_moves[i] = moves[indexes[i]];
}
free(indexes);
return gen_moves;
}
int *sampled_walk(Instance *instance, int *soltution, MoveApplicator move_applicator, int lambda, int max_eval)
{
nombre_evaluations = 0;
int nb_moves;
Move *moves = generate_swap_moves(instance->dim, &nb_moves);
int step = 0;
int *best_sol = soltution, *new_sol, *cur_sol;
Move *lambda_moves;
while (nombre_evaluations < max_eval)
{
// generate and choice best -> sortie : cur_sol
lambda_moves = generate_n_moves(moves, nb_moves, lambda);
for (int i = 0; i < lambda; i++)
{
new_sol = move_applicator(best_sol, instance->dim, lambda_moves[i]);
if (i == 0)
{
cur_sol = new_sol;
}
else if (cost_function(instance, new_sol) < cost_function(instance, cur_sol))
{
free(cur_sol);
cur_sol = new_sol;
}
else
{
free(new_sol);
}
}
// update best_sol
if (cost_function(instance, best_sol) > cost_function(instance, cur_sol))
{
best_sol = cur_sol;
}
free(lambda_moves);
// log purpose
step++;
}
free(cur_sol);
return best_sol;
}
int *sampled_walk_swap(Instance *instance, int *soltution, int lambda, int max_eval)
{
return sampled_walk(instance, soltution, apply_move_swap, lambda, max_eval);
}
int *sampled_walk_2opt(Instance *instance, int *soltution, int lambda, int max_eval)
{
return sampled_walk(instance, soltution, apply_move_2opt, lambda, max_eval);
}