Skip to content

Commit

Permalink
use R for Random and make default nondeterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
jonboh committed Jan 6, 2024
1 parent 50ec33f commit 63f01cf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion argmin-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub trait ArgminInv<T> {
/// Create a random number
pub trait ArgminRandom {
/// Get a random element between min and max,
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> Self;
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> Self;
}

/// Minimum and Maximum of type `T`
Expand Down
2 changes: 1 addition & 1 deletion argmin-math/src/nalgebra_m/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
DefaultAllocator: Allocator<N, R, C>,
{
#[inline]
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> OMatrix<N, R, C> {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> OMatrix<N, R, C> {
assert!(!min.is_empty());
assert_eq!(min.shape(), max.shape());

Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/ndarray_m/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::ArgminRandom;
macro_rules! make_random {
($t:ty) => {
impl ArgminRandom for ndarray::Array1<$t> {
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> ndarray::Array1<$t> {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> ndarray::Array1<$t> {
assert!(!min.is_empty());
assert_eq!(min.len(), max.len());

Expand All @@ -33,7 +33,7 @@ macro_rules! make_random {
}

impl ArgminRandom for ndarray::Array2<$t> {
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> ndarray::Array2<$t> {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> ndarray::Array2<$t> {
assert!(!min.is_empty());
assert_eq!(min.raw_dim(), max.raw_dim());

Expand Down
2 changes: 1 addition & 1 deletion argmin-math/src/primitives/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! make_random {
($t:ty) => {
impl ArgminRandom for $t {
#[inline]
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> $t {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> $t {
rng.gen_range(*min..*max)
}
}
Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/vec/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::Rng;
macro_rules! make_random {
($t:ty) => {
impl ArgminRandom for Vec<$t> {
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> Vec<$t> {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> Vec<$t> {
assert!(!min.is_empty());
assert_eq!(min.len(), max.len());

Expand All @@ -35,7 +35,7 @@ macro_rules! make_random {
}

impl ArgminRandom for Vec<Vec<$t>> {
fn rand_from_range<G: Rng>(min: &Self, max: &Self, rng: &mut G) -> Vec<Vec<$t>> {
fn rand_from_range<R: Rng>(min: &Self, max: &Self, rng: &mut R) -> Vec<Vec<$t>> {
assert!(!min.is_empty());
assert_eq!(min.len(), max.len());
min.iter()
Expand Down
20 changes: 10 additions & 10 deletions argmin/src/solver/particleswarm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use serde::{Deserialize, Serialize};
/// \[1\] <https://en.wikipedia.org/wiki/Particle_swarm_optimization>
#[derive(Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct ParticleSwarm<P, F, G> {
pub struct ParticleSwarm<P, F, R> {
/// Inertia weight
weight_inertia: F,
/// Cognitive acceleration coefficient
Expand All @@ -63,7 +63,7 @@ pub struct ParticleSwarm<P, F, G> {
/// Number of particles
num_particles: usize,
/// Random number generator
rng_generator: G,
rng_generator: R,
}

impl<P, F> ParticleSwarm<P, F, rand::rngs::StdRng>
Expand Down Expand Up @@ -103,15 +103,15 @@ where
weight_social: float!(0.5 + 2.0f64.ln()),
bounds,
num_particles,
rng_generator: rand::rngs::StdRng::seed_from_u64(42),
rng_generator: rand::rngs::StdRng::from_entropy(),
}
}
}
impl<P, F, G0> ParticleSwarm<P, F, G0>
impl<P, F, R0> ParticleSwarm<P, F, R0>
where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
G0: Rng,
R0: Rng,
{
/// Set the random number generator
///
Expand All @@ -131,7 +131,7 @@ where
/// # Ok(())
/// # }
/// ```
pub fn with_rng_generator<G1: Rng>(self, generator: G1) -> ParticleSwarm<P, F, G1> {
pub fn with_rng_generator<R1: Rng>(self, generator: R1) -> ParticleSwarm<P, F, R1> {
ParticleSwarm {
weight_inertia: self.weight_inertia,
weight_cognitive: self.weight_cognitive,
Expand All @@ -143,11 +143,11 @@ where
}
}

impl<P, F, G> ParticleSwarm<P, F, G>
impl<P, F, R> ParticleSwarm<P, F, R>
where
P: Clone + SyncAlias + ArgminSub<P, P> + ArgminMul<F, P> + ArgminRandom + ArgminZeroLike,
F: ArgminFloat,
G: Rng,
R: Rng,
{
/// Set inertia factor on particle velocity
///
Expand Down Expand Up @@ -276,7 +276,7 @@ where
}
}

impl<O, P, F, G> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F, G>
impl<O, P, F, R> Solver<O, PopulationState<Particle<P, F>, F>> for ParticleSwarm<P, F, R>
where
O: CostFunction<Param = P, Output = F> + SyncAlias,
P: SerializeAlias
Expand All @@ -289,7 +289,7 @@ where
+ ArgminRandom
+ ArgminMinMax,
F: ArgminFloat,
G: Rng,
R: Rng,
{
const NAME: &'static str = "Particle Swarm Optimization";

Expand Down

0 comments on commit 63f01cf

Please sign in to comment.