8
8
//! ```no_run
9
9
//! use ndarray::{array, Array2, ArrayView1, ArrayView2, Zip};
10
10
//! use egobox_doe::{Lhs, SamplingMethod};
11
- //! use egobox_ego::{EgorBuilder, EgorConfig, InfillStrategy, InfillOptimizer, ObjFunc, EgorSolver};
11
+ //! use egobox_ego::{EgorBuilder, EgorConfig, InfillStrategy, InfillOptimizer, ObjFunc, EgorSolver, to_xtypes };
12
12
//! use egobox_moe::MoeParams;
13
13
//! use rand_xoshiro::Xoshiro256Plus;
14
14
//! use ndarray_rand::rand::SeedableRng;
25
25
//! y
26
26
//! }
27
27
//! let rng = Xoshiro256Plus::seed_from_u64(42);
28
- //! let xlimits = array![[-2., 2.], [-2., 2.]];
28
+ //! let xtypes = to_xtypes(& array![[-2., 2.], [-2., 2.]]) ;
29
29
//! let fobj = ObjFunc::new(rosenb);
30
- //! let config = EgorConfig::default();
31
- //! let solver: EgorSolver<MoeParams<f64, Xoshiro256Plus>> = EgorSolver::new(config, &xlimits, rng);
30
+ //! let config = EgorConfig::default().xtypes(&xtypes) ;
31
+ //! let solver: EgorSolver<MoeParams<f64, Xoshiro256Plus>> = EgorSolver::new(config, rng);
32
32
//! let res = Executor::new(fobj, solver)
33
33
//! .configure(|state| state.max_iters(20))
34
34
//! .run()
47
47
//! ```no_run
48
48
//! use ndarray::{array, Array2, ArrayView1, ArrayView2, Zip};
49
49
//! use egobox_doe::{Lhs, SamplingMethod};
50
- //! use egobox_ego::{EgorBuilder, EgorConfig, InfillStrategy, InfillOptimizer, ObjFunc, EgorSolver};
50
+ //! use egobox_ego::{EgorBuilder, EgorConfig, InfillStrategy, InfillOptimizer, ObjFunc, EgorSolver, to_xtypes };
51
51
//! use egobox_moe::MoeParams;
52
52
//! use rand_xoshiro::Xoshiro256Plus;
53
53
//! use ndarray_rand::rand::SeedableRng;
83
83
//! let rng = Xoshiro256Plus::seed_from_u64(42);
84
84
//! let xlimits = array![[0., 3.], [0., 4.]];
85
85
//! let doe = Lhs::new(&xlimits).sample(10);
86
+ //! let xtypes = to_xtypes(&xlimits);
86
87
//!
87
88
//! let fobj = ObjFunc::new(f_g24);
88
89
//!
89
90
//! let config = EgorConfig::default()
91
+ //! .xtypes(&xtypes)
90
92
//! .n_cstr(2)
91
93
//! .infill_strategy(InfillStrategy::EI)
92
94
//! .infill_optimizer(InfillOptimizer::Cobyla)
93
95
//! .doe(&doe)
94
96
//! .target(-5.5080);
95
97
//!
96
98
//! let solver: EgorSolver<MoeParams<f64, Xoshiro256Plus>> =
97
- //! EgorSolver::new(config, &xlimits, rng);
99
+ //! EgorSolver::new(config, rng);
98
100
//!
99
101
//! let res = Executor::new(fobj, solver)
100
102
//! .configure(|state| state.max_iters(40))
@@ -112,7 +114,7 @@ use crate::mixint::*;
112
114
use crate :: optimizer:: * ;
113
115
114
116
use crate :: types:: * ;
115
- use crate :: utils:: { compute_cstr_scales, no_discrete , update_data} ;
117
+ use crate :: utils:: { compute_cstr_scales, update_data} ;
116
118
117
119
use egobox_doe:: { Lhs , LhsKind , SamplingMethod } ;
118
120
use egobox_moe:: { ClusteredSurrogate , Clustering , CorrelationSpec , MoeParams , RegressionSpec } ;
@@ -163,7 +165,12 @@ pub struct EgorSolver<SB: SurrogateBuilder> {
163
165
}
164
166
165
167
impl SurrogateBuilder for MoeParams < f64 , Xoshiro256Plus > {
166
- fn new_with_xtypes_rng ( _xtypes : & [ XType ] ) -> Self {
168
+ /// Constructor from domain space specified with types
169
+ /// **panic** if xtypes contains other types than continuous type `Float`
170
+ fn new_with_xtypes ( xtypes : & [ XType ] ) -> Self {
171
+ if crate :: utils:: discrete ( xtypes) {
172
+ panic ! ( "MoeParams cannot be created with discrete types!" ) ;
173
+ }
167
174
MoeParams :: new ( )
168
175
}
169
176
@@ -213,49 +220,18 @@ impl<SB: SurrogateBuilder> EgorSolver<SB> {
213
220
/// Constructor of the optimization of the function `f` with specified random generator
214
221
/// to get reproducibility.
215
222
///
216
- /// The function `f` should return an objective value but also constraint values if any.
217
- /// Design space is specified by the matrix `xlimits` which is `[nx, 2]`-shaped
218
- /// the ith row contains lower and upper bounds of the ith component of `x`.
219
- pub fn new (
220
- config : EgorConfig ,
221
- xlimits : & ArrayBase < impl Data < Elem = f64 > , Ix2 > ,
222
- rng : Xoshiro256Plus ,
223
- ) -> Self {
224
- let env = Env :: new ( ) . filter_or ( "EGOBOX_LOG" , "info" ) ;
225
- let mut builder = Builder :: from_env ( env) ;
226
- let builder = builder. target ( env_logger:: Target :: Stdout ) ;
227
- builder. try_init ( ) . ok ( ) ;
228
- EgorSolver {
229
- config : EgorConfig {
230
- xtypes : Some ( continuous_xlimits_to_xtypes ( xlimits) ) , // align xlimits and xtypes
231
- ..config
232
- } ,
233
- xlimits : xlimits. to_owned ( ) ,
234
- surrogate_builder : SB :: new_with_xtypes_rng ( & continuous_xlimits_to_xtypes ( xlimits) ) ,
235
- rng,
236
- }
237
- }
238
-
239
- /// Constructor of the optimization of the function `f` with specified random generator
240
- /// to get reproducibility. This constructor is used for mixed-integer optimization
241
- /// when `f` has discrete inputs to be specified with list of xtypes.
242
- ///
243
223
/// The function `f` should return an objective but also constraint values if any.
244
224
/// Design space is specified by a list of types for input variables `x` of `f` (see [`XType`]).
245
- pub fn new_with_xtypes ( config : EgorConfig , xtypes : & [ XType ] , rng : Xoshiro256Plus ) -> Self {
225
+ pub fn new ( config : EgorConfig , rng : Xoshiro256Plus ) -> Self {
246
226
let env = Env :: new ( ) . filter_or ( "EGOBOX_LOG" , "info" ) ;
247
227
let mut builder = Builder :: from_env ( env) ;
248
228
let builder = builder. target ( env_logger:: Target :: Stdout ) ;
249
229
builder. try_init ( ) . ok ( ) ;
250
- let v_xtypes = xtypes. to_vec ( ) ;
230
+ let xtypes = config . xtypes . clone ( ) ;
251
231
EgorSolver {
252
- config : EgorConfig {
253
- xtypes : Some ( v_xtypes) ,
254
- no_discrete : no_discrete ( xtypes) ,
255
- ..config
256
- } ,
257
- xlimits : unfold_xtypes_as_continuous_limits ( xtypes) ,
258
- surrogate_builder : SB :: new_with_xtypes_rng ( xtypes) ,
232
+ config,
233
+ xlimits : unfold_xtypes_as_continuous_limits ( & xtypes) ,
234
+ surrogate_builder : SB :: new_with_xtypes ( & xtypes) ,
259
235
rng,
260
236
}
261
237
}
@@ -294,7 +270,7 @@ impl<SB: SurrogateBuilder> EgorSolver<SB> {
294
270
/// Build `xtypes` from simple float bounds of `x` input components when x belongs to R^n.
295
271
/// xlimits are bounds of the x components expressed a matrix (dim, 2) where dim is the dimension of x
296
272
/// the ith row is the bounds interval [lower, upper] of the ith comonent of `x`.
297
- fn continuous_xlimits_to_xtypes ( xlimits : & ArrayBase < impl Data < Elem = f64 > , Ix2 > ) -> Vec < XType > {
273
+ pub fn to_xtypes ( xlimits : & ArrayBase < impl Data < Elem = f64 > , Ix2 > ) -> Vec < XType > {
298
274
let mut xtypes: Vec < XType > = vec ! [ ] ;
299
275
Zip :: from ( xlimits. rows ( ) ) . for_each ( |limits| xtypes. push ( XType :: Cont ( limits[ 0 ] , limits[ 1 ] ) ) ) ;
300
276
xtypes
@@ -336,10 +312,12 @@ where
336
312
let doe = hstart_doe. as_ref ( ) . or ( self . config . doe . as_ref ( ) ) ;
337
313
338
314
let ( y_data, x_data) = if let Some ( doe) = doe {
315
+ let doe = unfold_with_enum_mask ( & self . config . xtypes , doe) ;
316
+
339
317
if doe. ncols ( ) == self . xlimits . nrows ( ) {
340
318
// only x are specified
341
319
info ! ( "Compute initial DOE on specified {} points" , doe. nrows( ) ) ;
342
- ( self . eval_obj ( problem, doe) , doe. to_owned ( ) )
320
+ ( self . eval_obj ( problem, & doe) , doe. to_owned ( ) )
343
321
} else {
344
322
// split doe in x and y
345
323
info ! ( "Use specified DOE {} samples" , doe. nrows( ) ) ;
@@ -991,9 +969,11 @@ where
991
969
pb : & mut Problem < O > ,
992
970
x : & Array2 < f64 > ,
993
971
) -> Array2 < f64 > {
994
- let params = if let Some ( xtypes) = & self . config . xtypes {
995
- let xcast = cast_to_discrete_values ( xtypes, x) ;
996
- fold_with_enum_index ( xtypes, & xcast. view ( ) )
972
+ let params = if self . config . discrete ( ) {
973
+ // When xtypes is specified, we have to cast x to folded space
974
+ // as EgorSolver works internally in the continuous space
975
+ let xcast = cast_to_discrete_values ( & self . config . xtypes , x) ;
976
+ fold_with_enum_index ( & self . config . xtypes , & xcast. view ( ) )
997
977
} else {
998
978
x. to_owned ( )
999
979
} ;
0 commit comments