forked from numbbo/coco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_experiment.c
409 lines (340 loc) · 14.8 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* 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 <time.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);
/* Structure and functions needed for timing the experiment */
typedef struct {
size_t number_of_dimensions;
size_t current_idx;
char **output;
size_t previous_dimension;
size_t cumulative_evaluations;
time_t start_time;
time_t overall_start_time;
} timing_data_t;
static timing_data_t *timing_data_initialize(coco_suite_t *suite);
static void timing_data_time_problem(timing_data_t *timing_data, coco_problem_t *problem);
static void timing_data_finalize(timing_data_t *timing_data);
/**
* 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);
/**
* Start the actual experiments on a test suite and use a matching logger, for
* example one of the following:
* bbob 24 unconstrained noiseless single-objective functions
* bbob-biobj 55 unconstrained noiseless bi-objective functions
* bbob-biobj-ext 92 unconstrained noiseless bi-objective functions
* bbob-largescale 24 unconstrained noiseless single-objective functions in large dimension
*
* Adapt to your need. Note that the experiment is run according
* to the settings, defined in example_experiment(...) below.
*/
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 given suite with default instances
* that can serve also as a timing experiment.
*
* @param suite_name Name of the suite (e.g. "bbob" or "bbob-biobj").
* @param observer_name Name of the observer matching with the chosen suite (e.g. "bbob-biobj"
* when using the "bbob-biobj-ext" suite).
* @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;
timing_data_t *timing_data;
/* 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.
*
* For more details on how to change the default options, see
* http://numbbo.github.io/coco-doc/C/#suite-parameters and
* http://numbbo.github.io/coco-doc/C/#observer-parameters. */
suite = coco_suite(suite_name, "", "");
observer = coco_observer(observer_name, observer_options);
coco_free_memory(observer_options);
/* Initialize timing */
timing_data = timing_data_initialize(suite);
/* 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",
(unsigned long) evaluations_done, (unsigned long) dimension * BUDGET_MULTIPLIER);
break;
}
else if (coco_problem_get_evaluations(PROBLEM) < evaluations_done)
coco_error("Something unexpected happened - function evaluations were decreased!");
}
/* Keep track of time */
timing_data_time_problem(timing_data, PROBLEM);
}
/* Output and finalize the timing data */
timing_data_finalize(timing_data);
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);
}
/**
* Allocates memory for the timing_data_t object and initializes it.
*/
static timing_data_t *timing_data_initialize(coco_suite_t *suite) {
timing_data_t *timing_data = (timing_data_t *) coco_allocate_memory(sizeof(*timing_data));
size_t function_idx, dimension_idx, instance_idx, i;
/* Find out the number of all dimensions */
coco_suite_decode_problem_index(suite, coco_suite_get_number_of_problems(suite) - 1, &function_idx,
&dimension_idx, &instance_idx);
timing_data->number_of_dimensions = dimension_idx + 1;
timing_data->current_idx = 0;
timing_data->output = (char **) coco_allocate_memory(timing_data->number_of_dimensions * sizeof(char *));
for (i = 0; i < timing_data->number_of_dimensions; i++) {
timing_data->output[i] = NULL;
}
timing_data->previous_dimension = 0;
timing_data->cumulative_evaluations = 0;
time(&timing_data->start_time);
time(&timing_data->overall_start_time);
return timing_data;
}
/**
* Keeps track of the total number of evaluations and elapsed time. Produces an output string when the
* current problem is of a different dimension than the previous one or when NULL.
*/
static void timing_data_time_problem(timing_data_t *timing_data, coco_problem_t *problem) {
double elapsed_seconds = 0;
if ((problem == NULL) || (timing_data->previous_dimension != coco_problem_get_dimension(problem))) {
/* Output existing timing information */
if (timing_data->cumulative_evaluations > 0) {
time_t now;
time(&now);
elapsed_seconds = difftime(now, timing_data->start_time) / (double) timing_data->cumulative_evaluations;
timing_data->output[timing_data->current_idx++] = coco_strdupf("d=%lu done in %.2e seconds/evaluation\n",
timing_data->previous_dimension, elapsed_seconds);
}
if (problem != NULL) {
/* Re-initialize the timing_data */
timing_data->previous_dimension = coco_problem_get_dimension(problem);
timing_data->cumulative_evaluations = coco_problem_get_evaluations(problem);
time(&timing_data->start_time);
}
} else {
timing_data->cumulative_evaluations += coco_problem_get_evaluations(problem);
}
}
/**
* Outputs and finalizes the given timing data.
*/
static void timing_data_finalize(timing_data_t *timing_data) {
/* Record the last problem */
timing_data_time_problem(timing_data, NULL);
if (timing_data) {
size_t i;
double elapsed_seconds;
time_t now;
int hours, minutes, seconds;
time(&now);
elapsed_seconds = difftime(now, timing_data->overall_start_time);
printf("\n");
for (i = 0; i < timing_data->number_of_dimensions; i++) {
if (timing_data->output[i]) {
printf("%s", timing_data->output[i]);
coco_free_memory(timing_data->output[i]);
}
}
hours = (int) elapsed_seconds / 3600;
minutes = ((int) elapsed_seconds % 3600) / 60;
seconds = (int)elapsed_seconds - (hours * 3600) - (minutes * 60);
printf("Total elapsed time: %dh%02dm%02ds\n", hours, minutes, seconds);
coco_free_memory(timing_data->output);
coco_free_memory(timing_data);
}
}