forked from numbbo/coco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_experiment.c
192 lines (147 loc) · 5.72 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
/*
* An example of benchmarking random search on three COCO suites. A grid search optimizer is also
* implemented and can be used instead of random search.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "coco.h"
/*
* The max budget for optimization algorithms should be set to dim * BUDGET
*/
static const size_t BUDGET = 2;
/**
* A random search algorithm that can be used for single- as well as multi-objective optimization.
*/
void my_random_search(coco_problem_t *problem) {
coco_random_state_t *rng = coco_random_new(0xdeadbeef);
const double *lbounds = coco_problem_get_smallest_values_of_interest(problem);
const double *ubounds = coco_problem_get_largest_values_of_interest(problem);
size_t dimension = coco_problem_get_dimension(problem);
size_t number_of_objectives = coco_problem_get_number_of_objectives(problem);
double *x = coco_allocate_vector(dimension);
double *y = coco_allocate_vector(number_of_objectives);
double range;
size_t i, j;
size_t max_budget = dimension * BUDGET;
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 = ubounds[j] - lbounds[j];
x[j] = lbounds[j] + coco_random_uniform(rng) * range;
}
/* Call COCO's evaluate function where all the logging is performed */
coco_evaluate_function(problem, x, y);
}
coco_random_free(rng);
coco_free_memory(x);
coco_free_memory(y);
}
/**
* A grid search optimizer that can be used for single- as well as multi-objective optimization.
* 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(coco_problem_t *problem) {
const double *lbounds = coco_problem_get_smallest_values_of_interest(problem);
const double *ubounds = coco_problem_get_largest_values_of_interest(problem);
size_t dimension = coco_problem_get_dimension(problem);
size_t number_of_objectives = coco_problem_get_number_of_objectives(problem);
double *x = coco_allocate_vector(dimension);
double *y = coco_allocate_vector(number_of_objectives);
long *nodes = coco_allocate_memory(sizeof(long) * dimension);
double *grid_step = coco_allocate_vector(dimension);
size_t i, j;
size_t evaluations = 0;
size_t max_budget = dimension * BUDGET;
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] = (ubounds[j] - lbounds[j]) / (double) max_nodes;
}
while (evaluations < max_budget) {
/* Construct x and evaluate it */
for (j = 0; j < dimension; j++) {
x[j] = lbounds[j] + grid_step[j] * (double) nodes[j];
}
/* Call COCO's evaluate function where all the logging is performed */
coco_evaluate_function(problem, 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);
}
/**
* A simple example of benchmarking an optimization algorithm on the bbob suite with instances from 2009.
*/
void example_bbob(void) {
/* Some options of the bbob observer. See documentation for other options. */
const char *observer_options = "result_folder: RS_on_bbob \
algorithm_name: RS \
algorithm_info: \"A simple random search algorithm\"";
coco_suite_t *suite;
coco_observer_t *observer;
coco_problem_t *problem;
suite = coco_suite("bbob", "year: 2016", "dimensions: 2,3,5,10,20,40");
observer = coco_observer("bbob", observer_options);
while ((problem = coco_suite_get_next_problem(suite, observer)) != NULL) {
my_random_search(problem);
}
coco_observer_free(observer);
coco_suite_free(suite);
}
/**
* A simple example of benchmarking an optimization algorithm on the biobjective suite.
*/
void example_biobj(void) {
/* Some options of the biobjective observer. See documentation for other options. */
const char *observer_options = "result_folder: RS_on_bbob-biobj \
algorithm_name: RS \
algorithm_info: \"A simple random search algorithm\"";
coco_suite_t *suite;
coco_observer_t *observer;
coco_problem_t *problem;
suite = coco_suite("bbob-biobj", "year: 2016", "dimensions: 2,3,5,10,20,40");
observer = coco_observer("bbob-biobj", observer_options);
while ((problem = coco_suite_get_next_problem(suite, observer)) != NULL) {
my_random_search(problem);
}
coco_observer_free(observer);
coco_suite_free(suite);
}
/*
* The main method calls the basic bbob-biobj experiment (random search)
*/
int main(void) {
printf("Running the example experiment... (might take time, be patient)\n");
fflush(stdout);
/* Change the log level to "warning" to get less output */
coco_set_log_level("info");
example_biobj();
printf("Done!\n");
fflush(stdout);
return 0;
}