The Dantzig AST system supports pattern-based operations that allow you to write concise expressions like max(x[_]) instead of max(x[1], x[2], x[3], x[4], x[5]). This makes optimization modeling much more elegant and maintainable.
Pattern-based operations use the _ (underscore) wildcard to represent "all variables" in a dimension. This allows you to write expressions that automatically scale with the number of variables.
| Pattern | Meaning | Example |
|---|---|---|
x[_] |
All x variables | x[1], x[2], x[3], ..., x[n] |
x[_, j] |
All x variables with fixed second index | x[1,j], x[2,j], x[3,j], ..., x[n,j] |
x[i, _] |
All x variables with fixed first index | x[i,1], x[i,2], x[i,3], ..., x[i,m] |
x[_, _] |
All x variables (2D) | x[1,1], x[1,2], ..., x[n,m] |
# Pattern-based syntax
max(x[_]) # Maximum of all x variables
max(x[_, j]) # Maximum over first dimension
max(x[i, _]) # Maximum over second dimension
max(x[_, _]) # Maximum of all 2D x variables
# Equivalent explicit syntax
max(x[1], x[2], x[3], x[4], x[5])
max(x[1,j], x[2,j], x[3,j])
max(x[i,1], x[i,2], x[i,3])
max(x[1,1], x[1,2], x[2,1], x[2,2], x[3,1], x[3,2])# Pattern-based syntax
min(y[_]) # Minimum of all y variables
min(y[_, j]) # Minimum over first dimension
min(y[i, _]) # Minimum over second dimension
min(y[_, _]) # Minimum of all 2D y variables
# Equivalent explicit syntax
min(y[1], y[2], y[3])
min(y[1,j], y[2,j], y[3,j])
min(y[i,1], y[i,2], y[i,3])
min(y[1,1], y[1,2], y[2,1], y[2,2], y[3,1], y[3,2])# Pattern-based syntax
a[_] AND a[_] AND a[_] AND a[_] # All a variables must be true
# Equivalent explicit syntax
a[1] AND a[2] AND a[3] AND a[4]# Pattern-based syntax
b[_] OR b[_] OR b[_] # At least one b variable must be true
# Equivalent explicit syntax
b[1] OR b[2] OR b[3]# 1D variables
max(x[_]) # max(x[1], x[2], x[3], x[4], x[5])
min(y[_]) # min(y[1], y[2], y[3])
# 2D variables
max(z[_, j]) # max(z[1,j], z[2,j], z[3,j])
min(z[i, _]) # min(z[i,1], z[i,2])
max(z[_, _]) # max(z[1,1], z[1,2], z[2,1], z[2,2], z[3,1], z[3,2])
# Binary variables
a[_] AND a[_] AND a[_] # a[1] AND a[2] AND a[3]
b[_] OR b[_] OR b[_] # b[1] OR b[2] OR b[3]# Create 4D variables busy[i, j, k, l]
max(busy[_, j, k, l]) # max over i dimension
min(busy[i, _, k, l]) # min over j dimension
max(busy[i, j, _, l]) # max over k dimension
min(busy[i, j, k, _]) # min over l dimension
max(busy[_, _, _, _]) # max across all 4D entries# Mixed operations
max(x[_]) + min(y[_]) # max(x[1],...,x[n]) + min(y[1],...,y[m])
max(x[_, j]) - min(x[i, _]) # max(x[1,j],...,x[n,j]) - min(x[i,1],...,x[i,m])
# Nested operations
max(min(x[_, j]), min(x[i, _])) # max of minimums
a[_] AND (b[_] OR c[_]) # all a's AND (at least one b OR at least one c)# Maximize best return while minimizing worst risk
max(return[_]) - min(risk[_])# Find minimum distance to any customer
min(distance[_, customer])# All resources must be available
resource[_] AND resource[_] AND resource[_]# Best quality with fewest defects
max(quality[_]) AND min(defect[_])# Maximum flow to any destination
max(flow[_, destination])max(x[_])instead ofmax(x[1], x[2], x[3], x[4], x[5])- Reduces code verbosity significantly
- Works with any number of variables
- No need to update code when adding/removing variables
- No need to list variables explicitly
- Reduces chance of missing variables or typos
- Intent is clearer: "maximum of all x variables"
- Easier to understand the mathematical meaning
- Adding variables doesn't require code changes
- Easier to refactor and modify
- Supports complex patterns like
x[_, j]orx[i, _] - Can be combined with other operations
Pattern-based operations generate descriptive variable names:
# max(x[_]) creates variable: max_x_all
# min(y[_, j]) creates variable: min_y_all_j
# max(z[i, _]) creates variable: max_z_i_allThe naming convention is:
{operation}_{var_name}_{pattern_description}_becomesall- Fixed indices are included as-is
For max(x[_]) = z where x[_] represents x[1], x[2], ..., x[n]:
- Constraints:
z ≥ x[i]for alli = 1, 2, ..., n - Variable:
zis continuous
For min(x[_]) = z where x[_] represents x[1], x[2], ..., x[n]:
- Constraints:
z ≤ x[i]for alli = 1, 2, ..., n - Variable:
zis continuous
For x[_] AND x[_] AND ... AND x[_] = z (where all x[i] are binary):
- Constraints:
z ≤ x[i]for alli = 1, 2, ..., nz ≥ ∑x[i] - (n-1)
- Variable:
zis binary
For x[_] OR x[_] OR ... OR x[_] = z (where all x[i] are binary):
- Constraints:
z ≥ x[i]for alli = 1, 2, ..., nz ≤ ∑x[i]
- Variable:
zis binary
- DSL Syntax Reference - Complete syntax guide
- Variadic Operations - Variadic max/min/and/or functions
- Modeling Guide - Best practices