1use super::cutting_plane::OracleOptim;
2use ndarray::prelude::*;
3
4type Arr = Array1<f64>;
5
6#[derive(Debug, Default)]
7pub struct MyOracle;
8
9impl OracleOptim<Arr> for MyOracle {
10 type CutChoice = f64; fn assess_optim(&mut self, xc: &Arr, gamma: &mut f64) -> ((Arr, f64), bool) {
22 let x = xc[0];
23 let y = xc[1];
24 let f0 = x + y;
25 let f1 = f0 - 3.0;
26 if f1 > 0.0 {
27 return ((array![1.0, 1.0], f1), false);
28 }
29 let f2 = -x + y + 1.0;
30 if f2 > 0.0 {
31 return ((array![-1.0, 1.0], f2), false);
32 }
33 let f3 = *gamma - f0;
34 if f3 > 0.0 {
35 return ((array![-1.0, -1.0], f3), false);
36 }
37 *gamma = f0;
38 ((array![-1.0, -1.0], 0.0), true)
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45 use crate::cutting_plane::{cutting_plane_optim, Options};
46 use crate::ell::Ell;
47 #[test]
59 pub fn test_feasible() {
60 let mut ellip = Ell::new_with_scalar(10.0, array![0.0, 0.0]);
61 let mut oracle = MyOracle;
62 let mut gamma = f64::NEG_INFINITY;
63 let options = Options {
64 tolerance: 1e-10,
65 ..Default::default()
66 };
67 let (xbest, num_iters) = cutting_plane_optim(&mut oracle, &mut ellip, &mut gamma, &options);
68 assert!(xbest.is_some());
69 assert_eq!(num_iters, 25);
70 }
71
72 #[test]
73 pub fn test_infeasible1() {
74 let mut ellip = Ell::new(array![10.0, 10.0], array![100.0, 100.0]); let mut oracle = MyOracle;
77 let mut gamma = f64::NEG_INFINITY;
78 let options = Options::default();
79 let (xbest, _num_iters) =
80 cutting_plane_optim(&mut oracle, &mut ellip, &mut gamma, &options);
81 assert!(xbest.is_none());
82 }
83
84 #[test]
85 pub fn test_infeasible2() {
86 let mut ellip = Ell::new(array![10.0, 10.0], array![0.0, 0.0]);
87 let mut oracle = MyOracle;
88 let options = Options::default();
90 let (xbest, _niter) = cutting_plane_optim(&mut oracle, &mut ellip, &mut 100.0, &options);
91 assert!(xbest.is_none());
92 }
93}