Struct Group

Source
pub struct Group<T, FI, SI, RI, const N: usize, const R: usize>{ /* private fields */ }
Expand description

A group which contains GenerationIndividual.

This is implmented GroupTrait. Process three steps of one_cycle_with_output based on FitnessIndividual, SelectorIndividual, and ReplenisherIndividual of GenerationIndividual.

§Example

use scarlet_queen_core::{GroupTrait, InitializerTrait};
use scarlet_queen_fitness::ord::GeFitness;
use scarlet_queen_replenisher::TournamentReplenisherIndividual;
use scarlet_queen_selector::TournamentSelectorIndividual;
use scarlet_queen_generation::{Group, ResultOut};

struct InitializerSample {}
impl<const N: usize> InitializerTrait<u8, N> for InitializerSample {
    fn initialize() -> [u8; N] {
        let mut i: u8 = 0;
        [0; N]
            .map(|_| {
                i += 2;
                i - 2
            })
    }
}

type GroupSample<const N: usize, const R: usize> = Group<u8, GeFitness<u8>, TournamentSelectorIndividual<u8, R>, TournamentReplenisherIndividual<u8, N, R>, N, R>;
let mut group: GroupSample<5, 4> = GroupSample::init::<InitializerSample>();

let output: ResultOut<u8> = group.one_cycle_with_output().unwrap();

assert_eq!(serde_json::to_string(&output).unwrap(), r#"{
    "individuals_and_scores": [
        {
            "individual": {
                "id": 4,
                "value": "8"
            },
            "score": 4
        },
        {
            "individual": {
                "id": 3,
                "value": "6"
            },
            "score": 3
        },
        {
            "individual": {
                "id": 2,
                "value": "4"
            },
            "score": 2
        },
        {
            "individual": {
                "id": 1,
                "value": "2"
            },
            "score": 1
        },
        {
            "individual": {
                "id": 0,
                "value": "0"
            },
            "score": 0
        }
    ],
    "new_individuals": [
        {
            "id": 4,
            "value": "8"
        },
        {
            "id": 3,
            "value": "6"
        },
        {
            "id": 2,
            "value": "4"
        },
        {
            "id": 1,
            "value": "2"
        },
        {
            "id": 0,
            "value": "8"
        }
    ]
}"#.to_string().chars().filter_map(|c| if c.is_whitespace() { None } else { Some(c) }).collect::<String>());
assert_eq!(
    group.clone_values(),
    vec![8u8, 6, 4, 2, 8]
);

Trait Implementations§

Source§

impl<T, FI, SI, RI, const N: usize, const R: usize> Debug for Group<T, FI, SI, RI, N, R>

Source§

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

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

impl<T, FI, SI, RI, const N: usize, const R: usize> GroupTrait<T, N, R> for Group<T, FI, SI, RI, N, R>

Source§

type Err = GenerationError

An error of cycle.
Source§

type Out = ResultOut<T>

An output of cycle.
Source§

fn new(data: [T; N]) -> Group<T, FI, SI, RI, N, R>

Create Self from an array. The return value is already assigned a number. Read more
Source§

fn one_cycle_with_output( &mut self, ) -> Result<<Group<T, FI, SI, RI, N, R> as GroupTrait<T, N, R>>::Out, <Group<T, FI, SI, RI, N, R> as GroupTrait<T, N, R>>::Err>

Run one cycle with outputing and update individuals. The elements of self must be already assigned a number before calling this method.
Source§

fn iter<'a>(&'a self) -> impl Iterator<Item = &'a Individual<T>>
where T: 'a,

Create an iterator of individuals. Read more
Source§

fn init<I>() -> Self
where I: InitializerTrait<T, N>, Self: Sized,

Initialize Self by I algorithm. The elements of self must be already assigned a number. Read more
Source§

fn reset_id(&self)

Assign numbers to individuals in order.
Source§

fn clone_values(&self) -> Vec<T>
where T: Clone,

Clone individuals of this group.
Source§

impl<'a, T, FI, SI, RI, const N: usize, const R: usize> IntoIterator for &'a Group<T, FI, SI, RI, N, R>

Source§

type IntoIter = Iter<'a, GenerationIndividual<T, FI, SI, RI, N, R>>

Which kind of iterator are we turning this into?
Source§

type Item = &'a GenerationIndividual<T, FI, SI, RI, N, R>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> <&'a Group<T, FI, SI, RI, N, R> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, FI, SI, RI, const N: usize, const R: usize> PartialEq for Group<T, FI, SI, RI, N, R>

Source§

fn eq(&self, other: &Group<T, FI, SI, RI, N, R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, FI, SI, RI, const N: usize, const R: usize> Eq for Group<T, FI, SI, RI, N, R>

Source§

impl<T, FI, SI, RI, const N: usize, const R: usize> StructuralPartialEq for Group<T, FI, SI, RI, N, R>

Auto Trait Implementations§

§

impl<T, FI, SI, RI, const N: usize, const R: usize> Freeze for Group<T, FI, SI, RI, N, R>

§

impl<T, FI, SI, RI, const N: usize, const R: usize> !RefUnwindSafe for Group<T, FI, SI, RI, N, R>

§

impl<T, FI, SI, RI, const N: usize, const R: usize> !Send for Group<T, FI, SI, RI, N, R>

§

impl<T, FI, SI, RI, const N: usize, const R: usize> !Sync for Group<T, FI, SI, RI, N, R>

§

impl<T, FI, SI, RI, const N: usize, const R: usize> Unpin for Group<T, FI, SI, RI, N, R>
where FI: Unpin, SI: Unpin, RI: Unpin,

§

impl<T, FI, SI, RI, const N: usize, const R: usize> !UnwindSafe for Group<T, FI, SI, RI, N, R>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V