Skip to content

Error "cannot sample empty range" on very basic ParticleSwarm #631

Description

@sffc

Repro:

    #[test]
    fn test_simple_particle_swarm() {
        struct CostFn;
        impl CostFunction for CostFn {
            type Param = f64;
            type Output = f64;
            fn cost(&self, param: &f64) -> Result<f64, Error> {
                Ok(*param)
            }
        }
        let solver = ParticleSwarm::new((-5.0, 5.0), 15);
        let res = Executor::new(CostFn, solver)
            .configure(|state| state.max_iters(10))
            .run()
            .unwrap();
        println!("{res}");
    }

When I run this code, it immediately fails with the message

thread 'cal::chinese_internal::simple::test::test_simple_particle_swarm' panicked at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rand-0.8.5/src/rng.rs:134:9:
cannot sample empty range
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:75:14
   2: core::panicking::panic
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:145:5
   3: rand::rng::Rng::gen_range
             at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rand-0.8.5/src/rng.rs:134:9
   4: argmin_math::primitives::random::<impl argmin_math::ArgminRandom for f64>::rand_from_range
             at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/argmin-math-0.4.0/src/primitives/random.rs:16:21
   5: <argmin::solver::particleswarm::ParticleSwarm<P,F,R> as argmin::core::solver::Solver<O,argmin::core::state::populationstate::PopulationState<argmin::solver::particleswarm::Particle<P,F>,F>>>::next_iter::{{closure}}
             at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/argmin-0.10.0/src/solver/particleswarm/mod.rs:364:21
   6: core::iter::adapters::map::map_fold::{{closure}}
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/iter/adapters/map.rs:88:28
   7: <core::slice::iter::IterMut<T> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/slice/iter/macros.rs:255:27
   8: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/iter/adapters/map.rs:128:19
   9: core::iter::traits::iterator::Iterator::for_each
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/iter/traits/iterator.rs:827:14
  10: alloc::vec::Vec<T,A>::extend_trusted
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/alloc/src/vec/mod.rs:3774:26
  11: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/alloc/src/vec/spec_extend.rs:29:14
  12: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/alloc/src/vec/spec_from_iter_nested.rs:62:16
  13: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/alloc/src/vec/spec_from_iter.rs:34:9
  14: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/alloc/src/vec/mod.rs:3633:9
  15: core::iter::traits::iterator::Iterator::collect
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/iter/traits/iterator.rs:2027:9
  16: <argmin::solver::particleswarm::ParticleSwarm<P,F,R> as argmin::core::solver::Solver<O,argmin::core::state::populationstate::PopulationState<argmin::solver::particleswarm::Particle<P,F>,F>>>::next_iter
             at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/argmin-0.10.0/src/solver/particleswarm/mod.rs:380:14
  17: argmin::core::executor::Executor<O,S,I>::run
             at /Users/sffc/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/argmin-0.10.0/src/core/executor.rs:221:45
  18: icu_calendar::cal::chinese_internal::simple::test::test_simple_particle_swarm
             at ./src/cal/chinese/simple.rs:237:14
  19: icu_calendar::cal::chinese_internal::simple::test::test_simple_particle_swarm::{{closure}}
             at ./src/cal/chinese/simple.rs:225:36
  20: core::ops::function::FnOnce::call_once
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/ops/function.rs:253:5
  21: core::ops::function::FnOnce::call_once
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/ops/function.rs:253:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions