Skip to content

Commit

Permalink
Merge branch 'master' into master-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nikohansen authored Mar 20, 2019
2 parents 04c274d + 042e2d5 commit 011b70b
Show file tree
Hide file tree
Showing 146 changed files with 53,317 additions and 3,601 deletions.
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ jobs:
name: Run coco C tests
command: python do.py test-c
no_output_timeout: 1800
- run:
name: Run regression test
command: python do.py test-suites
no_output_timeout: 1800

test_ubuntu_latest_python2:
docker:
Expand Down Expand Up @@ -180,6 +184,11 @@ jobs:
name: Run coco C tests
command: python do.py test-c
no_output_timeout: 1800
- run:
name: Run regression test
command: python do.py test-suites
no_output_timeout: 1800


workflows:

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ test_script:
- cmd: python do.py test-java
- cmd: python do.py test-preprocessing
- cmd: python do.py test-postprocessing
- cmd: python do.py test-suites
# - cmd: python do.py test-postprocessing-all
2 changes: 1 addition & 1 deletion code-experiments/build/c/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ example_experiment: example_experiment.o coco.o
coco.o: coco.h coco.c
${CC} -c ${CCFLAGS} -o coco.o coco.c
example_experiment.o: coco.h coco.c example_experiment.c
${CC} -c ${CCFLAGS} -o example_experiment.o example_experiment.c
${CC} -c ${CCFLAGS} -o example_experiment.o example_experiment.c
146 changes: 100 additions & 46 deletions code-experiments/build/c/example_experiment.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>

#include "coco.h"

Expand Down Expand Up @@ -60,7 +61,9 @@ static void evaluate_constraint(const double *x, double *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 *suite_options,
const char *observer_name,
const char *observer_options,
coco_random_state_t *random_generator);

