Skip to main content

Sets and domains

Set​

Set(name, labels)

A named dimension with labels. labels is an array; name is what every reference to the dimension uses.

MemberReturns
name, labelsthe declared name and labels
len(set)the number of members
position_of(labels)the position of each label given
coordthe coordinate the labels resolve through
cyclicthe same set, with a lag that wraps instead of dropping
set - 1the set lagged, dropping the members outside the set
import numpy as np
from nimopt import Set

T = Set("T", np.array(["t0", "t1", "t2"]))

print(T.name, len(T))
print(T.labels)
print(T.position_of(np.array(["t2", "t0"])))
Output
T 3
['t0' 't1' 't2']
[2 0]

Alias​

Alias(name, set)

A second name for a set, sharing its labels and its coordinate. A parameter over a set and its alias is an ordinary two-dimensional array. A model relates a set to itself without declaring a second set. No labels are copied: the alias uses the coordinate the set already built.

import numpy as np
from nimopt import Alias, Param, Set

N = Set("N", np.array(["a", "b"]))
M = Alias("M", N)

flow = Param.from_dense("flow", (N, M), np.array([[0.0, 1.0], [1.0, 0.0]]))
print(flow.dims)
print(flow.materialise().to_dense())
Output
('N', 'M')
[[0. 1.]
[1. 0.]]

product​

product(sets)

Every member of a set product, as a domain. Passed to over=, it declares the rows of a constraint explicitly, for a constraint whose terms each cover some of its rows. A factor is a set or a domain: a set contributes every member, and a domain its own members. product((subset((G,), columns), T)) is the members of G in columns crossed with every member of T. Two factors over one dimension raise ValueError.

subset​

subset(sets, columns)

The members of a set product a model uses, given by label. columns contains one label column per set, keyed by the name of the set. The columns are read in parallel: the k-th entry of each column belongs to the same member. The result is a list of members, not a cross product.

subset_of​

subset_of(sets, index)

The same, given by position. Each column of index is one member. A caller with positions passes them directly and builds no labels to resolve back.

import numpy as np
from nimopt import Set, product, subset, subset_of

P = Set("P", np.array(["lisbon", "porto"]))
W = Set("W", np.array(["berlin", "paris", "rome"]))

print(product((P, W)).size)
print(
subset(
(P, W), {"P": np.array(["lisbon", "porto"]), "W": np.array(["berlin", "paris"])}
).size
)
print(subset_of((P, W), np.array([[0, 1], [0, 1]])).size)
Output
6
2
2

The product has six members. Both subsets have two, lisbon with berlin and porto with paris. The columns are read in parallel.