PermuPopulation

Struct PermuPopulation 

Source
pub struct PermuPopulation<T> {
    pub population: Vec<Permutation<T>>,
    pub size: usize,
}
Expand description

Population of Permutations.

Fields§

§population: Vec<Permutation<T>>§size: usize

Implementations§

Source§

impl<T> PermuPopulation<T>

Source

pub fn from_vec(vec: Vec<Permutation<T>>) -> PermuPopulation<T>

Returns a PermuPopulation created from a vector of Permutation.

§Example
use permu_rs::permutation::{Permutation, PermuPopulation};
let vec = vec![Permutation::identity(5),
               Permutation::random(5)];
let pop = PermuPopulation::<u8>::from_vec(vec);
assert_eq!(2, pop.size);
Source

pub fn zeros(size: usize, length: usize) -> PermuPopulation<T>

Returns a PermuPopulation of the size given with Permutations filled with zeros . The permutation’s length must be specified.

§Example
use permu_rs::permutation::PermuPopulation;

// Creates a population of 10 permutations with length 20
let pop : PermuPopulation<u8> = PermuPopulation::zeros(10, 20);

println!("Zeros population:\n{}", pop);
Source

pub fn identity(size: usize, length: usize) -> PermuPopulation<T>

Creates a PermuPopulation of identity Permutations. The number of Permutations in the returned PermuPopulation is given by size parameter and the length of Permutations is length.

§Example
use permu_rs::permutation as permu;

let population = permu::PermuPopulation::<u8>::identity(10, 5);
population.population.iter()
    .for_each(|p| assert_eq!(*p, permu::Permutation::<u8>::identity(5)));

println!("Identity population:\n{}", population);
Source

pub fn random(size: usize, length: usize) -> PermuPopulation<T>

Initializes a PermuPopulation of random Permutations of the size and length given.

§Example
use permu_rs::permutation::PermuPopulation;

let pop : PermuPopulation<u8> = PermuPopulation::random(10, 5);
pop.population.iter().for_each(|p| assert!(p.is_permu())); // All permutations

assert_eq!(pop.size, pop.population.len()); // PermuPopulation size check
Source

pub fn invert(&mut self)

Replaces all individuals from the PermuPopulation with its respective inverted PermuPopulation. For more info, check Permutation’s invert method.

§Panics

The function panics if an error occurs when running invert method to any Permutationof the PermuPopulation.

§Example
use permu_rs::permutation::*;

// Init populations
let permu = Permutation::<u8>::from_vec(vec![0,2,3,1]).unwrap();
let mut pop = PermuPopulation::from_vec(vec![permu]); 
println!("initial pop: {:?}", pop);

let target = Permutation::<u8>::from_vec(vec![0,3,1,2]).unwrap();
let target_pop = PermuPopulation::from_vec(vec![target]); 

// Calculate the inverted permutation of `permu`
pop.invert();

println!("inverted pop: {:?}", pop);
assert_eq!(target_pop, pop);
Source

pub fn central_permu(&self) -> Permutation<T>

Returns the central perutation of the current PermuPopulation. The central permutation is calculated with the Borda algorithm.

§Example
use permu_rs::permutation::*;

let pop = PermuPopulation::<u8>::from_vec(vec![
    Permutation::from_vec(vec![0, 1, 2]).unwrap(),
    Permutation::from_vec(vec![0, 2, 1]).unwrap(),
    Permutation::from_vec(vec![1, 0, 2]).unwrap(),
    Permutation::from_vec(vec![1, 2, 0]).unwrap(),
    Permutation::from_vec(vec![2, 0, 1]).unwrap(),
    Permutation::from_vec(vec![2, 1, 0]).unwrap(),
]);

let target = Permutation::<u8>::from_vec(vec![0, 1, 2]).unwrap();

let central = pop.central_permu();
assert_eq!(target, central);

Trait Implementations§

Source§

impl<T: Clone> Clone for PermuPopulation<T>

Source§

fn clone(&self) -> PermuPopulation<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for PermuPopulation<T>

Source§

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

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

impl<T> Display for PermuPopulation<T>
where T: Debug,

Source§

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

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

impl<T: PartialEq> PartialEq for PermuPopulation<T>

Source§

fn eq(&self, other: &PermuPopulation<T>) -> 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> Population<T> for PermuPopulation<T>

Source§

fn learn(&self) -> Distribution

Implementation of learn method for PermuPopulation.

§Example
use permu_rs::{Population, Distribution};
use permu_rs::permutation::{PermuPopulation, Permutation};

let v = vec![Permutation::<u8>::from_vec_unsec(vec![0,1,2,3]),
             Permutation::<u8>::from_vec_unsec(vec![1,2,0,3])];
let pop = PermuPopulation::from_vec(v); 
let distr = pop.learn();

let target = vec![vec![1,1,0,0],
                  vec![0,1,1,0],
                  vec![1,0,1,0],
                  vec![0,0,0,2]];

let target = Distribution::PermuDistribution(target, false);
assert_eq!(target, distr);
Source§

fn sample(&mut self, distr: &mut Distribution) -> Result<(), Error>

Implementation of sample method for PermuPopulation.

§Errors

Returns a LengthError if the length of the output population’s Permutations length is not equal to its population Permutation’s. Returns an IncorrectDistrType error if the given distribution is not PermuDistribution.

§Example
use permu_rs::permutation::PermuPopulation;
use permu_rs::{Population, Distribution};

let pop = PermuPopulation::<u8>::random(1, 5); // Population to learn from
let mut samples = PermuPopulation::<u8>::zeros(10, 5); // Population to fill with samples
 
let mut distr = pop.learn();

samples.sample(&mut distr).unwrap();

println!("{}", samples);
Source§

fn to_permus(&self, permus: &mut PermuPopulation<T>) -> Result<(), Error>

Fills the given PermuPopulation with the permutation vector representation of the current population .
Source§

fn from_permus(&mut self, permus: &PermuPopulation<T>) -> Result<(), Error>

Maps a given PermuPopulation into the current Population’s representation.
Source§

impl<T> StructuralPartialEq for PermuPopulation<T>

Auto Trait Implementations§

§

impl<T> Freeze for PermuPopulation<T>

§

impl<T> RefUnwindSafe for PermuPopulation<T>
where T: RefUnwindSafe,

§

impl<T> Send for PermuPopulation<T>
where T: Send,

§

impl<T> Sync for PermuPopulation<T>
where T: Sync,

§

impl<T> Unpin for PermuPopulation<T>
where T: Unpin,

§

impl<T> UnwindSafe for PermuPopulation<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.