-
Notifications
You must be signed in to change notification settings - Fork 19
/
expressions.jl
1077 lines (947 loc) · 33 KB
/
expressions.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# PARAMETER FUNCTION METHODS
################################################################################
# Extend dispatch_variable_ref
function dispatch_variable_ref(
model::InfiniteModel,
index::ParameterFunctionIndex
)::ParameterFunctionRef
return ParameterFunctionRef(model, index)
end
# Extend _add_data_object
function _add_data_object(
model::InfiniteModel,
object::ParameterFunctionData
)::ParameterFunctionIndex
return MOIUC.add_item(model.param_functions, object)
end
# Extend _data_dictionary (reference based)
function _data_dictionary(
fref::ParameterFunctionRef
)::MOIUC.CleverDict{ParameterFunctionIndex, ParameterFunctionData{<:ParameterFunction}}
return JuMP.owner_model(fref).param_functions
end
# Extend _data_object
function _data_object(fref::ParameterFunctionRef)
object = get(_data_dictionary(fref), JuMP.index(fref), nothing)
if isnothing(object)
error("Invalid parameter function reference, cannot find ",
"corresponding object in the model. This is likely ",
"caused by using the reference of a deleted function.")
end
return object
end
# Extend _core_variable_object
function _core_variable_object(fref::ParameterFunctionRef)
return _data_object(fref).func
end
# Extend _object_numbers
function _object_numbers(fref::ParameterFunctionRef)::Vector{Int}
return _core_variable_object(fref).object_nums
end
# Extend _parameter_numbers
function _parameter_numbers(fref::ParameterFunctionRef)::Vector{Int}
return _core_variable_object(fref).parameter_nums
end
"""
build_parameter_function(
_error::Function,
func::Function,
parameter_refs::Union{GeneralVariableRef, AbstractArray{<:GeneralVariableRef}, Tuple}
)::ParameterFunction
Build an [`ParameterFunction`](@ref) object that employs a parameter function
`func` that takes instances of the infinite parameter(s) as input. This can
ultimately by incorporated into expressions to enable nonlinear infinite parameter
behavior and/or incorporate data over infinite domains.
Here `func` should be of the form:
```
func(paramvals...)::Float64
```
where the formatting of `paramvals` is analagous to point variables (and will be
based on the tuple of infinite parameter references given in `parameter_refs`).
Errors if the infinite parameter tuple is formatted incorrectly. The allowed
format follows that of infinite variables. Also errors if the function doesn't
accept a support realization of the `parameter_refs` as input.
**Example**
```julia-repl
julia> f = build_parameter_function(error, sin, t);
```
"""
function build_parameter_function(
_error::Function,
func::Function,
parameter_refs::Union{GeneralVariableRef, AbstractArray{<:GeneralVariableRef}, Tuple};
extra_kwargs...
)
# check for unneeded keywords
for (kwarg, _) in extra_kwargs
_error("Keyword argument $kwarg is not for use with parameter functions.")
end
# process the parameter reference inputs and check
prefs = Collections.VectorTuple(parameter_refs)
_check_parameter_tuple(_error, prefs)
# check the function
supp_type = typeof(Tuple(Vector{Float64}(undef, length(prefs)), prefs))
if !hasmethod(func, supp_type)
_error("Parameter function method `$(func)` is not defined for `$(func)(",
join(Tuple(prefs), ", ") * ")`. Note that the infinite parameter ",
"arguments `" * join(Tuple(prefs), ", ") * "` are checked via a ",
"numeric support (each parameter is a `Float64`) of the same format.")
end
# get the parameter object numbers
object_nums = Int[]
for pref in prefs
union!(object_nums, _object_number(pref))
end
# get the parameter numbers
param_nums = [_parameter_number(pref) for pref in prefs]
# make the variable and return
return ParameterFunction(func, prefs, object_nums, param_nums)
end
# Fallback for weird macro inputs
function build_parameter_function(_error::Function, func, prefs; kwargs...)
_error("Invalid syntax causing unexpected parsing of the function and " *
"infinite parameter arguments.")
end
# Used to update parameter-parameter function mappings
function _update_param_var_mapping(
fref::ParameterFunctionRef,
prefs::Collections.VectorTuple
)::Nothing
for pref in prefs
dependency_list = _parameter_function_dependencies(pref)
if !(JuMP.index(fref) in dependency_list)
push!(dependency_list, JuMP.index(fref))
end
end
return
end
"""
add_parameter_function(model::InfiniteModel, pfunc::ParameterFunction,
[name::String])::GeneralVariableRef
Add an [`ParameterFunction`](@ref) `pfunc` to the `model` using `name` for
printing and return a `GeneralVariableRef` such that it can be embedded in
expressions. Errors if the parameter function `pfunc` points to do not belong to
`model`. Note that `pfunc` should be created using [`build_parameter_function`](@ref).
**Example**
```julia-repl
julia> f = build_parameter_function(error, sin, t);
julia> fref = add_parameter_function(model, f)
sin(t)
```
"""
function add_parameter_function(
model::InfiniteModel,
pfunc::ParameterFunction,
name::String = String(nameof(pfunc.func))
)::GeneralVariableRef
_check_parameters_valid(model, pfunc.parameter_refs)
data_object = ParameterFunctionData(pfunc, name)
findex = _add_data_object(model, data_object)
fref = ParameterFunctionRef(model, findex)
_update_param_var_mapping(fref, pfunc.parameter_refs)
return _make_variable_ref(model, findex)
end
"""
JuMP.name(fref::ParameterFunctionRef)::String
Extend `JuMP.name` to return the base name of `fref`.
**Example**
```julia-repl
julia> name(fref)
"func_name"
```
"""
function JuMP.name(fref::ParameterFunctionRef)::String
object = get(_data_dictionary(fref), JuMP.index(fref), nothing)
return isnothing(object) ? "" : object.name
end
"""
JuMP.set_name(fref::ParameterFunctionRef, name::String)::Nothing
Extend `JuMP.set_name` to set the name of a parameter function.
**Example**
```julia-repl
julia> set_name(fref, "func_name")
julia> name(fref)
"func_name"
```
"""
function JuMP.set_name(fref::ParameterFunctionRef, name::String)::Nothing
_data_object(fref).name = name
return
end
"""
parameter_function(func::Function,
pref_inputs::Union{GeneralVariableRef, AbstractArray{GeneralVariableRef}, Tuple};
[name::String = [the name of `func`]]
)::GeneralVariableRef
Make a parameter function and return a `GeneralVariableRef` that can be
embedded in InfiniteOpt expressions. This serves as a convenient wrapper for
[`build_parameter_function`](@ref) and [`add_parameter_function`](@ref). For an
even more convenient definition method see [`@parameter_function`](@ref).
Here `func` denotes the function that will take a support of infinite parameters as
input (formatted like `pref_inputs`) and will return a scalar value. Specifically,
`func` should be of the form:
```
func(paramvals...)::Float64
```
where the formatting of `paramvals` is analagous to point variables (and will be
based on the tuple of infinite parameter references given in `parameter_refs`).
Moreover, `func` must be a function that returns a scalar numeric value.
Errors if `func` will not take a support formatted like `pref_inputs` in
combination with the `fargs` and `fkwargs` specified. Also errors if `pref_inputs`
follow an invalid input format.
**Example**
```julia-repl
julia> p_func = parameter_function(sin, t)
sin(t)
julia> p_func3 = parameter_function((t_supp) -> 2 * sin(2 * t_supp), t, name = "mysin")
mysin(t)
julia> p_func4 = parameter_function(t, name = "mysin") do t_supp
if t_supp <= 5
return sin(t_supp)
else
return 2 * sin(2 * t_supp)
end
end
mysin(t)
```
"""
function parameter_function(
func::Function,
pref_inputs;
name::String = String(nameof(func))
)::GeneralVariableRef
f = build_parameter_function(error, func, pref_inputs)
model = JuMP.owner_model(first(f.parameter_refs))
return add_parameter_function(model, f, name)
end
"""
raw_parameter_refs(fref::ParameterFunctionRef)::VectorTuple
Return the raw [`VectorTuple`](@ref InfiniteOpt.Collections.VectorTuple) of the
parameter references that `fref` depends on. This is primarily an internal method
where [`parameter_refs`](@ref parameter_refs(::ParameterFunctionRef))
is intended as the preferred user function.
"""
function raw_parameter_refs(fref::ParameterFunctionRef)
return _core_variable_object(fref).parameter_refs
end
"""
parameter_refs(fref::ParameterFunctionRef)::Tuple
Return the parameter references associated with `fref`. This
is formatted as a Tuple of containing the parameter references as they inputted
to define `fref`.
**Example**
```julia-repl
julia> parameter_refs(p_func)
(t,)
```
"""
function parameter_refs(fref::ParameterFunctionRef)
return Tuple(raw_parameter_refs(fref))
end
"""
parameter_list(fref::ParameterFunctionRef)::Vector{GeneralVariableRef}
Return a vector of the parameter references that `fref` depends on. This is
primarily an internal method where [`parameter_refs`](@ref parameter_refs(::ParameterFunctionRef))
is intended as the preferred user function.
"""
function parameter_list(fref::ParameterFunctionRef)::Vector{GeneralVariableRef}
return raw_parameter_refs(fref).values
end
"""
raw_function(fref::ParameterFunctionRef)::Function
Returns the raw function behind `fref` that takes a particular support of `fref`'s
infinite parameters as input.
"""
function raw_function(fref::ParameterFunctionRef)
return _core_variable_object(fref).func
end
"""
call_function(fref::ParameterFunctionRef, support...)::Float64
Safely evaluates the [`raw_function`](@ref) of `fref` at a particular support `support`
point that matches the format of the infinite parameter tuple given when the `fref`
was defined. This is essentially equivalent to `raw_function(fref)(supps...)`.
"""
function call_function(fref::ParameterFunctionRef, supps...)::Float64
pfunc = _core_variable_object(fref)
return pfunc.func(supps...)
end
# Extend _semi_infinite_variable_dependencies
function _semi_infinite_variable_dependencies(
fref::ParameterFunctionRef
)::Vector{SemiInfiniteVariableIndex}
return _data_object(fref).semi_infinite_var_indices
end
# Extend _derivative_dependencies
function _derivative_dependencies(
fref::ParameterFunctionRef
)::Vector{DerivativeIndex}
return _data_object(fref).derivative_indices
end
# Extend _measure_dependencies
function _measure_dependencies(fref::ParameterFunctionRef)::Vector{MeasureIndex}
return _data_object(fref).measure_indices
end
# Extend _constraint_dependencies
function _constraint_dependencies(
fref::ParameterFunctionRef
)::Vector{InfOptConstraintIndex}
return _data_object(fref).constraint_indices
end
"""
used_by_semi_infinite_variable(fref::ParameterFunctionRef)::Bool
Return a `Bool` indicating if `fref` is used by a semi-infinite infinite variable.
**Example**
```julia-repl
julia> used_by_semi_infinite_variable(fref)
false
```
"""
function used_by_semi_infinite_variable(fref::ParameterFunctionRef)::Bool
return !isempty(_semi_infinite_variable_dependencies(fref))
end
"""
used_by_derivative(fref::ParameterFunctionRef)::Bool
Return a `Bool` indicating if `fref` is used by a derivative.
**Example**
```julia-repl
julia> used_by_derivative(vref)
true
```
"""
function used_by_derivative(fref::ParameterFunctionRef)::Bool
return !isempty(_derivative_dependencies(fref))
end
"""
used_by_measure(fref::ParameterFunctionRef)::Bool
Return a `Bool` indicating if `fref` is used by a measure.
**Example**
```julia-repl
julia> used_by_measure(fref)
true
```
"""
function used_by_measure(fref::ParameterFunctionRef)::Bool
return !isempty(_measure_dependencies(fref))
end
"""
used_by_constraint(fref::ParameterFunctionRef)::Bool
Return a `Bool` indicating if `fref` is used by a constraint.
**Example**
```julia-repl
julia> used_by_constraint(fref)
false
```
"""
function used_by_constraint(fref::ParameterFunctionRef)::Bool
return !isempty(_constraint_dependencies(fref))
end
"""
is_used(fref::ParameterFunctionRef)::Bool
Return a `Bool` indicating if `fref` is used in the model.
**Example**
```julia-repl
julia> is_used(fref)
true
```
"""
function is_used(fref::ParameterFunctionRef)::Bool
return used_by_measure(fref) || used_by_constraint(fref) ||
used_by_semi_infinite_variable(fref) || used_by_derivative(fref)
end
"""
JuMP.delete(model::InfiniteModel, fref::ParameterFunctionRef)::Nothing
Extend `JuMP.delete` to delete parameter functions and their dependencies. Errors
if `fref` is invalid, meaning it has already been deleted or it belongs to
another model.
"""
function JuMP.delete(model::InfiniteModel, fref::ParameterFunctionRef)::Nothing
@assert JuMP.is_valid(model, fref) "Parameter function is invalid."
# update the optimizer model status
if is_used(fref)
set_optimizer_model_ready(model, false)
end
# update parameter mapping
all_prefs = parameter_list(fref)
for pref in all_prefs
filter!(e -> e != JuMP.index(fref), _parameter_function_dependencies(pref))
end
gvref = _make_variable_ref(model, JuMP.index(fref))
# delete associated semi-infinite variables and mapping
for index in _semi_infinite_variable_dependencies(fref)
JuMP.delete(model, dispatch_variable_ref(model, index))
end
# delete associated derivative variables and mapping
for index in _derivative_dependencies(fref)
JuMP.delete(model, dispatch_variable_ref(model, index))
end
# remove from measures if used
for mindex in _measure_dependencies(fref)
mref = dispatch_variable_ref(model, mindex)
func = measure_function(mref)
data = measure_data(mref)
if func isa GeneralVariableRef
new_func = zero(JuMP.GenericAffExpr{Float64, GeneralVariableRef})
new_meas = Measure(new_func, data, Int[], Int[], true)
else
_remove_variable(func, gvref)
new_meas = build_measure(func, data)
end
_set_core_variable_object(mref, new_meas)
end
# remove from constraints if used
for cindex in copy(_constraint_dependencies(fref))
cref = _make_constraint_ref(model, cindex)
func = JuMP.jump_function(JuMP.constraint_object(cref))
if func isa GeneralVariableRef
set = JuMP.moi_set(JuMP.constraint_object(cref))
new_func = zero(JuMP.GenericAffExpr{Float64, GeneralVariableRef})
new_constr = JuMP.ScalarConstraint(new_func, set)
_set_core_constraint_object(cref, new_constr)
empty!(_object_numbers(cref))
elseif func isa AbstractArray && any(isequal(gvref), func)
JuMP.delete(model, cref)
else
_remove_variable(func, gvref)
_data_object(cref).object_nums = sort(_object_numbers(func))
end
end
# delete the data object
_delete_data_object(fref)
return
end
################################################################################
# VARIABLE ITERATION
################################################################################
## Create helper methods to interrogate the variables of an expr w/ a function
# Constant
function _interrogate_variables(interrogator::Function, c)
return
end
# GeneralVariableRef
function _interrogate_variables(
interrogator::Function,
v::JuMP.AbstractVariableRef
)
interrogator(v)
return
end
# AffExpr
function _interrogate_variables(
interrogator::Function,
aff::JuMP.GenericAffExpr
)
for (v, _) in aff.terms
interrogator(v)
end
return
end
# QuadExpr
function _interrogate_variables(
interrogator::Function,
quad::JuMP.GenericQuadExpr
)
for (p, _) in quad.terms
interrogator(p.a)
interrogator(p.b)
end
_interrogate_variables(interrogator, quad.aff)
return
end
# NonlinearExpr (avoid recursion to handle deeply nested expressions)
function _interrogate_variables(interrogator::Function, nlp::JuMP.NonlinearExpr)
stack = Vector{Any}[nlp.args]
while !isempty(stack)
args = pop!(stack)
for arg in args
if arg isa JuMP.NonlinearExpr
push!(stack, arg.args)
else
_interrogate_variables(interrogator, arg)
end
end
end
return
end
# AbstractArray
function _interrogate_variables(interrogator::Function, arr::AbstractArray)
for ex in arr
_interrogate_variables(interrogator, ex)
end
return
end
################################################################################
# VARIABLE LIST MAKING
################################################################################
## Determine which variables are present in a function
# GeneralVariableRef
function _all_function_variables(f::GeneralVariableRef)
return [f]
end
# GenericAffExpr
function _all_function_variables(f::JuMP.GenericAffExpr)
return collect(keys(f.terms))
end
# GenericQuadExpr
function _all_function_variables(f::JuMP.GenericQuadExpr)
vref_set = Set(keys(f.aff.terms))
for (pair, _) in f.terms
push!(vref_set, pair.a, pair.b)
end
return collect(vref_set)
end
# NonlinearExpr or array of expressions
function _all_function_variables(f::Union{JuMP.NonlinearExpr, AbstractArray})
vref_set = Set{GeneralVariableRef}()
_interrogate_variables(v -> push!(vref_set, v), f)
return collect(vref_set)
end
# Fallback
function _all_function_variables(f)
error("`_all_function_variables` not defined for expression of type $(typeof(f)).")
end
################################################################################
# OBJECT/PARAMETER NUMBER METHODS
################################################################################
## Return the unique set of object numbers in an expression
# Dispatch fallback (--> should be defined for each non-empty variable type)
_object_numbers(v::DispatchVariableRef) = Int[]
# GeneralVariableRef
function _object_numbers(v::GeneralVariableRef)
return _object_numbers(dispatch_variable_ref(v))
end
# Other
function _object_numbers(expr)
obj_nums = Set{Int}()
_interrogate_variables(v -> union!(obj_nums, _object_numbers(v)), expr)
return collect(obj_nums)
end
## Return the unique set of parameter numbers in an expression
# Dispatch fallback (--> should be defined for each non-empty variable type)
_parameter_numbers(v::DispatchVariableRef) = Int[]
# GeneralVariableRef
function _parameter_numbers(v::GeneralVariableRef)
return _parameter_numbers(dispatch_variable_ref(v))
end
# Other
function _parameter_numbers(expr)
param_nums = Set{Int}()
_interrogate_variables(v -> union!(param_nums, _parameter_numbers(v)), expr)
return collect(param_nums)
end
################################################################################
# MODEL EXTRACTION METHODS
################################################################################
## Get the model from an expression
# Constant
function _model_from_expr(::Union{Number, Bool})
return
end
# GeneralVariableRef
function _model_from_expr(expr::GeneralVariableRef)
return JuMP.owner_model(expr)
end
# AffExpr
function _model_from_expr(expr::JuMP.GenericAffExpr)
if isempty(expr.terms)
return
else
return JuMP.owner_model(first(keys(expr.terms)))
end
end
# QuadExpr
function _model_from_expr(expr::JuMP.GenericQuadExpr)
result = _model_from_expr(expr.aff)
if !isnothing(result)
return result
elseif isempty(expr.terms)
return
else
return JuMP.owner_model(first(keys(expr.terms)).a)
end
end
# NonlinearExpr (avoid recursion for deeply nested expressions)
function _model_from_expr(expr::JuMP.NonlinearExpr)
stack = Vector{Any}[expr.args]
while !isempty(stack)
args = pop!(stack)
for arg in args
if arg isa JuMP.NonlinearExpr
push!(stack, arg.args)
else
result = _model_from_expr(arg)
isnothing(result) || return result
end
end
end
return
end
# Vector{GeneralVariableRef}
function _model_from_expr(vrefs::Vector{GeneralVariableRef})
if isempty(vrefs)
return
else
return JuMP.owner_model(first(vrefs))
end
end
# Fallback
function _model_from_expr(expr)
error("`_model_from_expr` not defined for expr of type $(typeof(expr)).")
end
################################################################################
# VARIABLE REMOVAL BOUNDS
################################################################################
## Delete variables from an expression
# GenericAffExpr
function _remove_variable(f::JuMP.GenericAffExpr, vref::GeneralVariableRef)
if haskey(f.terms, vref)
delete!(f.terms, vref)
end
return
end
# GenericQuadExpr
function _remove_variable(f::JuMP.GenericQuadExpr, vref::GeneralVariableRef)
_remove_variable(f.aff, vref)
for (pair, _) in f.terms
if isequal(pair.a, vref) || isequal(pair.b, vref)
delete!(f.terms, pair)
end
end
return
end
# Helper functions for nonlinear variable deletion
function _remove_variable_from_leaf(c, vref)
return c
end
function _remove_variable_from_leaf(n_vref::GeneralVariableRef, vref)
return isequal(n_vref, vref) ? 0.0 : n_vref
end
function _remove_variable_from_leaf(
ex::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr},
vref
)
_remove_variable(ex, vref)
return ex
end
# Nonlinear (avoid recursion to handle deeply nested expressions)
function _remove_variable(f::JuMP.NonlinearExpr, vref::GeneralVariableRef)
stack = Tuple{Vector{Any}, Int}[]
for i in eachindex(f.args) # should be reverse, but order doesn't matter
push!(stack, (f.args, i))
end
while !isempty(stack)
arr, idx = pop!(stack)
expr = arr[idx]
if expr isa JuMP.NonlinearExpr
for i in eachindex(expr.args) # should be reverse, but order doesn't matter
push!(stack, (expr.args, i))
end
else
arr[idx] = _remove_variable_from_leaf(expr, vref)
end
end
return
end
# AbstractArray
function _remove_variable(arr::AbstractArray, vref::GeneralVariableRef)
for f in arr
_remove_variable(f, vref)
end
return
end
################################################################################
# MAPPING METHODS
################################################################################
"""
map_expression(transform::Function,
expr::JuMP.AbstractJuMPScalar)::JuMP.AbstractJuMPScalar
Map and return a new expression of `expr` where each variable is transformed
via `transform`. This can be helpful for writing user extensions.
"""
function map_expression(transform::Function, v::JuMP.AbstractVariableRef)
return transform(v)
end
# Constant
function map_expression(transform::Function, c::Number)
return c
end
# AffExpr
function map_expression(transform::Function, aff::JuMP.GenericAffExpr)
return _MA.@rewrite(sum(c * transform(v)
for (c, v) in JuMP.linear_terms(aff)) +
JuMP.constant(aff))
end
# QuadExpr
function map_expression(transform::Function, quad::JuMP.GenericQuadExpr)
return _MA.@rewrite(sum(c * transform(v1) * transform(v2)
for (c, v1, v2) in JuMP.quad_terms(quad)) +
map_expression(transform, quad.aff))
end
# NonlinearExpr
function map_expression(transform::Function, nlp::JuMP.NonlinearExpr)
# TODO: Figure out how to make the recursionless code work
# stack = Tuple{Vector{Any}, Vector{Any}}[]
# new_nlp = JuMP.NonlinearExpr{NewVrefType}(nlp.head, Any[]) # Need to add `NewVrefType` arg throughout pkg
# push!(stack, (nlp.args, new_nlp.args))
# while !isempty(stack)
# args, cloned = pop!(stack)
# for arg in args
# if arg isa JuMP.NonlinearExpr
# new_expr = JuMP.NonlinearExpr{NewVrefType}(arg.head, Any[])
# push!(stack, (arg.args, new_expr.args))
# else
# new_expr = map_expression(transform, arg)
# end
# push!(cloned, new_expr)
# end
# end
# return new_nlp
return JuMP.NonlinearExpr(nlp.head, Any[map_expression(transform, arg) for arg in nlp.args])
end
################################################################################
# COEFFICIENT METHODS
################################################################################
## Modify linear coefficient of variable in expression
# GeneralVariableRef
function _set_variable_coefficient!(expr::GeneralVariableRef,
var::GeneralVariableRef,
coeff::Real
)
# Determine if variable is that of the expression and change accordingly
if isequal(expr, var)
return Float64(coeff) * var
else
return expr + Float64(coeff) * var
end
end
# GenericAffExpr
function _set_variable_coefficient!(expr::JuMP.GenericAffExpr,
var::GeneralVariableRef,
coeff::Real
)
# Determine if variable is in the expression and change accordingly
if haskey(expr.terms, var)
expr.terms[var] = coeff
return expr
else
return expr + coeff * var
end
end
# GenericQuadExpr
function _set_variable_coefficient!(expr::JuMP.GenericQuadExpr,
var::GeneralVariableRef,
coeff::Real
)
# Determine if variable is in the expression and change accordingly
if haskey(expr.aff.terms, var)
expr.aff.terms[var] = coeff
return expr
else
return expr + coeff * var
end
end
# Fallback
function _set_variable_coefficient!(expr, var::GeneralVariableRef, coeff::Real)
error("Unsupported function type for coefficient modification.")
end
## Query the coefficient of a variable
# GeneralVariableRef
function _affine_coefficient(
func::GeneralVariableRef,
var::GeneralVariableRef
)
return isequal(func, var) ? 1.0 : 0.0
end
# GenericAffExpr
function _affine_coefficient(
func::GenericAffExpr,
var::GeneralVariableRef
)
return get(func.terms, var, 0.0)
end
# GenericQuadExpr
function _affine_coefficient(
func::GenericQuadExpr,
var::GeneralVariableRef
)
return get(func.aff.terms, var, 0.0)
end
# Fallback
function _affine_coefficient(func, var::GeneralVariableRef)
error("Unsupported function type for coefficient queries.")
end
################################################################################
# PARAMETER REFERENCE METHODS
################################################################################
## Return an element of a parameter reference tuple given the model and index
# IndependentParameterIndex
function _make_param_tuple_element(model::InfiniteModel,
idx::IndependentParameterIndex,
)::GeneralVariableRef
return _make_parameter_ref(model, idx)
end
# DependentParametersIndex
function _make_param_tuple_element(model::InfiniteModel,
idx::DependentParametersIndex,
)::Vector{GeneralVariableRef}
dpref = DependentParameterRef(model, DependentParameterIndex(idx, 1))
num_params = _num_parameters(dpref)
return [GeneralVariableRef(model, idx.value, DependentParameterIndex, i)
for i in 1:num_params]
end
"""
parameter_refs(expr)::Tuple
Return the tuple of parameter references that determine the infinite
dependencies of `expr`.
**Example**
```julia-repl
julia> parameter_refs(my_expr)
(t,)
```
"""
function parameter_refs(
expr::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, JuMP.NonlinearExpr}
)
model = _model_from_expr(expr)
if isnothing(model)
return ()
else
obj_nums = sort!(_object_numbers(expr))
obj_indices = _param_object_indices(model)[obj_nums]
return Tuple(_make_param_tuple_element(model, idx) for idx in obj_indices)
end
end
################################################################################
# VARIABLE ITERATION (IN PROGRESS)
################################################################################
# struct Variables{T}
# expr::T
# end
# function Base.iterate(itr::Variables{<:JuMP.AbstractVariableRef})
# return itr.tree_root, nothing
# end
# function Base.iterate(itr::Variables{<:JuMP.AbstractVariableRef}, ::Nothing)
# return
# end
# Base.length(itr::Variables{<:JuMP.AbstractVariableRef}) = 1
# Base.eltype(::Variables{V}) where {V <: JuMP.AbstractVariableRef} = V
# function Base.iterate(itr::Variables{<:JuMP.GenericAffExpr})
# out = iterate(itr.expr.terms)
# return out === nothing ? out : (out[1][1], out[2])
# end
# function Base.iterate(itr::Variables{<:JuMP.GenericAffExpr}, state)
# out = iterate(itr.expr.terms, state)
# return out === nothing ? out : (out[1][1], out[2])
# end
# Base.length(itr::Variables{<:JuMP.GenericAffExpr}) = length(itr.expr.terms)
# Base.eltype(::Variables{JuMP.GenericAffExpr{C, V}}) where {C, V} = V
# mutable struct _QuadItrData{V <: JuMP.AbstractVariableRef}
# state::Int
# use_aff::Bool
# has_prev_term::Bool
# next_term::V
# end
# function Base.iterate(itr::Variables{JuMP.GenericQuadExpr{C, V}}) where {C, V}
# out = iterate(itr.expr.terms)
# if out === nothing
# aff_out = iterate(itr.expr.aff.terms)
# if aff_out === nothing
# return
# else
# return aff_out[1][1], _QuadItrData{V}(aff_out[2], true, false, aff_out[1][1])
# end
# end
# return out[1][1].a, _QuadItrData{V}(out[2], false, true, out[1][1].b)
# end
# function Base.iterate(itr::Variables{<:JuMP.GenericQuadExpr}, state)
# if state.has_prev_term
# state.has_prev_term = false
# return state.next_term, state
# elseif !state.use_aff
# out = iterate(itr.expr.terms, state.state)
# if out === nothing
# aff_out = iterate(itr.expr.aff.terms)
# if aff_out === nothing
# return
# else
# state.use_aff = true
# state.state = aff_out[2]
# return aff_out[1][1], state
# end
# end
# state.state = out[2]
# state.has_prev_term = true
# state.next_term = out[1][1].b
# return out[1][1].a, state
# else
# out = iterate(itr.expr.aff.terms, state.state)
# if out === nothing