pub struct PermuPopulation<T> {
pub population: Vec<Permutation<T>>,
pub size: usize,
}Expand description
Population of Permutations.
Fields§
§population: Vec<Permutation<T>>§size: usizeImplementations§
Source§impl<T> PermuPopulation<T>
impl<T> PermuPopulation<T>
Sourcepub fn from_vec(vec: Vec<Permutation<T>>) -> PermuPopulation<T>
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);Sourcepub fn zeros(size: usize, length: usize) -> PermuPopulation<T>
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);Sourcepub fn identity(size: usize, length: usize) -> PermuPopulation<T>
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);Sourcepub fn random(size: usize, length: usize) -> PermuPopulation<T>
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 checkSourcepub fn invert(&mut self)
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);Sourcepub fn central_permu(&self) -> Permutation<T>
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>
impl<T: Clone> Clone for PermuPopulation<T>
Source§fn clone(&self) -> PermuPopulation<T>
fn clone(&self) -> PermuPopulation<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for PermuPopulation<T>
impl<T: Debug> Debug for PermuPopulation<T>
Source§impl<T> Display for PermuPopulation<T>where
T: Debug,
impl<T> Display for PermuPopulation<T>where
T: Debug,
Source§impl<T: PartialEq> PartialEq for PermuPopulation<T>
impl<T: PartialEq> PartialEq for PermuPopulation<T>
Source§impl<T> Population<T> for PermuPopulation<T>
impl<T> Population<T> for PermuPopulation<T>
Source§fn learn(&self) -> Distribution
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>
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>
fn to_permus(&self, permus: &mut PermuPopulation<T>) -> Result<(), Error>
PermuPopulation with the permutation vector
representation of the current population .Source§fn from_permus(&mut self, permus: &PermuPopulation<T>) -> Result<(), Error>
fn from_permus(&mut self, permus: &PermuPopulation<T>) -> Result<(), Error>
PermuPopulation into the current Population’s representation.