Skip to content

Commit

Permalink
Passing structures as void *
Browse files Browse the repository at this point in the history
  • Loading branch information
FMGS666 committed Oct 18, 2023
1 parent 7d9f48f commit a215a0a
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 92 deletions.
10 changes: 1 addition & 9 deletions code-experiments/src/coco.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,6 @@ typedef struct{
size_t number_of_peaks;
double penalty_scale;
}f_gallagher_args_t;

typedef struct {
f_step_ellipsoid_args_t *f_step_ellipsoid_args;
f_ellipsoid_args_t *f_ellipsoid_args;
f_schaffers_args_t *f_schaffers_args;
f_griewank_rosenbrock_args_t *f_griewank_rosenbrock_args;
f_gallagher_args_t *f_gallagher_args;
} f_args_t;
/**@}*/

/***********************************************************************************************************/
Expand Down Expand Up @@ -259,7 +251,7 @@ typedef coco_problem_t *(*coco_problem_bbob_allocator_args_t)(
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template);

Expand Down
17 changes: 1 addition & 16 deletions code-experiments/src/coco_problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,7 @@ void coco_problem_free(coco_problem_t *problem) {
problem->initial_solution = NULL;
coco_free_memory(problem);
}
}

/**
* @brief Allocates function arguments structure
*/
static f_args_t *coco_problem_allocate_f_args(
f_args_t * args
){
args = (f_args_t *) coco_allocate_memory(sizeof(*args));
args->f_ellipsoid_args = (f_ellipsoid_args_t *) coco_allocate_memory(sizeof(*(args->f_ellipsoid_args)));
args->f_step_ellipsoid_args = (f_step_ellipsoid_args_t *) coco_allocate_memory(sizeof(*(args->f_step_ellipsoid_args)));
args->f_schaffers_args = (f_schaffers_args_t *) coco_allocate_memory(sizeof(*(args->f_schaffers_args)));
args->f_griewank_rosenbrock_args = (f_griewank_rosenbrock_args_t *) coco_allocate_memory(sizeof(*(args->f_griewank_rosenbrock_args)));
args->f_gallagher_args = (f_gallagher_args_t *) coco_allocate_memory(sizeof(*(args->f_gallagher_args)));
return args;
}
}

/***********************************************************************************************************/

