[−][src]Struct argmin::solver::trustregion::trustregion_method::TrustRegion
The trust region method approximates the cost function within a certain region around the current point in parameter space. Depending on the quality of this approximation, the region is either expanded or contracted.
The calculation of the actual step length and direction is done by one of the following methods:
This subproblem can be set via set_subproblem(...)
. If this is not provided, it will default
to the Steihaug method.
Example
extern crate argmin; extern crate ndarray; use argmin::prelude::*; use argmin::solver::trustregion::{CauchyPoint, Dogleg, Steihaug, TrustRegion}; use argmin::testfunctions::{rosenbrock_2d, rosenbrock_2d_derivative, rosenbrock_2d_hessian}; use ndarray::{Array, Array1, Array2}; // Define cost function let cost = MyProblem {}; // Define inital parameter vector // easy case // let init_param: Array1<f64> = Array1::from_vec(vec![1.2, 1.2]); // tough case let init_param: Array1<f64> = Array1::from_vec(vec![-1.2, 1.0]); // Set up the subproblem let mut subproblem = Steihaug::new(cost.clone()); // let mut subproblem = CauchyPoint::new(cost.clone()); // let mut subproblem = Dogleg::new(cost.clone()); subproblem.set_max_iters(2); // Set up the subproblem let mut subproblem = Steihaug::new(cost.clone()); // let mut subproblem = CauchyPoint::new(cost.clone()); // let mut subproblem = Dogleg::new(cost.clone()); subproblem.set_max_iters(2); // Set up solver let mut solver = TrustRegion::new(cost, init_param, subproblem); // Set the maximum number of iterations solver.set_max_iters(2_000); // Attach a logger solver.add_logger(ArgminSlogLogger::term()); // Run solver solver.run()?; // Wait a second (lets the logger flush everything before printing again) std::thread::sleep(std::time::Duration::from_secs(1)); // Print result println!("{:?}", solver.result());
References:
[0] Jorge Nocedal and Stephen J. Wright (2006). Numerical Optimization. Springer. ISBN 0-387-30303-0.
Methods
impl<O, R> TrustRegion<O, R> where
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
[src]
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
pub fn new(operator: O, param: O::Param, subproblem: R) -> Self
[src]
Constructor
Parameters:
operator
: operator
pub fn set_radius(&mut self, radius: f64) -> &mut Self
[src]
set radius
pub fn set_max_radius(&mut self, max_radius: f64) -> &mut Self
[src]
Set maximum radius
pub fn set_eta(&mut self, eta: f64) -> Result<&mut Self, Error>
[src]
Set eta
Trait Implementations
impl<O, R> Serialize for TrustRegion<O, R> where
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
O: Serialize,
R: Serialize,
[src]
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
O: Serialize,
R: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, O, R> Deserialize<'de> for TrustRegion<O, R> where
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
O: Deserialize<'de>,
R: Deserialize<'de>,
[src]
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
O: Deserialize<'de>,
R: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl<O, R> ArgminSolver for TrustRegion<O, R> where
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
[src]
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
fn run(&mut self) -> Result<ArgminResult<Self::Param>, Error>
[src]
Run the optimization algorithm
fn run_fast(&mut self) -> Result<ArgminResult<Self::Param>, Error>
[src]
Run the essential parts of the optimization algorithm (no logging, no Ctrl-C handling)
fn apply(&mut self, param: &Self::Param) -> Result<Self::Output, Error>
[src]
Applies the cost function or operator to a parameter vector param
.
Returns an Err
if apply
of ArgminOperator
is not implemented.
fn gradient(&mut self, param: &Self::Param) -> Result<Self::Param, Error>
[src]
Computes the gradient at parameter param
.
Returns an Err
if gradient
of ArgminOperator
is not implemented.
fn hessian(&mut self, param: &Self::Param) -> Result<Self::Hessian, Error>
[src]
Computes the Hessian at parameter param
.
Returns an Err
if hessian
of ArgminOperator
is not implemented.
fn cur_param(&self) -> Self::Param
[src]
Returns the current parameter vector.
fn cur_grad(&self) -> Self::Param
[src]
Returns the most recently stored gradient.
fn cur_hessian(&self) -> Self::Hessian
[src]
Returns the most recently stored Hessian.
fn set_cur_param(&mut self, param: Self::Param)
[src]
Sets the current parameter to param
.
fn set_cur_grad(&mut self, grad: Self::Param)
[src]
Sets the current gradient to grad
.
fn set_cur_hessian(&mut self, hessian: Self::Hessian)
[src]
Sets the current Hessian to hessian
.
fn set_best_param(&mut self, param: Self::Param)
[src]
Sets the best parameter vector to param
.
fn modify(&self, param: &Self::Param, factor: f64) -> Result<Self::Param, Error>
[src]
Modify the parameter vector by calling the modify
method of the trait
ArgminOperator
. Will return an Err
if modify
is not implemented.
fn result(&self) -> ArgminResult<Self::Param>
[src]
Returns the result of the optimization.
fn set_max_iters(&mut self, iters: u64)
[src]
Sets the maximum number of iterations to iters
.
fn max_iters(&self) -> u64
[src]
Returns the maximum number of iterations.
fn increment_iter(&mut self)
[src]
Increments the iteration counter.
fn cur_iter(&self) -> u64
[src]
Returns the current number of iterations.
fn cur_cost(&self) -> f64
[src]
Returns the most recently stored cost function value.
fn set_cur_cost(&mut self, cost: f64)
[src]
Sets the current cost function value to cost
fn best_cost(&self) -> f64
[src]
Returns the best cost function value obtained so far.
fn set_best_cost(&mut self, cost: f64)
[src]
Sets the best cost function value.
fn set_target_cost(&mut self, cost: f64)
[src]
Sets the target cost function value to cost
. The optimization algorithm will be
terminated when this limit is reached.
fn increment_cost_func_count(&mut self)
[src]
Increments the counter for the computations of the cost function by 1.
fn increase_cost_func_count(&mut self, count: u64)
[src]
Increases the counter for the computations of the cost function by count
.
fn cost_func_count(&self) -> u64
[src]
Returns the current value of the counter for the computations of the cost function.
fn increment_grad_func_count(&mut self)
[src]
Increments the counter for the computations of the gradient by 1.
fn increase_grad_func_count(&mut self, count: u64)
[src]
Increases the counter for the computations of the gradient by count
.
fn grad_func_count(&self) -> u64
[src]
Returns the current value of the counter for the computations of the gradient.
fn increment_hessian_func_count(&mut self)
[src]
Increments the counter for the computations of the Hessian by 1.
fn increase_hessian_func_count(&mut self, count: u64)
[src]
Increases the counter for the computations of the Hessian by count
.
fn hessian_func_count(&self) -> u64
[src]
Returns the current value of the counter for the computations of the Hessian.
fn add_logger(&mut self, logger: Arc<dyn ArgminLog>)
[src]
Attaches a logger which implements ArgminLog
to the solver.
fn add_writer(&mut self, writer: Arc<dyn ArgminWrite<Param = Self::Param>>)
[src]
Attaches a writer which implements ArgminWrite
to the solver.
fn set_termination_reason(&mut self, reason: TerminationReason)
[src]
Sets the TerminationReason
fn terminate(&mut self) -> TerminationReason
[src]
Checks whether any of the conditions to terminate is true and terminates the algorithm.
fn base_reset(&mut self)
[src]
Resets the base
field to it's initial conditions. This is helpful for
implementing a solver which is initialized once, but called several times. It is
recommended to only call this method inside the init
function of
ArgminNextIter
.
fn set_checkpoint_dir(&mut self, dir: &str)
[src]
Set checkpoint directory
fn set_checkpoint_name(&mut self, name: &str)
[src]
Set checkpoint name
fn set_checkpoint_mode(&mut self, mode: CheckpointMode)
[src]
Set checkpoint mode
fn from_checkpoint<P>(path: P) -> Result<Self, Error> where
P: AsRef<Path>,
Self: DeserializeOwned,
[src]
P: AsRef<Path>,
Self: DeserializeOwned,
Load solver from checkpoint
impl<O, R> ArgminIter for TrustRegion<O, R> where
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
[src]
O: ArgminOp<Output = f64>,
O::Param: ArgminMul<f64, O::Param> + ArgminWeightedDot<O::Param, f64, O::Hessian> + ArgminNorm<f64> + ArgminDot<O::Param, f64> + ArgminAdd<O::Param, O::Param> + ArgminSub<O::Param, O::Param> + ArgminZero + ArgminMul<f64, O::Param>,
O::Hessian: ArgminDot<O::Param, O::Param>,
R: ArgminTrustRegion<Param = O::Param, Output = f64, Hessian = O::Hessian>,
Auto Trait Implementations
impl<O, R> Send for TrustRegion<O, R> where
R: Send,
<O as ArgminOp>::Hessian: Send,
<O as ArgminOp>::Param: Send,
R: Send,
<O as ArgminOp>::Hessian: Send,
<O as ArgminOp>::Param: Send,
impl<O, R> Sync for TrustRegion<O, R> where
R: Sync,
<O as ArgminOp>::Hessian: Sync,
<O as ArgminOp>::Param: Sync,
R: Sync,
<O as ArgminOp>::Hessian: Sync,
<O as ArgminOp>::Param: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,