forked from numbbo/coco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_experiment.c
286 lines (232 loc) · 10.3 KB
/
example_experiment.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
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
/**
* An example of benchmarking random search on a COCO suite. A grid search optimizer is also
* implemented and can be used instead of random search.
*
* Set the global parameter BUDGET_MULTIPLIER to suit your needs.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "coco.h"
/**
* The maximal budget for evaluations done by an optimization algorithm equals dimension * BUDGET_MULTIPLIER.
* Increase the budget multiplier value gradually to see how it affects the runtime.
*/
static const size_t BUDGET_MULTIPLIER = 2;
/**
* The maximal number of independent restarts allowed for an algorithm that restarts itself.
*/
static const size_t INDEPENDENT_RESTARTS = 1e5;
/**
* The random seed. Change if needed.
*/
static const uint32_t RANDOM_SEED = 0xdeadbeef;
/**
* A function type for evaluation functions, where the first argument is the vector to be evaluated and the
* second argument the vector to which the evaluation result is stored.
*/
typedef void (*evaluate_function_t)(const double *x, double *y);
/**
* A pointer to the problem to be optimized (needed in order to simplify the interface between the optimization
* algorithm and the COCO platform).
*/
static coco_problem_t *PROBLEM;
/**
* The function that calls the evaluation of the first vector on the problem to be optimized and stores the
* evaluation result in the second vector.
*/
static void evaluate_function(const double *x, double *y) {
coco_evaluate_function(PROBLEM, x, y);
}
/* Declarations of all functions implemented in this file (so that their order is not important): */
void example_experiment(const char *suite_name,
const char *observer_name,
coco_random_state_t *random_generator);
void my_random_search(evaluate_function_t evaluate,
const size_t dimension,
const size_t number_of_objectives,
const double *lower_bounds,
const double *upper_bounds,
const size_t max_budget,
coco_random_state_t *random_generator);
void my_grid_search(evaluate_function_t evaluate,
const size_t dimension,
const size_t number_of_objectives,
const double *lower_bounds,
const double *upper_bounds,
const size_t max_budget);
/**
* The main method initializes the random number generator and calls the example experiment on the
* bi-objective suite.
*/
int main(void) {
coco_random_state_t *random_generator = coco_random_new(RANDOM_SEED);
/* Change the log level to "warning" to get less output */
coco_set_log_level("info");
printf("Running the example experiment... (might take time, be patient)\n");
fflush(stdout);
example_experiment("bbob-biobj", "bbob-biobj", random_generator);
/* Uncomment the line below to run the same example experiment on the bbob suite
example_experiment("bbob", "bbob", random_generator); */
printf("Done!\n");
fflush(stdout);
coco_random_free(random_generator);
return 0;
}
/**
* A simple example of benchmarking random search on a suite with instances from 2016.
*
* @param suite_name Name of the suite (use "bbob" for the single-objective and "bbob-biobj" for the
* bi-objective suite).
* @param observer_name Name of the observer (use "bbob" for the single-objective and "bbob-biobj" for the
* bi-objective observer).
* @param random_generator The random number generator.
*/
void example_experiment(const char *suite_name,
const char *observer_name,
coco_random_state_t *random_generator) {
size_t run;
coco_suite_t *suite;
coco_observer_t *observer;
/* Set some options for the observer. See documentation for other options. */
char *observer_options =
coco_strdupf("result_folder: RS_on_%s "
"algorithm_name: RS "
"algorithm_info: \"A simple random search algorithm\"", suite_name);
/* Initialize the suite and observer */
suite = coco_suite(suite_name, "year: 2016", "dimensions: 2,3,5,10,20,40");
observer = coco_observer(observer_name, observer_options);
coco_free_memory(observer_options);
/* Iterate over all problems in the suite */
while ((PROBLEM = coco_suite_get_next_problem(suite, observer)) != NULL) {
size_t dimension = coco_problem_get_dimension(PROBLEM);
/* Run the algorithm at least once */
for (run = 1; run <= 1 + INDEPENDENT_RESTARTS; run++) {
size_t evaluations_done = coco_problem_get_evaluations(PROBLEM);
long evaluations_remaining = (long) (dimension * BUDGET_MULTIPLIER) - (long) evaluations_done;
/* Break the loop if the target was hit or there are no more remaining evaluations */
if (coco_problem_final_target_hit(PROBLEM) || (evaluations_remaining <= 0))
break;
/* Call the optimization algorithm for the remaining number of evaluations */
my_random_search(evaluate_function,
dimension,
coco_problem_get_number_of_objectives(PROBLEM),
coco_problem_get_smallest_values_of_interest(PROBLEM),
coco_problem_get_largest_values_of_interest(PROBLEM),
(size_t) evaluations_remaining,
random_generator);
/* Break the loop if the algorithm performed no evaluations or an unexpected thing happened */
if (coco_problem_get_evaluations(PROBLEM) == evaluations_done) {
printf("WARNING: Budget has not been exhausted (%lu/%lu evaluations done)!\n", evaluations_done,
dimension * BUDGET_MULTIPLIER);
break;
}
else if (coco_problem_get_evaluations(PROBLEM) < evaluations_done)
coco_error("Something unexpected happened - function evaluations were decreased!");
}
}
coco_observer_free(observer);
coco_suite_free(suite);
}
/**
* A random search algorithm that can be used for single- as well as multi-objective optimization.
*
* @param evaluate The evaluation function used to evaluate the solutions.
* @param dimension The number of variables.
* @param number_of_objectives The number of objectives.
* @param lower_bounds The lower bounds of the region of interested (a vector containing dimension values).
* @param upper_bounds The upper bounds of the region of interested (a vector containing dimension values).
* @param max_budget The maximal number of evaluations.
* @param random_generator Pointer to a random number generator able to produce uniformly and normally
* distributed random numbers.
*/
void my_random_search(evaluate_function_t evaluate,
const size_t dimension,
const size_t number_of_objectives,
const double *lower_bounds,
const double *upper_bounds,
const size_t max_budget,
coco_random_state_t *random_generator) {
double *x = coco_allocate_vector(dimension);
double *y = coco_allocate_vector(number_of_objectives);
double range;
size_t i, j;
for (i = 0; i < max_budget; ++i) {
/* Construct x as a random point between the lower and upper bounds */
for (j = 0; j < dimension; ++j) {
range = upper_bounds[j] - lower_bounds[j];
x[j] = lower_bounds[j] + coco_random_uniform(random_generator) * range;
}
/* Call the evaluate function to evaluate x on the current problem (this is where all the COCO logging
* is performed) */
evaluate(x, y);
}
coco_free_memory(x);
coco_free_memory(y);
}
/**
* A grid search optimizer that can be used for single- as well as multi-objective optimization.
*
* @param evaluate The evaluation function used to evaluate the solutions.
* @param dimension The number of variables.
* @param number_of_objectives The number of objectives.
* @param lower_bounds The lower bounds of the region of interested (a vector containing dimension values).
* @param upper_bounds The upper bounds of the region of interested (a vector containing dimension values).
* @param max_budget The maximal number of evaluations.
*
* If max_budget is not enough to cover even the smallest possible grid, only the first max_budget
* nodes of the grid are evaluated.
*/
void my_grid_search(evaluate_function_t evaluate,
const size_t dimension,
const size_t number_of_objectives,
const double *lower_bounds,
const double *upper_bounds,
const size_t max_budget) {
double *x = coco_allocate_vector(dimension);
double *y = coco_allocate_vector(number_of_objectives);
long *nodes = (long *) coco_allocate_memory(sizeof(long) * dimension);
double *grid_step = coco_allocate_vector(dimension);
size_t i, j;
size_t evaluations = 0;
long max_nodes = (long) floor(pow((double) max_budget, 1.0 / (double) dimension)) - 1;
/* Take care of the borderline case */
if (max_nodes < 1) max_nodes = 1;
/* Initialization */
for (j = 0; j < dimension; j++) {
nodes[j] = 0;
grid_step[j] = (upper_bounds[j] - lower_bounds[j]) / (double) max_nodes;
}
while (evaluations < max_budget) {
/* Construct x and evaluate it */
for (j = 0; j < dimension; j++) {
x[j] = lower_bounds[j] + grid_step[j] * (double) nodes[j];
}
/* Call the evaluate function to evaluate x on the current problem (this is where all the COCO logging
* is performed) */
evaluate(x, y);
evaluations++;
/* Inside the grid, move to the next node */
if (nodes[0] < max_nodes) {
nodes[0]++;
}
/* At an outside node of the grid, move to the next level */
else if (max_nodes > 0) {
for (j = 1; j < dimension; j++) {
if (nodes[j] < max_nodes) {
nodes[j]++;
for (i = 0; i < j; i++)
nodes[i] = 0;
break;
}
}
/* At the end of the grid, exit */
if ((j == dimension) && (nodes[j - 1] == max_nodes))
break;
}
}
coco_free_memory(x);
coco_free_memory(y);
coco_free_memory(nodes);
coco_free_memory(grid_step);
}