pub struct InversionPopulation<T> {
pub population: Vec<Inversion<T>>,
pub size: usize,
}
Expand description
Population of Inversion objects. Includes initilializers and transformation tools.
Fields§
§population: Vec<Inversion<T>>
§size: usize
Implementations§
Source§impl<T> InversionPopulation<T>
impl<T> InversionPopulation<T>
Sourcepub fn from_vec(vec: &Vec<Vec<T>>) -> Result<InversionPopulation<T>, Error>
pub fn from_vec(vec: &Vec<Vec<T>>) -> Result<InversionPopulation<T>, Error>
Creates an InversionPopulation
based on a given matrix.
§Example
use permu_rs::inversion::InversionPopulation;
let pop: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,2,0,0], vec![0,0,0,0]];
let pop = InversionPopulation::from_vec(&pop).unwrap();
println!("{}", pop);
// Now, the seond vector contais one item less
let pop: Vec<Vec<u16>> = vec![vec![0,2,0,0], vec![1,0,0], vec![0,0,0,0]];
let pop = InversionPopulation::from_vec(&pop); // This should return a LengthError
assert!(pop.is_err());
Sourcepub fn zeros(size: usize, length: usize) -> InversionPopulation<T>
pub fn zeros(size: usize, length: usize) -> InversionPopulation<T>
Creates a InversionPopulation
of the size given with Inversion
s of length specified, filled with 0s.
This population represents a population of identity permutations.
§Example
use permu_rs::*;
use permutation::{Permutation, PermuPopulation};
use inversion::{Inversion, InversionPopulation};
let (size, length) = (20,10);
let identity = PermuPopulation::from_vec(vec![Permutation::<u8>::identity(length);size]);
let inversions = InversionPopulation::<u8>::zeros(size,length-1);
let mut permus = PermuPopulation::<u8>::zeros(size, length);
inversions.to_permus(&mut permus);
assert_eq!(identity, permus);
println!("Zeros or identity population\n{}", inversions);
Sourcepub fn to_permus(&self, permu_pop: &mut PermuPopulation<T>) -> Result<(), Error>
pub fn to_permus(&self, permu_pop: &mut PermuPopulation<T>) -> Result<(), Error>
Transforms the Inversion
to its Permutation
representation. Fills a given PermuPopulation
based on the Inversion
s from the InversionPopulation
. The Inversion
-> Permutation
transformation is
done respecting the positions in the population.
§Errors
Returns a LengthError
if the size of both Populations
are not equal.
§Panics
The mothod will panic if a Inversion
of the InversionPopulation
has not a Permutation
representation.
§Example
use permu_rs::*;
let (size, length) = (5, 10);
let mut out_pop = permutation::PermuPopulation::<u8>::zeros(size, length); // Output permutation
let identity_pop = permutation::PermuPopulation::<u8>::identity(size, length);
let inversions = inversion::InversionPopulation::<u8>:: zeros(size, length-1);
inversions.to_permus(&mut out_pop);
assert_eq!(out_pop, identity_pop);
println!("{}\n", inversions);
println!("{}", out_pop);
Sourcepub fn from_permus(
permu_pop: &PermuPopulation<T>,
inversions: &mut InversionPopulation<T>,
) -> Result<(), Error>
pub fn from_permus( permu_pop: &PermuPopulation<T>, inversions: &mut InversionPopulation<T>, ) -> Result<(), Error>
Fills a given InversionPopulation
with the inversion
representations from a
PermuPopulation
. The transformation is done respecting the positions inside the
PermuPopulation
.
§Errors
Returns a LengthError
if the size of both populations are not equal or if the length
of the permutations is not equal to the length of the inversion vectors + 1.
§Panics
The function panics if the internal Inversion::from_permu
returns an Error
.
This will happen if a type conversion error occurs.
§Example
use permu_rs::permutation::{Permutation, PermuPopulation};
use permu_rs::inversion::{Inversion, InversionPopulation};
let (size, length) = (5, 4);
let mut population = vec![Inversion::<u16>::from_vec(vec![1,0,0]); size];
let mut inversions = InversionPopulation{ population, size };
let inversion_ok = InversionPopulation::<u16>::zeros(size, length-1); // Correct result
let permus = PermuPopulation::<u16>::identity(size, length);
InversionPopulation::from_permus(&permus, &mut inversions);
assert_eq!(inversion_ok, inversions);
println!("{}\n", permus);
println!("{}", inversions);
Trait Implementations§
Source§impl<T: Clone> Clone for InversionPopulation<T>
impl<T: Clone> Clone for InversionPopulation<T>
Source§fn clone(&self) -> InversionPopulation<T>
fn clone(&self) -> InversionPopulation<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 InversionPopulation<T>
impl<T: Debug> Debug for InversionPopulation<T>
Source§impl<T> Display for InversionPopulation<T>where
T: Debug,
impl<T> Display for InversionPopulation<T>where
T: Debug,
Source§impl<T: PartialEq> PartialEq for InversionPopulation<T>
impl<T: PartialEq> PartialEq for InversionPopulation<T>
Source§impl<T> Population<T> for InversionPopulation<T>
impl<T> Population<T> for InversionPopulation<T>
Source§fn learn(&self) -> Distribution
fn learn(&self) -> Distribution
Implementation of learn
method for InversionPopulation
.
§Example
use permu_rs::{Population, Distribution};
use permu_rs::inversion::{InversionPopulation, Inversion};
use InversionPopulation as invpop;
use Inversion as inv;
let pop: Vec<Vec<u8>> = vec![vec![2,1,0], vec![1,0,0], vec![0,0,0]];
let pop = InversionPopulation::from_vec(&pop).unwrap();
let target = vec![vec![1,1,1,0],vec![2,1,0,0],vec![3,0,0,0]];
let target = Distribution::InversionDistribution(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 PermuPopulation
.
§Errors
Returns a LengthError
if the length of the output population’s Inversion
’s length
is not equal to its population Inversions
’s. Returns an IncorrectDistrType
error if
the given distribution is not InversionPopulation
.
§Example
use permu_rs::{Population, Distribution};
use permu_rs::inversion::InversionPopulation;
// Initialize a custom distribution
let distr = vec![vec![1,1,1,0],vec![2,1,0,0],vec![3,0,0,0]];
let mut distr = Distribution::InversionDistribution(distr, false);
println!("Original distr:\n{}", distr);
// Init output population
let mut out = InversionPopulation::<u8>::zeros(10, 3);
// Sample distribution
out.sample(&mut distr).unwrap();
// Now the original distribution has been changed in order to soften it
println!("Now distr:\n{}", distr);
println!("Out:\n{}", out); // Sampled population
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.