Struct Hive

Source
pub struct Hive<Ctx: Context> { /* private fields */ }
Expand description

Runs the ABC algorithm, maintaining any necessary state.

Implementations§

Source§

impl<Ctx: Context> Hive<Ctx>

Source

pub fn get(&self) -> AbcResult<MutexGuard<'_, Candidate<Ctx::Solution>>>

Returns a guard for the current best solution found by the hive.

If the hive is running, you should drop the guard returned by this function as soon as convenient, since the logic of the hive can block on the availability of the associated mutex. If you plan on performing expensive computations, you should drop the guard as soon as possible, or acquire and clone it within a small block.

Source

pub fn run_for_rounds( &self, rounds: usize, ) -> AbcResult<Candidate<Ctx::Solution>>

Runs for a fixed number of rounds, then return the best solution found.

If one of the worker threads panics while working, this will return Err(abc::Error). Otherwise, it will return Ok with a Candidate.

Examples found in repository?
examples/max_i32.rs (line 35)
31fn main() {
32    let hive = HiveBuilder::<Foo>::new(Foo, 5)
33        .set_threads(5)
34        .set_scaling(scaling::power_rank(10_f64));
35    println!("{:?}", hive.build().unwrap().run_for_rounds(1_000));
36}
Source

pub fn run_forever(&self) -> AbcResult<()>

Run indefinitely.

If one of the worker threads panics while working, this will return Err(abc::Error). Otherwise, it will return Ok(()).

Source

pub fn stop(&self) -> AbcResult<()>

Stops a running hive.

If a worker thread has panicked, this returns Err(abc::Error).

Source

pub fn set_sender(&mut self, sender: Sender<Candidate<Ctx::Solution>>)

Each new best candidate will be sent to sender.

This is kept in a separate function so that the hive can be borrowed while running.

Source

pub fn get_round(&self) -> AbcResult<Option<usize>>

Returns the current round of a running hive.

If a worker thread has panicked and poisoned the task generator lock, get_round will return Err(abc::Error). If the hive has not been run, get_round will return Ok(None).

If the hive is running, this will return Ok(Some(n)). n will start at 0, and increment each time every task in the round has been claimed (though not necessarily completed) by a worker thread.

Source

pub fn context(&self) -> &Ctx

Get a reference to the hive’s context.

Source§

impl<Ctx: Context + 'static> Hive<Ctx>

Source

pub fn stream(self) -> Receiver<Candidate<Ctx::Solution>>

Runs indefinitely in the background, providing a stream of results.

This method consumes the hive, which will run until the HiveBuilder object is dropped. It returns an mpsc::Receiver, which receives a Candidate each time the hive improves on its best solution.

Examples found in repository?
examples/streaming.rs (line 38)
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}

Trait Implementations§

Source§

impl<Ctx: Context> Debug for Hive<Ctx>
where Ctx::Solution: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl<Ctx: Context> Drop for Hive<Ctx>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<Ctx> !Freeze for Hive<Ctx>

§

impl<Ctx> !RefUnwindSafe for Hive<Ctx>

§

impl<Ctx> Send for Hive<Ctx>

§

impl<Ctx> Sync for Hive<Ctx>

§

impl<Ctx> Unpin for Hive<Ctx>
where Ctx: Unpin, <Ctx as Context>::Solution: Unpin,

§

impl<Ctx> !UnwindSafe for Hive<Ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.