argmin-rs / argmin

Numerical optimization in pure Rust
http://argmin-rs.org
Apache License 2.0
1k stars 79 forks source link

ParticleSwarm is not reproducible #385

Closed rashidalabri closed 9 months ago

rashidalabri commented 9 months ago

Currently, the initial positions of the particles in ParticleSwarm are acquired through the rand_from_range function from the ArgminRandom trait. The implementation of the trait for standard types uses rand::thread_rng. Since there is no way to set a seed, we cannot reproduce results.

I think it would be really helpful to find a way to allow users to reproduce results. Perhaps we can let users set the initial positions and velocities? As long as the initial positions are the same, every step after should be deterministic.

I'd be happy to create a PR for this!

stefan-k commented 9 months ago

Thanks for the report! Luckily, this was fixed yesterday in #383. ArgminRandom::rand_from_range now accepts a RNG with a seed of choice. The PSO code was adapted accordingly and you can set the RNG with this method. Note that these changes aren't published yet.

Perhaps we can let users set the initial positions and velocities? As long as the initial positions are the same, every step after should be deterministic.

This is not quite correct, because every iteration involves randomness. Btw, it is possible to set an initial population via PopulationState::population, like this:

let res = Executor::new(cost_function, solver)
    .configure(|state| state.population(your_initial_population))
    .run()?;

It is not possible to set the initial velocities. If there is a use case which requires this, we can certainly discuss it.

rashidalabri commented 9 months ago

That's awesome! Thanks for the clarification Stefan. Any idea of when it might be published?

stefan-k commented 9 months ago

Unfortunately, since a new release came out a couple of days ago I'm afraid it won't be very soon.