-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwrapper.jl
More file actions
199 lines (187 loc) · 5.9 KB
/
wrapper.jl
File metadata and controls
199 lines (187 loc) · 5.9 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
struct L_BFGS_B
nmax::Int
mmax::Int
task::Vector{UInt8}
csave::Vector{UInt8}
lsave::Vector{Cint}
isave::Vector{Cint}
dsave::Vector{Cdouble}
wa::Vector{Cdouble}
iwa::Vector{Cint}
g::Vector{Cdouble}
nbd::Vector{Cint}
l::Vector{Cdouble}
u::Vector{Cdouble}
function L_BFGS_B(nmax, mmax)
task = fill(Cuchar(' '), 60)
csave = fill(Cuchar(' '), 60)
lsave = zeros(Cint, 4)
isave = zeros(Cint, 44)
dsave = zeros(Cdouble, 29)
wa = zeros(Cdouble, 2mmax*nmax + 5nmax + 11mmax*mmax + 8mmax)
iwa = zeros(Cint, 3*nmax)
g = zeros(Cdouble, nmax)
nbd = zeros(Cint, nmax)
l = zeros(Cdouble, nmax)
u = zeros(Cdouble, nmax)
new(nmax, mmax, task, csave, lsave, isave, dsave, wa, iwa, g, nbd, l, u)
end
end
function (obj::L_BFGS_B)(func, grad!, x0::AbstractVector, bounds::AbstractMatrix;
m=10, factr=1e7, pgtol=1e-5, iprint=-1, maxfun=15000, maxiter=15000)
x = copy(x0)
n = length(x)
f = 0.0
# clean up
fill!(obj.task, Cuchar(' '))
fill!(obj.csave, Cuchar(' '))
fill!(obj.lsave, zero(Cint))
fill!(obj.isave, zero(Cint))
fill!(obj.dsave, zero(Cdouble))
fill!(obj.wa, zero(Cdouble))
fill!(obj.iwa, zero(Cint))
fill!(obj.g, zero(Cdouble))
fill!(obj.nbd, zero(Cint))
fill!(obj.l, zero(Cdouble))
fill!(obj.u, zero(Cdouble))
# set bounds
for i = 1:n
obj.nbd[i] = bounds[1,i]
obj.l[i] = bounds[2,i]
obj.u[i] = bounds[3,i]
end
# start
obj.task[1:5] = b"START"
while true
iprint == 1 && flush(stdout) # PR 12
setulb(n, m, x, obj.l, obj.u, obj.nbd, f, obj.g, factr, pgtol, obj.wa,
obj.iwa, obj.task, iprint, obj.csave, obj.lsave, obj.isave, obj.dsave)
if obj.task[1:2] == b"FG"
f = func(x)
grad!(obj.g, x)
elseif obj.task[1:5] == b"NEW_X"
if obj.isave[30] ≥ maxiter
obj.task[1:43] = b"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT"
elseif obj.isave[34] ≥ maxfun
obj.task[1:52] = b"STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT"
end
else
return f, x
end
end
end
function (obj::L_BFGS_B)(func, x0::AbstractVector, bounds::AbstractMatrix;
m=10, factr=1e7, pgtol=1e-5, iprint=-1, maxfun=15000, maxiter=15000)
x = copy(x0)
n = length(x)
f = 0.0
# clean up
fill!(obj.task, Cuchar(' '))
fill!(obj.csave, Cuchar(' '))
fill!(obj.lsave, zero(Cint))
fill!(obj.isave, zero(Cint))
fill!(obj.dsave, zero(Cdouble))
fill!(obj.wa, zero(Cdouble))
fill!(obj.iwa, zero(Cint))
fill!(obj.g, zero(Cdouble))
fill!(obj.nbd, zero(Cint))
fill!(obj.l, zero(Cdouble))
fill!(obj.u, zero(Cdouble))
# set bounds
for i = 1:n
obj.nbd[i] = bounds[1,i]
obj.l[i] = bounds[2,i]
obj.u[i] = bounds[3,i]
end
# start
obj.task[1:5] = b"START"
while true
iprint == 1 && flush(stdout) # PR 12
setulb(n, m, x, obj.l, obj.u, obj.nbd, f, obj.g, factr, pgtol, obj.wa,
obj.iwa, obj.task, iprint, obj.csave, obj.lsave, obj.isave, obj.dsave)
if obj.task[1:2] == b"FG"
f, grad = func(x)
obj.g[1:n] = grad
elseif obj.task[1:5] == b"NEW_X"
if obj.isave[30] ≥ maxiter
obj.task[1:43] = b"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT"
elseif obj.isave[34] ≥ maxfun
obj.task[1:52] = b"STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT"
end
else
return f, x
end
end
end
struct InPlace{F}
func_grad!::F
end
function (obj::L_BFGS_B)(func::InPlace, x0::AbstractVector, bounds::AbstractMatrix;
m=10, factr=1e7, pgtol=1e-5, iprint=-1, maxfun=15000, maxiter=15000)
x = copy(x0)
n = length(x)
f = 0.0
# clean up
fill!(obj.task, Cuchar(' '))
fill!(obj.csave, Cuchar(' '))
fill!(obj.lsave, zero(Cint))
fill!(obj.isave, zero(Cint))
fill!(obj.dsave, zero(Cdouble))
fill!(obj.wa, zero(Cdouble))
fill!(obj.iwa, zero(Cint))
fill!(obj.g, zero(Cdouble))
fill!(obj.nbd, zero(Cint))
fill!(obj.l, zero(Cdouble))
fill!(obj.u, zero(Cdouble))
# set bounds
for i = 1:n
obj.nbd[i] = bounds[1,i]
obj.l[i] = bounds[2,i]
obj.u[i] = bounds[3,i]
end
# start
obj.task[1:5] = b"START"
while true
iprint == 1 && flush(stdout) # PR 12
setulb(n, m, x, obj.l, obj.u, obj.nbd, f, obj.g, factr, pgtol, obj.wa,
obj.iwa, obj.task, iprint, obj.csave, obj.lsave, obj.isave, obj.dsave)
if obj.task[1:2] == b"FG"
f = func.func_grad!(obj.g, x)
elseif obj.task[1:5] == b"NEW_X"
if obj.isave[30] ≥ maxiter
obj.task[1:43] = b"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT"
elseif obj.isave[34] ≥ maxfun
obj.task[1:52] = b"STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT"
end
else
return f, x
end
end
end
function typ_bnd(lb,ub)
lb > ub && error("Inconsistent bounds")
lbinf = lb==-Inf
ubinf = ub==+Inf
lbinf && ubinf && return 0
!lbinf && ubinf && return 1
!lbinf && !ubinf && return 2
lbinf && !ubinf && return 3
end
function _opt_bounds(n,m,lb,ub)
optimizer=L_BFGS_B(n,m)
bounds=zeros(3,n)
bounds[2,:].=lb
bounds[3,:].=ub
bounds[1,:].= typ_bnd.(lb,ub)
return optimizer,bounds
end
function lbfgsb(f,g!,x0;m=10,lb=[-Inf for i in x0],ub=[Inf for i in x0],kwargs...)
n=length(x0)
optimizer,bounds=_opt_bounds(n,m,lb,ub)
optimizer(f,g!,x0,bounds;m=m,kwargs...)
end
function lbfgsb(f,x0;m=10,lb=[-Inf for i in x0],ub=[Inf for i in x0],kwargs...)
n=length(x0)
optimizer,bounds=_opt_bounds(n,m,lb,ub)
optimizer(f,x0,bounds;m=m,kwargs...)
end