-
Notifications
You must be signed in to change notification settings - Fork 86
/
variables.jl
211 lines (181 loc) · 8.12 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
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
# Variables
"""
struct AddVariableNotAllowed <: NotAllowedError
message::String # Human-friendly explanation why the attribute cannot be set
end
An error indicating that variables cannot be added to the model.
"""
struct AddVariableNotAllowed <: NotAllowedError
message::String # Human-friendly explanation why the attribute cannot be set
end
AddVariableNotAllowed() = AddVariableNotAllowed("")
operation_name(::AddVariableNotAllowed) = "Adding variables"
"""
add_variables(model::ModelLike, n::Int)::Vector{VariableIndex}
Add `n` scalar variables to the model, returning a vector of variable indices.
A [`AddVariableNotAllowed`](@ref) error is thrown if adding variables cannot be
done in the current state of the model `model`.
"""
function add_variables(model::ModelLike, n)
return VariableIndex[add_variable(model) for _ in 1:n]
end
"""
add_variable(model::ModelLike)::VariableIndex
Add a scalar variable to the model, returning a variable index.
A [`AddVariableNotAllowed`](@ref) error is thrown if adding variables cannot be
done in the current state of the model `model`.
"""
add_variable(model::ModelLike) = throw(AddVariableNotAllowed())
"""
supports_add_constrained_variable(
model::ModelLike,
S::Type{<:AbstractScalarSet}
)::Bool
Return a `Bool` indicating whether `model` supports constraining a variable
to belong to a set of type `S` either on creation of the variable with
[`add_constrained_variable`](@ref) or after the variable is created with
[`add_constraint`](@ref).
By default, this function falls back to
`supports_add_constrained_variables(model, Reals) &&
supports_constraint(model, MOI.VariableIndex, S)` which is the correct
definition for most models.
## Example
Suppose that a solver supports only two kind of variables: binary variables
and continuous variables with a lower bound. If the solver decides not to
support `VariableIndex`-in-`Binary` and `VariableIndex`-in-`GreaterThan`
constraints, it only has to implement `add_constrained_variable` for these
two sets which prevents the user to add both a binary constraint and a
lower bound on the same variable. Moreover, if the user adds a
`VariableIndex`-in-`GreaterThan` constraint, implementing this interface (i.e.,
`supports_add_constrained_variables`) enables the constraint to be transparently
bridged into a supported constraint.
"""
function supports_add_constrained_variable(
model::ModelLike,
S::Type{<:AbstractScalarSet},
)
return supports_add_constrained_variables(model, Reals) &&
supports_constraint(model, VariableIndex, S)
end
"""
add_constrained_variable(
model::ModelLike,
set::AbstractScalarSet
)::Tuple{MOI.VariableIndex,
MOI.ConstraintIndex{MOI.VariableIndex, typeof(set)}}
Add to `model` a scalar variable constrained to belong to `set`, returning the
index of the variable created and the index of the constraint constraining the
variable to belong to `set`.
By default, this function falls back to creating a free variable with
[`add_variable`](@ref) and then constraining it to belong to `set` with
[`add_constraint`](@ref).
"""
function add_constrained_variable(model::ModelLike, set::AbstractScalarSet)
variable = add_variable(model)
constraint = add_constraint(model, variable, set)
return variable, constraint
end
"""
supports_add_constrained_variables(
model::ModelLike,
S::Type{<:AbstractVectorSet}
)::Bool
Return a `Bool` indicating whether `model` supports constraining a vector of
variables to belong to a set of type `S` either on creation of the vector of
variables with [`add_constrained_variables`](@ref) or after the variable is
created with [`add_constraint`](@ref).
By default, if `S` is `Reals` then this function returns `true` and otherwise,
it falls back to `supports_add_constrained_variables(model, Reals) &&
supports_constraint(model, MOI.VectorOfVariables, S)` which is the correct
definition for most models.
## Example
In the standard conic form (see [Duality](@ref)), the variables are grouped into
several cones and the constraints are affine equality constraints.
If `Reals` is not one of the cones supported by the solvers then it needs
to implement `supports_add_constrained_variables(::Optimizer, ::Type{Reals}) = false`
as free variables are not supported.
The solvers should then implement
`supports_add_constrained_variables(::Optimizer, ::Type{<:SupportedCones}) = true`
where `SupportedCones` is the union of all cone types that are supported;
it does not have to implement the method
`supports_constraint(::Type{VectorOfVariables}, Type{<:SupportedCones})`
as it should return `false` and it's the default.
This prevents the user to constrain the same variable in two different cones.
When a `VectorOfVariables`-in-`S` is added, the variables of the vector
have already been created so they already belong to given cones.
If bridges are enabled, the constraint will therefore be bridged by adding slack
variables in `S` and equality constraints ensuring that the slack variables are
equal to the corresponding variables of the given constraint function.
Note that there may also be sets for which
`!supports_add_constrained_variables(model, S)` and
`supports_constraint(model, MOI.VectorOfVariables, S)`.
For instance, suppose a solver supports positive semidefinite variable
constraints and two types of variables: binary variables and nonnegative
variables. Then the solver should support adding
`VectorOfVariables`-in-`PositiveSemidefiniteConeTriangle` constraints, but it
should not support creating variables constrained to belong to the
`PositiveSemidefiniteConeTriangle` because the variables in
`PositiveSemidefiniteConeTriangle` should first be created as either binary or
non-negative.
"""
function supports_add_constrained_variables(
model::ModelLike,
S::Type{<:AbstractVectorSet},
)
return supports_add_constrained_variables(model, Reals) &&
supports_constraint(model, VectorOfVariables, S)
end
supports_add_constrained_variables(::ModelLike, ::Type{Reals}) = true
"""
add_constrained_variables(
model::ModelLike,
sets::AbstractVector{<:AbstractScalarSet}
)::Tuple{
Vector{MOI.VariableIndex},
Vector{MOI.ConstraintIndex{MOI.VariableIndex,eltype(sets)}},
}
Add to `model` scalar variables constrained to belong to `sets`, returning the
indices of the variables created and the indices of the constraints constraining
the variables to belong to each set in `sets`. That is, if it returns `variables`
and `constraints`, `constraints[i]` is the index of the constraint constraining
`variable[i]` to belong to `sets[i]`.
By default, this function falls back to calling
[`add_constrained_variable`](@ref) on each set.
"""
function add_constrained_variables(
model::ModelLike,
sets::AbstractVector{<:AbstractScalarSet},
)
variables = Vector{VariableIndex}(undef, length(sets))
constraints =
Vector{ConstraintIndex{VariableIndex,eltype(sets)}}(undef, length(sets))
for (i, set) in enumerate(sets)
variables[i], constraints[i] = add_constrained_variable(model, set)
end
return variables, constraints
end
"""
add_constrained_variables(
model::ModelLike,
set::AbstractVectorSet,
)::Tuple{
Vector{MOI.VariableIndex},
MOI.ConstraintIndex{MOI.VectorOfVariables,typeof(set)},
}
Add to `model` a vector of variables constrained to belong to `set`, returning
the indices of the variables created and the index of the constraint
constraining the vector of variables to belong to `set`.
By default, this function falls back to creating free variables with
[`add_variables`](@ref) and then constraining it to belong to `set` with
[`add_constraint`](@ref).
"""
function add_constrained_variables(model::ModelLike, set::AbstractVectorSet)
variables = add_variables(model, dimension(set))
constraint = add_constraint(model, VectorOfVariables(variables), set)
return variables, constraint
end