Expand Down
6 changes: 4 additions & 2 deletions code-experiments/src/f_ellipsoid.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ static coco_problem_t *f_ellipsoid_rotated_bbob_problem_allocate(const size_t fu
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {
double *xopt, fopt;
coco_problem_t *problem = NULL;

f_ellipsoid_args_t *f_ellipsoid_args;
f_ellipsoid_args = ((f_ellipsoid_args_t *) args);
double *M = coco_allocate_vector(dimension * dimension);
double *b = coco_allocate_vector(dimension);
double **rot1;

double conditioning = args -> f_ellipsoid_args -> conditioning;
double conditioning = f_ellipsoid_args -> conditioning;
xopt = coco_allocate_vector(dimension);
bbob2009_compute_xopt(xopt, rseed, dimension);
fopt = bbob2009_compute_fopt(function, instance);
Expand Down
8 changes: 5 additions & 3 deletions code-experiments/src/f_gallagher.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static coco_problem_t *f_gallagher_bbob_problem_allocate(const size_t function,
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {

Expand All @@ -181,9 +181,11 @@ static coco_problem_t *f_gallagher_bbob_problem_allocate(const size_t function,
f_gallagher_permutation_t *rperm;
double *random_numbers;

f_gallagher_args_t *f_gallagher_args;
f_gallagher_args = ((f_gallagher_args_t *) args);

size_t number_of_peaks = args->f_gallagher_args->number_of_peaks;
double penalty_scale = args->f_gallagher_args->penalty_scale;
size_t number_of_peaks = f_gallagher_args->number_of_peaks;
double penalty_scale = f_gallagher_args->penalty_scale;
data = (f_gallagher_data_t *) coco_allocate_memory(sizeof(*data));

/* Allocate temporary storage and space for the rotation matrices */
Expand Down
14 changes: 10 additions & 4 deletions code-experiments/src/f_griewank_rosenbrock.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static coco_problem_t *f_griewank_rosenbrock_bbob_problem_allocate(const size_t
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {
double fopt;
Expand All @@ -112,7 +112,10 @@ static coco_problem_t *f_griewank_rosenbrock_bbob_problem_allocate(const size_t
}
}

problem = f_griewank_rosenbrock_allocate(dimension, args->f_griewank_rosenbrock_args->facftrue);

f_griewank_rosenbrock_args_t *f_griewank_rosenbrock_args;
f_griewank_rosenbrock_args = ((f_griewank_rosenbrock_args_t *) args);
problem = f_griewank_rosenbrock_allocate(dimension, f_griewank_rosenbrock_args->facftrue);
problem = transform_obj_shift(problem, fopt);
problem = transform_vars_shift(problem, shift, 0);
bbob2009_copy_rotation_matrix(rot1, M, b, dimension);
Expand Down Expand Up @@ -147,7 +150,7 @@ static coco_problem_t *f_griewank_rosenbrock_permblockdiag_bbob_bbob_problem_all
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {
double fopt;
Expand Down Expand Up @@ -187,7 +190,10 @@ static coco_problem_t *f_griewank_rosenbrock_permblockdiag_bbob_bbob_problem_all
coco_compute_truncated_uniform_swap_permutation(P1, rseed + 2000000, dimension, nb_swaps, swap_range);
coco_compute_truncated_uniform_swap_permutation(P2, rseed + 3000000, dimension, nb_swaps, swap_range);

problem = f_griewank_rosenbrock_allocate(dimension, args->f_griewank_rosenbrock_args->facftrue);
f_griewank_rosenbrock_args_t *f_griewank_rosenbrock_args;
f_griewank_rosenbrock_args = ((f_griewank_rosenbrock_args_t *) args);

problem = f_griewank_rosenbrock_allocate(dimension, f_griewank_rosenbrock_args->facftrue);
problem = transform_vars_shift(problem, shift, 0);
problem = transform_vars_scale(problem, scales);
problem = transform_vars_permutation(problem, P2, dimension);
Expand Down
9 changes: 6 additions & 3 deletions code-experiments/src/f_schaffers.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static coco_problem_t *f_schaffers_bbob_problem_allocate(const size_t function,
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {
double *xopt, fopt;
Expand All @@ -95,10 +95,13 @@ static coco_problem_t *f_schaffers_bbob_problem_allocate(const size_t function,
double *b = coco_allocate_vector(dimension);
double *current_row, **rot1, **rot2;

f_schaffers_args_t *f_schaffers_args;
f_schaffers_args = ((f_schaffers_args_t *) args);
f_schaffers_data_t *data;
data = (f_schaffers_data_t *) coco_allocate_memory(sizeof(*data));
data->conditioning = args->f_schaffers_args->conditioning;
data->penalty_scale = args->f_schaffers_args->penalty_scale;

data->conditioning = f_schaffers_args->conditioning;
data->penalty_scale = f_schaffers_args->penalty_scale;

xopt = coco_allocate_vector(dimension);
fopt = bbob2009_compute_fopt(function, instance);
Expand Down
8 changes: 6 additions & 2 deletions code-experiments/src/f_step_ellipsoid.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static coco_problem_t *f_step_ellipsoid_bbob_problem_allocate(const size_t funct
const size_t dimension,
const size_t instance,
const long rseed,
const f_args_t *args,
const void *args,
const char *problem_id_template,
const char *problem_name_template) {

Expand All @@ -144,7 +144,11 @@ static coco_problem_t *f_step_ellipsoid_bbob_problem_allocate(const size_t funct
data->xopt = coco_allocate_vector(dimension);
data->rot1 = bbob2009_allocate_matrix(dimension, dimension);
data->rot2 = bbob2009_allocate_matrix(dimension, dimension);
double penalty_scale = args->f_step_ellipsoid_args->penalty_scale;

f_step_ellipsoid_args_t *f_step_ellipsoid_args;
f_step_ellipsoid_args = ((f_step_ellipsoid_args_t *) args);

double penalty_scale = f_step_ellipsoid_args->penalty_scale;
data->fopt = bbob2009_compute_fopt(function, instance);
data->penalty_scale = penalty_scale;
bbob2009_compute_xopt(data->xopt, rseed, dimension);
Expand Down
45 changes: 25 additions & 20 deletions code-experiments/src/suite_bbob.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ static coco_problem_t *coco_get_bbob_problem(const size_t function,
const size_t dimension,
const size_t instance) {
coco_problem_t *problem = NULL;
f_args_t *args = NULL;
args = coco_problem_allocate_f_args(args);

args->f_ellipsoid_args->conditioning = 1.0e6;
args->f_step_ellipsoid_args->penalty_scale = 1.0;
args->f_griewank_rosenbrock_args ->facftrue = 10.0;
args->f_gallagher_args->penalty_scale = 1.0;

const char *problem_id_template = "bbob_f%03lu_i%02lu_d%02lu";
const char *problem_name_template = "BBOB suite problem f%lu instance %lu in %luD";
Expand Down Expand Up @@ -132,7 +125,9 @@ static coco_problem_t *coco_get_bbob_problem(const size_t function,
problem = f_attractive_sector_bbob_problem_allocate(function, dimension, instance, rseed,
problem_id_template, problem_name_template);
} else if (function == 7) {
problem = f_step_ellipsoid_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_step_ellipsoid_args_t args;
args.penalty_scale = 1.0;
problem = f_step_ellipsoid_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 8) {
problem = f_rosenbrock_bbob_problem_allocate(function, dimension, instance, rseed,
Expand All @@ -141,7 +136,9 @@ static coco_problem_t *coco_get_bbob_problem(const size_t function,
problem = f_rosenbrock_rotated_bbob_problem_allocate(function, dimension, instance, rseed,
problem_id_template, problem_name_template);
} else if (function == 10) {
problem = f_ellipsoid_rotated_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_ellipsoid_args_t args;
args.conditioning = 1.0e6;
problem = f_ellipsoid_rotated_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 11) {
problem = f_discus_bbob_problem_allocate(function, dimension, instance, rseed,
Expand All @@ -162,28 +159,36 @@ static coco_problem_t *coco_get_bbob_problem(const size_t function,
problem = f_weierstrass_bbob_problem_allocate(function, dimension, instance, rseed,
problem_id_template, problem_name_template);
} else if (function == 17) {
args->f_schaffers_args->conditioning = 10.0;
args->f_schaffers_args->penalty_scale = 10.0;
problem = f_schaffers_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_schaffers_args_t args;
args.conditioning = 10.0;
args.penalty_scale = 10.0;
problem = f_schaffers_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 18) {
args->f_schaffers_args->conditioning = 1000.0;
args->f_schaffers_args->penalty_scale = 10.0;
problem = f_schaffers_bbob_problem_allocate(function, dimension, instance, rseed_17, args,
f_schaffers_args_t args;
args.conditioning = 1000.0;
args.penalty_scale = 10.0;
problem = f_schaffers_bbob_problem_allocate(function, dimension, instance, rseed_17, &args,
problem_id_template, problem_name_template);
} else if (function == 19) {
problem = f_griewank_rosenbrock_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_griewank_rosenbrock_args_t args;
args.facftrue = 10.0;
problem = f_griewank_rosenbrock_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 20) {
problem = f_schwefel_bbob_problem_allocate(function, dimension, instance, rseed,
problem_id_template, problem_name_template);
} else if (function == 21) {
args->f_gallagher_args->number_of_peaks = (size_t) 101;
problem = f_gallagher_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_gallagher_args_t args;
args.number_of_peaks = (size_t) 101;
args.penalty_scale = 1.0;
problem = f_gallagher_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 22) {
args->f_gallagher_args->number_of_peaks = (size_t) 21;
problem = f_gallagher_bbob_problem_allocate(function, dimension, instance, rseed, args,
f_gallagher_args_t args;
args.number_of_peaks = (size_t) 21;
args.penalty_scale = 1.0;
problem = f_gallagher_bbob_problem_allocate(function, dimension, instance, rseed, &args,
problem_id_template, problem_name_template);
} else if (function == 23) {
problem = f_katsuura_bbob_problem_allocate(function, dimension, instance, rseed,
Expand Down

0 comments on commit a215a0a

Please sign in to comment.