forked from tmbb/dantzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_based_operations_example.exs
More file actions
298 lines (259 loc) · 9.5 KB
/
Copy pathpattern_based_operations_example.exs
File metadata and controls
298 lines (259 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Pattern-Based Operations Example
# Demonstrates the modern pattern-based syntax: max(x[_]), min(x[_]), etc.
require Dantzig.Problem, as: Problem
require Dantzig.Problem.DSL, as: DSL
IO.puts("=== PATTERN-BASED OPERATIONS DEMONSTRATION ===")
IO.puts("This example shows the new pattern-based syntax for variadic operations")
IO.puts("")
# ============================================================================
# EXAMPLE 1: PATTERN-BASED MAX OPERATION
# ============================================================================
IO.puts("1. PATTERN-BASED MAX OPERATION")
IO.puts("===============================")
IO.puts("Instead of: max(x(1), x(2), x(3), x(4), x(5))")
IO.puts("You can write: max(x(:_))")
IO.puts("")
# Create variables
problem =
Problem.define do
new(name: "Pattern-based Max")
variables("x", [i <- 1..5], :continuous, "Continuous variables")
end
# Get the variable map to show what variables were created
var_map = Problem.get_variables_nd(problem, "x")
IO.puts("✓ Created #{map_size(var_map)} continuous variables:")
Enum.each(var_map, fn {key, _monomial} ->
IO.puts(" - x#{inspect(key)}")
end)
IO.puts("")
IO.puts("Pattern-based syntax: max(x(:_)) would create constraints:")
IO.puts(" - max_x_all >= x(1)")
IO.puts(" - max_x_all >= x(2)")
IO.puts(" - max_x_all >= x(3)")
IO.puts(" - max_x_all >= x(4)")
IO.puts(" - max_x_all >= x(5)")
IO.puts("")
# ============================================================================
# EXAMPLE 2: PATTERN-BASED MIN OPERATION
# ============================================================================
IO.puts("2. PATTERN-BASED MIN OPERATION")
IO.puts("===============================")
IO.puts("Instead of: min(y(1), y(2), y(3))")
IO.puts("You can write: min(y(:_))")
IO.puts("")
# Create more variables
problem2 =
Problem.define do
new(name: "Pattern-based Min", direction: :minimize)
variables("y", [i <- 1..3], :continuous, "More continuous variables")
end
var_map2 = Problem.get_variables_nd(problem2, "y")
IO.puts("✓ Created #{map_size(var_map2)} continuous variables:")
Enum.each(var_map2, fn {key, _monomial} ->
IO.puts(" - y#{inspect(key)}")
end)
IO.puts("")
IO.puts("Pattern-based syntax: min(y(:_)) would create constraints:")
IO.puts(" - min_y_all <= y(1)")
IO.puts(" - min_y_all <= y(2)")
IO.puts(" - min_y_all <= y(3)")
IO.puts("")
# ============================================================================
# EXAMPLE 3: 2D PATTERN-BASED OPERATIONS
# ============================================================================
IO.puts("3. 2D PATTERN-BASED OPERATIONS")
IO.puts("===============================")
IO.puts("For 2D variables, you can use patterns like:")
IO.puts(" - max(x(:_, j)) - maximum over first dimension")
IO.puts(" - min(x(i, :_)) - minimum over second dimension")
IO.puts(" - max(x(:_, :_)) - maximum over all variables")
IO.puts("")
# Create 2D variables
problem3 =
Problem.define do
new(name: "Pattern-based 2D", direction: :minimize)
variables("z", [i <- 1..3, j <- 1..2], :continuous, "2D continuous variables")
end
var_map3 = Problem.get_variables_nd(problem3, "z")
IO.puts("✓ Created #{map_size(var_map3)} 2D continuous variables:")
Enum.each(var_map3, fn {key, _monomial} ->
IO.puts(" - z#{inspect(key)}")
end)
IO.puts("")
IO.puts("Pattern examples:")
IO.puts(" • max(z(:_, 1)) - max of z(1,1), z(2,1), z(3,1)")
IO.puts(" • min(z(2, :_)) - min of z(2,1), z(2,2)")
IO.puts(" • max(z(:_, :_)) - max of all z variables")
IO.puts("")
# ============================================================================
# EXAMPLE 4: BINARY VARIABLES WITH PATTERNS
# ============================================================================
IO.puts("4. BINARY VARIABLES WITH PATTERNS")
IO.puts("==================================")
IO.puts("Pattern-based operations work with binary variables too:")
IO.puts(" - a(1) AND a(2) AND a(3) AND a(4) → a(:_) AND a(:_) AND a(:_) AND a(:_)")
IO.puts(" - b(1) OR b(2) OR b(3) → b(:_) OR b(:_) OR b(:_)")
IO.puts("")
# Create binary variables
problem4 =
Problem.define do
new(name: "Pattern-based Binary", direction: :minimize)
variables("a", [i <- 1..4], :binary, "Binary variables")
end
var_map4 = Problem.get_variables_nd(problem4, "a")
IO.puts("✓ Created #{map_size(var_map4)} binary variables:")
Enum.each(var_map4, fn {key, _monomial} ->
IO.puts(" - a#{inspect(key)}")
end)
IO.puts("")
IO.puts("Pattern-based logical operations:")
IO.puts(" • a(:_) AND a(:_) AND a(:_) AND a(:_) - all must be true")
IO.puts(" • b(:_) OR b(:_) OR b(:_) - at least one must be true")
IO.puts("")
# ============================================================================
# EXAMPLE 5: COMPLEX PATTERN COMBINATIONS
# ============================================================================
IO.puts("5. COMPLEX PATTERN COMBINATIONS")
IO.puts("================================")
IO.puts("You can combine patterns with other operations:")
IO.puts("")
examples = [
"max(x(:_)) + min(y(:_))",
"max(x(:_, j)) - min(x(i, :_))",
"a(:_) AND (b(:_) OR c(:_))",
"max(x(:_)) * min(y(:_))",
"sum(x(:_)) == max(x(:_)) * count(x(:_))"
]
Enum.each(examples, fn example ->
IO.puts(" • #{example}")
end)
IO.puts("")
# ============================================================================
# EXAMPLE 6: PRACTICAL USE CASES
# ============================================================================
IO.puts("6. PRACTICAL USE CASES")
IO.puts("=======================")
IO.puts("")
use_cases = [
{
"Portfolio Optimization",
"max(return(:_)) - min(risk(:_))",
"Maximize best return while minimizing worst risk across all assets"
},
{
"Facility Location",
"min(distance(:_, customer))",
"Find minimum distance to any customer across all facilities"
},
{
"Resource Allocation",
"resource(:_) AND resource(:_) AND resource(:_)",
"All resources must be available (simplified syntax)"
},
{
"Quality Control",
"max(quality(:_)) AND min(defect(:_))",
"Best quality with fewest defects across all products"
},
{
"Network Optimization",
"max(flow(:_, destination))",
"Maximum flow to any destination across all sources"
}
]
Enum.each(use_cases, fn {name, expression, description} ->
IO.puts(" #{name}:")
IO.puts(" Expression: #{expression}")
IO.puts(" Purpose: #{description}")
IO.puts("")
end)
# ============================================================================
# EXAMPLE 7: SYNTAX COMPARISON
# ============================================================================
IO.puts("7. SYNTAX COMPARISON")
IO.puts("====================")
IO.puts("")
comparisons = [
{
"Old Syntax",
"max(x(1), x(2), x(3), x(4), x(5))",
"Verbose, error-prone, hard to maintain"
},
{
"New Pattern Syntax",
"max(x(:_))",
"Concise, clear, automatically scales"
},
{
"Old Syntax",
"min(y(1), y(2), y(3))",
"Must list all variables explicitly"
},
{
"New Pattern Syntax",
"min(y(:_))",
"Automatically includes all y variables"
},
{
"Old Syntax",
"a(1) AND a(2) AND a(3) AND a(4)",
"Repetitive, easy to miss variables"
},
{
"New Pattern Syntax",
"a(:_) AND a(:_) AND a(:_) AND a(:_)",
"Still explicit but more concise"
}
]
Enum.each(comparisons, fn {type, syntax, note} ->
IO.puts(" #{type}:")
IO.puts(" #{syntax}")
IO.puts(" #{note}")
IO.puts("")
end)
IO.puts("🎉 Pattern-based operations make optimization modeling much more elegant!")
IO.puts("")
IO.puts("KEY BENEFITS:")
IO.puts("• Concise syntax: max(x(:_)) instead of max(x(1), x(2), ..., x(n))")
IO.puts("• Automatic scaling: works with any number of variables")
IO.puts("• Less error-prone: no need to list variables explicitly")
IO.puts("• More readable: intent is clearer")
IO.puts("• Maintainable: adding variables doesn't require code changes")
IO.puts("• Flexible: supports complex patterns like x(:_, j) or x(i, :_)")
IO.puts("")
# ============================================================================
# EXAMPLE 8: 4D PATTERN-BASED OPERATIONS (busy[i, j, k, l])
# ============================================================================
IO.puts("8. 4D PATTERN-BASED OPERATIONS (busy[i, j, k, l])")
IO.puts("===================================================")
IO.puts("Demonstrates patterns with four indices:")
IO.puts(" - max(busy(:_, j, k, l)) # max over i dimension")
IO.puts(" - min(busy(i, :_, k, l)) # min over j dimension")
IO.puts(" - max(busy(i, j, :_, l)) # max over k dimension")
IO.puts(" - min(busy(i, j, k, :_)) # min over l dimension")
IO.puts(" - max(busy(:_, :_, :_, :_)) # max across all 4D entries")
IO.puts("")
# Create 4D variables busy[i, j, k, l]
problem5 =
Problem.define do
new(name: "Pattern-based 4D", direction: :minimize)
variables(
"busy",
[i <- 1..2, j <- 1..2, k <- 1..2, l <- 1..2],
:continuous,
"4D busy variables"
)
end
var_map5 = Problem.get_variables_nd(problem5, "busy")
IO.puts("✓ Created #{map_size(var_map5)} 4D continuous variables:")
Enum.each(var_map5, fn {key, _monomial} ->
IO.puts(" - busy#{inspect(key)}")
end)
IO.puts("")
IO.puts("Pattern examples (4D):")
IO.puts(" • max(busy(:_, 1, 2, 1)) - over i: busy(1,1,2,1), busy(2,1,2,1)")
IO.puts(" • min(busy(2, :_, 1, 1)) - over j: busy(2,1,1,1), busy(2,2,1,1)")
IO.puts(" • max(busy(1, 2, :_, 2)) - over k: busy(1,2,1,2), busy(1,2,2,2)")
IO.puts(" • min(busy(1, 1, 2, :_)) - over l: busy(1,1,2,1), busy(1,1,2,2)")
IO.puts(" • max(busy(:_, :_, :_, :_)) - max of all 16 busy variables")
IO.puts("")