You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have made minor changes to the LBFGS_nalgebra example to make it run the BFGS algorithm. However, the example does not seem to run. The error I get is:
51 | let res = Executor::new(cost, solver)
| ------------- ^^^^^^ the trait `argmin_math::ArgminDot<Matrix<f64, Dyn, Const<1>, VecStorage<f64, Dyn, Const<1>>>, Matrix<f64, Dyn, Dyn, VecStorage<f64, Dyn, Dyn>>>` is not implemented for `Matrix<f64, Dyn, Const<1>, VecStorage<f64, Dyn, Const<1>>>`, which is required by `BFGS<MoreThuenteLineSearch<_, _, {float}>, _>: Solver<Rosenbrock, _>`
| |
| required by a bound introduced by this call
My modified code, with comments on the modified lines is as follows:
// Copyright 2018-2024 argmin developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use argmin::{
core::{observers::ObserverMode, CostFunction, Error, Executor, Gradient},
solver::{linesearch::MoreThuenteLineSearch, quasinewton::BFGS}, // Changed dependency to BFGS
};
use argmin_observer_slog::SlogLogger;
use argmin_testfunctions::{rosenbrock, rosenbrock_derivative};
use nalgebra::{DVector, DMatrix}; // Added DMatrix dependency
struct Rosenbrock {}
impl CostFunction for Rosenbrock {
type Param = DVector<f64>;
type Output = f64;
fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
Ok(rosenbrock(p.as_slice()))
}
}
impl Gradient for Rosenbrock {
type Param = DVector<f64>;
type Gradient = DVector<f64>;
fn gradient(&self, p: &Self::Param) -> Result<Self::Gradient, Error> {
Ok(DVector::from(rosenbrock_derivative(p.as_slice())))
}
}
fn run() -> Result<(), Error> {
// Define cost function
let cost = Rosenbrock {};
// Define initial parameter vector
let init_param: DVector<f64> = DVector::from(vec![-1.2, 1.0]);
let init_hessian: DMatrix<f64> = DMatrix::identity(3, 3); // Added for BFGS solver
// set up a line search
let linesearch = MoreThuenteLineSearch::new().with_c(1e-4, 0.9)?;
// Set up solver
let solver = BFGS::new(linesearch); // Changed solver to BFGS
// Run solver
let res = Executor::new(cost, solver)
.configure(|state| state.param(init_param).inv_hessian(init_hessian).max_iters(100)) // Changed
// for
// BFGS
.add_observer(SlogLogger::term(), ObserverMode::Always)
.run()?;
// Print result
println!("{res}");
Ok(())
}
fn main() {
if let Err(ref e) = run() {
println!("{e}");
std::process::exit(1);
}
}
I am curious... is this a possible bug with BFGS and nalgebra?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have made minor changes to the LBFGS_nalgebra example to make it run the BFGS algorithm. However, the example does not seem to run. The error I get is:
My modified code, with comments on the modified lines is as follows:
I am curious... is this a possible bug with BFGS and nalgebra?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions