-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathvariables.jl
1806 lines (1494 loc) · 48.7 KB
/
variables.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
# 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/.
"""
num_variables(model::Model)::Int64
Returns number of variables in `model`.
"""
num_variables(model::Model)::Int64 = MOI.get(model, MOI.NumberOfVariables())
"""
AbstractVariable
Variable returned by [`build_variable`](@ref). It represents a variable that has
not been added yet to any model. It can be added to a given `model` with
[`add_variable`](@ref).
"""
abstract type AbstractVariable end
# Any fields can usually be either a number or an expression
mutable struct _VariableInfoExpr
has_lb::Bool
lower_bound::Any
has_ub::Bool
upper_bound::Any
has_fix::Bool
fixed_value::Any
has_start::Bool
start::Any
binary::Any
integer::Any
end
function _set_lower_bound_or_error(
_error::Function,
info::_VariableInfoExpr,
lower,
)
if info.has_lb
_error("Cannot specify variable lower_bound twice")
end
info.has_lb = true
info.lower_bound = lower
return
end
function _set_upper_bound_or_error(
_error::Function,
info::_VariableInfoExpr,
upper,
)
if info.has_ub
_error("Cannot specify variable upper_bound twice")
end
info.has_ub = true
info.upper_bound = upper
return
end
function _fix_or_error(_error::Function, info::_VariableInfoExpr, value)
if info.has_fix
_error("Cannot specify variable fixed value twice")
end
info.has_fix = true
info.fixed_value = value
return
end
function _set_binary_or_error(_error::Function, info::_VariableInfoExpr)
if info.binary
_error("'Bin' and 'binary' keyword argument cannot both be specified.")
end
info.binary = true
return
end
function _set_integer_or_error(_error::Function, info::_VariableInfoExpr)
if info.integer
_error("'Int' and 'integer' keyword argument cannot both be specified.")
end
info.integer = true
return
end
function _is_info_keyword(kw::Expr)
return kw.args[1] in [:lower_bound, :upper_bound, :start, :binary, :integer]
end
# :(start = 0) -> (:start, 0)
# :(start = i + 1) -> (:start, :($(Expr(:escape, :(i + 1)))))
function _keywordify(kw::Expr)
return (kw.args[1], _esc_non_constant(kw.args[2]))
end
function _VariableInfoExpr(;
lower_bound = NaN,
upper_bound = NaN,
start = NaN,
binary = false,
integer = false,
)
# isnan(::Expr) is not defined so we need to do !== NaN
return _VariableInfoExpr(
lower_bound !== NaN,
lower_bound,
upper_bound !== NaN,
upper_bound,
false,
NaN,
start !== NaN,
start,
binary,
integer,
)
end
# It isn't sufficient to use `isfinite` below, because some bounds are given as
# matrices. As a fallback, we define `_isfinite`, because overloading `isfinite`
# would be type piracy.
_isfinite(x::Number) = isfinite(x)
_isfinite(x) = true
"""
VariableInfo{S,T,U,V}
A struct by JuMP internally when creating variables. This may also be used by
JuMP extensions to create new types of variables.
See also: [`ScalarVariable`](@ref).
"""
struct VariableInfo{S,T,U,V}
has_lb::Bool
lower_bound::S
has_ub::Bool
upper_bound::T
has_fix::Bool
fixed_value::U
has_start::Bool
start::V
binary::Bool
integer::Bool
function VariableInfo(
has_lb::Bool,
lower_bound::S,
has_ub::Bool,
upper_bound::T,
has_fix::Bool,
fixed_value::U,
has_start::Bool,
start::V,
binary::Bool,
integer::Bool,
) where {S,T,U,V}
if has_lb && !_isfinite(lower_bound)
has_lb = false
lower_bound = NaN
end
if has_ub && !_isfinite(upper_bound)
has_ub = false
upper_bound = NaN
end
if has_fix && !_isfinite(fixed_value)
error("Unable to fix variable to $(fixed_value)")
end
return new{typeof(lower_bound),typeof(upper_bound),U,V}(
has_lb,
lower_bound,
has_ub,
upper_bound,
has_fix,
fixed_value,
has_start,
start,
binary,
integer,
)
end
end
function _constructor_expr(info::_VariableInfoExpr)
return :(VariableInfo(
$(info.has_lb),
$(info.lower_bound),
$(info.has_ub),
$(info.upper_bound),
$(info.has_fix),
$(info.fixed_value),
$(info.has_start),
$(info.start),
$(info.binary),
$(info.integer),
))
end
"""
ScalarVariable{S,T,U,V} <: AbstractVariable
A struct used when adding variables.
See also: [`add_variable`](@ref).
"""
struct ScalarVariable{S,T,U,V} <: AbstractVariable
info::VariableInfo{S,T,U,V}
end
"""
AbstractVariableRef
Variable returned by [`add_variable`](@ref). Affine (resp. quadratic) operations
with variables of type `V<:AbstractVariableRef` and coefficients of type `T`
create a `GenericAffExpr{T,V}` (resp. `GenericQuadExpr{T,V}`).
"""
abstract type AbstractVariableRef <: AbstractJuMPScalar end
variable_ref_type(v::AbstractVariableRef) = typeof(v)
Base.conj(v::AbstractVariableRef) = v
Base.real(v::AbstractVariableRef) = v
Base.imag(v::AbstractVariableRef) = zero(v)
Base.abs2(v::AbstractVariableRef) = v^2
Base.isreal(::AbstractVariableRef) = true
"""
VariableRef <: AbstractVariableRef
Holds a reference to the model and the corresponding MOI.VariableIndex.
"""
struct VariableRef <: AbstractVariableRef
model::Model
index::MOI.VariableIndex
end
# `AbstractVariableRef` types must override the default `owner_model` if the field
# name is not `model`.
"""
owner_model(v::AbstractVariableRef)
Returns the model to which `v` belongs.
## Example
```jldoctest
julia> model = Model();
julia> x = @variable(model)
_[1]
julia> owner_model(x) === model
true
```
"""
owner_model(v::AbstractVariableRef) = v.model
"""
struct VariableNotOwned{V<:AbstractVariableRef} <: Exception
variable::V
end
The variable `variable` was used in a model different to
`owner_model(variable)`.
"""
struct VariableNotOwned{V<:AbstractVariableRef} <: Exception
variable::V
end
"""
check_belongs_to_model(func::AbstractJuMPScalar, model::AbstractModel)
Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of one of the
variables of the function `func` is not `model`.
check_belongs_to_model(constraint::AbstractConstraint, model::AbstractModel)
Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of one of the
variables of the constraint `constraint` is not `model`.
"""
function check_belongs_to_model end
function check_belongs_to_model(v::AbstractVariableRef, model::AbstractModel)
if owner_model(v) !== model
throw(VariableNotOwned(v))
end
end
Base.iszero(::VariableRef) = false
Base.copy(v::VariableRef) = VariableRef(v.model, v.index)
Base.broadcastable(v::VariableRef) = Ref(v)
Base.zero(v::AbstractVariableRef) = zero(typeof(v))
function Base.zero(::Type{V}) where {V<:AbstractVariableRef}
return zero(GenericAffExpr{Float64,V})
end
Base.one(v::AbstractVariableRef) = one(typeof(v))
function Base.one(::Type{V}) where {V<:AbstractVariableRef}
return one(GenericAffExpr{Float64,V})
end
"""
coefficient(v1::VariableRef, v2::VariableRef)
Return `1.0` if `v1 == v2`, and `0.0` otherwise.
This is a fallback for other [`coefficient`](@ref) methods to simplify code in
which the expression may be a single variable.
"""
coefficient(v1::VariableRef, v2::VariableRef) = (v1 == v2 ? 1.0 : 0.0)
coefficient(v1::VariableRef, v2::VariableRef, v3::VariableRef) = 0.0
isequal_canonical(v::VariableRef, other::VariableRef) = isequal(v, other)
"""
delete(model::Model, variable_ref::VariableRef)
Delete the variable associated with `variable_ref` from the model `model`.
Note that `delete` does not unregister the name from the model, so adding a new
variable of the same name will throw an error. Use [`unregister`](@ref) to
unregister the name after deletion.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x)
x
julia> delete(model, x)
julia> unregister(model, :x)
julia> print(model)
Feasibility
Subject to
julia> model[:x]
ERROR: KeyError: key :x not found
Stacktrace:
[...]
```
"""
function delete(model::Model, variable_ref::VariableRef)
if model !== owner_model(variable_ref)
error(
"The variable reference you are trying to delete does not " *
"belong to the model.",
)
end
model.is_model_dirty = true
MOI.delete(backend(model), variable_ref.index)
return
end
"""
delete(model::Model, variable_refs::Vector{VariableRef})
Delete the variables associated with `variable_refs` from the model `model`.
Solvers may implement methods for deleting multiple variables that are
more efficient than repeatedly calling the single variable delete method.
See also: [`unregister`](@ref)
"""
function delete(model::Model, variable_refs::Vector{VariableRef})
if any(model !== owner_model(v) for v in variable_refs)
error(
"A variable reference you are trying to delete does not " *
"belong to the model.",
)
end
model.is_model_dirty = true
MOI.delete(backend(model), index.(variable_refs))
return
end
"""
is_valid(model::Model, variable_ref::VariableRef)
Return `true` if `variable` refers to a valid variable in `model`.
"""
function is_valid(model::Model, variable_ref::VariableRef)
return model === owner_model(variable_ref) &&
MOI.is_valid(backend(model), variable_ref.index)
end
# The default hash is slow. It's important for the performance of AffExpr to
# define our own.
# https://github.com/jump-dev/MathOptInterface.jl/issues/234#issuecomment-366868878
function Base.hash(v::VariableRef, h::UInt)
return hash(objectid(owner_model(v)), hash(v.index.value, h))
end
function Base.isequal(v1::VariableRef, v2::VariableRef)
return owner_model(v1) === owner_model(v2) && v1.index == v2.index
end
"""
index(v::VariableRef)::MOI.VariableIndex
Return the index of the variable that corresponds to `v` in the MOI backend.
"""
index(v::VariableRef) = v.index
function VariableRef(m::Model)
index = MOI.add_variable(backend(m))
return VariableRef(m, index)
end
"""
VariableRef(c::ConstraintRef)
Get the variable associated with a `ConstraintRef`, if `c` is a constraint on a
single variable.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, x >= 0)
x
julia> c = LowerBoundRef(x)
x ≥ 0.0
julia> VariableRef(c) == x
true
```
"""
function VariableRef(
c::ConstraintRef{<:AbstractModel,<:MOI.ConstraintIndex{MOI.VariableIndex}},
)
return VariableRef(owner_model(c), MOI.VariableIndex(index(c).value))
end
# Name setter/getters
# These functions need to be implemented for all `AbstractVariableRef`s
"""
name(v::VariableRef)::String
Get a variable's name attribute.
"""
function name(v::VariableRef)
model = owner_model(v)
if !MOI.supports(backend(model), MOI.VariableName(), MOI.VariableIndex)
return ""
end
return MOI.get(model, MOI.VariableName(), v)::String
end
"""
set_name(v::VariableRef, s::AbstractString)
Set a variable's name attribute.
"""
function set_name(v::VariableRef, s::String)
MOI.set(owner_model(v), MOI.VariableName(), v, s)
return
end
"""
variable_by_name(
model::AbstractModel,
name::String,
)::Union{AbstractVariableRef,Nothing}
Returns the reference of the variable with name attribute `name` or `Nothing` if
no variable has this name attribute. Throws an error if several variables have
`name` as their name attribute.
## Examples
```jldoctest objective_function; filter = r"Stacktrace:.*"s
julia> model = Model();
julia> @variable(model, x)
x
julia> variable_by_name(model, "x")
x
julia> @variable(model, base_name="x")
x
julia> variable_by_name(model, "x")
ERROR: Multiple variables have the name x.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] get(::MOIU.Model{Float64}, ::Type{MathOptInterface.VariableIndex}, ::String) at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/model.jl:222
[3] get at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:201 [inlined]
[4] get(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MOIU.Model{Float64}}}, ::Type{MathOptInterface.VariableIndex}, ::String) at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:490
[5] variable_by_name(::Model, ::String) at /home/blegat/.julia/dev/JuMP/src/variables.jl:268
[6] top-level scope at none:0
julia> var = @variable(model, base_name="y")
y
julia> variable_by_name(model, "y")
y
julia> set_name(var, "z")
julia> variable_by_name(model, "y")
julia> variable_by_name(model, "z")
z
julia> @variable(model, u[1:2])
2-element Array{VariableRef,1}:
u[1]
u[2]
julia> variable_by_name(model, "u[2]")
u[2]
```
"""
function variable_by_name(model::Model, name::String)
index = MOI.get(backend(model), MOI.VariableIndex, name)
if index === nothing
return nothing
end
return VariableRef(model, index)
end
MOI.VariableIndex(v::VariableRef) = index(v)
moi_function(variable::AbstractVariableRef) = index(variable)
moi_function_type(::Type{<:AbstractVariableRef}) = MOI.VariableIndex
# Note: No validation is performed that the variables belong to the same model.
function MOI.VectorOfVariables(vars::Vector{VariableRef})
return MOI.VectorOfVariables(index.(vars))
end
function moi_function(variables::Vector{<:AbstractVariableRef})
return MOI.VectorOfVariables(variables)
end
function moi_function_type(::Type{<:Vector{<:AbstractVariableRef}})
return MOI.VectorOfVariables
end
function jump_function_type(::Model, ::Type{MOI.VectorOfVariables})
return Vector{VariableRef}
end
function jump_function(model::Model, variables::MOI.VectorOfVariables)
return VariableRef[VariableRef(model, v) for v in variables.variables]
end
jump_function_type(::Model, ::Type{MOI.VariableIndex}) = VariableRef
function jump_function(model::Model, variable::MOI.VariableIndex)
return VariableRef(model, variable)
end
## Bound setter/getters
# lower bounds
"""
has_lower_bound(v::VariableRef)
Return `true` if `v` has a lower bound. If `true`, the lower bound can be
queried with [`lower_bound`](@ref).
See also [`LowerBoundRef`](@ref), [`lower_bound`](@ref),
[`set_lower_bound`](@ref), [`delete_lower_bound`](@ref).
"""
function has_lower_bound(v::VariableRef)
return _moi_has_lower_bound(backend(owner_model(v)), v)
end
# _moi_* methods allow us to work around the type instability of the backend of
# a model.
function _moi_has_lower_bound(moi_backend, v::VariableRef)
return MOI.is_valid(moi_backend, _lower_bound_index(v))
end
function _lower_bound_index(v::VariableRef)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.GreaterThan{Float64}}(
index(v).value,
)
end
"""
set_lower_bound(v::VariableRef, lower::Number)
Set the lower bound of a variable. If one does not exist, create a new lower
bound constraint.
See also [`LowerBoundRef`](@ref), [`has_lower_bound`](@ref),
[`lower_bound`](@ref), [`delete_lower_bound`](@ref).
"""
function set_lower_bound(v::VariableRef, lower::Number)
if !isfinite(lower)
error(
"Unable to set lower bound to $(lower). To remove the bound, use " *
"`delete_lower_bound`.",
)
end
model = owner_model(v)
model.is_model_dirty = true
_moi_set_lower_bound(backend(model), v, lower)
return
end
function _moi_set_lower_bound(moi_backend, v::VariableRef, lower::Number)
new_set = MOI.GreaterThan(convert(Float64, lower))
if _moi_has_lower_bound(moi_backend, v)
cindex = _lower_bound_index(v)
MOI.set(moi_backend, MOI.ConstraintSet(), cindex, new_set)
else
@assert !_moi_is_fixed(moi_backend, v)
_moi_add_constraint(moi_backend, index(v), new_set)
end
return
end
"""
LowerBoundRef(v::VariableRef)
Return a constraint reference to the lower bound constraint of `v`. Errors if
one does not exist.
See also [`has_lower_bound`](@ref), [`lower_bound`](@ref),
[`set_lower_bound`](@ref), [`delete_lower_bound`](@ref).
"""
function LowerBoundRef(v::VariableRef)
return ConstraintRef(owner_model(v), _lower_bound_index(v), ScalarShape())
end
"""
delete_lower_bound(v::VariableRef)
Delete the lower bound constraint of a variable.
See also [`LowerBoundRef`](@ref), [`has_lower_bound`](@ref),
[`lower_bound`](@ref), [`set_lower_bound`](@ref).
"""
function delete_lower_bound(variable_ref::VariableRef)
delete(owner_model(variable_ref), LowerBoundRef(variable_ref))
return
end
"""
lower_bound(v::VariableRef)
Return the lower bound of a variable. Error if one does not exist.
See also [`LowerBoundRef`](@ref), [`has_lower_bound`](@ref),
[`set_lower_bound`](@ref), [`delete_lower_bound`](@ref).
"""
function lower_bound(v::VariableRef)
if !has_lower_bound(v)
error("Variable $(v) does not have a lower bound.")
end
set = MOI.get(owner_model(v), MOI.ConstraintSet(), LowerBoundRef(v))
return set.lower::Float64
end
# upper bounds
"""
has_upper_bound(v::VariableRef)
Return `true` if `v` has a upper bound. If `true`, the upper bound can be
queried with [`upper_bound`](@ref).
See also [`UpperBoundRef`](@ref), [`upper_bound`](@ref),
[`set_upper_bound`](@ref), [`delete_upper_bound`](@ref).
"""
function has_upper_bound(v::VariableRef)
return _moi_has_upper_bound(backend(owner_model(v)), v)
end
function _moi_has_upper_bound(moi_backend, v::VariableRef)
return MOI.is_valid(moi_backend, _upper_bound_index(v))
end
function _upper_bound_index(v::VariableRef)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.LessThan{Float64}}(
index(v).value,
)
end
"""
set_upper_bound(v::VariableRef, upper::Number)
Set the upper bound of a variable. If one does not exist, create an upper bound
constraint.
See also [`UpperBoundRef`](@ref), [`has_upper_bound`](@ref),
[`upper_bound`](@ref), [`delete_upper_bound`](@ref).
"""
function set_upper_bound(v::VariableRef, upper::Number)
if !isfinite(upper)
error(
"Unable to set upper bound to $(upper). To remove the bound, use " *
"`delete_upper_bound`.",
)
end
model = owner_model(v)
model.is_model_dirty = true
_moi_set_upper_bound(backend(model), v, upper)
return
end
function _moi_set_upper_bound(moi_backend, v::VariableRef, upper::Number)
new_set = MOI.LessThan(convert(Float64, upper))
if _moi_has_upper_bound(moi_backend, v)
cindex = _upper_bound_index(v)
MOI.set(moi_backend, MOI.ConstraintSet(), cindex, new_set)
else
@assert !_moi_is_fixed(moi_backend, v)
_moi_add_constraint(moi_backend, index(v), new_set)
end
return
end
"""
UpperBoundRef(v::VariableRef)
Return a constraint reference to the upper bound constraint of `v`. Errors if
one does not exist.
See also [`has_upper_bound`](@ref), [`upper_bound`](@ref),
[`set_upper_bound`](@ref), [`delete_upper_bound`](@ref).
"""
function UpperBoundRef(v::VariableRef)
return ConstraintRef(owner_model(v), _upper_bound_index(v), ScalarShape())
end
"""
delete_upper_bound(v::VariableRef)
Delete the upper bound constraint of a variable.
See also [`UpperBoundRef`](@ref), [`has_upper_bound`](@ref),
[`upper_bound`](@ref), [`set_upper_bound`](@ref).
"""
function delete_upper_bound(variable_ref::VariableRef)
delete(owner_model(variable_ref), UpperBoundRef(variable_ref))
return
end
"""
upper_bound(v::VariableRef)
Return the upper bound of a variable. Error if one does not exist.
See also [`UpperBoundRef`](@ref), [`has_upper_bound`](@ref),
[`set_upper_bound`](@ref), [`delete_upper_bound`](@ref).
"""
function upper_bound(v::VariableRef)
if !has_upper_bound(v)
error("Variable $(v) does not have an upper bound.")
end
set = MOI.get(owner_model(v), MOI.ConstraintSet(), UpperBoundRef(v))
return set.upper::Float64
end
# fixed value
"""
is_fixed(v::VariableRef)
Return `true` if `v` is a fixed variable. If `true`, the fixed value can be
queried with [`fix_value`](@ref).
See also [`FixRef`](@ref), [`fix_value`](@ref), [`fix`](@ref), [`unfix`](@ref).
"""
function is_fixed(v::VariableRef)
return _moi_is_fixed(backend(owner_model(v)), v)
end
function _moi_is_fixed(moi_backend, v::VariableRef)
return MOI.is_valid(moi_backend, _fix_index(v))
end
function _fix_index(v::VariableRef)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{Float64}}(
index(v).value,
)
end
"""
fix(v::VariableRef, value::Number; force::Bool = false)
Fix a variable to a value. Update the fixing constraint if one exists, otherwise
create a new one.
If the variable already has variable bounds and `force=false`, calling `fix`
will throw an error. If `force=true`, existing variable bounds will be deleted,
and the fixing constraint will be added. Note a variable will have no bounds
after a call to [`unfix`](@ref).
See also [`FixRef`](@ref), [`is_fixed`](@ref), [`fix_value`](@ref),
[`unfix`](@ref).
"""
function fix(variable::VariableRef, value::Number; force::Bool = false)
if !isfinite(value)
error("Unable to fix variable to $(value)")
end
model = owner_model(variable)
model.is_model_dirty = true
_moi_fix(backend(model), variable, value, force)
return
end
function _moi_fix(
moi_backend,
variable::VariableRef,
value::Number,
force::Bool,
)
new_set = MOI.EqualTo(convert(Float64, value))
if _moi_is_fixed(moi_backend, variable) # Update existing fixing constraint.
c_index = _fix_index(variable)
MOI.set(moi_backend, MOI.ConstraintSet(), c_index, new_set)
else # Add a new fixing constraint.
if _moi_has_upper_bound(moi_backend, variable) ||
_moi_has_lower_bound(moi_backend, variable)
if !force
error(
"Unable to fix $(variable) to $(value) because it has " *
"existing variable bounds. Consider calling " *
"`JuMP.fix(variable, value; force=true)` which will " *
"delete existing bounds before fixing the variable.",
)
end
if _moi_has_upper_bound(moi_backend, variable)
MOI.delete(moi_backend, _upper_bound_index(variable))
end
if _moi_has_lower_bound(moi_backend, variable)
MOI.delete(moi_backend, _lower_bound_index(variable))
end
end
_moi_add_constraint(moi_backend, index(variable), new_set)
end
return
end
"""
unfix(v::VariableRef)
Delete the fixing constraint of a variable.
See also [`FixRef`](@ref), [`is_fixed`](@ref), [`fix_value`](@ref),
[`fix`](@ref).
"""
function unfix(variable_ref::VariableRef)
delete(owner_model(variable_ref), FixRef(variable_ref))
return
end
"""
fix_value(v::VariableRef)
Return the value to which a variable is fixed. Error if one does not exist.
See also [`FixRef`](@ref), [`is_fixed`](@ref), [`fix`](@ref), [`unfix`](@ref).
"""
function fix_value(v::VariableRef)
set = MOI.get(owner_model(v), MOI.ConstraintSet(), FixRef(v))
return set.value::Float64
end
"""
FixRef(v::VariableRef)
Return a constraint reference to the constraint fixing the value of `v`. Errors
if one does not exist.
See also [`is_fixed`](@ref), [`fix_value`](@ref), [`fix`](@ref),
[`unfix`](@ref).
"""
function FixRef(v::VariableRef)
return ConstraintRef(owner_model(v), _fix_index(v), ScalarShape())
end
"""
is_integer(v::VariableRef)
Return `true` if `v` is constrained to be integer.
See also [`IntegerRef`](@ref), [`set_integer`](@ref), [`unset_integer`](@ref).
"""
function is_integer(v::VariableRef)
return _moi_is_integer(backend(owner_model(v)), v)
end
function _moi_is_integer(moi_backend, v::VariableRef)
return MOI.is_valid(moi_backend, _integer_index(v))
end
function _integer_index(v::VariableRef)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.Integer}(index(v).value)
end
"""
set_integer(variable_ref::VariableRef)
Add an integrality constraint on the variable `variable_ref`.
See also [`IntegerRef`](@ref), [`is_integer`](@ref), [`unset_integer`](@ref).
"""
function set_integer(v::VariableRef)
model = owner_model(v)
model.is_model_dirty = true
_moi_set_integer(backend(model), v)
return
end
function _moi_set_integer(moi_backend, variable_ref::VariableRef)
if _moi_is_integer(moi_backend, variable_ref)
return
elseif _moi_is_binary(moi_backend, variable_ref)
error(
"Cannot set the variable_ref $(variable_ref) to integer as it " *
"is already binary.",
)
end
_moi_add_constraint(moi_backend, index(variable_ref), MOI.Integer())
return
end
"""
unset_integer(variable_ref::VariableRef)
Remove the integrality constraint on the variable `variable_ref`.
See also [`IntegerRef`](@ref), [`is_integer`](@ref), [`set_integer`](@ref).
"""
function unset_integer(variable_ref::VariableRef)
delete(owner_model(variable_ref), IntegerRef(variable_ref))
return
end
"""
IntegerRef(v::VariableRef)
Return a constraint reference to the constraint constraining `v` to be integer.
Errors if one does not exist.
See also [`is_integer`](@ref), [`set_integer`](@ref), [`unset_integer`](@ref).
"""
function IntegerRef(v::VariableRef)
return ConstraintRef(owner_model(v), _integer_index(v), ScalarShape())
end
"""
is_binary(v::VariableRef)
Return `true` if `v` is constrained to be binary.
See also [`BinaryRef`](@ref), [`set_binary`](@ref), [`unset_binary`](@ref).
"""
function is_binary(v::VariableRef)
return _moi_is_binary(backend(owner_model(v)), v)
end
function _moi_is_binary(moi_backend, v::VariableRef)
return MOI.is_valid(moi_backend, _binary_index(v))
end
function _binary_index(v::VariableRef)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.ZeroOne}(index(v).value)
end
"""
set_binary(v::VariableRef)
Add a constraint on the variable `v` that it must take values in the set
``\\{0,1\\}``.
See also [`BinaryRef`](@ref), [`is_binary`](@ref), [`unset_binary`](@ref).
"""
function set_binary(v::VariableRef)
model = owner_model(v)
model.is_model_dirty = true
_moi_set_binary(backend(model), v)
return
end
function _moi_set_binary(moi_backend, variable_ref)
if _moi_is_binary(moi_backend, variable_ref)
return
elseif _moi_is_integer(moi_backend, variable_ref)
error(
"Cannot set the variable_ref $(variable_ref) to binary as it " *
"is already integer.",
)
end
_moi_add_constraint(moi_backend, index(variable_ref), MOI.ZeroOne())
return