<commit_before>#include "numbbo.h"

#include "numbbo_utilities.c"

static void _default_initial_solution(const numbbo_problem_t *problem, 
                                      double *initial_solution);
/**
 * numbbo_allocate_problem(number_of_variables):
 *
 * Allocate and pre-populate a new numbbo_problem_t for a problem with
 * ${number_of_variables}.
 */
static numbbo_problem_t *numbbo_allocate_problem(const size_t number_of_variables,
                                                 const size_t number_of_objectives,
                                                 const size_t number_of_constraints) {
    numbbo_problem_t *problem;
    problem = (numbbo_problem_t *)numbbo_allocate_memory(sizeof(numbbo_problem_t));
    /* Initialize fields to sane/safe defaults */
    problem->initial_solution = _default_initial_solution;
    problem->evaluate_function = NULL;
    problem->evaluate_constraint = NULL;
    problem->recommend_solutions = NULL;
    problem->free_problem = NULL;
    problem->number_of_variables = number_of_variables;
    problem->number_of_objectives = number_of_objectives;
    problem->number_of_constraints = number_of_constraints;
    problem->lower_bounds = numbbo_allocate_vector(number_of_variables);
    problem->upper_bounds = numbbo_allocate_vector(number_of_variables);
    problem->best_parameter = numbbo_allocate_vector(number_of_variables);
    problem->best_value = numbbo_allocate_vector(number_of_objectives);
    problem->problem_name = NULL;
    problem->problem_id = NULL;
    return problem;
}

/**
 * Generic wrapper for a transformed numbbo_problem_t. 
 */
typedef struct numbbo_transformed_problem {
    /* must be defined first to make inheritance / overload happen */
    numbbo_problem_t problem;
    numbbo_problem_t *inner_problem;
    void *state;
} numbbo_transformed_problem_t;

static void _tfp_evaluate_function(numbbo_problem_t *self, double *x, double *y) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_evaluate_function(problem->inner_problem, x, y);
}

static void _tfp_evaluate_constraint(numbbo_problem_t *self, double *x, double *y) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_evaluate_constraint(problem->inner_problem, x, y);
}

static void _tfp_recommend_solutions(numbbo_problem_t *self,
                                     double *x, size_t number_of_solutions) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_recommend_solutions(problem->inner_problem, x, number_of_solutions);
}

static void _tfp_free_problem(numbbo_problem_t *self) {
    numbbo_transformed_problem_t *obj = (numbbo_transformed_problem_t *)self;
    numbbo_problem_t *problem = (numbbo_problem_t *)obj;
    if (obj->inner_problem != NULL)
        numbbo_free_problem(obj->inner_problem);
    obj->inner_problem = NULL;
    if (obj->state != NULL)
        numbbo_free_memory(obj->state);
    /* 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.
     */
    problem->free_problem = NULL;
    numbbo_free_problem(problem);
}

/**
 * numbbo_allocate_transformed_problem(inner_problem):
 *
 * Allocate a transformed problem that wraps ${inner_problem}. By
 * default all methods will dispatch to the ${inner_problem} method.
 *
 */
static numbbo_transformed_problem_t *
numbbo_allocate_transformed_problem(numbbo_problem_t *inner_problem) {
    numbbo_transformed_problem_t *obj =
        (numbbo_transformed_problem_t *)numbbo_allocate_memory(sizeof(numbbo_transformed_problem_t));
    numbbo_problem_t *problem = (numbbo_problem_t *)obj;

    problem->evaluate_function = _tfp_evaluate_function;
    problem->evaluate_constraint = _tfp_evaluate_constraint;
    problem->recommend_solutions = _tfp_recommend_solutions;
    problem->free_problem = _tfp_free_problem;
    problem->number_of_variables = inner_problem->number_of_variables;
    problem->number_of_objectives = inner_problem->number_of_objectives;
    problem->number_of_constraints = inner_problem->number_of_constraints;
    problem->lower_bounds = 
        numbbo_duplicate_vector(inner_problem->lower_bounds,
                                inner_problem->number_of_variables);
    problem->upper_bounds = 
        numbbo_duplicate_vector(inner_problem->upper_bounds,
                                inner_problem->number_of_variables);
    problem->best_value = 
        numbbo_duplicate_vector(inner_problem->best_value,
                                inner_problem->number_of_objectives);
    problem->best_parameter = 
        numbbo_duplicate_vector(inner_problem->best_parameter,
                                inner_problem->number_of_objectives);
    problem->problem_name = numbbo_strdup(inner_problem->problem_id);
    problem->problem_id = numbbo_strdup(inner_problem->problem_id);
    obj->inner_problem = inner_problem;
    obj->state = NULL;
    return obj;
}

static void _default_initial_solution(const numbbo_problem_t *problem, 
                                      double *initial_solution) {
    size_t i;
    assert(problem != NULL);
    assert(problem->lower_bounds != NULL);
    assert(problem->upper_bounds != NULL);
    for (i = 0; i < problem->number_of_variables; ++i) 
        initial_solution[i] = 0.5 * (problem->lower_bounds[i] + problem->upper_bounds[i]);
}<commit_msg>Add documentation to _default_initial_solution.<commit_after>#include "numbbo.h"

#include "numbbo_utilities.c"

/**
 * _default_initial_solution(problem, initial_solution)
 *
 * Default implementation for the initial_solution method. The center
 * of ${problem}s region of interest is stored in the
 * vector pointed to by ${initial_solution}.
 */
