1 core.variable_dict.VariableDict
core.variable_dict.VariableDict(
name,
keys,
lb=None,
ub=None,
domain='continuous',
)A dictionary-keyed collection of optimization variables.
Variables are indexed by string keys (e.g., product names, cities) rather than integer indices. Supports weighted sums, partial sums, and dict-like iteration.
1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| name | str | Base name for all variables. Individual variables are named "{name}[{key}]". |
required |
| keys | Sequence[str] | Sequence of string keys. | required |
| lb | float | Mapping[str, float] | None | Lower bound — scalar (broadcast to all), or dict (per-key). | None |
| ub | float | Mapping[str, float] | None | Upper bound — scalar (broadcast to all), or dict (per-key). | None |
| domain | Literal['continuous', 'integer', 'binary'] | Variable domain — 'continuous', 'integer', or 'binary'. |
'continuous' |
1.2 Example
foods = [“hamburger”, “chicken”, “pizza”, “salad”] buy = VariableDict(“buy”, foods, lb=0) buy[“hamburger”] # Variable(“buy[hamburger]”) buy.sum() # sum of all buy variables buy.prod({“hamburger”: 2.49, “chicken”: 2.89, …}) # weighted sum
1.3 Methods
| Name | Description |
|---|---|
| get_variables | Return all Variable objects in key order. |
| items | Return (key, variable) pairs. |
| keys | Return the keys of this VariableDict. |
| prod | Weighted sum (inner product) of variables with coefficients. |
| sum | Sum of variables, optionally over a subset of keys. |
| to_dict | Extract variable values from a solution as a {key: value} dict. |
| values | Return the variables of this VariableDict. |
1.3.1 get_variables
core.variable_dict.VariableDict.get_variables()Return all Variable objects in key order.
1.3.2 items
core.variable_dict.VariableDict.items()Return (key, variable) pairs.
1.3.3 keys
core.variable_dict.VariableDict.keys()Return the keys of this VariableDict.
1.3.4 prod
core.variable_dict.VariableDict.prod(coefficients)Weighted sum (inner product) of variables with coefficients.
1.3.4.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| coefficients | Mapping[str, float] | Sequence[float] | Either a dict mapping keys to coefficients, or a sequence of coefficients in key order. | required |
1.3.4.2 Returns
| Name | Type | Description |
|---|---|---|
| Expression | Expression representing the weighted sum. |
1.3.4.3 Example
cost = {“hamburger”: 2.49, “chicken”: 2.89} buy.prod(cost) # 2.49buy[hamburger] + 2.89buy[chicken] + …
1.3.5 sum
core.variable_dict.VariableDict.sum(keys=None)Sum of variables, optionally over a subset of keys.
1.3.5.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| keys | Sequence[str] | None | If provided, sum only these keys. Otherwise sum all. | None |
1.3.5.2 Returns
| Name | Type | Description |
|---|---|---|
| Expression | Expression representing the sum. |
1.3.5.3 Example
buy.sum() # sum of all buy.sum([“hamburger”, “chicken”]) # partial sum
1.3.6 to_dict
core.variable_dict.VariableDict.to_dict(solution)Extract variable values from a solution as a {key: value} dict.
This is a convenience wrapper around solution[variable_dict].
1.3.6.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| solution | Solution | A :class:~optyx.solution.Solution object. |
required |
1.3.6.2 Returns
| Name | Type | Description |
|---|---|---|
| dict[str, float] | Dict mapping each key to its optimal value. |
1.3.7 values
core.variable_dict.VariableDict.values()Return the variables of this VariableDict.