/** * @brief Returns the best known value for indicator_name matching the given key if the key is found, and * throws a coco_error otherwise. */ static double suite_biobj_get_best_value(const char *indicator_name, const char *key) { size_t i, count; double best_value = 0; char *curr_key; if (strcmp(indicator_name, "hyp") == 0) { curr_key = coco_allocate_string(COCO_PATH_MAX); count = sizeof(suite_biobj_best_values_hyp) / sizeof(char *); for (i = 0; i < count; i++) { sscanf(suite_biobj_best_values_hyp[i], "%s %lf", curr_key, &best_value); if (strcmp(curr_key, key) == 0) { coco_free_memory(curr_key); return best_value; } } coco_free_memory(curr_key); coco_warning("suite_biobj_get_best_value(): best value of %s could not be found; set to 1.0", key); return 1.0; } else { coco_error("suite_biobj_get_best_value(): indicator %s not supported", indicator_name); return 0; /* Never reached */ } coco_error("suite_biobj_get_best_value(): unexpected exception"); return 0; /* Never reached */ }
/** * A random search optimizer. */ void my_optimizer(coco_problem_t *problem) { const size_t budget = 2; 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; for (i = 0; i < budget; ++i) { for (j = 0; j < dimension; ++j) { range = ubounds[j] - lbounds[j]; x[j] = lbounds[j] + coco_random_uniform(rng) * range; } coco_evaluate_function(problem, x, y); } coco_random_free(rng); coco_free_memory(x); coco_free_memory(y); }
/** * @brief Frees the memory of the given indicator. */ static void logger_biobj_indicator_free(void *stuff) { logger_biobj_indicator_t *indicator; assert(stuff != NULL); indicator = (logger_biobj_indicator_t *) stuff; if (indicator->name != NULL) { coco_free_memory(indicator->name); indicator->name = NULL; } if (indicator->dat_file != NULL) { fclose(indicator->dat_file); indicator->dat_file = NULL; } if (indicator->tdat_file != NULL) { fclose(indicator->tdat_file); indicator->tdat_file = NULL; } if (indicator->info_file != NULL) { fclose(indicator->info_file); indicator->info_file = NULL; } coco_free_memory(stuff); }
void coco_problem_free(coco_problem_t *problem) { assert(problem != NULL); if (problem->problem_free_function != NULL) { problem->problem_free_function(problem); } else { /* Best guess at freeing all relevant structures */ if (problem->smallest_values_of_interest != NULL) coco_free_memory(problem->smallest_values_of_interest); if (problem->largest_values_of_interest != NULL) coco_free_memory(problem->largest_values_of_interest); if (problem->best_parameter != NULL) coco_free_memory(problem->best_parameter); if (problem->best_value != NULL) coco_free_memory(problem->best_value); if (problem->nadir_value != NULL) coco_free_memory(problem->nadir_value); if (problem->problem_name != NULL) coco_free_memory(problem->problem_name); if (problem->problem_id != NULL) coco_free_memory(problem->problem_id); if (problem->problem_type != NULL) coco_free_memory(problem->problem_type); if (problem->data != NULL) coco_free_memory(problem->data); problem->smallest_values_of_interest = NULL; problem->largest_values_of_interest = NULL; problem->best_parameter = NULL; problem->best_value = NULL; problem->nadir_value = NULL; problem->suite = NULL; problem->data = NULL; coco_free_memory(problem); } }
void coco_problem_free(coco_problem_t *self) { assert(self != NULL); if (self->free_problem != NULL) { self->free_problem(self); } else { /* Best guess at freeing all relevant structures */ if (self->smallest_values_of_interest != NULL) coco_free_memory(self->smallest_values_of_interest); if (self->largest_values_of_interest != NULL) coco_free_memory(self->largest_values_of_interest); if (self->best_parameter != NULL) coco_free_memory(self->best_parameter); if (self->best_value != NULL) coco_free_memory(self->best_value); if (self->nadir_value != NULL) coco_free_memory(self->nadir_value); if (self->problem_name != NULL) coco_free_memory(self->problem_name); if (self->problem_id != NULL) coco_free_memory(self->problem_id); if (self->problem_type != NULL) coco_free_memory(self->problem_type); if (self->data != NULL) coco_free_memory(self->data); self->smallest_values_of_interest = NULL; self->largest_values_of_interest = NULL; self->best_parameter = NULL; self->best_value = NULL; self->nadir_value = NULL; self->data = NULL; coco_free_memory(self); } }
/** * @brief Frees the data of the given logger_biobj_avl_item_t. */ static void logger_biobj_node_free(logger_biobj_avl_item_t *item, void *userdata) { coco_free_memory(item->x); coco_free_memory(item->y); coco_free_memory(item); (void) userdata; /* To silence the compiler */ }
/** * @brief Allocates a problem constructed by stacking two COCO problems. * * This is particularly useful for generating multi-objective problems, e.g. a bi-objective problem from two * single-objective problems. The stacked problem must behave like a normal COCO problem accepting the same * input. The region of interest in the decision space is defined by parameters smallest_values_of_interest * and largest_values_of_interest, which are two arrays of size equal to the dimensionality of both problems. * * @note Regions of interest in the decision space must either agree or at least one of them must be NULL. * @note Best parameter becomes somewhat meaningless, but the nadir value make sense now. */ static coco_problem_t *coco_problem_stacked_allocate(coco_problem_t *problem1, coco_problem_t *problem2, const double *smallest_values_of_interest, const double *largest_values_of_interest) { const size_t number_of_variables = coco_problem_get_dimension(problem1); const size_t number_of_objectives = coco_problem_get_number_of_objectives(problem1) + coco_problem_get_number_of_objectives(problem2); const size_t number_of_constraints = coco_problem_get_number_of_constraints(problem1) + coco_problem_get_number_of_constraints(problem2); size_t i; char *s; coco_problem_stacked_data_t *data; coco_problem_t *problem; /* the new coco problem */ assert(coco_problem_get_dimension(problem1) == coco_problem_get_dimension(problem2)); problem = coco_problem_allocate(number_of_variables, number_of_objectives, number_of_constraints); s = coco_strconcat(coco_problem_get_id(problem1), "__"); problem->problem_id = coco_strconcat(s, coco_problem_get_id(problem2)); coco_free_memory(s); s = coco_strconcat(coco_problem_get_name(problem1), " + "); problem->problem_name = coco_strconcat(s, coco_problem_get_name(problem2)); coco_free_memory(s); problem->evaluate_function = coco_problem_stacked_evaluate_function; if (number_of_constraints > 0) problem->evaluate_constraint = coco_problem_stacked_evaluate_constraint; assert(smallest_values_of_interest); assert(largest_values_of_interest); for (i = 0; i < number_of_variables; ++i) { problem->smallest_values_of_interest[i] = smallest_values_of_interest[i]; problem->largest_values_of_interest[i] = largest_values_of_interest[i]; } if (problem->best_parameter) /* logger_bbob doesn't work then anymore */ coco_free_memory(problem->best_parameter); problem->best_parameter = NULL; /* Compute the ideal and nadir values */ assert(problem->best_value); assert(problem->nadir_value); problem->best_value[0] = problem1->best_value[0]; problem->best_value[1] = problem2->best_value[0]; coco_evaluate_function(problem1, problem2->best_parameter, &problem->nadir_value[0]); coco_evaluate_function(problem2, problem1->best_parameter, &problem->nadir_value[1]); /* setup data holder */ data = (coco_problem_stacked_data_t *) coco_allocate_memory(sizeof(*data)); data->problem1 = problem1; data->problem2 = problem2; problem->data = data; problem->problem_free_function = coco_problem_stacked_free; return problem; }
static coco_problem_t *f_weierstrass_bbob_problem_allocate(const size_t function, const size_t dimension, const size_t instance, const long rseed, const char *problem_id_template, const char *problem_name_template) { double *xopt, fopt; coco_problem_t *problem = NULL; size_t i, j, k; double *M = coco_allocate_vector(dimension * dimension); double *b = coco_allocate_vector(dimension); double *current_row, **rot1, **rot2; const double condition = 100.0; const double penalty_factor = 10.0 / (double) dimension; xopt = coco_allocate_vector(dimension); fopt = bbob2009_compute_fopt(function, instance); bbob2009_compute_xopt(xopt, rseed, dimension); rot1 = bbob2009_allocate_matrix(dimension, dimension); rot2 = bbob2009_allocate_matrix(dimension, dimension); bbob2009_compute_rotation(rot1, rseed + 1000000, dimension); bbob2009_compute_rotation(rot2, rseed, dimension); for (i = 0; i < dimension; ++i) { b[i] = 0.0; current_row = M + i * dimension; for (j = 0; j < dimension; ++j) { current_row[j] = 0.0; for (k = 0; k < dimension; ++k) { const double base = 1.0 / sqrt(condition); const double exponent = 1.0 * (int) k / ((double) (long) dimension - 1.0); current_row[j] += rot1[i][k] * pow(base, exponent) * rot2[k][j]; } } } problem = f_weierstrass_allocate(dimension); problem = f_transform_obj_shift(problem, fopt); problem = f_transform_vars_affine(problem, M, b, dimension); problem = f_transform_vars_oscillate(problem); bbob2009_copy_rotation_matrix(rot1, M, b, dimension); problem = f_transform_vars_affine(problem, M, b, dimension); problem = f_transform_vars_shift(problem, xopt, 0); problem = f_transform_obj_penalize(problem, penalty_factor); bbob2009_free_matrix(rot1, dimension); bbob2009_free_matrix(rot2, dimension); coco_problem_set_id(problem, problem_id_template, function, instance, dimension); coco_problem_set_name(problem, problem_name_template, function, instance, dimension); coco_problem_set_type(problem, "4-multi-modal"); coco_free_memory(M); coco_free_memory(b); coco_free_memory(xopt); return problem; }
static void bbob2009_free_matrix(double **matrix, const size_t n) { size_t i; for (i = 0; i < n; ++i) { if (matrix[i] != NULL) { coco_free_memory(matrix[i]); matrix[i] = NULL; } } coco_free_memory(matrix); }
void coco_archive_free(coco_archive_t *archive) { assert(archive != NULL); avl_tree_destruct(archive->tree); coco_free_memory(archive->ideal); coco_free_memory(archive->nadir); coco_free_memory(archive); }
/** * Also serves as a finalize run method so. Must be called at the end * of Each run to correctly fill the index file * * TODO: make sure it is called at the end of each run or move the * writing into files to another function */ static void logger_bbob2009_free(void *stuff) { /* TODO: do all the "non simply freeing" stuff in another function * that can have problem as input */ logger_bbob2009_t *data = stuff; if (bbob2009_logger_verbosity > 2 && data && data->number_of_evaluations > 0) { printf("best f=%e after %ld fevals (done observing)\n", data->best_fvalue, data->number_of_evaluations); } if (data->alg_name != NULL) { coco_free_memory((void*) data->alg_name); data->alg_name = NULL; } if (data->path != NULL) { coco_free_memory(data->path); data->path = NULL; } if (data->index_file != NULL) { fprintf(data->index_file, ":%ld|%.1e", data->number_of_evaluations, data->best_fvalue - data->optimal_fvalue); fclose(data->index_file); data->index_file = NULL; } if (data->fdata_file != NULL) { fclose(data->fdata_file); data->fdata_file = NULL; } if (data->tdata_file != NULL) { /* TODO: make sure it handles restarts well. i.e., it writes * at the end of a single run, not all the runs on a given * instance. Maybe start with forcing it to generate a new * "instance" of problem for each restart in the beginning */ if (!data->written_last_eval) { logger_bbob2009_write_data(data->tdata_file, data->number_of_evaluations, data->last_fvalue, data->best_fvalue, data->optimal_fvalue, data->best_solution, data->number_of_variables); } fclose(data->tdata_file); data->tdata_file = NULL; } if (data->rdata_file != NULL) { fclose(data->rdata_file); data->rdata_file = NULL; } if (data->best_solution != NULL) { coco_free_memory(data->best_solution); data->best_solution = NULL; } bbob2009_logger_is_open = 0; }
/** * The archive always contains the two extreme solutions */ coco_archive_t *coco_archive(const char *suite_name, const size_t function, const size_t dimension, const size_t instance) { coco_archive_t *archive = coco_archive_allocate(); int output_precision = 15; coco_suite_t *suite; char *suite_instance = coco_strdupf("instances: %lu", (unsigned long) instance); char *suite_options = coco_strdupf("dimensions: %lu function_indices: %lu", (unsigned long) dimension, (unsigned long) function); coco_problem_t *problem; char *text; int update; suite = coco_suite(suite_name, suite_instance, suite_options); if (suite == NULL) { coco_error("coco_archive(): cannot create suite '%s'", suite_name); return NULL; /* Never reached */ } problem = coco_suite_get_next_problem(suite, NULL); if (problem == NULL) { coco_error("coco_archive(): cannot create problem f%02lu_i%02lu_d%02lu in suite '%s'", (unsigned long) function, (unsigned long) instance, (unsigned long) dimension, suite_name); return NULL; /* Never reached */ } /* Store the ideal and nadir points */ archive->ideal = coco_duplicate_vector(problem->best_value, 2); archive->nadir = coco_duplicate_vector(problem->nadir_value, 2); /* Add the extreme points to the archive */ text = coco_strdupf("0\t%.*e\t%.*e\n", output_precision, archive->nadir[0], output_precision, archive->ideal[1]); update = coco_archive_add_solution(archive, archive->nadir[0], archive->ideal[1], text); coco_free_memory(text); assert(update == 1); text = coco_strdupf("0\t%.*e\t%.*e\n", output_precision, archive->ideal[0], output_precision, archive->nadir[1]); update = coco_archive_add_solution(archive, archive->ideal[0], archive->nadir[1], text); coco_free_memory(text); assert(update == 1); archive->extreme1 = archive->tree->head; archive->extreme2 = archive->tree->tail; assert(archive->extreme1 != archive->extreme2); coco_free_memory(suite_instance); coco_free_memory(suite_options); coco_suite_free(suite); return archive; }
/** * @brief Frees the Lunacek bi-Rastrigin data object. */ static void f_lunacek_bi_rastrigin_free(coco_problem_t *problem) { f_lunacek_bi_rastrigin_data_t *data; data = (f_lunacek_bi_rastrigin_data_t *) problem->data; coco_free_memory(data->x_hat); coco_free_memory(data->z); coco_free_memory(data->xopt); bbob2009_free_matrix(data->rot1, problem->number_of_variables); bbob2009_free_matrix(data->rot2, problem->number_of_variables); /* Let the generic free problem code deal with all of the * coco_problem_t fields. */ problem->problem_free_function = NULL; coco_problem_free(problem); }
static coco_problem_t *f_weierstrass_allocate(const size_t number_of_variables) { f_weierstrass_data_t *data; size_t i; double *non_unique_best_value; coco_problem_t *problem = coco_problem_allocate_from_scalars("Weierstrass function", f_weierstrass_evaluate, NULL, number_of_variables, -5.0, 5.0, NAN); coco_problem_set_id(problem, "%s_d%02lu", "weierstrass", number_of_variables); data = coco_allocate_memory(sizeof(*data)); data->f0 = 0.0; for (i = 0; i < WEIERSTRASS_SUMMANDS; ++i) { data->ak[i] = pow(0.5, (double) i); data->bk[i] = pow(3., (double) i); data->f0 += data->ak[i] * cos(2 * coco_pi * data->bk[i] * 0.5); } problem->data = data; /* Compute best solution */ non_unique_best_value = coco_allocate_vector(number_of_variables); for (i = 0; i < number_of_variables; i++) non_unique_best_value[i] = 0.0; f_weierstrass_evaluate(problem, non_unique_best_value, problem->best_value); coco_free_memory(non_unique_best_value); return problem; }
/** * @brief Creates the BBOB Rastrigin problem. */ static coco_problem_t *f_rastrigin_bbob_problem_allocate(const size_t function, const size_t dimension, const size_t instance, const long rseed, const char *problem_id_template, const char *problem_name_template) { double *xopt, fopt; coco_problem_t *problem = NULL; xopt = coco_allocate_vector(dimension); fopt = bbob2009_compute_fopt(function, instance); bbob2009_compute_xopt(xopt, rseed, dimension); problem = f_rastrigin_allocate(dimension); problem = transform_vars_conditioning(problem, 10.0); problem = transform_vars_asymmetric(problem, 0.2); problem = transform_vars_oscillate(problem); problem = transform_vars_shift(problem, xopt, 0); problem = transform_obj_shift(problem, fopt); coco_problem_set_id(problem, problem_id_template, function, instance, dimension); coco_problem_set_name(problem, problem_name_template, function, instance, dimension); coco_problem_set_type(problem, "1-separable"); coco_free_memory(xopt); return problem; }
static void transformed_free_problem(coco_problem_t *self) { coco_transformed_data_t *data; assert(self != NULL); assert(self->data != NULL); data = self->data; assert(data->inner_problem != NULL); if (data->inner_problem != NULL) { coco_problem_free(data->inner_problem); data->inner_problem = NULL; } if (data->data != NULL) { if (data->free_data != NULL) { data->free_data(data->data); data->free_data = NULL; } coco_free_memory(data->data); data->data = NULL; } /* Let the generic free problem code deal with the rest of the * fields. For this we clear the free_problem function pointer and * recall the generic function. */ self->free_problem = NULL; coco_problem_free(self); }
static void coco_stacked_problem_free(coco_problem_t *self) { coco_stacked_problem_data_t *data; assert(self != NULL); assert(self->data != NULL); data = self->data; if (data->problem1 != NULL) { coco_problem_free(data->problem1); data->problem1 = NULL; } if (data->problem2 != NULL) { coco_problem_free(data->problem2); data->problem2 = NULL; } if (data->data != NULL) { if (data->free_data != NULL) { data->free_data(data->data); data->free_data = NULL; } coco_free_memory(data->data); data->data = NULL; } /* Let the generic free problem code deal with the rest of the * fields. For this we clear the free_problem function pointer and * recall the generic function. */ self->free_problem = NULL; coco_problem_free(self); }
/** * @brief Allocates a transformed problem that wraps the inner_problem. * * By default all methods will dispatch to the inner_problem. A prefix is prepended to the problem name * in order to reflect the transformation somewhere. */ static coco_problem_t *coco_problem_transformed_allocate(coco_problem_t *inner_problem, void *user_data, coco_data_free_function_t data_free_function, const char *name_prefix) { coco_problem_transformed_data_t *problem; coco_problem_t *inner_copy; char *old_name = coco_strdup(inner_problem->problem_name); problem = (coco_problem_transformed_data_t *) coco_allocate_memory(sizeof(*problem)); problem->inner_problem = inner_problem; problem->data = user_data; problem->data_free_function = data_free_function; inner_copy = coco_problem_duplicate(inner_problem); inner_copy->evaluate_function = coco_problem_transformed_evaluate_function; inner_copy->evaluate_constraint = coco_problem_transformed_evaluate_constraint; inner_copy->recommend_solution = coco_problem_transformed_recommend_solution; inner_copy->problem_free_function = coco_problem_transformed_free; inner_copy->data = problem; coco_problem_set_name(inner_copy, "%s(%s)", name_prefix, old_name); coco_free_memory(old_name); return inner_copy; }
static void f_gallagher_free(coco_problem_t *self) { f_gallagher_data_t *data; data = self->data; coco_free_memory(data->xopt); coco_free_memory(data->peak_values); bbob2009_free_matrix(data->rotation, self->number_of_variables); bbob2009_free_matrix(data->x_local, self->number_of_variables); bbob2009_free_matrix(data->arr_scales, data->number_of_peaks); self->free_problem = NULL; coco_problem_free(self); if (gallagher_peaks != NULL) { coco_free_memory(gallagher_peaks); gallagher_peaks = NULL; } }
/* * Class: CocoJNI * Method: cocoEvaluateConstraint * Signature: (J[D)[D */ JNIEXPORT jdoubleArray JNICALL Java_org_moeaframework_problem_BBOB2016_CocoJNI_cocoEvaluateConstraint (JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer, jdoubleArray jx) { coco_problem_t *problem = NULL; double *y = NULL; double *x = NULL; int number_of_objectives; jdoubleArray jy; /* This test is both to prevent warning because interface_cls was not used and to check for exceptions */ if (interface_cls == NULL) { jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception"); (*jenv)->ThrowNew(jenv, Exception, "Exception in cocoEvaluateConstraint\n"); } problem = (coco_problem_t *) jproblem_pointer; number_of_objectives = (int) coco_problem_get_number_of_objectives(problem); /* Call coco_evaluate_constraint */ x = (*jenv)->GetDoubleArrayElements(jenv, jx, NULL); y = coco_allocate_vector(number_of_objectives); coco_evaluate_constraint(problem, x, y); /* Prepare the return value */ jy = (*jenv)->NewDoubleArray(jenv, number_of_objectives); (*jenv)->SetDoubleArrayRegion(jenv, jy, 0, number_of_objectives, y); /* Free resources */ coco_free_memory(y); (*jenv)->ReleaseDoubleArrayElements(jenv, jx, x, JNI_ABORT); return jy; }
static int coco_problem_id_is_fine(const char *id, ...) { va_list args; const int reject = 0; const int OK = 1; const char *cp; char *s; int result = OK; va_start(args, id); s = coco_vstrdupf(id, args); va_end(args); for (cp = s; *cp != '\0'; ++cp) { if (('A' <= *cp) && (*cp <= 'Z')) continue; if (('a' <= *cp) && (*cp <= 'z')) continue; if ((*cp == '_') || (*cp == '-')) continue; if (('0' <= *cp) && (*cp <= '9')) continue; result = reject; } coco_free_memory(s); return result; }
/** * @brief Creates the BBOB Griewank-Rosenbrock problem. */ static coco_problem_t *f_griewank_rosenbrock_bbob_problem_allocate(const size_t function, const size_t dimension, const size_t instance, const long rseed, const char *problem_id_template, const char *problem_name_template) { double fopt; coco_problem_t *problem = NULL; size_t i, j; double *M = coco_allocate_vector(dimension * dimension); double *b = coco_allocate_vector(dimension); double *shift = coco_allocate_vector(dimension); double scales, **rot1; fopt = bbob2009_compute_fopt(function, instance); for (i = 0; i < dimension; ++i) { shift[i] = -0.5; } rot1 = bbob2009_allocate_matrix(dimension, dimension); bbob2009_compute_rotation(rot1, rseed, dimension); scales = coco_double_max(1., sqrt((double) dimension) / 8.); for (i = 0; i < dimension; ++i) { for (j = 0; j < dimension; ++j) { rot1[i][j] *= scales; } } problem = f_griewank_rosenbrock_allocate(dimension); problem = transform_obj_shift(problem, fopt); problem = transform_vars_shift(problem, shift, 0); bbob2009_copy_rotation_matrix(rot1, M, b, dimension); problem = transform_vars_affine(problem, M, b, dimension); bbob2009_free_matrix(rot1, dimension); coco_problem_set_id(problem, problem_id_template, function, instance, dimension); coco_problem_set_name(problem, problem_name_template, function, instance, dimension); coco_problem_set_type(problem, "4-multi-modal"); coco_free_memory(M); coco_free_memory(b); coco_free_memory(shift); return problem; }
/** * Formatted printing of a problem name, mimicking * sprintf(coco_problem_get_name(problem), name, ...) while taking care * of memory (de-)allocation, tentative, needs at the minimum some (more) testing. * */ void coco_problem_set_name(coco_problem_t *problem, const char *name, ...) { va_list args; va_start(args, name); coco_free_memory(problem->problem_name); problem->problem_name = coco_vstrdupf(name, args); va_end(args); }
/** * @brief Implements the Lunacek bi-Rastrigin function without connections to any COCO structures. */ static double f_lunacek_bi_rastrigin_raw(const double *x, const size_t number_of_variables, f_lunacek_bi_rastrigin_data_t *data) { double result; static const double condition = 100.; size_t i, j; double penalty = 0.0; static const double mu0 = 2.5; static const double d = 1.; const double s = 1. - 0.5 / (sqrt((double) (number_of_variables + 20)) - 4.1); const double mu1 = -sqrt((mu0 * mu0 - d) / s); double *tmpvect, sum1 = 0., sum2 = 0., sum3 = 0.; assert(number_of_variables > 1); for (i = 0; i < number_of_variables; ++i) { double tmp; tmp = fabs(x[i]) - 5.0; if (tmp > 0.0) penalty += tmp * tmp; } /* x_hat */ for (i = 0; i < number_of_variables; ++i) { data->x_hat[i] = 2. * x[i]; if (data->xopt[i] < 0.) { data->x_hat[i] *= -1.; } } tmpvect = coco_allocate_vector(number_of_variables); /* affine transformation */ for (i = 0; i < number_of_variables; ++i) { double c1; tmpvect[i] = 0.0; c1 = pow(sqrt(condition), ((double) i) / (double) (number_of_variables - 1)); for (j = 0; j < number_of_variables; ++j) { tmpvect[i] += c1 * data->rot2[i][j] * (data->x_hat[j] - mu0); } } for (i = 0; i < number_of_variables; ++i) { data->z[i] = 0; for (j = 0; j < number_of_variables; ++j) { data->z[i] += data->rot1[i][j] * tmpvect[j]; } } /* Computation core */ for (i = 0; i < number_of_variables; ++i) { sum1 += (data->x_hat[i] - mu0) * (data->x_hat[i] - mu0); sum2 += (data->x_hat[i] - mu1) * (data->x_hat[i] - mu1); sum3 += cos(2 * coco_pi * data->z[i]); } result = coco_double_min(sum1, d * (double) number_of_variables + s * sum2) + 10. * ((double) number_of_variables - sum3) + 1e4 * penalty; coco_free_memory(tmpvect); return result; }
/** * @brief Sets the problem_type using formatted printing (as in printf). * * Takes care of memory (de-)allocation. */ static void coco_problem_set_type(coco_problem_t *problem, const char *type, ...) { va_list args; va_start(args, type); if (problem->problem_type != NULL) coco_free_memory(problem->problem_type); problem->problem_type = coco_vstrdupf(type, args); va_end(args); }
static void lnd_evaluate_function(coco_problem_t *self, const double *x, double *y) { _log_nondominating_t *data; data = coco_get_transform_data(self); coco_evaluate_function(coco_get_transform_inner_problem(self), x, y); data->number_of_evaluations++; /* Open logfile if it is not alread open */ if (data->logfile == NULL) { data->logfile = fopen(data->path, "w"); if (data->logfile == NULL) { char *buf; const char *error_format = "lnd_evaluate_function() failed to open log file '%s'."; size_t buffer_size = snprintf(NULL, 0, error_format, data->path); buf = (char *)coco_allocate_memory(buffer_size); snprintf(buf, buffer_size, error_format, data->path); coco_error(buf); coco_free_memory(buf); /* Never reached */ } fprintf(data->logfile, "# %zu variables | %zu objectives | func eval number\n", coco_get_number_of_variables(coco_get_transform_inner_problem(self)), coco_get_number_of_objectives(coco_get_transform_inner_problem(self))); /*********************************************************************/ /* TODO: Temporary put it here, to check later */ /* Allocate memory for the archive */ mo_archive = (struct mococo_solutions_archive *) malloc(1 * sizeof(struct mococo_solutions_archive)); mococo_allocate_archive(mo_archive, data->max_size_of_archive, coco_get_number_of_variables(coco_get_transform_inner_problem(self)), coco_get_number_of_objectives(coco_get_transform_inner_problem(self)), 1); /*********************************************************************/ } /********************************************************************************/ /* Finish evaluations of 1 single solution of the pop, with nObj objectives, * now update the archive with this newly evaluated solution and check its nondomination. */ mococo_push_to_archive(&x, &y, mo_archive, 1, data->number_of_evaluations); mococo_pareto_filtering(mo_archive); /***** TODO: IMPROVE THIS ROUTINE *****/ mococo_mark_updates(mo_archive, data->number_of_evaluations); /* Write out a line for this newly evaluated solution if it is nondominated */ // write main info to the log file for pfront for (size_t i=0; i < mo_archive->updatesize; i++) { entry = mo_archive->update[i]; for (size_t j=0; j < coco_get_number_of_variables(coco_get_transform_inner_problem(self)); j++) // all decision variables of a solution fprintf(data->logfile, "%13.10e\t", entry->var[j]); for (size_t k=0; k < coco_get_number_of_objectives(coco_get_transform_inner_problem(self)); k++) // all objective values of a solution fprintf(data->logfile, "%13.10e\t", entry->obj[k]); fprintf(data->logfile, "%zu", entry->birth); // its timestamp (FEval) fprintf(data->logfile, "\n"); // go to the next line for another solution } /********************************************************************************/ /* Flush output so that impatient users can see progress. */ fflush(data->logfile); }
/** * Error when trying to create the file "path" */ static void logger_bbob2009_error_io(FILE *path, int errnum) { char *buf; const char *error_format = "Error opening file: %s\n "; /* "bbob2009_logger_prepare() failed to open log file '%s'.";*/ size_t buffer_size = (size_t) snprintf(NULL, 0, error_format, path); /* to silence warning */ buf = (char *) coco_allocate_memory(buffer_size); snprintf(buf, buffer_size, error_format, strerror(errnum), path); coco_error(buf); coco_free_memory(buf); }
/** * @brief Frees the memory of the given bi-objective suite. */ static void suite_biobj_free(void *stuff) { suite_biobj_t *data; size_t i; assert(stuff != NULL); data = (suite_biobj_t *) stuff; if (data->new_instances) { for (i = 0; i < data->max_new_instances; i++) { if (data->new_instances[i]) { coco_free_memory(data->new_instances[i]); data->new_instances[i] = NULL; } } } coco_free_memory(data->new_instances); data->new_instances = NULL; }
/** * @brief Updates the archive fields returned by the getters. */ static void coco_archive_update(coco_archive_t *archive) { if (!archive->is_up_to_date) { avl_node_t *node, *left_node; coco_archive_avl_item_t *node_item, *left_node_item; double *node_objectives, *left_node_objectives; /* Updates number_of_solutions */ archive->number_of_solutions = avl_count(archive->tree); /* Updates hypervolume */ node = archive->tree->head; archive->hypervolume = 0; /* Hypervolume of the extreme point equals 0 */ while (node->next) { /* Add hypervolume contributions of the other points that are within ROI */ left_node = node->next; node_item = (coco_archive_avl_item_t *) node->item; left_node_item = (coco_archive_avl_item_t *) left_node->item; node_objectives = coco_archive_node_item_get_vector(node_item); left_node_objectives = coco_archive_node_item_get_vector(left_node_item); if (mo_solution_is_within_ROI(left_node_objectives, archive->ideal, archive->nadir, archive->number_of_objectives)) { if (mo_solution_is_within_ROI(node_objectives, archive->ideal, archive->nadir, archive->number_of_objectives)) archive->hypervolume += (node_item->f1 - left_node_item->f1) * (archive->nadir[1] - left_node_item->f2); else archive->hypervolume += (archive->nadir[0] - left_node_item->f1) * (archive->nadir[1] - left_node_item->f2); } coco_free_memory(node_objectives); coco_free_memory(left_node_objectives); node = left_node; } /* Performs normalization */ archive->hypervolume /= ((archive->nadir[0] - archive->ideal[0]) * (archive->nadir[1] - archive->ideal[1])); archive->is_up_to_date = 1; archive->current_solution = NULL; archive->extremes_already_returned = 0; } }
/** * Formatted printing of a problem ID, mimicking * sprintf(coco_problem_get_id(problem), id, ...) while taking care * of memory (de-)allocations. * */ void coco_problem_set_id(coco_problem_t *problem, const char *id, ...) { va_list args; va_start(args, id); coco_free_memory(problem->problem_id); problem->problem_id = coco_vstrdupf(id, args); va_end(args); if (!coco_problem_id_is_fine(problem->problem_id)) { coco_error("Problem id should only contain standard chars, not like '%s'", coco_problem_get_id(problem)); } }