Table of Contents

Struct Permutation

Namespace
Evolutionary
Assembly
Evolutionary.dll

Represents a permutation of the numbers 0 to n-1. The permutation is an immutable individual for permutation-based evolutionary algorithms.

public readonly struct Permutation : IEquatable<Permutation>, IReadOnlyList<int>, IReadOnlyCollection<int>, IEnumerable<int>, IEnumerable
Implements
Inherited Members

Remarks

A permutation can either be linear or circular. A circular permutation treats the first and the last element as adjacent. The default instance of the struct is an empty linear permutation.

Constructors

Permutation(int)

Initializes a new linear permutation of the numbers 0 to n-1.

public Permutation(int n)

Parameters

n int

The number of elements.

Permutation(int, bool)

Initializes a new permutation of the numbers 0 to n-1.

public Permutation(int n, bool isCircular)

Parameters

n int

The number of elements.

isCircular bool

Indicates whether the permutation is circular.

Permutation(int[], bool)

Initializes a new permutation from the given array. The array is not copied and not validated.

public Permutation(int[] array, bool isCircular)

Parameters

array int[]

The elements of the permutation.

isCircular bool

Indicates whether the permutation is circular.

Properties

Count

Gets the number of elements.

public int Count { get; }

Property Value

int

IsCircular

Gets a value indicating whether the permutation is circular.

public bool IsCircular { get; }

Property Value

bool

this[int]

Gets the element at the given index.

public int this[int index] { get; }

Parameters

index int

The zero-based index of the element.

Property Value

int

The element.

Methods

CreateCircularRandomly(int, Random)

Creates a new circular permutation whose elements are shuffled randomly. The first element is always 0, as the circular permutation has no well-defined first element.

public static Permutation CreateCircularRandomly(int n, Random random)

Parameters

n int

The number of elements.

random Random

The random number generator.

Returns

Permutation

The random circular permutation.

CreateRandomly(int, Random)

Creates a new linear permutation whose elements are shuffled randomly.

public static Permutation CreateRandomly(int n, Random random)

Parameters

n int

The number of elements.

random Random

The random number generator.

Returns

Permutation

The random permutation.

CutAndCrossfillCrossover(Permutation, Permutation, int)

Creates a child by a cut and crossfill crossover. The prefix of the first parent is copied into the child, and the child is completed by the remaining elements of the second parent in their original order.

public static Permutation CutAndCrossfillCrossover(Permutation p1, Permutation p2, int position)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

position int

The number of elements that are copied from the first parent.

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

InvalidOperationException

Thrown if the permutation is too short for this operation.

ArgumentOutOfRangeException

Thrown if the position is out of the valid range.

CutAndCrossfillCrossover(Permutation, Permutation, Random)

Creates a child by a cut and crossfill crossover. The prefix of the first parent is copied into the child, and the child is completed by the remaining elements of the second parent in their original order.

public static Permutation CutAndCrossfillCrossover(Permutation p1, Permutation p2, Random random)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

random Random

The random number generator.

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

InvalidOperationException

Thrown if the permutation is too short for this operation.

CycleCrossover(Permutation, Permutation)

Creates a child by a cycle crossover (CX). The elements at cycle positions are taken from the first parent and the others from the second parent.

public static Permutation CycleCrossover(Permutation p1, Permutation p2)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

CycleCrossover(Permutation, Permutation, Random)

Creates a child by a cycle crossover (CX). The random number generator is not used; the overload exists for API consistency with the other recombination operators.

public static Permutation CycleCrossover(Permutation p1, Permutation p2, Random random)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

random Random

The random number generator (unused).

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

Equals(Permutation)

Indicates whether the current permutation and the given one are equal. Two permutations are equal if they have the same circularity and the same sequence of elements.

public bool Equals(Permutation other)

Parameters

other Permutation

The other permutation.

Returns

bool

true if both permutations are equal.

Equals(object?)

Indicates whether the current permutation and the given object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with.

Returns

bool

true if both are equal.

GetEnumerator()

Returns an enumerator that iterates through the elements.

