-
Notifications
You must be signed in to change notification settings - Fork 20
/
mcp.jl
453 lines (360 loc) · 13.2 KB
/
mcp.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
const return_type =[
:Solved, # 1 - The problem was solved
:StationaryPointFound, # 2 - A stationary point was found
:MajorIterationLimit, # 3 - Major iteration limit met
:CumulativeMinorIterationLimit, # 4 - Cumulative minor iterlim met
:TimeLimit, # 5 - Ran out of time
:UserInterrupt, # 6 - Control-C, typically
:BoundError, # 7 - Problem has a bound error (lb is not less than ub)
:DomainError, # 8 - Could not find starting point
:Infeasible, # 9 - Problem has no solution
:Error, #10 - An error occurred within the code
:LicenseError, #11 - License could not be found
:OK #12 - OK
]
mutable struct ComplementarityType
lb::AbstractFloat
var::JuMP.VariableRef
ub::AbstractFloat
F::JuMP.NonlinearExpression
raw_idx::Integer
var_name::String
F_name::String
result_value::AbstractFloat
end
function MCPModel()
m = JuMP.Model()
m.ext[:MCP] = ComplementarityType[]
return m
end
function get_MCP_data(m::JuMP.Model)
if haskey(m.ext, :MCP)
return m.ext[:MCP]::Array
else
error("The 'get_MCP_data' function is only for MCP models as in ComplementarityType.jl")
end
end
raw_index(v::JuMP.VariableRef) = JuMP.index(v).value
function get_bounds_in_raw_index(mcp_data)
n = length(mcp_data)
lb = zeros(n)
ub = ones(n)
for i in 1:n
lb[raw_index(mcp_data[i].var)] = mcp_data[i].lb
ub[raw_index(mcp_data[i].var)] = mcp_data[i].ub
end
return lb, ub
end
function get_names_in_raw_index(mcp_data)
n = length(mcp_data)
var_name = Array{String}(undef, n)
F_name = Array{String}(undef, n)
for i in 1:n
var_name[raw_index(mcp_data[i].var)] = mcp_data[i].var_name
F_name[raw_index(mcp_data[i].var)] = mcp_data[i].F_name
end
return var_name, F_name
end
function get_initial_values_in_raw_index(m, mcp_data)
n = length(mcp_data)
initial_values = Array{Float64}(undef, n)
for i in 1:n
init_val = JuMP.start_value(mcp_data[i].var)
if init_val == nothing
init_val = mcp_data[i].lb
end
initial_values[raw_index(mcp_data[i].var)] = init_val
end
return initial_values
end
function solveLCP(m::JuMP.Model; solver=:PATH, method=:trust_region)
@warn("The solveLCP function has been deprecated. Use solveMCP instead.")
return solveMCP(m, solver=solver, method=method)
end
# This function copies all current bound information from the variables
# in the JuMP model over to the MCP data structure.
function update_bounds!(m::JuMP.Model)
mcp_data = get_MCP_data(m)
for dim in mcp_data
dim.lb = JuMP.is_fixed(dim.var) ? JuMP.fix_value(dim.var) : JuMP.has_lower_bound(dim.var) ? JuMP.lower_bound(dim.var) : -Inf
dim.ub = JuMP.is_fixed(dim.var) ? JuMP.fix_value(dim.var) : JuMP.has_upper_bound(dim.var) ? JuMP.upper_bound(dim.var) : Inf
end
end
function solveMCP(m::JuMP.Model; solver=:PATH, method=:trust_region, linear=false, kwargs...)
if linear
@warn("The linear keyword argument has been deprecated. You don't need to set it anymore.")
end
update_bounds!(m)
if solver == :PATH
return _solve_path!(m; kwargs...)
elseif solver == :NLsolve
return _solve_nlsolve!(m, method=method)
else
error("Choose :PATH or :NLsolve as the solver for MCP.")
end
end
function sortperm_MCP_data(obj::Array{ComplementarityType,1})
n = length(obj)
ref = Array{Int}(undef, n)
for i in 1:n
ref[i] = obj[i].raw_idx
end
return sortperm(ref)
end
# Using PATHSolver
function _solve_path!(m::JuMP.Model; kwargs...)
d = JuMP.NLPEvaluator(m)
function function_callback(n::Cint, z::Vector{Cdouble}, F_val::Vector{Cdouble})
@assert n == length(z) == length(F_val)
# z is in RawIndex, passed from PATHSolver
MOI.initialize(d, [:Jac])
MOI.eval_constraint(d, F_val, z)
# F_val also should be in RawIndex
# since it is the order in which constraints are added
return Cint(0)
end
function j_eval(z::Vector{Cdouble})
MOI.initialize(d, [:Jac])
J_struct = MOI.jacobian_structure(d)
I = first.(J_struct)
J = last.(J_struct)
jac_val = zeros(length(J))
MOI.eval_constraint_jacobian(d, jac_val, z)
# return matrix also should be in RawIndex
# since it is the order in which constraints are added
nc = length(d.constraints) # number of constraints
Jac = sparse(I, J, jac_val, nc, nc) # SparseMatrixCSC
return Jac
end
function jacobian_callback(
n::Cint,
nnz::Cint,
z::Vector{Cdouble},
col_start::Vector{Cint},
col_len::Vector{Cint},
row::Vector{Cint},
data::Vector{Cdouble}
)
@assert n == length(z) == length(col_start) == length(col_len)
@assert nnz == length(row) == length(data)
# z is in RawIndex, passed from PATHSolver
Jac = j_eval(z) # SparseMatrixCSC
# Pouring Jac::SparseMatrixCSC to the format used in PATH
for i in 1:n
col_start[i] = Jac.colptr[i]
col_len[i] = Jac.colptr[i + 1] - Jac.colptr[i]
end
rv = rowvals(Jac)
nz = nonzeros(Jac)
num_nonzeros = SparseArrays.nnz(Jac)
for i in 1:num_nonzeros
row[i] = rv[i]
data[i] = nz[i]
end
return Cint(0)
end
mcp_data = get_MCP_data(m)
n = length(mcp_data)
# Two Indices
# MCP_Index: the order stored in MCPModel = array index of Array{ComplementarityType}
# RawIndex: the order used in JuMP / MathOptInteface
# Declaring MCP mapping F as constraints
# in order to query Jacobian using AutoDiff thru MathProgBase
# i = RawIndex
# Add constraint in the order of RawIndex
p = sortperm_MCP_data(mcp_data)
JuMP.@NLconstraint(m, [i=1:n], mcp_data[p[i]].F == 0)
# lb and ub in RawIndex
lb, ub = get_bounds_in_raw_index(mcp_data)
# var_name, F_name in RawIndex
var_name, F_name = get_names_in_raw_index(mcp_data)
# initial values
initial_values = get_initial_values_in_raw_index(m, mcp_data)
# overestimating number of nonzeros in Jacobian
nnz = max( SparseArrays.nnz(j_eval(lb)), SparseArrays.nnz(j_eval(ub)) )
nnz = max( nnz, SparseArrays.nnz(j_eval(initial_values)) )
for i in 1:2
z_rand = max.(lb, min.(ub, rand(Float64, n)))
nnz_rand = SparseArrays.nnz(j_eval(z_rand))
nnz = max( nnz, nnz_rand )
end
nnz = min( 2 * nnz, n^2 )
# Solve the MCP using PATHSolver
status, z, info = PATHSolver.solve_mcp(
function_callback,
jacobian_callback,
lb,
ub,
initial_values;
nnz = nnz,
variable_names = var_name,
constraint_names = F_name,
kwargs...
)
# z is in RawIndex
# After solving set the values in m::JuMP.Model to the solution obtained.
for i in 1:n
set_result_value(mcp_data[i], z[mcp_data[i].raw_idx])
end
# Cleanup. Remove all dummy @NLconstraints added,
# so that the model can be re-used for multiple runs
m.nlp_data.nlconstr = []
# This function has changed the content of m already.
return return_type[Int(status)]
end
function _solve_nlsolve!(m::JuMP.Model; method=:trust_region)
d = JuMP.NLPEvaluator(m)
function function_callback!(fvec, z)
# z is in RawIndex, passed from PATHSolver
MOI.initialize(d, [:Jac])
F_val = zeros(n)
MOI.eval_constraint(d, F_val, z)
copyto!(fvec, F_val)
end
function jacobian_callback!(fjac, z)
# z is in RawIndex, passed from PATHSolver
MOI.initialize(d, [:Jac])
J_struct = MOI.jacobian_structure(d)
I = first.(J_struct)
J = last.(J_struct)
jac_val = zeros(length(J))
MOI.eval_constraint_jacobian(d, jac_val, z)
sparse_fjac = sparse(I, J, jac_val)
copyto!(fjac, full(sparse_fjac))
end
mcp_data = get_MCP_data(m)
n = length(mcp_data)
# Two Indices
# MCP_Index: the order stored in MCPModel = array index of Array{ComplementarityType}
# RawIndex: the order used in JuMP / MathProgBase
# Declaring MCP mapping F as constraints
# in order to query Jacobian using AutoDiff thru MathProgBase
# i = RawIndex
# Add constraint in the order of RawIndex
p = sortperm_MCP_data(mcp_data)
JuMP.@NLconstraint(m, [i=1:n], mcp_data[p[i]].F == 0)
# lb and ub in RawIndex
lb, ub = get_bounds_in_raw_index(mcp_data)
# initial values
initial_values = get_initial_values_in_raw_index(m, mcp_data)
# Solve the MCP using NLsolve
# ALL inputs to NLsolve must be in RawIndex
r = NLsolve.mcpsolve(function_callback!, jacobian_callback!, lb, ub, initial_values, method = method,
iterations = 10_000)
# function mcpsolve{T}(f,
# j,
# lower::Vector,
# upper::Vector,
# initial_x::AbstractArray{T};
# method::Symbol = :trust_region,
# reformulation::Symbol = :smooth,
# xtol::Real = zero(T),
# ftol::Real = convert(T,1e-8),
# iterations::Integer = 1_000,
# store_trace::Bool = false,
# show_trace::Bool = false,
# extended_trace::Bool = false,
# linesearch = LineSearches.BackTracking(),
# factor::Real = one(T),
# autoscale = true,
# inplace = true)
# julia> fieldnames(typeof(r))
# 12-element Array{Symbol,1}:
# :method
# :initial_x
# :zero
# :residual_norm
# :iterations
# :x_converged
# :xtol
# :f_converged
# :ftol
# :trace
# :f_calls
# :g_calls
# r.zero is in RawIndex
for i in 1:n
set_result_value(mcp_data[i], r.zero[mcp_data[i].raw_idx])
end
# Cleanup. Remove all dummy @NLconstraints added,
# so that the model can be re-used for multiple runs
m.nlp_data.nlconstr = []
# This function has changed the content of m already.
return r
end
function set_result_value(mcp_data::ComplementarityType, value::Float64)
mcp_data.result_value = value
end
function result_value(v::JuMP.VariableRef)
mcp_data = get_MCP_data(v.model)
result_value = NaN
for i in 1:length(mcp_data)
if mcp_data[i].var == v
result_value = mcp_data[i].result_value
break
end
end
return result_value
end
# https://stackoverflow.com/questions/50084877/how-to-alias-a-macro-in-julia#comment87277306_50085297
@eval const $(Symbol("@mapping")) = $(Symbol("@NLexpression"))
function add_complementarity(m::JuMP.Model, var::JuMP.VariableRef, F::JuMP.NonlinearExpression, F_name::String)
lb = JuMP.is_fixed(var) ? JuMP.fix_value(var) : JuMP.has_lower_bound(var) ? JuMP.lower_bound(var) : -Inf
ub = JuMP.is_fixed(var) ? JuMP.fix_value(var) : JuMP.has_upper_bound(var) ? JuMP.upper_bound(var) : Inf
var_name = JuMP.name(var)
new_dimension = ComplementarityType(lb, var, ub, F, raw_index(var), var_name, F_name, NaN)
mcp_data = get_MCP_data(m)
push!(mcp_data, new_dimension)
end
macro complementarity(m, F, var)
F_base_name = string(F)
F_sym = string(F)
var_sym = string(var)
m = esc(m)
F = esc(F)
var = esc(var)
quote
# if isa($F, JuMP.JuMPArray) || isa($F, Array)
# @assert length($F) == length($var)
# end
# when var is a single JuMP variable
if isa($var, JuMP.VariableRef)
ex_var = Meta.parse(JuMP.name($var))
ex_F = Meta.parse($F_base_name)
if typeof(ex_var) == Symbol || typeof(ex_F) == Symbol
add_complementarity($m, $var, $F, $F_base_name)
elseif typeof(ex_var) == Expr && typeof(ex_F) == Expr
@assert ex_var.head == :ref
@assert ex_F.head == :ref
ex_F.args[2] = ex_var.args[2]
F_name = string(ex_F)
add_complementarity($m, $var, $F, F_name)
else
error("Error in @complementarity. Please file an issue with an example.")
end
# when var is a single dimensional Array of JuMP.Variable
elseif isa($var, Array{JuMP.VariableRef})
for idx in CartesianIndices(size($var))
idx_name = idx.I
F_name = string($F_base_name, "[", idx_name, "]")
var_idx = $var[idx]
F_idx = $F[idx]
add_complementarity($m, var_idx, F_idx, F_name)
end
else # isa($var, JuMP.JuMPArray) && length(($var).indexsets) == 1 or > 1
# when var is a multi-dimensional JuMP variable array
ex = :(Base.product())
for i in 1:length($var.axes)
push!(ex.args, $var.axes[i])
end
idx_list = collect(eval(ex))
for idx in idx_list
F_name = string($F_base_name, "[", join(idx,","), "]")
var_idx = $var[idx...]
F_idx = $F[idx...]
add_complementarity($m, var_idx, F_idx, F_name)
end
end # end of if
end # end of quote
end # end of @complementarity
#