Skip to content

Commit 82be61f

Browse files
committed
Add get_result for ask-and-tell interface
1 parent 3ab6851 commit 82be61f

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

python/egobox/tests/test_egor.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,14 @@ def test_egor_service(self):
175175
xlimits = egx.to_specs([[0.0, 25.0]])
176176
egor = egx.Egor(xlimits, seed=42)
177177
x_doe = egx.lhs(xlimits, 3, seed=42)
178-
print(x_doe)
179178
y_doe = xsinx(x_doe)
180-
print(y_doe)
181179
for _ in range(10):
182180
x = egor.suggest(x_doe, y_doe)
183181
x_doe = np.concatenate((x_doe, x))
184182
y_doe = np.concatenate((y_doe, xsinx(x)))
183+
res = egor.get_result(x_doe, y_doe)
184+
self.assertAlmostEqual(-15.125, res.y_opt[0], delta=1e-3)
185+
self.assertAlmostEqual(18.935, res.x_opt[0], delta=1e-3)
185186

186187

187188
if __name__ == "__main__":

src/egor.rs

+54-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
//!
1212
1313
use crate::types::*;
14+
use egobox_ego::find_best_result_index;
1415
use ndarray::{concatenate, Array1, Axis};
1516
use numpy::ndarray::{Array2, ArrayView2};
16-
use numpy::{IntoPyArray, PyArray1, PyArray2, PyReadonlyArray2};
17+
use numpy::{IntoPyArray, PyArray1, PyArray2, PyReadonlyArray2, ToPyArray};
1718
use pyo3::exceptions::PyValueError;
1819
use pyo3::prelude::*;
1920

@@ -241,7 +242,7 @@ impl Egor {
241242
///
242243
/// # Returns
243244
/// optimization result
244-
/// x_opt (array[1, nx]): x value where fun is at its minimum subject to constraint
245+
/// x_opt (array[1, nx]): x value where fun is at its minimum subject to constraints
245246
/// y_opt (array[1, nx]): fun(x_opt)
246247
///
247248
#[pyo3(signature = (fun, max_iters = 20))]
@@ -284,7 +285,7 @@ impl Egor {
284285
///
285286
/// # Parameters
286287
/// x_doe (array[ns, nx]): ns samples where function has been evaluated
287-
/// y_doe (array[ns, 1 + n_cstr]): ns values of objetcive and constraints
288+
/// y_doe (array[ns, 1 + n_cstr]): ns values of objecctive and constraints
288289
///
289290
///
290291
/// # Returns
@@ -307,7 +308,56 @@ impl Egor {
307308
.min_within_mixint_space(&xtypes);
308309

309310
let x_suggested = py.allow_threads(|| mixintegor.suggest(&x_doe, &y_doe));
310-
x_suggested.into_pyarray(py).to_owned()
311+
x_suggested.to_pyarray(py).into()
312+
}
313+
314+
/// This function gives the best evaluation index given the outputs
315+
/// of the function (objective wrt constraints) under minimization.
316+
///
317+
/// # Parameters
318+
/// y_doe (array[ns, 1 + n_cstr]): ns values of objective and constraints
319+
///
320+
/// # Returns
321+
/// index in y_doe of the best evaluation
322+
///
323+
#[pyo3(signature = (y_doe))]
324+
fn get_result_index(&self, y_doe: PyReadonlyArray2<f64>) -> usize {
325+
let y_doe = y_doe.as_array();
326+
find_best_result_index(&y_doe, &self.cstr_tol())
327+
}
328+
329+
/// This function gives the best result given inputs and outputs
330+
/// of the function (objective wrt constraints) under minimization.
331+
///
332+
/// # Parameters
333+
/// x_doe (array[ns, nx]): ns samples where function has been evaluated
334+
/// y_doe (array[ns, 1 + n_cstr]): ns values of objective and constraints
335+
///
336+
/// # Returns
337+
/// optimization result
338+
/// x_opt (array[1, nx]): x value where fun is at its minimum subject to constraints
339+
/// y_opt (array[1, nx]): fun(x_opt)
340+
///
341+
#[pyo3(signature = (x_doe, y_doe))]
342+
fn get_result(
343+
&self,
344+
py: Python,
345+
x_doe: PyReadonlyArray2<f64>,
346+
y_doe: PyReadonlyArray2<f64>,
347+
) -> OptimResult {
348+
let x_doe = x_doe.as_array();
349+
let y_doe = y_doe.as_array();
350+
let idx = find_best_result_index(&y_doe, &self.cstr_tol());
351+
let x_opt = x_doe.row(idx).to_pyarray(py).into();
352+
let y_opt = y_doe.row(idx).to_pyarray(py).into();
353+
let x_hist = x_doe.to_pyarray(py).into();
354+
let y_hist = y_doe.to_pyarray(py).into();
355+
OptimResult {
356+
x_opt,
357+
y_opt,
358+
x_hist,
359+
y_hist,
360+
}
311361
}
312362
}
313363

0 commit comments

Comments
 (0)