Variables¶
ypea.Variable¶
Base class of the decision variable types.
Every variable is stored as a block of codes in [0, 1]. A variable
knows how many codes it needs (code_size), how to turn codes into
values (decode) and how to turn a value back into codes (encode).
decode is batch first: codes have size n x code_size. A scalar
variable gives n x 1 values, a vector gives n x d, and a matrix of
size r x c gives n x r x c. Partition variables give an n x 1 cell.
value_at(batch, i) returns the value of solution i.
ypea.Real¶
Continuous values in [lb, ub].
ypea.Real(name, shape=d, lb=..., ub=...). The bounds may be arrays,
and the shape follows them when it is not given. A shape d gives a
row vector, [r c] a matrix.
Example:
x = ypea.Real("x", shape=20, lb=-10, ub=10);
ypea.Integer¶
Integer values in [lb, ub], both included.
ypea.Integer(name, shape=d, lb=..., ub=...). The bounds may be
arrays, and the shape follows them when it is not given.
Example:
k = ypea.Integer("k", shape=5, lb=0, ub=9);
ypea.Binary¶
Values 0 or 1.
ypea.Binary(name, shape=d).
Example:
x = ypea.Binary("x", shape=20);
ypea.Permutation¶
A permutation of 1..n.
ypea.Permutation(name, n) gives a 1 x n row. With count=m, the value
is an m x n matrix with one permutation per row.
Example:
tour = ypea.Permutation("tour", 20);
ypea.Selection¶
An ordered selection of k distinct items out of 1..n.
ypea.Selection(name, n, k) gives a 1 x k row of item indices. With
count=m, the value is an m x k matrix.
Example:
team = ypea.Selection("team", 30, 5);
ypea.BinarySelection¶
A 0/1 mask of length n with exactly k ones.
ypea.BinarySelection(name, n, k) gives a 1 x n row. With count=m,
the value is an m x n matrix.
Example:
chosen = ypea.BinarySelection("chosen", 30, 5);
ypea.Partition¶
Split items 1..n into k groups (some may be empty).
ypea.Partition(name, n, k) gives a 1 x k cell array of index row
vectors. With count=m, the value is an m x k cell array.
The code has n + k - 1 keys: n items and k - 1 separators, sorted
together. Partition values are cell arrays, so problems with a
Partition cannot be vectorized.
Example:
groups = ypea.Partition("groups", 12, 3);
ypea.BinaryPartition¶
Like Partition, but decodes to a k x n 0/1 assignment matrix.
ypea.BinaryPartition(name, n, k) gives a k x n matrix whose entry
(j, i) is 1 when item i is in group j. With count=m, the value is a
k x n x m array, one assignment matrix per page.
Example:
assign = ypea.BinaryPartition("assign", 12, 3);
ypea.FixedSum¶
Nonnegative reals with a fixed sum.
Differs from Python: Takes dim in place of Python's axis: dim=2 fixes the sum of each row.
ypea.FixedSum(name, shape, target=1, dim=[]). With dim empty, all
elements sum to target. With dim=1, each column sums to its target
(one value, or one per column). With dim=2, each row sums to its
target (one value, or one per row), as in sum(x, dim).
Example:
w = ypea.FixedSum("w", 5); % 5 weights that sum to 1
a = ypea.FixedSum("a", [3 4], target=10, dim=2); % each row sums to 10