-
Notifications
You must be signed in to change notification settings - Fork 58
/
C_wrapper.jl
458 lines (443 loc) · 10.9 KB
/
C_wrapper.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
mutable struct IpoptProblem
ipopt_problem::Ptr{Cvoid} # Reference to the internal data structure
n::Int # Num vars
m::Int # Num cons
x::Vector{Float64} # Starting and final solution
g::Vector{Float64} # Final constraint values
mult_g::Vector{Float64} # lagrange multipliers on constraints
mult_x_L::Vector{Float64} # lagrange multipliers on lower bounds
mult_x_U::Vector{Float64} # lagrange multipliers on upper bounds
obj_val::Float64 # Final objective
status::Cint # Final status
# Callbacks
eval_f::Function
eval_g::Function
eval_grad_f::Function
eval_jac_g::Function
eval_h::Union{Function,Nothing}
intermediate::Union{Function,Nothing}
end
function _Eval_F_CB(
n::Cint,
x_ptr::Ptr{Float64},
x_new::Cint,
obj_value::Ptr{Float64},
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
x = unsafe_wrap(Array, x_ptr, Int(n))
if x_new == Cint(1)
prob.x .= x
end
new_obj = convert(Float64, prob.eval_f(x))::Float64
unsafe_store!(obj_value, new_obj)
return Cint(1)
end
function _Eval_Grad_F_CB(
n::Cint,
x_ptr::Ptr{Float64},
# A Bool indicating if `x` is a new point. We don't make use of this.
::Cint,
grad_f::Ptr{Float64},
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
new_grad_f = unsafe_wrap(Array, grad_f, Int(n))
x = unsafe_wrap(Array, x_ptr, Int(n))
prob.eval_grad_f(x, new_grad_f)
return Cint(1)
end
function _Eval_G_CB(
n::Cint,
x_ptr::Ptr{Float64},
x_new::Cint,
m::Cint,
g_ptr::Ptr{Float64},
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
new_g = unsafe_wrap(Array, g_ptr, Int(m))
x = unsafe_wrap(Array, x_ptr, Int(n))
if x_new == Cint(1)
prob.x .= x
end
prob.eval_g(x, new_g)
return Cint(1)
end
function _Eval_Jac_G_CB(
n::Cint,
x_ptr::Ptr{Float64},
::Cint,
::Cint,
nele_jac::Cint,
iRow::Ptr{Cint},
jCol::Ptr{Cint},
values_ptr::Ptr{Float64},
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
x = unsafe_wrap(Array, x_ptr, Int(n))
rows = unsafe_wrap(Array, iRow, Int(nele_jac))
cols = unsafe_wrap(Array, jCol, Int(nele_jac))
if values_ptr == C_NULL
prob.eval_jac_g(x, rows, cols, nothing)
else
values = unsafe_wrap(Array, values_ptr, Int(nele_jac))
prob.eval_jac_g(x, rows, cols, values)
end
return Cint(1)
end
function _Eval_H_CB(
n::Cint,
x_ptr::Ptr{Float64},
::Cint,
obj_factor::Float64,
m::Cint,
lambda_ptr::Ptr{Float64},
::Cint,
nele_hess::Cint,
iRow::Ptr{Cint},
jCol::Ptr{Cint},
values_ptr::Ptr{Float64},
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
if prob.eval_h === nothing
# No hessian. Return FALSE for failure.
return Cint(0)
end
x = unsafe_wrap(Array, x_ptr, Int(n))
lambda = unsafe_wrap(Array, lambda_ptr, Int(m))
rows = unsafe_wrap(Array, iRow, Int(nele_hess))
cols = unsafe_wrap(Array, jCol, Int(nele_hess))
if values_ptr == C_NULL
prob.eval_h(x, rows, cols, obj_factor, lambda, nothing)
else
values = unsafe_wrap(Array, values_ptr, Int(nele_hess))
prob.eval_h(x, rows, cols, obj_factor, lambda, values)
end
return Cint(1) # Return TRUE for success.
end
function _Intermediate_CB(
alg_mod::Cint,
iter_count::Cint,
obj_value::Float64,
inf_pr::Float64,
inf_du::Float64,
mu::Float64,
d_norm::Float64,
regularization_size::Float64,
alpha_du::Float64,
alpha_pr::Float64,
ls_trials::Cint,
user_data::Ptr{Cvoid},
)
prob = unsafe_pointer_to_objref(user_data)::IpoptProblem
ret = prob.intermediate(
alg_mod,
iter_count,
obj_value,
inf_pr,
inf_du,
mu,
d_norm,
regularization_size,
alpha_du,
alpha_pr,
ls_trials,
)
# Return TRUE if the optimization should continue.
return ret ? Cint(1) : Cint(0)
end
function CreateIpoptProblem(
n::Int,
x_L::Vector{Float64},
x_U::Vector{Float64},
m::Int,
g_L::Vector{Float64},
g_U::Vector{Float64},
nele_jac::Int,
nele_hess::Int,
eval_f,
eval_g,
eval_grad_f,
eval_jac_g,
eval_h,
)
@assert n == length(x_L) == length(x_U)
@assert m == length(g_L) == length(g_U)
eval_f_cb = @cfunction(
_Eval_F_CB,
Cint,
(Cint, Ptr{Float64}, Cint, Ptr{Float64}, Ptr{Cvoid}),
)
eval_g_cb = @cfunction(
_Eval_G_CB,
Cint,
(Cint, Ptr{Float64}, Cint, Cint, Ptr{Float64}, Ptr{Cvoid}),
)
eval_grad_f_cb = @cfunction(
_Eval_Grad_F_CB,
Cint,
(Cint, Ptr{Float64}, Cint, Ptr{Float64}, Ptr{Cvoid}),
)
eval_jac_g_cb = @cfunction(
_Eval_Jac_G_CB,
Cint,
(
Cint,
Ptr{Float64},
Cint,
Cint,
Cint,
Ptr{Cint},
Ptr{Cint},
Ptr{Float64},
Ptr{Cvoid},
),
)
eval_h_cb = @cfunction(
_Eval_H_CB,
Cint,
(
Cint,
Ptr{Float64},
Cint,
Float64,
Cint,
Ptr{Float64},
Cint,
Cint,
Ptr{Cint},
Ptr{Cint},
Ptr{Float64},
Ptr{Cvoid},
),
)
ipopt_problem = ccall(
(:CreateIpoptProblem, libipopt),
Ptr{Cvoid},
(
Cint,
Ptr{Float64},
Ptr{Float64},
Cint,
Ptr{Float64},
Ptr{Float64},
Cint,
Cint,
Cint,
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cvoid},
Ptr{Cvoid},
),
n,
x_L,
x_U,
m,
g_L,
g_U,
nele_jac,
nele_hess,
1, # 1 = Fortran style indexing
eval_f_cb,
eval_g_cb,
eval_grad_f_cb,
eval_jac_g_cb,
eval_h_cb,
)
if ipopt_problem == C_NULL
if n == 0
error(
"IPOPT: Failed to construct problem because there are 0 " *
"variables. If you intended to construct an empty problem, " *
"one work-around is to add a variable fixed to 0.",
)
else
error("IPOPT: Failed to construct problem for some unknown reason.")
end
end
prob = IpoptProblem(
ipopt_problem,
n,
m,
zeros(Float64, n),
zeros(Float64, m),
zeros(Float64, m),
zeros(Float64, n),
zeros(Float64, n),
0.0,
0,
eval_f,
eval_g,
eval_grad_f,
eval_jac_g,
eval_h,
nothing,
)
finalizer(FreeIpoptProblem, prob)
return prob
end
function FreeIpoptProblem(prob::IpoptProblem)
ccall(
(:FreeIpoptProblem, libipopt),
Cvoid,
(Ptr{Cvoid},),
prob.ipopt_problem,
)
return
end
function AddIpoptStrOption(prob::IpoptProblem, keyword::String, value::String)
if !(isascii(keyword) && isascii(value))
error("IPOPT: Non ASCII parameters not supported")
end
ret = ccall(
(:AddIpoptStrOption, libipopt),
Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt8}),
prob.ipopt_problem,
keyword,
value,
)
if ret == 0
error("IPOPT: Couldn't set option '$keyword' to value '$value'.")
end
return
end
function AddIpoptNumOption(prob::IpoptProblem, keyword::String, value::Float64)
if !isascii(keyword)
error("IPOPT: Non ASCII parameters not supported")
end
ret = ccall(
(:AddIpoptNumOption, libipopt),
Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Float64),
prob.ipopt_problem,
keyword,
value,
)
if ret == 0
error("IPOPT: Couldn't set option '$keyword' to value '$value'.")
end
return
end
function AddIpoptIntOption(prob::IpoptProblem, keyword::String, value::Integer)
if !isascii(keyword)
error("IPOPT: Non ASCII parameters not supported")
end
ret = ccall(
(:AddIpoptIntOption, libipopt),
Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Cint),
prob.ipopt_problem,
keyword,
value,
)
if ret == 0
error("IPOPT: Couldn't set option '$keyword' to value '$value'.")
end
return
end
function OpenIpoptOutputFile(
prob::IpoptProblem,
file_name::String,
print_level::Int,
)
if !isascii(file_name)
error("IPOPT: Non ASCII parameters not supported")
end
ret = ccall(
(:OpenIpoptOutputFile, libipopt),
Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Cint),
prob.ipopt_problem,
file_name,
print_level,
)
if ret == 0
error("IPOPT: Couldn't open output file.")
end
return
end
function SetIpoptProblemScaling(
prob::IpoptProblem,
obj_scaling::Float64,
x_scaling::Union{Ptr{Cvoid},Vector{Float64}},
g_scaling::Union{Ptr{Cvoid},Vector{Float64}},
)
ret = ccall(
(:SetIpoptProblemScaling, libipopt),
Cint,
(Ptr{Cvoid}, Float64, Ptr{Float64}, Ptr{Float64}),
prob.ipopt_problem,
obj_scaling,
x_scaling,
g_scaling,
)
if ret == 0
error("IPOPT: Error setting problem scaling.")
end
return
end
function SetIntermediateCallback(prob::IpoptProblem, intermediate::Function)
intermediate_cb = @cfunction(
_Intermediate_CB,
Cint,
(
Cint,
Cint,
Float64,
Float64,
Float64,
Float64,
Float64,
Float64,
Float64,
Float64,
Cint,
Ptr{Cvoid},
),
)
ret = ccall(
(:SetIntermediateCallback, libipopt),
Cint,
(Ptr{Cvoid}, Ptr{Cvoid}),
prob.ipopt_problem,
intermediate_cb,
)
if ret == 0
error("IPOPT: Something went wrong setting the intermediate callback.")
end
prob.intermediate = intermediate
return
end
function IpoptSolve(prob::IpoptProblem)
final_objval = Ref{Cdouble}(0.0)
ret = ccall(
(:IpoptSolve, libipopt),
Cint,
(
Ptr{Cvoid},
Ptr{Float64},
Ptr{Float64},
Ptr{Float64},
Ptr{Float64},
Ptr{Float64},
Ptr{Float64},
Ptr{Cvoid},
),
prob.ipopt_problem,
prob.x,
prob.g,
final_objval,
prob.mult_g,
prob.mult_x_L,
prob.mult_x_U,
pointer_from_objref(prob),
)
prob.obj_val = final_objval[]
prob.status = ret
return prob.status
end