Skip to main content

Permutation

Struct Permutation 

Source
pub struct Permutation { /* private fields */ }
Expand description

Permutation genome for ordering problems

Represents a permutation of indices 0..n, commonly used for:

  • Traveling Salesman Problem (TSP)
  • Job Shop Scheduling
  • Vehicle Routing Problems
  • Any problem where the solution is an ordering of elements

Implementations§

Source§

impl Permutation

Source

pub fn new(perm: Vec<usize>) -> Self

Create a new permutation from a vector of indices

§Panics

Panics if the input is not a valid permutation of 0..n

Source

pub fn new_unchecked(perm: Vec<usize>) -> Self

Create a permutation from a vector without validation

§Safety

The caller must ensure the input is a valid permutation of 0..n

Source

pub fn try_new(perm: Vec<usize>) -> Result<Self, GenomeError>

Try to create a permutation, returning an error if invalid

Source

pub fn identity(n: usize) -> Self

Create the identity permutation [0, 1, 2, …, n-1]

Source

pub fn random<R: Rng>(n: usize, rng: &mut R) -> Self

Create a random permutation of size n

Source

pub fn len(&self) -> usize

Get the length of the permutation

Source

pub fn is_empty(&self) -> bool

Check if the permutation is empty

Source

pub fn get(&self, i: usize) -> Option<usize>

Get the element at index i

Source

pub fn inverse(&self) -> Self

Get the inverse permutation

If perm[i] = j, then inverse[j] = i

Source

pub fn compose(&self, other: &Self) -> Result<Self, GenomeError>

Compose this permutation with another

Returns a permutation where result[i] = other[self[i]]

Source

pub fn swap(&mut self, i: usize, j: usize)

Swap two elements at positions i and j

Source

pub fn reverse_segment(&mut self, start: usize, end: usize)

Reverse a segment from start to end (inclusive)

Source

pub fn insert(&mut self, from: usize, to: usize)

Insert element at position from to position to

Source

pub fn inversions(&self) -> usize

Calculate the number of inversions (disorder measure)

An inversion is a pair (i, j) where i < j but perm[i] > perm[j]. Returns a value in [0, n*(n-1)/2] where 0 means sorted.

Source

pub fn kendall_tau_distance(&self, other: &Self) -> Result<usize, GenomeError>

Calculate Kendall tau distance to another permutation

Counts the number of pairwise disagreements (i.e., pairs that are in different order in the two permutations).

Source

pub fn is_cyclic(&self) -> bool

Check if this is a cyclic permutation (single cycle)

Source

pub fn into_inner(self) -> Vec<usize>

Get the underlying vector

Source

pub fn as_slice(&self) -> &[usize]

Get a reference to the underlying slice

Trait Implementations§

Source§

impl Clone for Permutation

Source§

fn clone(&self) -> Permutation

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 CrossoverOperator<Permutation> for CxCrossover

Source§

fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, _rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>

Apply crossover to two parents and produce two offspring
Source§

fn crossover_probability(&self) -> f64

Get the probability of crossover being applied
Source§

impl CrossoverOperator<Permutation> for EdgeRecombinationCrossover

Source§

fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>

Apply crossover to two parents and produce two offspring
Source§

fn crossover_probability(&self) -> f64

Get the probability of crossover being applied
Source§

impl CrossoverOperator<Permutation> for OxCrossover

Source§

fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>

Apply crossover to two parents and produce two offspring
Source§

fn crossover_probability(&self) -> f64

Get the probability of crossover being applied
Source§

impl CrossoverOperator<Permutation> for PmxCrossover

Source§

fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>

Apply crossover to two parents and produce two offspring
Source§

fn crossover_probability(&self) -> f64

Get the probability of crossover being applied
Source§

impl Debug for Permutation

Source§

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

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

impl<'de> Deserialize<'de> for Permutation

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl EvolutionaryGenome for Permutation

Source§

fn to_trace(&self) -> Trace

Convert Permutation to Fugue trace.

Each position is stored at address “perm#i” where i is the index.

Source§

fn from_trace(trace: &Trace) -> Result<Self, GenomeError>

Reconstruct Permutation from Fugue trace.

Reads values from addresses “perm#0”, “perm#1”, … until no more are found.

Source§

type Allele = usize

The allele type for individual genes
Source§

type Phenotype = Vec<usize>

The phenotype or decoded solution type
Source§

fn decode(&self) -> Self::Phenotype

Decode genome into phenotype for fitness evaluation
Source§

fn dimension(&self) -> usize

Compute dimensionality for adaptive operators
Source§

fn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self

Generate a random genome within the given bounds
Source§

fn distance(&self, other: &Self) -> f64

Distance metric between two genomes (default: 0.0)
Source§

fn trace_prefix() -> &'static str

Get the address prefix used for trace storage (default: “gene”)
Source§

fn as_slice(&self) -> Option<&[Self::Allele]>

Get the genome’s genes as a slice (for numeric genomes)
Source§

fn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]>

Get the genome’s genes as a mutable slice (for numeric genomes)
Source§

impl From<Permutation> for Vec<usize>

Source§

fn from(p: Permutation) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<usize>> for Permutation

Source§

fn from(perm: Vec<usize>) -> Self

Converts to this type from the input type.
Source§

impl Hash for Permutation

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for Permutation

Source§

type Output = usize

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Permutation

Source§

type Item = &'a usize

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, usize>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Permutation

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<usize>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl MutationOperator<Permutation> for AdaptivePermutationMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl MutationOperator<Permutation> for DisplacementMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl MutationOperator<Permutation> for InsertMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl MutationOperator<Permutation> for InversionMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl MutationOperator<Permutation> for PermutationScrambleMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl MutationOperator<Permutation> for PermutationSwapMutation

Source§

fn mutate<R: Rng>(&self, genome: &mut Permutation, rng: &mut R)

Apply mutation to a genome in place
Source§

fn mutation_probability(&self) -> f64

Get the mutation probability per gene
Source§

impl PartialEq for Permutation

Source§

fn eq(&self, other: &Permutation) -> 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 PermutationGenome for Permutation

Source§

fn permutation(&self) -> &[usize]

Get the permutation as a slice
Source§

fn permutation_mut(&mut self) -> &mut [usize]

Get the permutation as a mutable slice
Source§

fn from_permutation(perm: Vec<usize>) -> Result<Self, GenomeError>

Create from a vector of indices
Source§

fn is_valid_permutation(&self) -> bool

Check if the genome represents a valid permutation
Source§

impl Serialize for Permutation

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Permutation

Source§

impl StructuralPartialEq for Permutation

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, 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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,