Table of Contents

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

bits bool[]

The bits of the bit string.

BitString(int)

Initializes a new bit string with all bits set to false.

public BitString(int n)

Parameters

n int

The number of bits.

Properties

Count

Gets the number of bits.

public int Count { get; }

Property Value

int

this[int]

Gets the bit at the given index.

public bool this[int index] { get; }

Parameters

index int

The zero-based index of the bit.

Property Value

bool

The bit.

Length

Gets the number of bits.

public int Length { get; }

Property Value

int

Methods

CreateRandomly(int, Random)

Creates a new bit string whose bits are randomly initialized.

public static BitString CreateRandomly(int n, Random random)

Parameters

n int

The number of bits.

random Random

The random number generator.

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

other BitString

The other bit string.

Returns

bool

true if both bit strings are equal.

Equals(object?)

Indicates whether the current bit string 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.

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

bitString BitString

The bit string to mutate.

random Random

The random number generator.

probability double

The 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

p1 BitString

The first parent.

p2 BitString

The second parent.

random Random

The 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

p1 BitString

The first parent.

p2 BitString

The second parent.

random Random

The 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

lhs BitString

The first bit string.

rhs BitString

The second bit string.

Returns

bool

true if both bit strings are equal.

operator !=(BitString, BitString)

Indicates whether two bit strings are not equal.

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

Parameters

lhs BitString

The first bit string.

rhs BitString

The second bit string.

Returns

bool

true if both bit strings are not equal.