-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "example-timeout" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
argmin = { version = "*", path = "../../argmin" } | ||
argmin-math = { version = "*", features = ["vec"], path = "../../argmin-math" } | ||
argmin-observer-slog = { version = "*", path = "../../observers/slog/" } | ||
argmin_testfunctions = "*" | ||
rand = "0.8.5" | ||
rand_xoshiro = "0.6.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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. | ||
|
||
// This example shows how to add a timeout ot an optimization run. The optimization will be | ||
// terminated once the 3 seconds timeout is reached. | ||
|
||
use argmin::{ | ||
core::{observers::ObserverMode, CostFunction, Error, Executor}, | ||
solver::simulatedannealing::{Anneal, SATempFunc, SimulatedAnnealing}, | ||
}; | ||
use argmin_observer_slog::SlogLogger; | ||
use argmin_testfunctions::rosenbrock; | ||
use rand::{distributions::Uniform, prelude::*}; | ||
use rand_xoshiro::Xoshiro256PlusPlus; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
struct Rosenbrock { | ||
a: f64, | ||
b: f64, | ||
lower_bound: Vec<f64>, | ||
upper_bound: Vec<f64>, | ||
rng: Arc<Mutex<Xoshiro256PlusPlus>>, | ||
} | ||
|
||
impl Rosenbrock { | ||
pub fn new(a: f64, b: f64, lower_bound: Vec<f64>, upper_bound: Vec<f64>) -> Self { | ||
Rosenbrock { | ||
a, | ||
b, | ||
lower_bound, | ||
upper_bound, | ||
rng: Arc::new(Mutex::new(Xoshiro256PlusPlus::from_entropy())), | ||
} | ||
} | ||
} | ||
|
||
impl CostFunction for Rosenbrock { | ||
type Param = Vec<f64>; | ||
type Output = f64; | ||
|
||
fn cost(&self, param: &Self::Param) -> Result<Self::Output, Error> { | ||
Ok(rosenbrock(param, self.a, self.b)) | ||
} | ||
} | ||
|
||
impl Anneal for Rosenbrock { | ||
type Param = Vec<f64>; | ||
type Output = Vec<f64>; | ||
type Float = f64; | ||
|
||
fn anneal(&self, param: &Vec<f64>, temp: f64) -> Result<Vec<f64>, Error> { | ||
let mut param_n = param.clone(); | ||
let mut rng = self.rng.lock().unwrap(); | ||
let distr = Uniform::from(0..param.len()); | ||
for _ in 0..(temp.floor() as u64 + 1) { | ||
let idx = rng.sample(distr); | ||
let val = rng.sample(Uniform::new_inclusive(-0.1, 0.1)); | ||
param_n[idx] += val; | ||
param_n[idx] = param_n[idx].clamp(self.lower_bound[idx], self.upper_bound[idx]); | ||
} | ||
Ok(param_n) | ||
} | ||
} | ||
|
||
fn run() -> Result<(), Error> { | ||
let lower_bound: Vec<f64> = vec![-5.0, -5.0]; | ||
let upper_bound: Vec<f64> = vec![5.0, 5.0]; | ||
let operator = Rosenbrock::new(1.0, 100.0, lower_bound, upper_bound); | ||
let init_param: Vec<f64> = vec![1.0, 1.2]; | ||
let temp = 15.0; | ||
let solver = SimulatedAnnealing::new(temp)?.with_temp_func(SATempFunc::Boltzmann); | ||
|
||
let res = Executor::new(operator, solver) | ||
.configure(|state| state.param(init_param).max_iters(10_000_000)) | ||
.add_observer(SlogLogger::term(), ObserverMode::Always) | ||
///////////////////////////////////////////////////////////////////////////////////////// | ||
// // | ||
// Add a timeout of 3 seconds // | ||
// // | ||
///////////////////////////////////////////////////////////////////////////////////////// | ||
.timeout(std::time::Duration::from_secs(3)) | ||
.run()?; | ||
|
||
// Print result | ||
println!("{res}"); | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
if let Err(ref e) = run() { | ||
println!("{e}"); | ||
std::process::exit(1); | ||
} | ||
} |