forked from alphaville/optimization-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanoc_ex1.rs
More file actions
65 lines (55 loc) · 1.91 KB
/
panoc_ex1.rs
File metadata and controls
65 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! # PANOC Example 1
//!
//! This example shows how to minimize the [Rosenbrock function] subject to constraints.
//!
//! [Rosenbrock function]: https://en.wikipedia.org/wiki/Rosenbrock_function
use optimization_engine::{constraints::*, panoc::*, *};
fn rosenbrock_cost(a: f64, b: f64, u: &[f64]) -> f64 {
(a - u[0]).powi(2) + b * (u[1] - u[0].powi(2)).powi(2)
}
fn rosenbrock_grad(a: f64, b: f64, u: &[f64], grad: &mut [f64]) {
grad[0] = 2.0 * u[0] - 2.0 * a - 4.0 * b * u[0] * (-u[0].powi(2) + u[1]);
grad[1] = b * (-2.0 * u[0].powi(2) + 2.0 * u[1]);
}
fn main() {
/* USER PARAMETERS */
let tolerance = 1e-14;
let a = 1.0;
let b = 200.0;
let problem_size = 2;
let lbfgs_memory_size = 10;
let max_iters = 80;
let mut u = [-1.5, 0.9];
let radius = 1.0;
// define the cost function and its gradient
let df = |u: &[f64], grad: &mut [f64]| -> Result<(), SolverError> {
if a < 0.0 || b < 0.0 {
Err(SolverError::Cost(
"Rosenbrock parameters must be nonnegative",
))
} else {
rosenbrock_grad(a, b, u, grad);
Ok(())
}
};
let f = |u: &[f64], c: &mut f64| -> Result<(), SolverError> {
if a < 0.0 || b < 0.0 {
Err(SolverError::Cost(
"Rosenbrock parameters must be nonnegative",
))
} else {
*c = rosenbrock_cost(a, b, u);
Ok(())
}
};
// define the constraints
let bounds = Ball2::new(None, radius);
/* PROBLEM STATEMENT */
let problem = Problem::new(&bounds, df, f);
let mut panoc_cache = PANOCCache::new(problem_size, tolerance, lbfgs_memory_size);
let mut panoc = PANOCOptimizer::new(problem, &mut panoc_cache).with_max_iter(max_iters);
// Invoke the solver
let status = panoc.solve(&mut u);
println!("Panoc status: {:#?}", status);
println!("Panoc solution: {:#?}", u);
}