/
ktr_callbacks.jl
148 lines (130 loc) · 5.24 KB
/
ktr_callbacks.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
export
set_func_callback,
set_grad_callback,
set_hess_callback,
# set_newpt_callback,
# set_ms_process_callback,
set_mip_node_callback #,
# set_ms_initpt_callback,
# set_puts_callback,
callback_params = (Cint, Cint, Cint, Cint, Cint, Ptr{Cdouble},
Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble},
Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble},
Ptr{Cdouble}, Ptr{Void})
"""
Set the callback function that evaluates `obj` and `c` at `x`.
It may also evaluate `objGrad` and `jac` if EVALFC and EVALGA are
combined into a single call.
"""
function set_func_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, callback_params)
return_code = @ktr_ccall(set_func_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting function callback")
end
end
# /**
# * Do not modify "hessian" or "hessVector".
# */
# int KNITRO_API KTR_set_grad_callback (KTR_context_ptr kc,
# KTR_callback * const fnPtr);
"""
Set the callback function that evaluates `objGrad` and `jac` at `x`.
It may do nothing if EVALFC and EVALGA are combined into a single call.
"""
function set_grad_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, callback_params)
return_code = @ktr_ccall(set_grad_callback, Int32,(Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting gradient callback")
end
end
"""
Set the callback function that evaluates second derivatives at (x, lambda).
If `evalRequestCode` equals KTR_RC_EVALH, then the function must
return nonzeroes in `hess`. If it equals KTR_RC_EVALHV, then the
function multiplies second derivatives by `hessVector` and returns the
product in `hessVector`.
"""
function set_hess_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, callback_params)
return_code = @ktr_ccall(set_hess_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting hessian callback")
end
end
"""
Set the callback function that is invoked after KNITRO computes a
new estimate of the solution point (i.e., after every major iteration).
"""
function set_newpt_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, (Ptr{Void}, Cint, Cint, Cint, Ptr{Cdouble},
Ptr{Cdouble}, Cdouble, Ptr{Cdouble}, Ptr{Cdouble},
Ptr{Cdouble}, Ptr{Void}))
return_code = @ktr_ccall(set_newpt_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting newpoint callback")
end
end
"""
This callback function is for multistart (MS) problems only.
Set the callback function that is invoked after KNITRO finishes
processing a multistart solve. The function should not modify any
KNITRO arguments. Arguments `x` and `lambda` contain the solution from
the last solve. Arguments `obj` and `c` contain objective and constraint
values at `x`. First and second derivative arguments are not currently
defined and should not be examined.
"""
function set_ms_process_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, callback_params)
return_code = @ktr_ccall(set_ms_process_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting multi-start callback")
end
end
"""
This callback function is for mixed integer (MIP) problems only.
Set the callback function that is invoked after KNITRO finishes
processing a node on the branch-and-bound tree.
"""
function set_mip_node_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, callback_params)
return_code = @ktr_ccall(set_mip_node_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code != 0
error("KNITRO: Error setting MIP node callback")
end
end
"""
This callback function is for multistart (MS) problems only.
Set the callback that allows applications to specify an initial point
before each local solve in the multistart procedure.
"""
function set_ms_initpt_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, (Cint, Cint, Cint, Ptr{Cdouble}, Ptr{Cdouble},
Ptr{Cdouble}, Ptr{cdouble}, Ptr{Void}))
return_code = @ktr_ccall(set_ms_initpt_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code < 0
error("KNITRO: Error setting multi-start initial-point callback")
end
end
"""
Set a put-string callback function to handle output generated by the KNITRO solver.
The callback function should return the number of characters that were printed.
By default KNITRO prints to stdout or a file named `knitro.log`,
as determined by KTR_PARAM_OUTMODE.
"""
function set_puts_callback(kp::KnitroProblem, f::Function)
cb = cfunction(f, Cint, (Ptr{Cchar}, Ptr{Void}))
return_code = @ktr_ccall(set_puts_callback, Int32, (Ptr{Void},
Ptr{Void}), kp.env, cb)
if return_code < 0
error("KNITRO: Error setting put-string callback")
end
end