-
Notifications
You must be signed in to change notification settings - Fork 4
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
24 changed files
with
1,013 additions
and
839 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Gomez tests | ||
name: CI | ||
|
||
on: | ||
push: | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Gomez publish | ||
name: Publish | ||
|
||
on: | ||
push: | ||
|
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,75 @@ | ||
use fastrand::Rng; | ||
use gomez::nalgebra as na; | ||
use gomez::{Domain, Function, Optimizer, OptimizerDriver, Problem, Sample}; | ||
use na::{storage::StorageMut, Dyn, IsContiguous, Vector}; | ||
|
||
struct Random { | ||
rng: Rng, | ||
} | ||
|
||
impl Random { | ||
fn new(rng: Rng) -> Self { | ||
Self { rng } | ||
} | ||
} | ||
|
||
impl<F: Function> Optimizer<F> for Random | ||
where | ||
F::Field: Sample, | ||
{ | ||
const NAME: &'static str = "Random"; | ||
type Error = std::convert::Infallible; | ||
|
||
fn opt_next<Sx>( | ||
&mut self, | ||
f: &F, | ||
dom: &Domain<F::Field>, | ||
x: &mut Vector<F::Field, Dyn, Sx>, | ||
) -> Result<F::Field, Self::Error> | ||
where | ||
Sx: StorageMut<F::Field, Dyn> + IsContiguous, | ||
{ | ||
// Randomly sample in the domain. | ||
dom.sample(x, &mut self.rng); | ||
|
||
// We must compute the value. | ||
Ok(f.apply(x)) | ||
} | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/Rosenbrock_function | ||
struct Rosenbrock { | ||
a: f64, | ||
b: f64, | ||
} | ||
|
||
impl Problem for Rosenbrock { | ||
type Field = f64; | ||
|
||
fn domain(&self) -> Domain<Self::Field> { | ||
Domain::rect(vec![-10.0, -10.0], vec![10.0, 10.0]) | ||
} | ||
} | ||
|
||
impl Function for Rosenbrock { | ||
fn apply<Sx>(&self, x: &na::Vector<Self::Field, Dyn, Sx>) -> Self::Field | ||
where | ||
Sx: na::Storage<Self::Field, Dyn> + IsContiguous, | ||
{ | ||
(self.a - x[0]).powi(2) + self.b * (x[1] - x[0].powi(2)).powi(2) | ||
} | ||
} | ||
|
||
fn main() { | ||
let f = Rosenbrock { a: 1.0, b: 1.0 }; | ||
let mut optimizer = OptimizerDriver::builder(&f) | ||
.with_algo(|_, _| Random::new(Rng::new())) | ||
.build(); | ||
|
||
optimizer | ||
.find(|state| { | ||
println!("f(x) = {}\tx = {:?}", state.fx(), state.x()); | ||
state.iter() >= 100 | ||
}) | ||
.unwrap(); | ||
} |
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,53 @@ | ||
use gomez::nalgebra as na; | ||
use gomez::{Domain, Function, OptimizerDriver, Problem}; | ||
use na::{Dyn, IsContiguous}; | ||
|
||
// https://en.wikipedia.org/wiki/Rosenbrock_function | ||
struct Rosenbrock { | ||
a: f64, | ||
b: f64, | ||
} | ||
|
||
impl Problem for Rosenbrock { | ||
type Field = f64; | ||
|
||
fn domain(&self) -> Domain<Self::Field> { | ||
Domain::unconstrained(2) | ||
} | ||
} | ||
|
||
impl Function for Rosenbrock { | ||
fn apply<Sx>(&self, x: &na::Vector<Self::Field, Dyn, Sx>) -> Self::Field | ||
where | ||
Sx: na::Storage<Self::Field, Dyn> + IsContiguous, | ||
{ | ||
(self.a - x[0]).powi(2) + self.b * (x[1] - x[0].powi(2)).powi(2) | ||
} | ||
} | ||
|
||
fn main() -> Result<(), String> { | ||
let f = Rosenbrock { a: 1.0, b: 1.0 }; | ||
let mut optimizer = OptimizerDriver::builder(&f) | ||
.with_initial(vec![10.0, -5.0]) | ||
.build(); | ||
|
||
let tolerance = 1e-6; | ||
|
||
let (_, value) = optimizer | ||
.find(|state| { | ||
println!( | ||
"iter = {}\tf(x) = {}\tx = {:?}", | ||
state.iter(), | ||
state.fx(), | ||
state.x() | ||
); | ||
state.fx() <= tolerance || state.iter() >= 100 | ||
}) | ||
.map_err(|error| format!("{error}"))?; | ||
|
||
if value <= tolerance { | ||
Ok(()) | ||
} else { | ||
Err("did not converge".to_string()) | ||
} | ||
} |
Oops, something went wrong.