-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.jl
More file actions
412 lines (362 loc) · 10.6 KB
/
utils.jl
File metadata and controls
412 lines (362 loc) · 10.6 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
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
using LinearAlgebra, SparseArrays
using NLPModels
import NLPModels.increment!, NLPModels.decrement!
using JuMP, MathOptInterface
const MOI = MathOptInterface
# ScalarAffineFunctions and VectorAffineFunctions
const SAF = MOI.ScalarAffineFunction{Float64}
const VAF = MOI.VectorAffineFunction{Float64}
const AF = Union{SAF, VAF}
# AffLinSets and VecLinSets
const ALS = Union{
MOI.EqualTo{Float64},
MOI.GreaterThan{Float64},
MOI.LessThan{Float64},
MOI.Interval{Float64},
}
const VLS = Union{MOI.Nonnegatives, MOI.Nonpositives, MOI.Zeros}
const LS = Union{ALS, VLS}
const VI = MOI.VariableIndex
const SQF = MOI.ScalarQuadraticFunction{Float64}
const OBJ = Union{VI, SAF, SQF}
mutable struct COO
rows::Vector{Int}
cols::Vector{Int}
vals::Vector{Float64}
end
COO() = COO(Int[], Int[], Float64[])
mutable struct LinearConstraints
jacobian::COO
nnzj::Int
end
mutable struct LinearEquations
jacobian::COO
constants::Vector{Float64}
nnzj::Int
end
mutable struct Objective
type::String
constant::Float64
gradient::SparseVector{Float64}
hessian::COO
nnzh::Int
end
"""
replace!(ex, x)
Walk the expression `ex` and substitute in the actual variables `x`.
"""
function replace!(ex, x)
if isa(ex, Expr)
for (i, arg) in enumerate(ex.args)
if isa(arg, Expr)
if arg.head == :ref && arg.args[1] == :x
ex.args[i] = x[arg.args[2].value]
else
replace!(arg, x)
end
end
end
end
end
"""
coo_sym_dot(rows, cols, vals, x, y)
Compute the product `xᵀAy` of a symmetric matrix `A` given by `(rows, cols, vals)`
Only one triangle of `A` should be passed.
"""
function coo_sym_dot(
rows::AbstractVector{<:Integer},
cols::AbstractVector{<:Integer},
vals::AbstractVector,
x::AbstractVector,
y::AbstractVector,
)
xᵀAy = 0.0
nnz = length(vals)
@inbounds for k = 1:nnz
i, j, c = rows[k], cols[k], vals[k]
xᵀAy += c * x[i] * y[j]
if i ≠ j
xᵀAy += c * x[j] * y[i]
end
end
return xᵀAy
end
"""
parser_SAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
Parse a `ScalarAffineFunction` fun with its associated set.
`linrows`, `lincols`, `linvals`, `lin_lcon` and `lin_ucon` are updated.
"""
function parser_SAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
# Parse a ScalarAffineTerm{Float64}(coefficient, variable)
for term in fun.terms
push!(linrows, nlin + 1)
push!(lincols, term.variable.value)
push!(linvals, term.coefficient)
end
if typeof(set) in (MOI.Interval{Float64}, MOI.GreaterThan{Float64})
push!(lin_lcon, -fun.constant + set.lower)
elseif typeof(set) == MOI.EqualTo{Float64}
push!(lin_lcon, -fun.constant + set.value)
else
push!(lin_lcon, -Inf)
end
if typeof(set) in (MOI.Interval{Float64}, MOI.LessThan{Float64})
push!(lin_ucon, -fun.constant + set.upper)
elseif typeof(set) == MOI.EqualTo{Float64}
push!(lin_ucon, -fun.constant + set.value)
else
push!(lin_ucon, Inf)
end
end
"""
parser_VAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
Parse a `VectorAffineFunction` fun with its associated set.
`linrows`, `lincols`, `linvals`, `lin_lcon` and `lin_ucon` are updated.
"""
function parser_VAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
# Parse a VectorAffineTerm{Float64}(output_index, scalar_term)
for term in fun.terms
push!(linrows, nlin + term.output_index)
push!(lincols, term.scalar_term.variable.value)
push!(linvals, term.scalar_term.coefficient)
end
if typeof(set) in (MOI.Nonnegatives, MOI.Zeros)
append!(lin_lcon, -fun.constants)
else
append!(lin_lcon, -Inf * ones(set.dimension))
end
if typeof(set) in (MOI.Nonpositives, MOI.Zeros)
append!(lin_ucon, -fun.constants)
else
append!(lin_ucon, Inf * ones(set.dimension))
end
end
"""
parser_MOI(moimodel)
Parse linear constraints of a `MOI.ModelLike`.
"""
function parser_MOI(moimodel)
# Variables associated to linear constraints
nlin = 0
linrows = Int[]
lincols = Int[]
linvals = Float64[]
lin_lcon = Float64[]
lin_ucon = Float64[]
contypes = MOI.get(moimodel, MOI.ListOfConstraintTypesPresent())
for (F, S) in contypes
F == VI && continue
F <: AF || @warn("Function $F is not supported.")
S <: LS || @warn("Set $S is not supported.")
conindices = MOI.get(moimodel, MOI.ListOfConstraintIndices{F, S}())
for cidx in conindices
fun = MOI.get(moimodel, MOI.ConstraintFunction(), cidx)
set = MOI.get(moimodel, MOI.ConstraintSet(), cidx)
if typeof(fun) <: SAF
parser_SAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
nlin += 1
end
if typeof(fun) <: VAF
parser_VAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon)
nlin += set.dimension
end
end
end
coo = COO(linrows, lincols, linvals)
nnzj = length(linvals)
lincon = LinearConstraints(coo, nnzj)
return nlin, lincon, lin_lcon, lin_ucon
end
"""
parser_JuMP(jmodel)
Parse variables informations of a `JuMP.Model`.
"""
function parser_JuMP(jmodel)
# Number of variables and bounds constraints
nvar = Int(num_variables(jmodel))
vars = all_variables(jmodel)
lvar = map(
var -> is_fixed(var) ? fix_value(var) : (has_lower_bound(var) ? lower_bound(var) : -Inf),
vars,
)
uvar = map(
var -> is_fixed(var) ? fix_value(var) : (has_upper_bound(var) ? upper_bound(var) : Inf),
vars,
)
# Initial solution
x0 = zeros(nvar)
for (i, val) ∈ enumerate(start_value.(vars))
if val !== nothing
x0[i] = val
end
end
return nvar, lvar, uvar, x0
end
"""
parser_objective_MOI(moimodel, nvar)
Parse linear and quadratic objective of a `MOI.ModelLike`.
"""
function parser_objective_MOI(moimodel, nvar)
# Variables associated to linear and quadratic objective
type = "UNKNOWN"
constant = 0.0
vect = spzeros(Float64, nvar)
rows = Int[]
cols = Int[]
vals = Float64[]
fobj = MOI.get(moimodel, MOI.ObjectiveFunction{OBJ}())
# Single Variable
if typeof(fobj) == VI
type = "LINEAR"
vect[fobj.value] = 1.0
end
# Linear objective
if typeof(fobj) == SAF
type = "LINEAR"
constant = fobj.constant
for term in fobj.terms
vect[term.variable.value] = term.coefficient
end
end
# Quadratic objective
if typeof(fobj) == SQF
type = "QUADRATIC"
constant = fobj.constant
for term in fobj.affine_terms
vect[term.variable.value] = term.coefficient
end
for term in fobj.quadratic_terms
i = term.variable_1.value
j = term.variable_2.value
if i ≥ j
push!(rows, i)
push!(cols, j)
else
push!(cols, j)
push!(rows, i)
end
push!(vals, term.coefficient)
end
end
return Objective(type, constant, vect, COO(rows, cols, vals), length(vals))
end
"""
parser_linear_expression(cmodel, nvar, F)
Parse linear expressions of type `GenericAffExpr{Float64,VariableRef}`.
"""
function parser_linear_expression(cmodel, nvar, F)
# Variables associated to linear expressions
rows = Int[]
cols = Int[]
vals = Float64[]
constants = Float64[]
# Linear least squares model
nlinequ = 0
F_is_array_of_containers = F isa Array{<:AbstractArray}
if F_is_array_of_containers
@objective(
cmodel,
Min,
0.0 +
0.5 *
sum(sum(Fi^2 for Fi in FF if typeof(Fi) == GenericAffExpr{Float64, VariableRef}) for FF in F)
)
for FF in F, expr in FF
if typeof(expr) == GenericAffExpr{Float64, VariableRef}
nlinequ += 1
for (i, key) in enumerate(expr.terms.keys)
push!(rows, nlinequ)
push!(cols, key.index.value)
push!(vals, expr.terms.vals[i])
end
push!(constants, expr.constant)
end
end
else
@objective(
cmodel,
Min,
0.0 + 0.5 * sum(Fi^2 for Fi in F if typeof(Fi) == GenericAffExpr{Float64, VariableRef})
)
for expr in F
if typeof(expr) == GenericAffExpr{Float64, VariableRef}
nlinequ += 1
for (i, key) in enumerate(expr.terms.keys)
push!(rows, nlinequ)
push!(cols, key.index.value)
push!(vals, expr.terms.vals[i])
end
push!(constants, expr.constant)
end
end
end
moimodel = backend(cmodel)
lls = parser_objective_MOI(moimodel, nvar)
return lls, LinearEquations(COO(rows, cols, vals), constants, length(vals)), nlinequ
end
"""
parser_nonlinear_expression(cmodel, nvar, F)
Parse nonlinear expressions of type `NonlinearExpression`.
"""
function parser_nonlinear_expression(cmodel, nvar, F; hessian::Bool = true)
# Nonlinear least squares model
nnlnequ = 0
F_is_array_of_containers = F isa Array{<:AbstractArray}
if F_is_array_of_containers
nnlnequ = sum(sum(typeof(Fi) == NonlinearExpression for Fi in FF) for FF in F)
if nnlnequ > 0
@NLobjective(
cmodel,
Min,
0.5 * sum(sum(Fi^2 for Fi in FF if typeof(Fi) == NonlinearExpression) for FF in F)
)
end
else
nnlnequ = sum(typeof(Fi) == NonlinearExpression for Fi in F)
if nnlnequ > 0
@NLobjective(cmodel, Min, 0.5 * sum(Fi^2 for Fi in F if typeof(Fi) == NonlinearExpression))
end
end
ceval = cmodel.nlp_data == nothing ? nothing : NLPEvaluator(cmodel)
(ceval ≠ nothing) &&
(nnlnequ == 0) &&
MOI.initialize(ceval, hessian ? [:Grad, :Jac, :Hess, :HessVec] : [:Grad, :Jac]) # Add :JacVec when available
(ceval ≠ nothing) &&
(nnlnequ > 0) &&
MOI.initialize(
ceval,
hessian ? [:Grad, :Jac, :Hess, :HessVec, :ExprGraph] : [:Grad, :Jac, :ExprGraph],
) # Add :JacVec when available
if nnlnequ == 0
Feval = nothing
else
Fmodel = JuMP.Model()
@variable(Fmodel, x[1:nvar])
JuMP._init_NLP(Fmodel)
@objective(Fmodel, Min, 0.0)
Fmodel.nlp_data.user_operators = cmodel.nlp_data.user_operators
if F_is_array_of_containers
for FF in F, Fi in FF
if typeof(Fi) == NonlinearExpression
expr = ceval.subexpressions_as_julia_expressions[Fi.index]
replace!(expr, x)
expr = :($expr == 0)
JuMP.add_nonlinear_constraint(Fmodel, expr)
end
end
else
for Fi in F
if typeof(Fi) == NonlinearExpression
expr = ceval.subexpressions_as_julia_expressions[Fi.index]
replace!(expr, x)
expr = :($expr == 0)
JuMP.add_nonlinear_constraint(Fmodel, expr)
end
end
end
Feval = NLPEvaluator(Fmodel)
MOI.initialize(Feval, hessian ? [:Grad, :Jac, :Hess, :HessVec] : [:Grad, :Jac]) # Add :JacVec when available
Feval.user_output_buffer = ceval.user_output_buffer
end
return ceval, Feval, nnlnequ
end