/
KNITRO.jl
388 lines (353 loc) · 14.6 KB
/
KNITRO.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
__precompile__()
module KNITRO
import Base.Libdl: RTLD_GLOBAL
function __init__()
@linux_only begin
# fixes missing symbols in libknitro.so
Libdl.dlopen("libdl", RTLD_GLOBAL)
Libdl.dlopen("libgomp", RTLD_GLOBAL)
end
end
@unix_only const libknitro = "libknitro"
@windows_only const libknitro = "knitro"
export
KnitroProblem,
createProblem, freeProblem,
initializeProblem,
solveProblem,
restartProblem,
setFuncCallback, setGradCallback, setHessCallback,
setCallbacks, setMIPCallback,
loadOptionsFile,
loadTunerFile,
setOption, getOption,
applicationReturnStatus
"A macro to make calling KNITRO's C API a little cleaner"
macro ktr_ccall(func, args...)
f = Base.Meta.quot(symbol("KTR_$(func)"))
args = [esc(a) for a in args]
quote
ccall(($f,libknitro), $(args...))
end
end
type KnitroProblem
# For KNITRO
env::Ptr{Void} # pointer to KTR_context
eval_status::Int32 # scalar input used only for reverse comms
status::Int32 # Final status
mip::Bool # whether it is a Mixed Integer Problem
# For MathProgBase
x::Vector{Float64} # Starting and final solution
lambda::Vector{Float64}
g::Vector{Float64} # Final constraint values
obj_val::Vector{Float64} # (length 1) Final objective
# Callbacks
eval_f::Function
eval_g::Function
eval_grad_f::Function
eval_jac_g::Function
eval_h::Function
eval_hv::Function
eval_mip_node::Function
function KnitroProblem()
kp = new(newcontext(),
0,
100, # Code for :Uninitialized
false)
finalizer(kp, freeProblem)
kp
end
end
createProblem() = KnitroProblem()
function freeProblem(kp::KnitroProblem)
kp.env == C_NULL && return
return_code = @ktr_ccall(free, Int32, (Ptr{Void},), [kp.env])
if return_code != 0
error("KNITRO: Error freeing memory")
end
kp.env = C_NULL
end
function initializeKP(kp, x0, lambda0, g; mip = false)
kp.status = 101 # code for :Initialized
kp.mip = mip
kp.x = x0
kp.lambda = lambda0
kp.g = g
kp.obj_val = zeros(Float64, 1)
end
function initializeProblem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb,
g_ub, jac_var, jac_con, hess_row, hess_col;
initial_x = C_NULL, initial_lambda = C_NULL)
initializeKP(kp, (initial_x != C_NULL) ? initial_x :
zeros(Float64, length(x_l)),
(initial_lambda != C_NULL) ? initial_lambda :
zeros(Float64, length(x_l) + length(g_lb)),
zeros(Float64, length(g_lb)))
init_problem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb, g_ub,
jac_var, jac_con, hess_row, hess_col; initial_x=kp.x,
initial_lambda=kp.lambda)
end
function initializeProblem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb,
g_ub, jac_var, jac_con; initial_x = C_NULL,
initial_lambda = C_NULL)
hessopt = Array(Int32, 1)
getOption(kp, "hessopt", hessopt)
@assert hessopt[1] != KTR_HESSOPT_EXACT
# KNITRO documentation:
# If user option hessopt is not set to KTR_HESSOPT_EXACT, then Hessian
# nonzeros will not be used. In this case, set nnzH=0, and pass NULL
# pointers for hessIndexRows and hessIndexCols.
initializeKP(kp, (initial_x != C_NULL) ? initial_x :
zeros(Float64, length(x_l)),
(initial_lambda != C_NULL) ? initial_lambda :
zeros(Float64, length(x_l) + length(g_lb)),
zeros(Float64, length(g_lb)))
init_problem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb, g_ub,
jac_var, jac_con; initial_x=kp.x,initial_lambda=kp.lambda)
end
# Initialization for MIP
function initializeProblem(kp, objGoal, objType, objFnType,
x_Type, x_l, x_u, c_Type, c_FnType, g_lb,
g_ub, jac_var, jac_con, hess_row, hess_col;
initial_x = C_NULL, initial_lambda = C_NULL)
initializeKP(kp, (initial_x != C_NULL) ? initial_x :
zeros(Float64, length(x_l)),
(initial_lambda != C_NULL) ? initial_lambda :
zeros(Float64, length(x_l) + length(g_lb)),
zeros(Float64, length(g_lb)),
mip=true)
mip_init_problem(kp, objGoal, objType, objFnType, x_Type, x_l, x_u,
c_Type, c_FnType, g_lb, g_ub, jac_var, jac_con,
hess_row, hess_col; initial_x=kp.x,
initial_lambda=kp.lambda)
end
function initializeProblem(kp, objGoal, objType, objFnType,
x_Type, x_l, x_u, c_Type, c_FnType, g_lb,
g_ub, jac_var, jac_con; initial_x = C_NULL,
initial_lambda = C_NULL)
hessopt = Array(Int32, 1)
getOption(kp, "hessopt", hessopt)
@assert hessopt[1] != KTR_HESSOPT_EXACT
# KNITRO documentation:
# If user option hessopt is not set to KTR_HESSOPT_EXACT, then Hessian
# nonzeros will not be used. In this case, set nnzH=0, and pass NULL
# pointers for hessIndexRows and hessIndexCols.
initializeKP(kp, (initial_x != C_NULL) ? initial_x :
zeros(Float64, length(x_l)),
(initial_lambda != C_NULL) ? initial_lambda :
zeros(Float64, length(x_l) + length(g_lb)),
zeros(Float64, length(g_lb)),
mip=true)
mip_init_problem(kp, objGoal, objType, objFnType, x_Type, x_l, x_u,
c_Type, c_FnType, g_lb, g_ub, jac_var, jac_con;
initial_x=kp.x, initial_lambda=kp.lambda)
end
function solveProblem(kp::KnitroProblem)
if kp.mip
kp.status = mip_solve_problem(kp, kp.x, kp.lambda, kp.eval_status,
kp.obj_val)
else
kp.status = solve_problem(kp, kp.x, kp.lambda, kp.eval_status,
kp.obj_val)
end
end
function solveProblem(kp::KnitroProblem,
cons::Vector{Float64},
objGrad::Vector{Float64},
jac::Vector{Float64},
hess::Vector{Float64},
hessVector::Vector{Float64})
if kp.mip
kp.status = mip_solve_problem(kp, kp.x, kp.lambda, kp.eval_status,
kp.obj_val, cons, objGrad, jac, hess,
hessVector)
else
kp.status = solve_problem(kp, kp.x, kp.lambda, kp.eval_status,
kp.obj_val, cons, objGrad, jac, hess,
hessVector)
end
end
function restartProblem(kp, x0, lambda0)
kp.status = 101 # code for :Initialized
kp.eval_status = 0
restart_problem(kp, x0, lambda0)
end
function restartProblem(kp)
kp.status = 101 # code for :Initialized
kp.eval_status = 0
restart_problem(kp)
end
# -----
# Callback Wrappers
# -----
function eval_fc_wrapper(evalRequestCode::Cint,
n::Cint,
m::Cint,
nnzJ::Cint,
nnzH::Cint,
x_::Ptr{Cdouble},
lambda_::Ptr{Cdouble},
obj_::Ptr{Cdouble},
c_::Ptr{Cdouble},
g_::Ptr{Cdouble},
J_::Ptr{Cdouble},
H_::Ptr{Cdouble},
HV_::Ptr{Cdouble},
userParams_::Ptr{Void})
if evalRequestCode != KTR_RC_EVALFC
return KTR_RC_CALLBACK_ERR
end
kp = unsafe_pointer_to_objref(userParams_)::KnitroProblem
x = pointer_to_array(x_,n)
# calculate the new objective function value
unsafe_store!(obj_, kp.eval_f(x))
# calculate the new constraint values
kp.eval_g(x,pointer_to_array(c_,m))
Int32(0)
end
function eval_ga_wrapper(evalRequestCode::Cint,
n::Cint,
m::Cint,
nnzJ::Cint,
nnzH::Cint,
x_::Ptr{Cdouble},
lambda_::Ptr{Cdouble},
obj_::Ptr{Cdouble},
c_::Ptr{Cdouble},
g_::Ptr{Cdouble},
J_::Ptr{Cdouble},
H_::Ptr{Cdouble},
HV_::Ptr{Cdouble},
userParams_::Ptr{Void})
if evalRequestCode != KTR_RC_EVALGA
return KTR_RC_CALLBACK_ERR
end
kp = unsafe_pointer_to_objref(userParams_)::KnitroProblem
x = pointer_to_array(x_,n)
# evaluate the gradient
kp.eval_grad_f(x,pointer_to_array(g_,n))
# evaluate the jacobian
kp.eval_jac_g(x,pointer_to_array(J_,nnzJ))
Int32(0)
end
function eval_hess_wrapper(evalRequestCode::Cint,
n::Cint,
m::Cint,
nnzJ::Cint,
nnzH::Cint,
x_::Ptr{Cdouble},
lambda_::Ptr{Cdouble},
obj_::Ptr{Cdouble},
c_::Ptr{Cdouble},
g_::Ptr{Cdouble},
J_::Ptr{Cdouble},
H_::Ptr{Cdouble},
HV_::Ptr{Cdouble},
userParams_::Ptr{Void})
kp = unsafe_pointer_to_objref(userParams_)::KnitroProblem
x = pointer_to_array(x_, n)
lambda = pointer_to_array(lambda_, m+n)
if evalRequestCode == KTR_RC_EVALH
kp.eval_h(x, lambda, 1.0, pointer_to_array(H_, nnzH))
elseif evalRequestCode == KTR_RC_EVALH_NO_F
kp.eval_h(x, lambda, 0.0, pointer_to_array(H_, nnzH))
elseif evalRequestCode == KTR_RC_EVALHV
kp.eval_hv(x, lambda, 1.0, pointer_to_array(HV_, n))
elseif evalRequestCode == KTR_RC_EVALHV_NO_F
kp.eval_hv(x, lambda, 0.0, pointer_to_array(HV_, n))
else
return KTR_RC_CALLBACK_ERR
end
Int32(0)
end
function eval_mip_node_wrapper(evalRequestCode::Cint,
n::Cint,
m::Cint,
nnzJ::Cint,
nnzH::Cint,
x_::Ptr{Cdouble},
lambda_::Ptr{Cdouble},
obj_::Ptr{Cdouble},
c_::Ptr{Cdouble},
g_::Ptr{Cdouble},
J_::Ptr{Cdouble},
H_::Ptr{Cdouble},
HV_::Ptr{Cdouble},
userParams_::Ptr{Void})
kp = unsafe_pointer_to_objref(userParams_)::KnitroProblem
obj = unsafe_load(obj_)
kp.eval_mip_node(kp,obj)
Int32(0)
end
function setFuncCallback(kp::KnitroProblem,
eval_f::Function,
eval_g::Function)
kp.eval_f = eval_f
kp.eval_g = eval_g
set_func_callback(kp,eval_fc_wrapper)
end
function setGradCallback(kp::KnitroProblem,
eval_grad_f::Function,
eval_jac_g::Function)
kp.eval_grad_f = eval_grad_f
kp.eval_jac_g = eval_jac_g
set_grad_callback(kp,eval_ga_wrapper)
end
function setHessCallback(kp::KnitroProblem,
eval_h::Function,
eval_hv::Function)
kp.eval_h = eval_h
kp.eval_hv = eval_hv
set_hess_callback(kp,eval_hess_wrapper)
end
function setCallbacks(kp::KnitroProblem,
eval_f::Function,
eval_g::Function,
eval_grad_f::Function,
eval_jac_g::Function,
eval_h::Function,
eval_hv::Function)
setFuncCallback(kp, eval_f, eval_g)
setGradCallback(kp, eval_grad_f, eval_jac_g)
setHessCallback(kp, eval_h, eval_hv)
end
function setMIPCallback(kp::KnitroProblem, eval_mip_node::Function)
kp.eval_mip_node = eval_mip_node
set_mip_node_callback(kp,eval_mip_node_wrapper)
end
# Getters and Setters for Parameters/Options
loadOptionsFile(kp, filename) = load_param_file(kp, filename)
loadTunerFile(kp, filename) = load_tuner_file(kp, filename)
setOption(args...) = set_param(args...)
getOption(args...) = get_param(args...)
function applicationReturnStatus(kp::KnitroProblem)
if kp.status == 100
# chosen not to clash with any of the KTR_RC_* codes
return :Uninitialized
elseif kp.status == 101
# chosen not to clash with any of the KTR_RC_* codes
return :Initialized
elseif kp.status == 0
return :Optimal
elseif 1 <= kp.status <= 11
return :ReverseComms
elseif -199 <= kp.status <= -100
return :FeasibleApproximate
elseif -299 <= kp.status <= -200
return :Infeasible
elseif kp.status == -300
return :Unbounded
elseif -499 <= kp.status <= -400
return :UserLimit
elseif -599 <= kp.status <= -500
return :KnitroError
else
return :Undefined
end
end
include("ktr_callbacks.jl")
include("ktr_functions.jl")
include("ktr_defines.jl")
include("ktr_params.jl")
include("KnitroSolverInterface.jl")
end