platypus.types module

class Binary(nbits)

Bases: Type

Represents a binary string.

Binary strings are useful for problems dealing with subsets, where from a set of N items a subset of 0 to N elements are selected. For example, see the Knapsack problem.

Internally, in Python, the binary string is stored as a list of boolean values, where False represents the 0 (off) bit and and True represents the 1 (on) bit.

Parameters

nbitsint

The number of bits.

rand()

Produces a random but valid encoded value for this type.

class Integer(min_value, max_value)

Bases: Binary

Represents an integer value with min and max bounds.

Integers extend the Binary representation and encodes the integer as a gray-encoded binary value. The gray-encoding ensures that adjacent integers (e.g., i and i+1) differ only by one bit.

Given the bounds, the underlying representation chooses the minimum number of bits required to store the integer in a binary string. If the range is a power of 2, then each binary string maps to a single integer value. Otherwise, the mapping “wraps” around, causing some values to have a slightly higher probability of occurrence.

Parameters

min_valueint

The minimum value (inclusive)

max_value: int

The maximum value (inclusive)

decode(value)

Decodes a value from its internal representation.

encode(value)

Encodes a value into its internal representation.

rand()

Produces a random but valid encoded value for this type.

class Permutation(elements)

Bases: Type

Represents a permutation.

Permutations are stored as a list of elements in a specific order. All elements will appear in the list exactly once. For example, this is used to represent the traversal through a graph, such as for the Traveling Salesman Problem.

Most commonly, a permutation is created using integers:

Permutation(range(10))

But any collection of objects can be used. Here we create tuples:

Permutation([(a1, a2), (b1, b2), (c1, c2), (d1, d2)])

Parameters

elementslist of objects

The list of elements that appear in the permutation.

rand()

Produces a random but valid encoded value for this type.

class Real(min_value, max_value)

Bases: Type

Represents a floating-point value with min and max bounds.

Parameters

min_valuefloat

The minimum value (inclusive)

max_value: float

The maximum value (inclusive)

rand()

Produces a random but valid encoded value for this type.

class Subset(elements, size)

Bases: Type

Represents a fixed-size subset.

Use a subset when you must select K elements from a collection of N items, where K <= N.

Binary representation are also often used to represent a subset of items. The key difference is a subset is guaranteed to select exactly K items, whereas a binary representation can choose any number of items.

Similar to a permutation, the elements are typically integers but can be any object:

Subset(range(10), 2)

Parameters

elementslist of objects

The set of elements.

sizeint

The size of the subset.

rand()

Produces a random but valid encoded value for this type.

class Type

Bases: object

The type of a decision variable.

The type of a decision variable defines its bounds, provides a mechanism to produce a random value within those bounds, and defines any encoding / decoding to convert between the value and the internal representation.

An example of the value differing from the internal representation is binary integers, where the value is an integer (e.g., 27) but its internal representation is a binary string (e.g., “11011” or in Python [True, True, False, True, True]).

Subclasses should override __repr__() and __str__() to provide a human readable representation of the type. The current standard is to return TypeName(Arg1, Arg2, ...).

decode(value)

Decodes a value from its internal representation.

encode(value)

Encodes a value into its internal representation.

abstract rand()

Produces a random but valid encoded value for this type.

bin2gray(bits)

Converts a binary string into a gray-encoded binary string.

Parameters

bitslist or tuple of bool

The binary string as a list of True/False values.

bin2int(bits)

Converts a binary string into its integer value.

Parameters

bitslist or tuple of bool

The binary string as a list of True/False values.

gray2bin(bits)

Converts a gray-encoded binary string into a binary string.

Parameters

bitslist or tuple of bool

The gray-encoded binary string as a list of True/False values.

int2bin(n, nbits)

Converts an integer into a binary string.

Parameters

nint

The integer value.

nbits:

The number of bits used to encode the value.