pub struct RimPopulation<T> {
pub population: Vec<Rim<T>>,
pub size: usize,
}Expand description
Population of Rim vectors.
Fields§
§population: Vec<Rim<T>>§size: usizeImplementations§
Source§impl<T> RimPopulation<T>
impl<T> RimPopulation<T>
Sourcepub fn from_vec(vec: &Vec<Vec<T>>) -> Result<RimPopulation<T>, Error>
pub fn from_vec(vec: &Vec<Vec<T>>) -> Result<RimPopulation<T>, Error>
Creates an InversionPopulation based on a given matrix.
§Errors
Returns a LengthError if the length of all vectors is not equal.
§Example
use permu_rs::rim::RimPopulation;
let pop_matrix: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,2,0,0], vec![0,0,0,0]];
let pop = RimPopulation::from_vec(&pop_matrix).unwrap();
println!("{}", pop);
// Now, the second vector contais one item less
let pop_matrix: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,0,0], vec![0,0,0,0]];
let pop = RimPopulation::from_vec(&pop_matrix); // This should return a LengthError
assert!(pop.is_err());Sourcepub fn zeros(size: usize, length: usize) -> RimPopulation<T>
pub fn zeros(size: usize, length: usize) -> RimPopulation<T>
Creates a RimPopulation of zero valued Rim vectors of the size and length given.
§Example
use permu_rs::rim::RimPopulation;
let (size, length) = (7, 5);
let pop = RimPopulation::<u8>::zeros(size, length);
println!("{}", pop);Sourcepub fn to_permus(&self, permu_pop: &mut PermuPopulation<T>) -> Result<(), Error>
pub fn to_permus(&self, permu_pop: &mut PermuPopulation<T>) -> Result<(), Error>
Takes a PermuPopulation as an output and fills this population with the Permutation
representation of each Rimvector in the RimPopulation. RimPopulation to its
Permutation representation. Positions of vectors are respected.
§Errors
Returns a LengthError if the size of both population isn’t equal or the length
of the Permutations isn’t the length of the Rim vectors + 1.
§Example
use permu_rs::{
permutation::{ Permutation, PermuPopulation },
rim::RimPopulation };
let (size, length) = (10, 5);
let rim_zeros = RimPopulation::<u16>::zeros(size, length-1);
let mut permus = PermuPopulation::<u16>::random(size, length);
// The output should look like this
let target = PermuPopulation::<u16>::from_vec(vec![
Permutation::<u16>::from_vec(vec![4,3,2,1,0]).unwrap();size]);
// Convert the rim population to its permutation representation
rim_zeros.to_permus(&mut permus).unwrap();
assert_eq!(target, permus);Sourcepub fn from_permus(
permu_pop: &PermuPopulation<T>,
rim_pop: &mut RimPopulation<T>,
) -> Result<(), Error>
pub fn from_permus( permu_pop: &PermuPopulation<T>, rim_pop: &mut RimPopulation<T>, ) -> Result<(), Error>
Fills a RimPopulation with the rim vector representation of each
permutation vector inside a given PermuPopulation. Note that the sizes
of both populations must match and the length of permutations must be
equal to the length of the rim vectors + 1.
§Errors
Returns a LengthError if the size of both population isn’t equal or the length
of the Permutations isn’t the length of the Rim vectors + 1.
§Example
use permu_rs::{
permutation::PermuPopulation,
rim::RimPopulation,
};
// Create a target population of random permutations
let mut permus = PermuPopulation::<u8>::random(10, 5);
let target = permus.clone();
// Init rim population
let mut rims = RimPopulation::<u8>::zeros(10, 4);
// Convert the permutations into rim vectors and then recover the
// original permutations from the rim vectors.
RimPopulation::from_permus(&permus, &mut rims).unwrap();
RimPopulation::to_permus(&rims, &mut permus).unwrap();
assert_eq!(target, permus);Trait Implementations§
Source§impl<T: Clone> Clone for RimPopulation<T>
impl<T: Clone> Clone for RimPopulation<T>
Source§fn clone(&self) -> RimPopulation<T>
fn clone(&self) -> RimPopulation<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 RimPopulation<T>
impl<T: Debug> Debug for RimPopulation<T>
Source§impl<T> Display for RimPopulation<T>where
T: Debug,
impl<T> Display for RimPopulation<T>where
T: Debug,
Source§impl<T: PartialEq> PartialEq for RimPopulation<T>
impl<T: PartialEq> PartialEq for RimPopulation<T>
Source§impl<T> Population<T> for RimPopulation<T>
impl<T> Population<T> for RimPopulation<T>
Source§fn learn(&self) -> Distribution
fn learn(&self) -> Distribution
Implementation of learn method for RimPopulation.
§Example
use permu_rs::{Distribution, Population, rim::RimPopulation};
// Init a population of custom rim vectors
let pop: Vec<Vec<u8>> = vec![vec![2,1,0], vec![1,0,0], vec![0,0,0]];
let pop = RimPopulation::from_vec(&pop).unwrap();
// Cratethe target distribution for the created rim population
let target = vec![vec![1,1,1,0],vec![2,1,0,0],vec![3,0,0,0]];
let target = Distribution::RimDistribution(target, false);
let distr = pop.learn();
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 RimPopulation.
§Example
use permu_rs::{Distribution, Population, rim::RimPopulation};
// Init a population of custom rim vectors
let pop: Vec<Vec<u8>> = vec![vec![2,1,0], vec![1,0,0], vec![0,0,0]];
let pop = RimPopulation::from_vec(&pop).unwrap();
// Init a population to store the samples
let mut samples = RimPopulation::<u8>::zeros(7, 3);
let mut distr = pop.learn();
println!("Distribution:\n{}", distr);
samples.sample(&mut distr).unwrap();
println!("Distribution after sampling:\n{}", distr);
println!("Original population:\n{}", pop);
println!("Sampled population:\n{}", 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.