code-experiments/build/c/example_experiment.c
/**
 * A grid search optimizer that can be used for single- as well as multi-objective optimization.
 *
 * @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.
 */
A grid search optimizer that can be used for single- as well as multi-objective optimization.

@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.
A grid search optimizer that can be used for single - as well as multi - objective optimization . @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_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 *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 *) coco_allocate_memory(sizeof(long) * dimension);
  long integer_nodes = 1;
  for (j = 0; j < dimension; j++) {
    nodes[j] = 0;
    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;
      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) {
    if ((number_of_integer_variables == dimension) && (evaluations >= integer_nodes))
      break;
    for (j = 0; j < dimension; j++) {
      x[j] = lower_bounds[j] + grid_step[j] * (double) nodes[j];
    }
    if (number_of_constraints > 0 )
      evaluate_cons(x, cons_values);
    evaluate_func(x, func_values);
    evaluations++;
    if (nodes[0] < max_nodes[0]) {
      nodes[0]++;
    }
    else if (max_nodes[0] > 0) {
      for (j = 1; j < dimension; j++) {
        if (nodes[j] < max_nodes[j]) {
          nodes[j]++;
          for (i = 0; i < j; i++)
            nodes[i] = 0;
          break;
        }
      }
      if ((j == dimension) && (nodes[j - 1] == max_nodes[j - 1]))
        break;
    }
  }
  coco_free_memory(x);
  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);
}
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 * 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 * ) coco_allocate_memory ( sizeof ( long ) * dimension ) ; long integer_nodes = 1 ; for ( j = 0 ; j < dimension ; j ++ ) { nodes [ j ] = 0 ; 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 ; 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 ) { if ( ( number_of_integer_variables == dimension ) && ( evaluations >= integer_nodes ) ) break ; for ( j = 0 ; j < dimension ; j ++ ) { x [ j ] = lower_bounds [ j ] + grid_step [ j ] * ( double ) nodes [ j ] ; } if ( number_of_constraints > 0 ) evaluate_cons ( x , cons_values ) ; evaluate_func ( x , func_values ) ; evaluations ++ ; if ( nodes [ 0 ] < max_nodes [ 0 ] ) { nodes [ 0 ] ++ ; } else if ( max_nodes [ 0 ] > 0 ) { for ( j = 1 ; j < dimension ; j ++ ) { if ( nodes [ j ] < max_nodes [ j ] ) { nodes [ j ] ++ ; for ( i = 0 ; i < j ; i ++ ) nodes [ i ] = 0 ; break ; } } if ( ( j == dimension ) && ( nodes [ j - 1 ] == max_nodes [ j - 1 ] ) ) break ; } } coco_free_memory ( x ) ; 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 ) ; }
A grid search optimizer that can be used for single- as well as multi-objective optimization.
A grid search optimizer that can be used for single - as well as multi - objective optimization .
/* Initialization */ /* Take care of the borderline case */ /* Stop if there are no more nodes */ /* Construct x and evaluate it */ /* Evaluate COCO's constraints function if problem is constrained */ /* Call COCO's evaluate function where all the logging is performed */ /* Inside the grid, move to the next node */ /* At an outside node of the grid, move to the next level */ /* At the end of the grid, exit */