Struct roulette_wheel::RouletteWheel
[−]
[src]
pub struct RouletteWheel<T> { /* fields omitted */ }
A roulette-wheel container
Methods
impl<T> RouletteWheel<T>
[src]
fn new() -> RouletteWheel<T>
create a new empty random-wheel.
Example
use roulette_wheel::RouletteWheel; let rw = RouletteWheel::<u8>::new();
fn with_capacity(cap: usize) -> RouletteWheel<T>
Creates an empty RouletteWheel with space for at least n elements.
Example
use roulette_wheel::RouletteWheel; let rw = RouletteWheel::<u8>::with_capacity(15); assert_eq!(rw.len(), 0);
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted.
The collection may reserve more space to avoid frequent reallocations.
Example
use roulette_wheel::RouletteWheel; let mut rw = RouletteWheel::<u8>::new(); rw.reserve(20); assert_eq!(rw.len(), 0);
fn len(&self) -> usize
Returns the number of elements in the wheel.
Example
use roulette_wheel::RouletteWheel; let rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect(); assert_eq!(rw.len(), 3);
fn is_empty(&self) -> bool
Returns true
if empty else return false
.
Example
use roulette_wheel::RouletteWheel; let empty_rw = RouletteWheel::<u8>::new(); assert_eq!(empty_rw.is_empty(), true); let non_empty_rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect(); assert_eq!(non_empty_rw.is_empty(), false);
fn clear(&mut self)
Remove all elements in this wheel.
Example
use roulette_wheel::RouletteWheel; let mut rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect(); assert_eq!(rw.len(), 3); rw.clear(); assert_eq!(rw.len(), 0);
fn push(&mut self, fitness: f32, individual: T)
Add an element associated with a probability.
Panics
This function might panic if the fitness is less than zero
or if the total fitness gives a non-finite fitness (Inf
).
Example
use roulette_wheel::RouletteWheel; let mut rw = RouletteWheel::new(); rw.push(1.0, 'r'); rw.push(1.0, 'c'); rw.push(1.0, 'a'); assert_eq!(rw.len(), 3);
unsafe fn unchecked_push(&mut self, fitness: f32, individual: T)
Add an element associated with a probability. This unsafe function doesn't check for total fitness overflow nether fitness positivity.
Example
use roulette_wheel::RouletteWheel; let mut rw = RouletteWheel::new(); unsafe { rw.unchecked_push(1.0, 'r') }; unsafe { rw.unchecked_push(1.0, 'c') }; unsafe { rw.unchecked_push(1.0, 'a') }; assert_eq!(rw.len(), 3);
fn total_fitness(&self) -> f32
Returns the sum of all individual fitnesses.
Example
use roulette_wheel::RouletteWheel; let mut rw = RouletteWheel::new(); rw.push(3.0, 'r'); rw.push(2.0, 'c'); rw.push(1.5, 'a'); assert_eq!(rw.total_fitness(), 6.5);
fn select_iter(&self) -> SelectIter<ThreadRng, T>
Returns an iterator over the RouletteWheel.
Examples
use roulette_wheel::RouletteWheel; let rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect(); let mut iterator = rw.select_iter(); assert_eq!(iterator.next(), Some((0.5, &20))); assert_eq!(iterator.next(), Some((0.1, &10))); assert_eq!(iterator.next(), Some((0.2, &15))); assert_eq!(iterator.next(), None);
Trait Implementations
impl<T: Clone> Clone for RouletteWheel<T>
[src]
fn clone(&self) -> RouletteWheel<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T> FromIterator<(f32, T)> for RouletteWheel<T>
[src]
fn from_iter<A>(iter: A) -> Self where
A: IntoIterator<Item = (f32, T)>,
A: IntoIterator<Item = (f32, T)>,
Creates a value from an iterator. Read more