Struct BitString
- Namespace
- Evolutionary
- Assembly
- Evolutionary.dll
Provides factory methods for random bit strings.
public readonly struct BitString : IEquatable<BitString>, IReadOnlyList<bool>, IReadOnlyCollection<bool>, IEnumerable<bool>, IEnumerable
- Implements
- Inherited Members
Remarks
A bit string is the natural representation for selection problems like the OneMax problem or the knapsack problem, where each bit encodes a yes/no decision. The default instance of the struct is an empty bit string.
Constructors
BitString(bool[])
Initializes a new bit string from the given array. The array is not copied and not validated.
public BitString(bool[] bits)
Parameters
bitsbool[]The bits of the bit string.
BitString(int)
Initializes a new bit string with all bits set to false.
public BitString(int n)
Parameters
nintThe number of bits.
Properties
Count
Gets the number of bits.
public int Count { get; }
Property Value
this[int]
Gets the bit at the given index.
public bool this[int index] { get; }
Parameters
indexintThe zero-based index of the bit.
Property Value
- bool
The bit.
Length
Gets the number of bits.
public int Length { get; }
Property Value
Methods
CreateRandomly(int, Random)
Creates a new bit string whose bits are randomly initialized.
public static BitString CreateRandomly(int n, Random random)
Parameters
Returns
- BitString
The random bit string.
Equals(BitString)
Indicates whether the current bit string and the given one are equal.
public bool Equals(BitString other)
Parameters
otherBitStringThe other bit string.
Returns
Equals(object?)
Indicates whether the current bit string and the given object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with.
Returns
FlipMutation(BitString, Random, double)
Flips each bit independently with the given probability (bit-flip mutation). A common choice for the probability is 1/Length.
public static BitString FlipMutation(BitString bitString, Random random, double probability)
Parameters
bitStringBitStringThe bit string to mutate.
randomRandomThe random number generator.
probabilitydoubleThe probability to flip a single bit.
Returns
- BitString
The mutated bit string.
GetEnumerator()
Returns an enumerator that iterates through the bits.
public IEnumerator<bool> GetEnumerator()
Returns
- IEnumerator<bool>
The enumerator.
GetHashCode()
Returns a hash code for the bit string. Two equal bit strings have the same hash code.
public override int GetHashCode()
Returns
- int
The hash code.
OnePointCrossover(BitString, BitString, Random)
Creates a child by a one-point crossover. The bits before a randomly selected cut position are taken from the first parent and the remaining bits from the second parent.
public static BitString OnePointCrossover(BitString p1, BitString p2, Random random)
Parameters
p1BitStringThe first parent.
p2BitStringThe second parent.
randomRandomThe random number generator.
Returns
- BitString
The child.
Exceptions
- ArgumentException
Thrown if the lengths of both bit strings differ.
- InvalidOperationException
Thrown if the bit string is too short for this operation.
PopCount()
Counts the bits that are set (the population count), in analogy to System.Numerics.BitOperations.PopCount.
public int PopCount()
Returns
- int
The number of bits that are set.
ToArray()
Returns a copy of the bits as array.
public bool[] ToArray()
Returns
- bool[]
The bits.
ToString()
Returns the bits as a string in binary literal notation prefixed
with '0b', e.g. 0b101.
public override string ToString()
Returns
- string
The string representation.
UniformCrossover(BitString, BitString, Random)
Creates a child by a uniform crossover. Each bit is taken from one of the parents with equal probability.
public static BitString UniformCrossover(BitString p1, BitString p2, Random random)
Parameters
p1BitStringThe first parent.
p2BitStringThe second parent.
randomRandomThe random number generator.
Returns
- BitString
The child.
Exceptions
- ArgumentException
Thrown if the lengths of both bit strings differ.
Operators
operator ==(BitString, BitString)
Indicates whether two bit strings are equal.
public static bool operator ==(BitString lhs, BitString rhs)
Parameters
Returns
operator !=(BitString, BitString)
Indicates whether two bit strings are not equal.
public static bool operator !=(BitString lhs, BitString rhs)