void my_random_search(evaluate_function_t evaluate_func,
Expand All @@ -70,14 +73,18 @@ void my_random_search(evaluate_function_t evaluate_func,
const size_t number_of_constraints,
const double *lower_bounds,
const double *upper_bounds,
const size_t number_of_integer_variables,
const size_t max_budget,
coco_random_state_t *random_generator);

void my_grid_search(evaluate_function_t evaluate,
void my_grid_search(evaluate_function_t evaluate_func,
evaluate_function_t evaluate_cons,
const size_t dimension,
const size_t number_of_objectives,
const size_t number_of_constraints,
const double *lower_bounds,
const double *upper_bounds,
const size_t number_of_integer_variables,
const size_t max_budget);

/* Structure and functions needed for timing the experiment */
Expand All @@ -96,7 +103,7 @@ 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.
* bbob suite.
*/
int main(void) {

Expand All @@ -108,21 +115,30 @@ int main(void) {
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:
* 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-constrained* 48 constrained noiseless single-objective functions]
* [bbob-largescale* 24 unconstrained noiseless single-objective functions in large dimension]
* bbob-largescale 24 unconstrained noiseless single-objective functions in large dimension
* bbob-mixint 24 unconstrained noiseless single-objective functions with mixed-integer variables
* bbob-biobj-mixint 92 unconstrained noiseless bi-objective functions with mixed-integer variables
*
* Suites with a star are partly implemented but not yet fully supported.
*
* 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);
coco_set_log_level("info");

/**
* For more details on how to change the default suite and observer options, see
* http://numbbo.github.io/coco-doc/C/#suite-parameters and
* http://numbbo.github.io/coco-doc/C/#observer-parameters. */

example_experiment("bbob", "", "bbob", "result_folder: RS_on_bbob", random_generator);

printf("Done!\n");
fflush(stdout);
Expand All @@ -137,51 +153,44 @@ int main(void) {
* 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"
* @param suite_options Options of the suite (e.g. "dimensions: 2,3,5,10,20 instance_indices: 1-5").
* @param observer_name Name of the observer matching with the chosen suite (e.g. "bbob-biobj"
* when using the "bbob-biobj-ext" suite).
* @param observer_options Options of the observer (e.g. "result_folder: folder_name")
* @param random_generator The random number generator.
*/
void example_experiment(const char *suite_name,
const char *suite_options,
const char *observer_name,
const char *observer_options,
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, "", "");
/* Initialize the suite and observer. */
suite = coco_suite(suite_name, "", suite_options);
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++) {

long evaluations_done = (long) (coco_problem_get_evaluations(PROBLEM) +
long evaluations_done = (long) (coco_problem_get_evaluations(PROBLEM) +
coco_problem_get_evaluations_constraints(PROBLEM));
long evaluations_remaining = (long) (dimension * BUDGET_MULTIPLIER) - evaluations_done;

/* Break the loop if the target was hit or there are no more remaining evaluations */
if ((coco_problem_final_target_hit(PROBLEM) &&
if ((coco_problem_final_target_hit(PROBLEM) &&
coco_problem_get_number_of_constraints(PROBLEM) == 0)
|| (evaluations_remaining <= 0))
break;
Expand All @@ -194,9 +203,10 @@ void example_experiment(const char *suite_name,
coco_problem_get_number_of_constraints(PROBLEM),
coco_problem_get_smallest_values_of_interest(PROBLEM),
coco_problem_get_largest_values_of_interest(PROBLEM),
coco_problem_get_number_of_integer_variables(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",
Expand All @@ -220,15 +230,18 @@ void example_experiment(const char *suite_name,
}

/**
* A random search algorithm that can be used for single- as well as multi-objective optimization.
* A random search algorithm that can be used for single- as well as multi-objective optimization. The
* problem's initial solution is evaluated first.
*
* @param evaluate_function The function used to evaluate the objective function.
* @param evaluate_constraint The function used to evaluate the constraints.
* @param evaluate_func The function used to evaluate the objective function.
* @param evaluate_cons The function used to evaluate the constraints.
* @param dimension The number of variables.
* @param number_of_objectives The number of objectives.
* @param number_of_constraints The number of constraints.
* @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 number_of_integer_variables The number of integer variables (if > 0, all integer variables come
* before any continuous ones).
* @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.
Expand All @@ -240,6 +253,7 @@ void my_random_search(evaluate_function_t evaluate_func,
const size_t number_of_constraints,
const double *lower_bounds,
const double *upper_bounds,
const size_t number_of_integer_variables,
const size_t max_budget,
coco_random_state_t *random_generator) {

Expand All @@ -248,18 +262,24 @@ void my_random_search(evaluate_function_t evaluate_func,
double *constraints_values = NULL;
double range;
size_t i, j;

if (number_of_constraints > 0 )
constraints_values = coco_allocate_vector(number_of_constraints);

for (i = 0; i < max_budget; ++i) {
coco_problem_get_initial_solution(PROBLEM, x);
evaluate_func(x, functions_values);

for (i = 1; 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;
/* Round the variable if integer */
if (j < number_of_integer_variables)
x[j] = floor(x[j] + 0.5);
}

/* Evaluate COCO's constraints function if problem is constrained */
if (number_of_constraints > 0 )
evaluate_cons(x, constraints_values);
Expand All @@ -278,61 +298,92 @@ void my_random_search(evaluate_function_t evaluate_func,
/**
* 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 evaluate_func The evaluation function used to evaluate the solutions.
* @param evaluate_cons The function used to evaluate the constraints.
* @param dimension The number of variables.
* @param number_of_objectives The number of objectives.
* @param number_of_constraints The number of constraints.
* @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 number_of_integer_variables The number of integer variables (if > 0, all integer variables come
* before any continuous ones).
* @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,
void my_grid_search(evaluate_function_t evaluate_func,
evaluate_function_t evaluate_cons,
const size_t dimension,
const size_t number_of_objectives,
const size_t number_of_constraints,
const double *lower_bounds,
const double *upper_bounds,
const size_t number_of_integer_variables,
const size_t max_budget) {


double *x = coco_allocate_vector(dimension);
double *y = coco_allocate_vector(number_of_objectives);
double *func_values = coco_allocate_vector(number_of_objectives);
double *cons_values = NULL;
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;
long *max_nodes = (long *) coco_allocate_memory(sizeof(long) * dimension);
long integer_nodes = 1;

/* Initialization */
for (j = 0; j < dimension; j++) {
nodes[j] = 0;
grid_step[j] = (upper_bounds[j] - lower_bounds[j]) / (double) max_nodes;
if (j < number_of_integer_variables) {
grid_step[j] = 1;
max_nodes[j] = (long) floor(upper_bounds[j] + 0.5);
assert(fabs(lower_bounds[j]) < 1e-6);
assert(max_nodes[j] > 0);
integer_nodes *= max_nodes[j];
}
else {
max_nodes[j] = (long) floor(pow((double) max_budget / (double) integer_nodes,
1 / (double) (dimension - number_of_integer_variables))) - 1;
/* Take care of the borderline case */
if (max_nodes[j] < 1)
max_nodes[j] = 1;
grid_step[j] = (upper_bounds[j] - lower_bounds[j]) / (double) max_nodes[j];
}
}

if (number_of_constraints > 0 )
cons_values = coco_allocate_vector(number_of_constraints);

while (evaluations < max_budget) {

/* Stop if there are no more nodes */
if ((number_of_integer_variables == dimension) && (evaluations >= integer_nodes))
break;

/* 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);
/* Evaluate COCO's constraints function if problem is constrained */
if (number_of_constraints > 0 )
evaluate_cons(x, cons_values);

/* Call COCO's evaluate function where all the logging is performed */
evaluate_func(x, func_values);
evaluations++;

/* Inside the grid, move to the next node */
if (nodes[0] < max_nodes) {
if (nodes[0] < max_nodes[0]) {
nodes[0]++;
}

/* At an outside node of the grid, move to the next level */
else if (max_nodes > 0) {
else if (max_nodes[0] > 0) {
for (j = 1; j < dimension; j++) {
if (nodes[j] < max_nodes) {
if (nodes[j] < max_nodes[j]) {
nodes[j]++;
for (i = 0; i < j; i++)
nodes[i] = 0;
Expand All @@ -341,15 +392,18 @@ void my_grid_search(evaluate_function_t evaluate,
}

/* At the end of the grid, exit */
if ((j == dimension) && (nodes[j - 1] == max_nodes))
if ((j == dimension) && (nodes[j - 1] == max_nodes[j - 1]))
break;
}
}

coco_free_memory(x);
coco_free_memory(y);
coco_free_memory(func_values);
if (number_of_constraints > 0 )
coco_free_memory(cons_values);
coco_free_memory(nodes);
coco_free_memory(grid_step);
coco_free_memory(max_nodes);
}

/**
Expand Down
Loading

0 comments on commit 011b70b

Please sign in to comment.