From 71854eee8f1457c23bd77e5f2861a7591b387a22 Mon Sep 17 00:00:00 2001 From: FMGS666 Date: Wed, 22 Nov 2023 23:01:46 +0100 Subject: [PATCH] Not cheating anymore --- code-experiments/build/python/src/cocoex/function.pyx | 9 +++++++-- code-experiments/build/python/test/test_function.py | 3 +++ code-experiments/build/rust/coco-sys/wrapper.h | 3 +++ code-experiments/build/rust/src/problem.rs | 8 ++++++++ code-experiments/src/coco_problem.c | 10 ++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/code-experiments/build/python/src/cocoex/function.pyx b/code-experiments/build/python/src/cocoex/function.pyx index 1e6ec8625..029787378 100644 --- a/code-experiments/build/python/src/cocoex/function.pyx +++ b/code-experiments/build/python/src/cocoex/function.pyx @@ -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. @@ -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 diff --git a/code-experiments/build/python/test/test_function.py b/code-experiments/build/python/test/test_function.py index 30d80b11f..95af6fabe 100644 --- a/code-experiments/build/python/test/test_function.py +++ b/code-experiments/build/python/test/test_function.py @@ -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) @@ -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()) + diff --git a/code-experiments/build/rust/coco-sys/wrapper.h b/code-experiments/build/rust/coco-sys/wrapper.h index 633d325a5..ea677371d 100644 --- a/code-experiments/build/rust/coco-sys/wrapper.h +++ b/code-experiments/build/rust/coco-sys/wrapper.h @@ -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); diff --git a/code-experiments/build/rust/src/problem.rs b/code-experiments/build/rust/src/problem.rs index d96041945..7f2cbb2a6 100644 --- a/code-experiments/build/rust/src/problem.rs +++ b/code-experiments/build/rust/src/problem.rs @@ -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) } diff --git a/code-experiments/src/coco_problem.c b/code-experiments/src/coco_problem.c index febae2f70..240dad147 100644 --- a/code-experiments/src/coco_problem.c +++ b/code-experiments/src/coco_problem.c @@ -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; @@ -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.