public IEnumerator<int> GetEnumerator()

Returns

IEnumerator<int>

The enumerator.

GetHashCode()

Returns a hash code for the permutation. Two equal permutations have the same hash code.

public override int GetHashCode()

Returns

int

The hash code.

InsertMutation(Permutation, Random)

Removes a randomly selected element and reinserts it at another randomly selected position. The elements in between are shifted (insertion mutation).

public static Permutation InsertMutation(Permutation permutation, Random random)

Parameters

permutation Permutation

The permutation to mutate.

random Random

The random number generator.

Returns

Permutation

The mutated permutation.

Exceptions

InvalidOperationException

Thrown if the permutation is too short for this operation.

InversionMutation(Permutation, Random)

Reverses the elements between two randomly selected positions (inversion mutation).

public static Permutation InversionMutation(Permutation permutation, Random random)

Parameters

permutation Permutation

The permutation to mutate.

random Random

The random number generator.

Returns

Permutation

The mutated permutation.

Exceptions

InvalidOperationException

Thrown if the permutation is too short for this operation.

PartiallyMappedCrossover(Permutation, Permutation, int, int)

Creates a child by a partially mapped crossover (PMX). The selected segment of the first parent is copied into the child, and the remaining elements are taken from the second parent, with duplicates being resolved by the partial mapping.

public static Permutation PartiallyMappedCrossover(Permutation p1, Permutation p2, int start, int length)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

start int

The start position of the mapped segment.

length int

The length of the mapped segment.

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

InvalidOperationException

Thrown if the permutation is too short for this operation.

ArgumentOutOfRangeException

Thrown if the start position or the length is out of the valid range.

PartiallyMappedCrossover(Permutation, Permutation, Random)

Creates a child by a partially mapped crossover (PMX). A randomly selected segment of the first parent is copied into the child, and the remaining elements are taken from the second parent, with duplicates being resolved by the partial mapping.

public static Permutation PartiallyMappedCrossover(Permutation p1, Permutation p2, Random random)

Parameters

p1 Permutation

The first parent.

p2 Permutation

The second parent.

random Random

The random number generator.

Returns

Permutation

The child.

Exceptions

ArgumentException

Thrown if the element counts or the circularity of both permutations differ.

InvalidOperationException

Thrown if the permutation is too short for this operation.

ScrambleMutation(Permutation, Random)

Randomly shuffles the elements between two randomly selected positions (scramble mutation).

public static Permutation ScrambleMutation(Permutation permutation, Random random)

Parameters

permutation Permutation

The permutation to mutate.

random Random

The random number generator.

Returns

Permutation

The mutated permutation.

Exceptions

InvalidOperationException

Thrown if the permutation is too short for this operation.

SwapMutation(Permutation, Random)

Swaps the elements at two randomly selected positions (swap mutation).

public static Permutation SwapMutation(Permutation permutation, Random random)

Parameters

permutation Permutation

The permutation to mutate.

random Random

The random number generator.

Returns

Permutation

The mutated permutation.

Exceptions

InvalidOperationException

Thrown if the permutation is too short for this operation.

ToArray()

Returns a copy of the permutation as array.

public int[] ToArray()

Returns

int[]

The elements of the permutation.

Operators

operator ==(Permutation, Permutation)

Indicates whether two permutations are equal.

public static bool operator ==(Permutation lhs, Permutation rhs)

Parameters

lhs Permutation

The first permutation.

rhs Permutation

The second permutation.

Returns

bool

true if both permutations are equal.

implicit operator ReadOnlySpan<int>(Permutation)

Converts the permutation into a read-only span of its elements.

public static implicit operator ReadOnlySpan<int>(Permutation permutation)

Parameters

permutation Permutation

The permutation.

Returns

ReadOnlySpan<int>

operator !=(Permutation, Permutation)

Indicates whether two permutations are not equal.

public static bool operator !=(Permutation lhs, Permutation rhs)

Parameters

lhs Permutation

The first permutation.

rhs Permutation

The second permutation.

Returns

bool

true if both permutations are not equal.