static void _default_initial_solution(const numbbo_problem_t *problem, 
                                      double *initial_solution) {
    assert(problem != NULL);
    assert(problem->lower_bounds != NULL);
    assert(problem->upper_bounds != NULL);
    for (size_t i = 0; i < problem->number_of_variables; ++i) 
        initial_solution[i] = 0.5 * (problem->lower_bounds[i] + problem->upper_bounds[i]);
}

/**
 * numbbo_allocate_problem(number_of_variables):
 *
 * Allocate and pre-populate a new numbbo_problem_t for a problem with
 * ${number_of_variables}.
 */
static numbbo_problem_t *numbbo_allocate_problem(const size_t number_of_variables,
                                                 const size_t number_of_objectives,
                                                 const size_t number_of_constraints) {
    numbbo_problem_t *problem;
    problem = (numbbo_problem_t *)numbbo_allocate_memory(sizeof(numbbo_problem_t));
    /* Initialize fields to sane/safe defaults */
    problem->initial_solution = _default_initial_solution;
    problem->evaluate_function = NULL;
    problem->evaluate_constraint = NULL;
    problem->recommend_solutions = NULL;
    problem->free_problem = NULL;
    problem->number_of_variables = number_of_variables;
    problem->number_of_objectives = number_of_objectives;
    problem->number_of_constraints = number_of_constraints;
    problem->lower_bounds = numbbo_allocate_vector(number_of_variables);
    problem->upper_bounds = numbbo_allocate_vector(number_of_variables);
    problem->best_parameter = numbbo_allocate_vector(number_of_variables);
    problem->best_value = numbbo_allocate_vector(number_of_objectives);
    problem->problem_name = NULL;
    problem->problem_id = NULL;
    return problem;
}

/**
 * Generic wrapper for a transformed numbbo_problem_t. 
 */
typedef struct numbbo_transformed_problem {
    /* must be defined first to make inheritance / overload happen */
    numbbo_problem_t problem;
    numbbo_problem_t *inner_problem;
    void *state;
} numbbo_transformed_problem_t;

static void _tfp_evaluate_function(numbbo_problem_t *self, double *x, double *y) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_evaluate_function(problem->inner_problem, x, y);
}

static void _tfp_evaluate_constraint(numbbo_problem_t *self, double *x, double *y) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_evaluate_constraint(problem->inner_problem, x, y);
}

static void _tfp_recommend_solutions(numbbo_problem_t *self,
                                     double *x, size_t number_of_solutions) {
    numbbo_transformed_problem_t *problem = (numbbo_transformed_problem_t *)self;
    assert(problem->inner_problem != NULL);
    numbbo_recommend_solutions(problem->inner_problem, x, number_of_solutions);
}

static void _tfp_free_problem(numbbo_problem_t *self) {
    numbbo_transformed_problem_t *obj = (numbbo_transformed_problem_t *)self;
    numbbo_problem_t *problem = (numbbo_problem_t *)obj;
    if (obj->inner_problem != NULL)
        numbbo_free_problem(obj->inner_problem);
    obj->inner_problem = NULL;
    if (obj->state != NULL)
        numbbo_free_memory(obj->state);
    /* 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.
     */
    problem->free_problem = NULL;
    numbbo_free_problem(problem);
}

/**
 * numbbo_allocate_transformed_problem(inner_problem):
 *
 * Allocate a transformed problem that wraps ${inner_problem}. By
 * default all methods will dispatch to the ${inner_problem} method.
 *
 */
static numbbo_transformed_problem_t *
numbbo_allocate_transformed_problem(numbbo_problem_t *inner_problem) {
    numbbo_transformed_problem_t *obj =
        (numbbo_transformed_problem_t *)numbbo_allocate_memory(sizeof(numbbo_transformed_problem_t));
    numbbo_problem_t *problem = (numbbo_problem_t *)obj;

    problem->evaluate_function = _tfp_evaluate_function;
    problem->evaluate_constraint = _tfp_evaluate_constraint;
    problem->recommend_solutions = _tfp_recommend_solutions;
    problem->free_problem = _tfp_free_problem;
    problem->number_of_variables = inner_problem->number_of_variables;
    problem->number_of_objectives = inner_problem->number_of_objectives;
    problem->number_of_constraints = inner_problem->number_of_constraints;
    problem->lower_bounds = 
        numbbo_duplicate_vector(inner_problem->lower_bounds,
                                inner_problem->number_of_variables);
    problem->upper_bounds = 
        numbbo_duplicate_vector(inner_problem->upper_bounds,
                                inner_problem->number_of_variables);
    problem->best_value = 
        numbbo_duplicate_vector(inner_problem->best_value,
                                inner_problem->number_of_objectives);
    problem->best_parameter = 
        numbbo_duplicate_vector(inner_problem->best_parameter,
                                inner_problem->number_of_objectives);
    problem->problem_name = numbbo_strdup(inner_problem->problem_id);
    problem->problem_id = numbbo_strdup(inner_problem->problem_id);
    obj->inner_problem = inner_problem;
    obj->state = NULL;
    return obj;
}
{'fraction_non_alphanumeric': 0.08694503171247357, 'fraction_numerical': 0.0005285412262156448, 'mean_word_length': 3.883010752688172, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2445208', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3369, 'n_words': 603}