Skip to content

Commit 6bfb21e

Browse files
committed
Add an example of optimization as a service
1 parent b3325d9 commit 6bfb21e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Diff for: ego/examples/g24.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use egobox_doe::{Lhs, SamplingMethod};
2+
use egobox_ego::EgorServiceBuilder;
3+
use ndarray::{array, concatenate, Array2, ArrayBase, ArrayView2, Axis, Data, Ix1, Zip};
4+
5+
// Objective
6+
fn g24(x: &ArrayBase<impl Data<Elem = f64>, Ix1>) -> f64 {
7+
// Function G24: 1 global optimum y_opt = -5.5080 at x_opt =(2.3295, 3.1785)
8+
-x[0] - x[1]
9+
}
10+
11+
// Constraints < 0
12+
fn g24_c1(x: &ArrayBase<impl Data<Elem = f64>, Ix1>) -> f64 {
13+
-2.0 * x[0].powf(4.0) + 8.0 * x[0].powf(3.0) - 8.0 * x[0].powf(2.0) + x[1] - 2.0
14+
}
15+
16+
fn g24_c2(x: &ArrayBase<impl Data<Elem = f64>, Ix1>) -> f64 {
17+
-4.0 * x[0].powf(4.0) + 32.0 * x[0].powf(3.0) - 88.0 * x[0].powf(2.0) + 96.0 * x[0] + x[1]
18+
- 36.0
19+
}
20+
21+
fn f_g24(x: &ArrayView2<f64>) -> Array2<f64> {
22+
let mut y = Array2::zeros((x.nrows(), 3));
23+
Zip::from(y.rows_mut())
24+
.and(x.rows())
25+
.for_each(|mut yi, xi| {
26+
yi.assign(&array![g24(&xi), g24_c1(&xi), g24_c2(&xi)]);
27+
});
28+
y
29+
}
30+
31+
fn main() {
32+
let xlimits = array![[0., 3.], [0., 4.]];
33+
let mut doe = Lhs::new(&xlimits).sample(3);
34+
35+
// We use Egor optimizer as a service
36+
let egor = EgorServiceBuilder::optimize()
37+
.configure(|config| config.n_cstr(2))
38+
.random_seed(42)
39+
.min_within(&xlimits);
40+
41+
let mut y_doe = f_g24(&doe.view());
42+
for _i in 0..10 {
43+
// We tell function values and ask for next x location
44+
let x_suggested = egor.suggest(&doe, &y_doe);
45+
46+
doe = concatenate![Axis(0), doe, x_suggested];
47+
y_doe = f_g24(&doe.view());
48+
}
49+
50+
println!("G24 optim x suggestion history = {doe:?}");
51+
}

0 commit comments

Comments
 (0)