/
@NL.jl
708 lines (603 loc) · 20.3 KB
/
@NL.jl
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""
_parse_nonlinear_expression(model::GenericModel, x::Expr)
JuMP needs to build Nonlinear expression objects in macro scope. This has two
main challenges:
1. We need to evaluate local variables into the expressions. This is reasonably
easy, anywhere we see a symbol that is not a function call, replace it by
`esc(x)`.
2. We need to identify un-registered user-defined functions so that we can
attempt to automatically register them if their symbolic name exists in the
scope. I (@odow) originally introduced the auto-registration in
https://github.com/jump-dev/JuMP.jl/pull/2537 to fix a common pain-point in
JuMP, but after working through this I believe it was a mistake. It's a lot
of hassle! One problem is that the design of Nonlinear has moved the
expression parsing from macro-expansion time to runtime. I think this is a
big win for readability of the system, but it means we loose access to the
caller's local scope. My solution to maintain backwards compatibility is to
check that every function call is registered before parsing the expression.
"""
function _parse_nonlinear_expression(model, x)
code = quote
_init_NLP($model)
end
operators = Set{Tuple{Symbol,Int}}()
_assert_constant_comparison(code, x)
y = _parse_nonlinear_expression_inner(code, x, operators)
user_defined_operators = filter(operators) do (op, i)
if op in (:<=, :>=, :(==), :<, :>, :&&, :||)
return false
elseif i == 1 && op in MOI.Nonlinear.DEFAULT_UNIVARIATE_OPERATORS
return false
elseif i > 1 && op in MOI.Nonlinear.DEFAULT_MULTIVARIATE_OPERATORS
return false
end
return true
end
if length(user_defined_operators) > 0
op_var = gensym()
push!(code.args, :($op_var = $(model).nlp_model.operators))
for (op, i) in collect(user_defined_operators)
push!(code.args, _auto_register_expression(op_var, op, i))
end
end
return code, y
end
# JuMP special-cases the error for constant LHS and RHS terms in a comparison
# expression. In JuMP v1.1 and earlier, it evaluated the outer terms in the
# current scope, and checked to see if they were Real. To keep the same behavior
# we do the same here.
function _assert_constant_comparison(code::Expr, expr::Expr)
if Meta.isexpr(expr, :comparison)
lhs, rhs = gensym(), gensym()
push!(code.args, esc(:($lhs = $(expr.args[1]))))
push!(code.args, esc(:($rhs = $(expr.args[5]))))
expr.args[1], expr.args[5] = lhs, rhs
end
return
end
_assert_constant_comparison(::Expr, ::Any) = nothing
function _auto_register_expression(op_var, op, i)
q_op = Meta.quot(op)
return quote
try
MOI.Nonlinear.register_operator_if_needed(
$op_var,
$q_op,
$i,
$(esc(op)),
)
catch
end
MOI.Nonlinear.assert_registered($op_var, $q_op, $i)
end
end
function _normalize_unicode(x::Symbol)
if x == :≤
return :<=
elseif x == :≥
return :>=
else
return x
end
end
function _parse_nonlinear_expression_inner(::Any, x::Symbol, ::Any)
x = _normalize_unicode(x)
if x in (:<=, :>=, :(==), :<, :>, :&&, :||)
return Meta.quot(x)
end
return esc(x)
end
# Numbers and other literal constants.
_parse_nonlinear_expression_inner(::Any, x, ::Any) = x
function _is_generator(x)
return Meta.isexpr(x, :call) &&
length(x.args) >= 2 &&
(
Meta.isexpr(x.args[end], :generator) ||
Meta.isexpr(x.args[end], :flatten)
)
end
function _parse_nonlinear_expression_inner(code, x::Expr, operators)
if Meta.isexpr(x, :block)
error(
"`begin...end` blocks are not supported in nonlinear macros. The " *
"nonlinear expression must be a single statement.",
)
end
if Meta.isexpr(x, :ref)
return esc(x)
elseif Meta.isexpr(x, :.)
return esc(x)
elseif _is_generator(x)
return _parse_generator_expression(code, x, operators)
elseif Meta.isexpr(x, Symbol("'"))
# Treat the adjoint operator as a special case, because people often
# use linear algebra in macros.
return esc(x)
end
y = gensym()
y_expr = :($y = Expr($(Meta.quot(x.head))))
offset = 1
if Meta.isexpr(x, :call)
if !(x.args[1] isa Symbol)
error(
"Unsupported function $(x.args[1]). All function calls must " *
"be `Symbol`s.",
)
end
op = _normalize_unicode(x.args[1])
push!(operators, (op, length(x.args) - 1))
push!(y_expr.args[2].args, Meta.quot(op))
offset += 1
end
for i in offset:length(x.args)
arg = _parse_nonlinear_expression_inner(code, x.args[i], operators)
push!(y_expr.args[2].args, arg)
end
push!(code.args, y_expr)
return y
end
_is_sum(s::Symbol) = (s == :sum) || (s == :∑) || (s == :Σ)
_is_prod(s::Symbol) = (s == :prod) || (s == :∏)
function _parse_generator_expression(code, x, operators)
y = gensym()
y_expr, default = if _is_sum(x.args[1])
:($y = Expr(:call, :+)), 0
elseif _is_prod(x.args[1])
:($y = Expr(:call, :*)), 1
elseif x.args[1] == :maximum
:($y = Expr(:call, :max)), nothing
elseif x.args[1] == :minimum
:($y = Expr(:call, :min)), nothing
else
error("Unsupported generator `:$(x.args[1])`")
end
body = x.args[2]
has_init = false
# foo(generator; init = value)
if Meta.isexpr(x.args[2], :parameters)
for kw in x.args[2].args
if !Meta.isexpr(kw, :kw) || kw.args[1] != :init
error("Unsupported nonlinear expression: $x")
end
if kw.args[2] != default
default = esc(kw.args[2])
push!(y_expr.args[2].args, default)
has_init = true
end
end
body = x.args[3]
end
# foo(generator, init = value)
if Meta.isexpr(x.args[2], :generator, 3)
kw = x.args[2].args[3]
if Meta.isexpr(kw, :(=), 2) && kw.args[1] == :init
pop!(x.args[2].args)
if kw.args[2] != default
default = esc(kw.args[2])
push!(y_expr.args[2].args, default)
has_init = true
end
end
end
block = _MA.rewrite_generator(
body,
t -> begin
new_code = quote end
arg = _parse_nonlinear_expression_inner(new_code, t, operators)
push!(new_code.args, :(push!($y.args, $arg)))
new_code
end,
)
# Special case that was handled by JuMP in the past.
error_string = "reducing over an empty collection in `$(x.args[1])` is not allowed"
push!(code.args, quote
$y_expr
$block
if length($y.args) == $(has_init ? 2 : 1)
if $default === nothing
throw(ArgumentError($error_string))
else
$y = $default
end
end
end)
return y
end
###
### @NLobjective(s)
###
"""
@NLobjective(model, sense, expression)
Add a nonlinear objective to `model` with optimization sense `sense`.
`sense` must be `Max` or `Min`.
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace `@NLobjective` with [`@objective`](@ref).
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x)
x
julia> @NLobjective(model, Max, 2x + 1 + sin(x))
julia> print(model)
Max 2.0 * x + 1.0 + sin(x)
Subject to
```
"""
macro NLobjective(model, sense, x)
error_fn =
Containers.build_error_fn(:NLobjective, (model, sense, x), __source__)
sense_expr = _parse_moi_sense(error_fn, sense)
esc_model = esc(model)
parsing_code, expr = _parse_nonlinear_expression(esc_model, x)
code = quote
$parsing_code
set_nonlinear_objective($esc_model, $sense_expr, $expr)
end
return _finalize_macro(esc_model, code, __source__)
end
###
### @NLconstraint(s)
###
"""
@NLconstraint(model::GenericModel, expr)
Add a constraint described by the nonlinear expression `expr`. See also
[`@constraint`](@ref).
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace `@NLconstraint` with [`@constraint`](@ref).
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x)
x
julia> @NLconstraint(model, sin(x) <= 1)
sin(x) - 1.0 ≤ 0
julia> @NLconstraint(model, [i = 1:3], sin(i * x) <= 1 / i)
3-element Vector{NonlinearConstraintRef{ScalarShape}}:
(sin(1.0 * x) - 1.0 / 1.0) - 0.0 ≤ 0
(sin(2.0 * x) - 1.0 / 2.0) - 0.0 ≤ 0
(sin(3.0 * x) - 1.0 / 3.0) - 0.0 ≤ 0
```
"""
macro NLconstraint(m, x, args...)
error_fn =
Containers.build_error_fn(:NLconstraint, (m, x, args...), __source__)
esc_m = esc(m)
if Meta.isexpr(x, :block)
error_fn("Invalid syntax. Did you mean to use `@NLconstraints`?")
end
# Two formats:
# - @NLconstraint(m, a*x <= 5)
# - @NLconstraint(m, myref[a=1:5], sin(x^a) <= 5)
extra, kw_args, requested_container = Containers._extract_kw_args(args)
if length(extra) > 1 || length(kw_args) > 0
error_fn("too many arguments.")
end
# Canonicalize the arguments
c = length(extra) == 1 ? x : nothing
con = length(extra) == 1 ? extra[1] : x
# Strategy: build up the code for non-macro add_constraint, and if needed
# we will wrap in loops to assign to the ConstraintRefs
name, idxvars, indices =
Containers.parse_ref_sets(error_fn, c; invalid_index_variables = [m])
parsing_code, expr = _parse_nonlinear_expression(esc_m, con)
code = quote
$parsing_code
add_nonlinear_constraint($esc_m, $expr)
end
looped =
Containers.container_code(idxvars, indices, code, requested_container)
creation_code = quote
_init_NLP($esc_m)
$looped
end
return _finalize_macro(
esc_m,
creation_code,
__source__;
register_name = name,
)
end
"""
@NLconstraints(model, args...)
Adds multiple nonlinear constraints to model at once, in the same fashion as
the [`@NLconstraint`](@ref) macro.
The model must be the first argument, and multiple constraints can be added on
multiple lines wrapped in a `begin ... end` block.
The macro returns a tuple containing the constraints that were defined.
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace `@NLconstraints` with [`@constraints`](@ref).
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> @variable(model, y);
julia> @variable(model, t);
julia> @variable(model, z[1:2]);
julia> a = [4, 5];
julia> @NLconstraints(model, begin
t >= sqrt(x^2 + y^2)
[i = 1:2], z[i] <= log(a[i])
end)
((t - sqrt(x ^ 2.0 + y ^ 2.0)) - 0.0 ≥ 0, NonlinearConstraintRef{ScalarShape}[(z[1] - log(4.0)) - 0.0 ≤ 0, (z[2] - log(5.0)) - 0.0 ≤ 0])
```
"""
macro NLconstraints(model, block)
return _plural_macro_code(model, block, Symbol("@NLconstraint"))
end
###
### @NLexpression(s)
###
"""
@NLexpression(args...)
Efficiently build a nonlinear expression which can then be inserted in other
nonlinear constraints and the objective. See also [`@expression`].
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace `@NLexpression` with [`@expression`](@ref).
## Example
```jldoctest api_nlexpression
julia> model = Model();
julia> @variable(model, x)
x
julia> @variable(model, y)
y
julia> @NLexpression(model, my_expr, sin(x)^2 + cos(x^2))
subexpression[1]: sin(x) ^ 2.0 + cos(x ^ 2.0)
julia> @NLconstraint(model, my_expr + y >= 5)
(subexpression[1] + y) - 5.0 ≥ 0
julia> @NLobjective(model, Min, my_expr)
```
Indexing over sets and anonymous expressions are also supported:
```jldoctest api_nlexpression
julia> @NLexpression(model, my_expr_1[i=1:3], sin(i * x))
3-element Vector{NonlinearExpression}:
subexpression[2]: sin(1.0 * x)
subexpression[3]: sin(2.0 * x)
subexpression[4]: sin(3.0 * x)
julia> my_expr_2 = @NLexpression(model, log(1 + sum(exp(my_expr_1[i]) for i in 1:2)))
subexpression[5]: log(1.0 + (exp(subexpression[2]) + exp(subexpression[3])))
```
"""
macro NLexpression(args...)
error_fn = Containers.build_error_fn(:NLexpression, args, __source__)
args, kw_args, requested_container = Containers._extract_kw_args(args)
if length(args) <= 1
error_fn(
"To few arguments ($(length(args))); must pass the model and nonlinear expression as arguments.",
)
elseif length(args) == 2
m, x = args
c = nothing
elseif length(args) == 3
m, c, x = args
end
if Meta.isexpr(args[2], :block)
error_fn("Invalid syntax. Did you mean to use `@NLexpressions`?")
end
if length(args) > 3 || length(kw_args) > 0
error_fn("To many arguments ($(length(args))).")
end
name, idxvars, indices = Containers.parse_ref_sets(error_fn, c)
name, idxvars, indices = Containers.parse_ref_sets(
error_fn,
c;
invalid_index_variables = [args[1]],
)
esc_m = esc(m)
parsing_code, expr = _parse_nonlinear_expression(esc_m, x)
code = quote
$parsing_code
add_nonlinear_expression($esc_m, $expr)
end
creation_code =
Containers.container_code(idxvars, indices, code, requested_container)
return _finalize_macro(
esc_m,
creation_code,
__source__;
register_name = name,
)
end
"""
@NLexpressions(model, args...)
Adds multiple nonlinear expressions to model at once, in the same fashion as the
[`@NLexpression`](@ref) macro.
The model must be the first argument, and multiple expressions can be added on
multiple lines wrapped in a `begin ... end` block.
The macro returns a tuple containing the expressions that were defined.
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace `@NLexpressions` with [`@expressions`](@ref).
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> @variable(model, y);
julia> @variable(model, z[1:2]);
julia> a = [4, 5];
julia> @NLexpressions(model, begin
my_expr, sqrt(x^2 + y^2)
my_expr_1[i = 1:2], log(a[i]) - z[i]
end)
(subexpression[1]: sqrt(x ^ 2.0 + y ^ 2.0), NonlinearExpression[subexpression[2]: log(4.0) - z[1], subexpression[3]: log(5.0) - z[2]])
```
"""
macro NLexpressions(model, block)
return _plural_macro_code(model, block, Symbol("@NLexpression"))
end
###
### @NLparameter(s)
###
"""
@NLparameter(model, param == value)
Create and return a nonlinear parameter `param` attached to the model `model`
with initial value set to `value`. Nonlinear parameters may be used only in
nonlinear expressions.
## Example
```jldoctest
julia> model = Model();
julia> @NLparameter(model, x == 10)
x == 10.0
julia> value(x)
10.0
```
@NLparameter(model, value = param_value)
Create and return an anonymous nonlinear parameter `param` attached to the model
`model` with initial value set to `param_value`. Nonlinear parameters may be
used only in nonlinear expressions.
## Example
```jldoctest
julia> model = Model();
julia> x = @NLparameter(model, value = 10)
parameter[1] == 10.0
julia> value(x)
10.0
```
@NLparameter(model, param_collection[...] == value_expr)
Create and return a collection of nonlinear parameters `param_collection`
attached to the model `model` with initial value set to `value_expr` (may
depend on index sets).
Uses the same syntax for specifying index sets as [`@variable`](@ref).
## Example
```jldoctest
julia> model = Model();
julia> @NLparameter(model, y[i = 1:3] == 2 * i)
3-element Vector{NonlinearParameter}:
parameter[1] == 2.0
parameter[2] == 4.0
parameter[3] == 6.0
julia> value(y[2])
4.0
```
@NLparameter(model, [...] == value_expr)
Create and return an anonymous collection of nonlinear parameters attached to
the model `model` with initial value set to `value_expr` (may depend on index
sets). Uses the same syntax for specifying index sets as [`@variable`](@ref).
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace a call like `@NLparameter(model, p == value)` with
`@variable(model, p in Parameter(value))`.
## Example
```jldoctest
julia> model = Model();
julia> y = @NLparameter(model, [i = 1:3] == 2 * i)
3-element Vector{NonlinearParameter}:
parameter[1] == 2.0
parameter[2] == 4.0
parameter[3] == 6.0
julia> value(y[2])
4.0
```
"""
macro NLparameter(model, args...)
esc_m = esc(model)
error_fn =
Containers.build_error_fn(:NLparameter, (model, args...), __source__)
pos_args, kw_args, requested_container = Containers._extract_kw_args(args)
value = missing
for arg in kw_args
if arg.args[1] == :value
value = arg.args[2]
end
end
kw_args = filter(kw -> kw.args[1] != :value, kw_args)
if !ismissing(value) && length(pos_args) > 0
error_fn(
"Invalid syntax: no positional args allowed for anonymous " *
"parameters.",
)
elseif length(pos_args) > 1
error_fn("Invalid syntax: too many positional arguments.")
elseif length(kw_args) > 0
error_fn("Invalid syntax: unsupported keyword arguments.")
elseif ismissing(value) && Meta.isexpr(pos_args[1], :block)
error_fn("Invalid syntax: did you mean to use `@NLparameters`?")
elseif ismissing(value)
ex = pos_args[1]
if !Meta.isexpr(ex, :call) ||
length(ex.args) != 3 ||
ex.args[1] != :(==)
error_fn(
"Invalid syntax: expected syntax of form `param == value`.",
)
end
end
param = nothing
if ismissing(value)
param, value = pos_args[1].args[2], pos_args[1].args[3]
end
name, index_vars, index_values = Containers.parse_ref_sets(
error_fn,
param;
invalid_index_variables = [model],
)
code = quote
if !isa($(esc(value)), Number)
$(esc(error_fn))("Parameter value is not a number.")
end
add_nonlinear_parameter($esc_m, $(esc(value)))
end
creation_code = Containers.container_code(
index_vars,
index_values,
code,
requested_container,
)
return _finalize_macro(
esc_m,
creation_code,
__source__;
register_name = name,
)
end
"""
@NLparameters(model, args...)
Create and return multiple nonlinear parameters attached to model `model`, in
the same fashion as [`@NLparameter`](@ref) macro.
The model must be the first argument, and multiple parameters can be added on
multiple lines wrapped in a `begin ... end` block. Distinct parameters need to
be placed on separate lines as in the following example.
The macro returns a tuple containing the parameters that were defined.
!!! compat
This macro is part of the legacy nonlinear interface. Consider using the
new nonlinear interface documented in [Nonlinear Modeling](@ref). In most
cases, you can replace a call like
```julia
@NLparameters(model, begin
p == value
end)
```
with
```julia
@variables(model, begin
p in Parameter(value)
end)
```
## Example
```jldoctest
julia> model = Model();
julia> @NLparameters(model, begin
x == 10
b == 156
end);
julia> value(x)
10.0
```
"""
macro NLparameters(model, block)
return _plural_macro_code(model, block, Symbol("@NLparameter"))
end