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
impl Permutation
Sourcepub fn new(perm: Vec<usize>) -> Self
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
Sourcepub fn from_vec_unchecked(perm: Vec<usize>) -> Self
pub fn from_vec_unchecked(perm: Vec<usize>) -> Self
Create a permutation from a vector without validating it.
§Invariants
The caller must guarantee that perm is a valid permutation of
0..perm.len() — every index in that range appears exactly once.
Violating this invariant does not fail here, but corrupts downstream
operations that assume it:
inversepanics with an out-of-bounds index for an out-of-range value, or silently produces a meaningless result for in-range duplicates.composepanics on out-of-range indices.
Prefer try_new (or new) unless validity
has already been established elsewhere and you need to skip the O(n)
re-check. In debug builds this constructor still asserts validity to
catch contract violations early.
Sourcepub fn try_new(perm: Vec<usize>) -> Result<Self, GenomeError>
pub fn try_new(perm: Vec<usize>) -> Result<Self, GenomeError>
Try to create a permutation, returning an error if invalid
Sourcepub fn generate_with_len<R: Rng>(rng: &mut R, len: usize) -> Self
pub fn generate_with_len<R: Rng>(rng: &mut R, len: usize) -> Self
Generate a random permutation of an explicit length.
This is the honest constructor for random generation: unlike
EvolutionaryGenome::generate,
which overloads MultiBounds and only reads its dimension count, this
takes the permutation length directly. Equivalent to random.
Sourcepub fn compose(&self, other: &Self) -> Result<Self, GenomeError>
pub fn compose(&self, other: &Self) -> Result<Self, GenomeError>
Compose this permutation with another
Returns a permutation where result[i] = other[self[i]]
Sourcepub fn reverse_segment(&mut self, start: usize, end: usize)
pub fn reverse_segment(&mut self, start: usize, end: usize)
Reverse a segment from start to end (inclusive)
Sourcepub fn insert(&mut self, from: usize, to: usize)
pub fn insert(&mut self, from: usize, to: usize)
Insert element at position from to position to
Sourcepub fn inversions(&self) -> usize
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.
Sourcepub fn kendall_tau_distance(&self, other: &Self) -> Result<usize, GenomeError>
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).
Sourcepub fn into_inner(self) -> Vec<usize>
pub fn into_inner(self) -> Vec<usize>
Get the underlying vector
Trait Implementations§
Source§impl Clone for Permutation
impl Clone for Permutation
Source§fn clone(&self) -> Permutation
fn clone(&self) -> Permutation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl CrossoverOperator<Permutation> for PmxCrossover
impl CrossoverOperator<Permutation> for PmxCrossover
Source§fn crossover<R: Rng>(
&self,
parent1: &Permutation,
parent2: &Permutation,
rng: &mut R,
) -> OperatorResult<(Permutation, Permutation)>
fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>
Source§fn crossover_probability(&self) -> f64
fn crossover_probability(&self) -> f64
Source§impl CrossoverOperator<Permutation> for OxCrossover
impl CrossoverOperator<Permutation> for OxCrossover
Source§fn crossover<R: Rng>(
&self,
parent1: &Permutation,
parent2: &Permutation,
rng: &mut R,
) -> OperatorResult<(Permutation, Permutation)>
fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>
Source§fn crossover_probability(&self) -> f64
fn crossover_probability(&self) -> f64
Source§impl CrossoverOperator<Permutation> for CxCrossover
impl CrossoverOperator<Permutation> for CxCrossover
Source§fn crossover<R: Rng>(
&self,
parent1: &Permutation,
parent2: &Permutation,
_rng: &mut R,
) -> OperatorResult<(Permutation, Permutation)>
fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, _rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>
Source§fn crossover_probability(&self) -> f64
fn crossover_probability(&self) -> f64
Source§impl CrossoverOperator<Permutation> for EdgeRecombinationCrossover
impl CrossoverOperator<Permutation> for EdgeRecombinationCrossover
Source§fn crossover<R: Rng>(
&self,
parent1: &Permutation,
parent2: &Permutation,
rng: &mut R,
) -> OperatorResult<(Permutation, Permutation)>
fn crossover<R: Rng>( &self, parent1: &Permutation, parent2: &Permutation, rng: &mut R, ) -> OperatorResult<(Permutation, Permutation)>
Source§fn crossover_probability(&self) -> f64
fn crossover_probability(&self) -> f64
Source§impl Debug for Permutation
impl Debug for Permutation
Source§impl<'de> Deserialize<'de> for Permutation
impl<'de> Deserialize<'de> for Permutation
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Permutation
Source§impl EvolutionaryGenome for Permutation
impl EvolutionaryGenome for Permutation
Source§fn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self
fn generate<R: Rng>(rng: &mut R, bounds: &MultiBounds) -> Self
Generate a random permutation.
Only bounds.dimension() is consulted — it is the permutation length —
and the per-dimension min/max values are ignored. Prefer
Permutation::generate_with_len to make the length explicit.
Source§fn try_distance(&self, other: &Self) -> Result<f64, GenomeError>
fn try_distance(&self, other: &Self) -> Result<f64, GenomeError>
Source§fn as_slice(&self) -> Option<&[Self::Allele]>
fn as_slice(&self) -> Option<&[Self::Allele]>
Source§fn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]>
fn as_mut_slice(&mut self) -> Option<&mut [Self::Allele]>
Source§impl From<Permutation> for Vec<usize>
impl From<Permutation> for Vec<usize>
Source§fn from(p: Permutation) -> Self
fn from(p: Permutation) -> Self
Source§impl Hash for Permutation
impl Hash for Permutation
Source§impl Index<usize> for Permutation
impl Index<usize> for Permutation
Source§impl IntoIterator for Permutation
impl IntoIterator for Permutation
Source§impl<'a> IntoIterator for &'a Permutation
impl<'a> IntoIterator for &'a Permutation
Source§impl MutationOperator<Permutation> for InsertMutation
impl MutationOperator<Permutation> for InsertMutation
Source§impl PartialEq for Permutation
impl PartialEq for Permutation
Source§impl PermutationGenome for Permutation
impl PermutationGenome for Permutation
Source§fn permutation(&self) -> &[usize]
fn permutation(&self) -> &[usize]
Source§fn permutation_mut(&mut self) -> &mut [usize]
fn permutation_mut(&mut self) -> &mut [usize]
Source§fn from_permutation(perm: Vec<usize>) -> Result<Self, GenomeError>
fn from_permutation(perm: Vec<usize>) -> Result<Self, GenomeError>
Source§fn is_valid_permutation(&self) -> bool
fn is_valid_permutation(&self) -> bool
Source§impl Serialize for Permutation
impl Serialize for Permutation
impl StructuralPartialEq for Permutation
Source§impl TraceGenome for Permutation
Available on crate feature ppl only.
impl TraceGenome for Permutation
ppl only.Source§fn to_trace(&self) -> Trace
fn to_trace(&self) -> Trace
Convert Permutation to Fugue trace using the Lehmer-code (rank)
encoding: position i stores the rank of perm[i] among the values
not yet used at positions < i (a Usize in 0..n-i).
This encoding (rather than storing raw values) is what makes the trace
generative: any in-range assignment of ranks decodes to a valid
permutation, so a single-site change of one rank is a valid move — the
value encoding would make every single-site change a duplicate. It
coincides site-for-site with the sequential categorical prior model in
crate::inference::prior::PermutationPrior.
Source§fn from_trace(trace: &Trace) -> Result<Self, GenomeError>
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. A missing address terminates the scan (normal end of the
sequence), but an address that is present with the wrong value type is
a corrupt trace and yields GenomeError::TypeMismatch rather than
silently truncating — which for a permutation is especially dangerous,
since a truncated prefix can itself pass the validity check.
Source§fn trace_prefix() -> &'static str
fn trace_prefix() -> &'static str
"gene").Auto Trait Implementations§
impl Freeze for Permutation
impl RefUnwindSafe for Permutation
impl Send for Permutation
impl Sync for Permutation
impl Unpin for Permutation
impl UnsafeUnpin for Permutation
impl UnwindSafe for Permutation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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
impl<T> Pointable for T
impl<T> Scalar for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.