11
11
//!
12
12
13
13
use crate :: types:: * ;
14
+ use egobox_ego:: find_best_result_index;
14
15
use ndarray:: { concatenate, Array1 , Axis } ;
15
16
use numpy:: ndarray:: { Array2 , ArrayView2 } ;
16
- use numpy:: { IntoPyArray , PyArray1 , PyArray2 , PyReadonlyArray2 } ;
17
+ use numpy:: { IntoPyArray , PyArray1 , PyArray2 , PyReadonlyArray2 , ToPyArray } ;
17
18
use pyo3:: exceptions:: PyValueError ;
18
19
use pyo3:: prelude:: * ;
19
20
@@ -241,7 +242,7 @@ impl Egor {
241
242
///
242
243
/// # Returns
243
244
/// 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
245
246
/// y_opt (array[1, nx]): fun(x_opt)
246
247
///
247
248
#[ pyo3( signature = ( fun, max_iters = 20 ) ) ]
@@ -284,7 +285,7 @@ impl Egor {
284
285
///
285
286
/// # Parameters
286
287
/// 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
288
289
///
289
290
///
290
291
/// # Returns
@@ -307,7 +308,56 @@ impl Egor {
307
308
. min_within_mixint_space ( & xtypes) ;
308
309
309
310
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
+ }
311
361
}
312
362
}
313
363
0 commit comments