Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hot start mechanism due to bad loaded DOE exploitation #156

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions ego/src/egor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<O: GroupFunc> EgorBuilder<O> {
/// Build an Egor optimizer to minimize the function R^n -> R^p taking
/// inputs specified with given xtypes where some of components may be
/// discrete variables (mixed-integer optimization).
pub fn min_within_mixint_space(self, xtypes: &[XType]) -> Egor<O, MixintGpMixParams> {
pub fn min_within_mixint_space(self, xtypes: &[XType]) -> Egor<O, MixintGpMixtureParams> {
let rng = if let Some(seed) = self.config.seed {
Xoshiro256Plus::seed_from_u64(seed)
} else {
Expand Down Expand Up @@ -260,6 +260,9 @@ mod tests {
#[test]
#[serial]
fn test_xsinx_ei_quadratic_egor_builder() {
let outdir = "target/test_egor_builder_01";
let outfile = format!("{outdir}/{DOE_INITIAL_FILE}");
let _ = std::fs::remove_file(&outfile);
let initial_doe = array![[0.], [7.], [25.]];
let res = EgorBuilder::optimize(xsinx)
.configure(|cfg| {
Expand All @@ -269,14 +272,15 @@ mod tests {
.max_iters(30)
.doe(&initial_doe)
.target(-15.1)
.outdir("target/tests")
.outdir(outdir)
})
.min_within(&array![[0.0, 25.0]])
.run()
.expect("Egor should minimize xsinx");
let expected = array![-15.1];
assert_abs_diff_eq!(expected, res.y_opt, epsilon = 0.5);
let saved_doe: Array2<f64> = read_npy(format!("target/tests/{DOE_INITIAL_FILE}")).unwrap();
let saved_doe: Array2<f64> = read_npy(&outfile).unwrap();
let _ = std::fs::remove_file(&outfile);
assert_abs_diff_eq!(initial_doe, saved_doe.slice(s![..3, ..1]), epsilon = 1e-6);
}

Expand Down Expand Up @@ -312,14 +316,17 @@ mod tests {
#[test]
#[serial]
fn test_xsinx_with_hotstart_egor_builder() {
let outdir = "target/test_hotstart_01";
let _ = std::fs::remove_file(format!("{outdir}/{DOE_INITIAL_FILE}"));
let _ = std::fs::remove_file(format!("{outdir}/{DOE_FILE}"));
let xlimits = array![[0.0, 25.0]];
let doe = Lhs::new(&xlimits).sample(10);
let res = EgorBuilder::optimize(xsinx)
.configure(|config| {
config
.max_iters(15)
.doe(&doe)
.outdir("target/tests")
.outdir(outdir)
.random_seed(42)
})
.min_within(&xlimits)
Expand All @@ -332,13 +339,15 @@ mod tests {
.configure(|config| {
config
.max_iters(5)
.outdir("target/tests")
.outdir(outdir)
.hot_start(true)
.random_seed(42)
})
.min_within(&xlimits)
.run()
.expect("Egor should minimize xsinx");
let _ = std::fs::remove_file(format!("target/test_egor_builder/{DOE_INITIAL_FILE}"));
let _ = std::fs::remove_file(format!("target/test_egor_builder/{DOE_FILE}"));
let expected = array![18.9];
assert_abs_diff_eq!(expected, res.x_opt, epsilon = 1e-1);
}
Expand Down Expand Up @@ -582,4 +591,50 @@ mod tests {
println!("res={:?}", res);
assert_abs_diff_eq!(&array![-15.], &res.y_opt, epsilon = 1.);
}

#[test]
#[serial]
fn test_mixobj_mixint_hotstart_egor_builder() {
let env = env_logger::Env::new().filter_or("EGOBOX_LOG", "info");
let mut builder = env_logger::Builder::from_env(env);
let builder = builder.target(env_logger::Target::Stdout);
builder.try_init().ok();

let outdir = "target/test_hotstart_02";
let outfile = format!("{outdir}/{DOE_INITIAL_FILE}");
let _ = std::fs::remove_file(format!("{outdir}/{DOE_INITIAL_FILE}"));
let _ = std::fs::remove_file(format!("{outdir}/{DOE_FILE}"));

let xtypes = vec![
XType::Cont(-5., 5.),
XType::Enum(3),
XType::Enum(2),
XType::Ord(vec![0., 2., 3.]),
];
let xlimits = as_continuous_limits::<f64>(&xtypes);

EgorBuilder::optimize(mixobj)
.configure(|config| config.outdir(outdir).max_iters(1).random_seed(42))
.min_within_mixint_space(&xtypes)
.run()
.unwrap();

// Check saved DOE has continuous (unfolded) dimension + one output
let saved_doe: Array2<f64> = read_npy(outfile).unwrap();
assert_eq!(saved_doe.shape()[1], xlimits.nrows() + 1);

// Check that with no iteration, obj function is never called
// as the DOE does not need to be evaluated!
EgorBuilder::optimize(|_x| panic!("Should not call objective function!"))
.configure(|config| {
config
.outdir(outdir)
.hot_start(true)
.max_iters(0)
.random_seed(42)
})
.min_within_mixint_space(&xtypes)
.run()
.unwrap();
}
}
2 changes: 1 addition & 1 deletion ego/src/egor_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl EgorServiceBuilder {
/// Build an Egor optimizer to minimize the function R^n -> R^p taking
/// inputs specified with given xtypes where some of components may be
/// discrete variables (mixed-integer optimization).
pub fn min_within_mixint_space(self, xtypes: &[XType]) -> EgorService<MixintGpMixParams> {
pub fn min_within_mixint_space(self, xtypes: &[XType]) -> EgorService<MixintGpMixtureParams> {
let rng = if let Some(seed) = self.config.seed {
Xoshiro256Plus::seed_from_u64(seed)
} else {
Expand Down
10 changes: 4 additions & 6 deletions ego/src/egor_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl SurrogateBuilder for GpMixtureParams<f64, Xoshiro256Plus> {
/// **panic** if xtypes contains other types than continuous type `Float`
fn new_with_xtypes(xtypes: &[XType]) -> Self {
if crate::utils::discrete(xtypes) {
panic!("GpMixParams cannot be created with discrete types!");
panic!("GpMixtureParams cannot be created with discrete types!");
}
GpMixtureParams::new()
}
Expand Down Expand Up @@ -314,12 +314,10 @@ where
let doe = hstart_doe.as_ref().or(self.config.doe.as_ref());

let (y_data, x_data) = if let Some(doe) = doe {
let doe = to_continuous_space(&self.config.xtypes, doe);

if doe.ncols() == self.xlimits.nrows() {
// only x are specified
info!("Compute initial DOE on specified {} points", doe.nrows());
(self.eval_obj(problem, &doe), doe.to_owned())
(self.eval_obj(problem, doe), doe.to_owned())
} else {
// split doe in x and y
info!("Use specified DOE {} samples", doe.nrows());
Expand All @@ -343,7 +341,7 @@ where
let path = self.config.outdir.as_ref().unwrap();
std::fs::create_dir_all(path)?;
let filepath = std::path::Path::new(path).join(DOE_INITIAL_FILE);
info!("Save initial doe {:?} in {:?}", doe.shape(), filepath);
info!("Save initial doe shape {:?} in {:?}", doe.shape(), filepath);
write_npy(filepath, &doe).expect("Write initial doe");
}

Expand Down Expand Up @@ -500,7 +498,7 @@ where
let path = self.config.outdir.as_ref().unwrap();
std::fs::create_dir_all(path)?;
let filepath = std::path::Path::new(path).join(DOE_FILE);
info!("Save doe in {:?}", filepath);
info!("Save doe shape {:?} in {:?}", doe.shape(), filepath);
write_npy(filepath, &doe).expect("Write current doe");
}
let best_index = find_best_result_index(&y_data, &new_state.cstr_tol);
Expand Down
20 changes: 10 additions & 10 deletions ego/src/mixint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ impl MixintMoeValidParams {

/// Parameters for mixture of experts surrogate model
#[derive(Clone, Serialize, Deserialize)]
pub struct MixintGpMixParams(MixintMoeValidParams);
pub struct MixintGpMixtureParams(MixintMoeValidParams);

impl MixintGpMixParams {
/// Constructor given `xtypes` specifications and given surrogate builder
impl MixintGpMixtureParams {
/// Constructor given `xtypes` specifications and given surrogate builder
pub fn new(xtypes: &[XType], surrogate_builder: &MoeBuilder) -> Self {
MixintGpMixParams(MixintMoeValidParams {
MixintGpMixtureParams(MixintMoeValidParams {
surrogate_builder: surrogate_builder.clone(),
xtypes: xtypes.to_vec(),
work_in_folded_space: false,
Expand Down Expand Up @@ -387,9 +387,9 @@ impl MixintMoeValidParams {
}
}

impl SurrogateBuilder for MixintGpMixParams {
impl SurrogateBuilder for MixintGpMixtureParams {
fn new_with_xtypes(xtypes: &[XType]) -> Self {
MixintGpMixParams::new(xtypes, &GpMixtureParams::new())
MixintGpMixtureParams::new(xtypes, &GpMixtureParams::new())
}

/// Sets the allowed regression models used in gaussian processes.
Expand Down Expand Up @@ -471,7 +471,7 @@ impl<D: Data<Elem = f64>> Fit<ArrayBase<D, Ix2>, ArrayBase<D, Ix2>, EgoError>
}
}

impl ParamGuard for MixintGpMixParams {
impl ParamGuard for MixintGpMixtureParams {
type Checked = MixintMoeValidParams;
type Error = EgoError;

Expand All @@ -485,9 +485,9 @@ impl ParamGuard for MixintGpMixParams {
}
}

impl From<MixintMoeValidParams> for MixintGpMixParams {
impl From<MixintMoeValidParams> for MixintGpMixtureParams {
fn from(item: MixintMoeValidParams) -> Self {
MixintGpMixParams(item)
MixintGpMixtureParams(item)
}
}

Expand Down Expand Up @@ -722,7 +722,7 @@ impl MixintContext {
surrogate_builder: &MoeBuilder,
dataset: &DatasetBase<Array2<f64>, Array2<f64>>,
) -> Result<MixintMoe> {
let mut params = MixintGpMixParams::new(&self.xtypes, surrogate_builder);
let mut params = MixintGpMixtureParams::new(&self.xtypes, surrogate_builder);
let params = params.work_in_folded_space(self.work_in_folded_space);
params.fit(dataset)
}
Expand Down
Loading