Skip to content

Commit

Permalink
Not cheating anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-consoli authored and olafmersmann committed Nov 22, 2023
1 parent 74e885f commit 71854ee
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions code-experiments/build/python/src/cocoex/function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ cdef extern from 'coco.h':
# add a declaration to the generated source files.
cdef extern coco_problem_t *coco_get_bbob_problem(size_t function, size_t
dimension, size_t instance)

cdef extern double coco_problem_get_best_value(coco_problem_t *p)

cdef class BenchmarkFunction:
"""A bare benchmark function from one of the available suites.
Expand Down Expand Up @@ -102,7 +103,11 @@ cdef class BenchmarkFunction:
def __del__(self):
if self._problem != NULL:
coco_problem_free(self._problem)


def best_value(self):
"""Return the best (lowest) possible function value"""
return coco_problem_get_best_value(self._problem)

def __str__(self):
return self.id

Expand Down
3 changes: 3 additions & 0 deletions code-experiments/build/python/test/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_bbob():
for instance in [0, 1, 10, 100, 1000]:
fn = BenchmarkFunction("bbob", fid, dimension, instance)
assert str(fn) == f"bbob_f{fid:03d}_i{instance:02d}_d{dimension:02d}"
assert fn(x0) >= fn.best_value()

def test_list():
fn = BenchmarkFunction("bbob", 1, 4, 1)
Expand All @@ -28,3 +29,5 @@ def test_multiple_parameters():
X = np.random.uniform(-5, 5, size=(n, 4))
y = fn(X)
assert len(y) == n
assert np.all(y >= fn.best_value())

3 changes: 3 additions & 0 deletions code-experiments/build/rust/coco-sys/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
/** Makes the problem returned by coco_suite_get_next_problem owned **/
void coco_suite_forget_current_problem(coco_suite_t *suite);

/** Returns the optimal function value of the problem **/
double coco_problem_get_best_value(const coco_problem_t *problem);

/** Returns the optimal function value + delta of the problem **/
double coco_problem_get_final_target_fvalue1(const coco_problem_t *problem);
8 changes: 8 additions & 0 deletions code-experiments/build/rust/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ impl Problem<'_> {
unsafe { coco_sys::coco_problem_get_final_target_fvalue1(self.inner) }
}

/// Returns the optimal function value of the problem
///
/// To check whether the target has been reached use [[Problem::final_target_value]]
/// or [[Problem::final_target_hit]] instead.
pub fn best_value(&self) -> f64 {
unsafe { coco_sys::coco_problem_get_best_value(self.inner) }
}

/// Returns the best observed value for the first objective.
pub fn best_observed_value(&self) -> f64 {
unsafe { coco_sys::coco_problem_get_best_observed_fvalue1(self.inner) }
Expand Down
10 changes: 10 additions & 0 deletions code-experiments/src/coco_problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void coco_problem_free(coco_problem_t *problem) {
problem->smallest_values_of_interest = NULL;
problem->largest_values_of_interest = NULL;
problem->best_parameter = NULL;
problem->data = NULL;
problem->best_value = NULL;
problem->nadir_value = NULL;
problem->suite = NULL;
Expand Down Expand Up @@ -462,6 +463,15 @@ double coco_problem_get_best_observed_fvalue1(const coco_problem_t *problem) {
return problem->best_observed_fvalue[0];
}

/**
* @brief Returns the optimal function value of the problem
*/
double coco_problem_get_best_value(const coco_problem_t *problem) {
assert(problem != NULL);
assert(problem->best_value != NULL);
return problem->best_value[0];
}

/**
* @note This function breaks the black-box property: the returned value is not
* meant to be used by the optimization algorithm.
Expand Down

0 comments on commit 71854ee

Please sign in to comment.