Skip to main content

streaming/
streaming.rs

1extern crate abc;
2extern crate rand;
3
4use rand::{Rng, thread_rng};
5
6use abc::{Context, Candidate, HiveBuilder, scaling};
7
8#[derive(Clone, Debug)]
9struct Foo;
10
11impl Context for Foo {
12    type Solution = i32;
13
14    fn make(&self) -> i32 {
15        thread_rng().gen_range(0, 100)
16    }
17
18    fn evaluate_fitness(&self, solution: &Self::Solution) -> f64 {
19        let mut x = 0;
20        for _ in 0..1_000 {
21            x += 1;
22        }
23        (x - x) as f64 + *solution as f64
24    }
25
26    fn explore(&self, field: &[Candidate<i32>], n: usize) -> i32 {
27        field[n].solution + thread_rng().gen_range(-10, 10)
28    }
29}
30
31fn main() {
32    let hive = HiveBuilder::<Foo>::new(Foo, 5)
33        .set_threads(5)
34        .set_observers(4)
35        .set_scaling(scaling::power_rank(10_f64));
36    for candidate in hive.build()
37                         .unwrap()
38                         .stream()
39                         .iter()
40                         .skip_while(|c| c.fitness < 200_f64)
41                         .take(5) {
42        println!("{:?}", candidate);
43    }
44}