-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathupdate.f90
More file actions
407 lines (353 loc) · 18.1 KB
/
update.f90
File metadata and controls
407 lines (353 loc) · 18.1 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
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
module update_cobyla_mod
!--------------------------------------------------------------------------------------------------!
! This module contains subroutines concerning the update of the interpolation set.
!
! Coded by Zaikun ZHANG (www.zhangzk.net) based on Powell's code and the COBYLA paper.
!
! Dedicated to the late Professor M. J. D. Powell FRS (1936--2015).
!
! Started: July 2021
!
! Last Modified: Thu 14 Aug 2025 07:34:04 AM CST
!--------------------------------------------------------------------------------------------------!
implicit none
private
public :: updatexfc, updatepole, findpole
contains
subroutine updatexfc(jdrop, constr, cpen, cstrv, d, f, conmat, cval, fval, sim, simi, info)
!--------------------------------------------------------------------------------------------------!
! This subroutine revises the simplex by updating the elements of SIM, SIMI, FVAL, CONMAT, and CVAL.
!--------------------------------------------------------------------------------------------------!
! Common modules
use, non_intrinsic :: consts_mod, only : IK, RP, ONE, TENTH, DEBUGGING
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf, is_finite
use, non_intrinsic :: infos_mod, only : INFO_DFT, DAMAGING_ROUNDING
use, non_intrinsic :: linalg_mod, only : matprod, inprod, outprod, maximum, eye, inv, isinv
use, non_intrinsic :: debug_mod, only : assert
implicit none
! Inputs
integer(IK), intent(in) :: jdrop
real(RP), intent(in) :: constr(:) ! CONSTR(M)
real(RP), intent(in) :: cpen
real(RP), intent(in) :: cstrv
real(RP), intent(in) :: d(:) ! D(N)
real(RP), intent(in) :: f
! In-outputs
real(RP), intent(inout) :: conmat(:, :) ! CONMAT(M, N+1)
real(RP), intent(inout) :: cval(:) ! CVAL(N+1)
real(RP), intent(inout) :: fval(:) ! FVAL(N+1)
real(RP), intent(inout) :: sim(:, :)! SIM(N, N+1)
real(RP), intent(inout) :: simi(:, :) ! SIMI(N, N)
! Outputs
integer(IK), intent(out) :: info
! Local variables
character(len=*), parameter :: srname = 'UPDATEXFC'
integer(IK) :: m
integer(IK) :: n
real(RP) :: erri
real(RP) :: erri_test
real(RP) :: sim_old(size(sim, 1), size(sim, 2))
real(RP) :: simi_jdrop(size(simi, 2))
real(RP) :: simi_old(size(simi, 1), size(simi, 2))
real(RP) :: simi_test(size(simi, 1), size(simi, 2))
real(RP) :: simid(size(simi, 1))
real(RP) :: sum_simi(size(simi, 2))
real(RP), parameter :: itol = ONE
! Sizes
m = int(size(constr), kind(m))
n = int(size(sim, 1), kind(n))
! Preconditions
if (DEBUGGING) then
call assert(m >= 0, 'M >= 0', srname)
call assert(n >= 1, 'N >= 1', srname)
call assert(jdrop >= 0 .and. jdrop <= n + 1, '1 <= JDROP <= N+1', srname)
call assert(.not. any(is_nan(constr) .or. is_posinf(constr)), 'CONSTR does not contain NaN/+Inf', srname)
call assert(.not. (is_nan(cstrv) .or. is_posinf(cstrv)), 'CSTRV is not NaN/+Inf', srname)
call assert(size(d) == n .and. all(is_finite(d)), 'SIZE(D) == N, D is finite', srname)
call assert(.not. (is_nan(f) .or. is_posinf(f)), 'F is not NaN/+Inf', srname)
call assert(size(conmat, 1) == m .and. size(conmat, 2) == n + 1, 'SIZE(CONMAT) = [M, N+1]', srname)
call assert(.not. any(is_nan(conmat) .or. is_posinf(conmat)), 'CONMAT does not contain NaN/+Inf', srname)
call assert(size(cval) == n + 1 .and. .not. any(cval < 0 .or. is_nan(cval) .or. is_posinf(cval)), &
& 'SIZE(CVAL) == N+1 and CVAL does not contain negative values or NaN/+Inf', srname)
call assert(size(fval) == n + 1 .and. .not. any(is_nan(fval) .or. is_posinf(fval)), &
& 'SIZE(FVAL) == N+1 and FVAL is not NaN/+Inf', srname)
call assert(size(sim, 1) == n .and. size(sim, 2) == n + 1, 'SIZE(SIM) == [N, N+1]', srname)
call assert(all(is_finite(sim)), 'SIM is finite', srname)
call assert(all(sum(abs(sim(:, 1:n)), dim=1) > 0), 'SIM(:, 1:N) has no zero column', srname)
call assert(size(simi, 1) == n .and. size(simi, 2) == n, 'SIZE(SIMI) == [N, N]', srname)
call assert(all(is_finite(simi)), 'SIMI is finite', srname)
call assert(isinv(sim(:, 1:n), simi, itol), 'SIMI = SIM(:, 1:N)^{-1}', srname)
end if
!====================!
! Calculation starts !
!====================!
! Do nothing when JDROP is 0. This can only happen after a trust-region step.
if (jdrop <= 0) then ! JDROP < 0 is impossible if the input is correct.
info = INFO_DFT ! INFO must be set, as it is an output!
return
end if
sim_old = sim
simi_old = simi
! N.B.: The use of OUTPROD is expensive memory-wise, but it is not our concern in this implementation.
if (jdrop <= n) then
sim(:, jdrop) = d
simi_jdrop = simi(jdrop, :) / inprod(simi(jdrop, :), d)
simi = simi - outprod(matprod(simi, d), simi_jdrop)
simi(jdrop, :) = simi_jdrop
else ! JDROP = N+1
sim(:, n + 1) = sim(:, n + 1) + d
sim(:, 1:n) = sim(:, 1:n) - spread(d, dim=2, ncopies=n)
simid = matprod(simi, d)
sum_simi = sum(simi, dim=1)
simi = simi + outprod(simid, sum_simi / (ONE - sum(simid)))
end if
! Check whether SIMI is a poor approximation to the inverse of SIM(:, 1:N).
! Calculate SIMI from scratch if the current one is damaged by rounding errors.
erri = maximum(abs(matprod(simi, sim(:, 1:n)) - eye(n))) ! MAXIMUM(X) returns NaN if X contains NaN
if (erri > TENTH * itol .or. is_nan(erri)) then
simi_test = inv(sim(:, 1:n))
erri_test = maximum(abs(matprod(simi_test, sim(:, 1:n)) - eye(n)))
if (erri_test < erri .or. (is_nan(erri) .and. .not. is_nan(erri_test))) then
simi = simi_test
erri = erri_test
end if
end if
! If SIMI is satisfactory, then update FVAL, CONMAT, CVAL, and the pole position. Otherwise, restore
! SIM and SIMI, and return with INFO = DAMAGING_ROUNDING.
if (erri <= itol) then
fval(jdrop) = f
conmat(:, jdrop) = constr
cval(jdrop) = cstrv
! Switch the best vertex to the pole position SIM(:, N+1) if it is not there already.
call updatepole(cpen, conmat, cval, fval, sim, simi, info)
else ! ERRI > ITOL or ERRI is NaN
info = DAMAGING_ROUNDING
sim = sim_old
simi = simi_old
end if
!====================!
! Calculation ends !
!====================!
! Postconditions
if (DEBUGGING) then
call assert(size(conmat, 1) == m .and. size(conmat, 2) == n + 1, 'SIZE(CONMAT) = [M, N+1]', srname)
call assert(.not. any(is_nan(conmat) .or. is_posinf(conmat)), 'CONMAT does not contain NaN/+Inf', srname)
call assert(size(cval) == n + 1 .and. .not. any(cval < 0 .or. is_nan(cval) .or. is_posinf(cval)), &
& 'SIZE(CVAL) == N+1 and CVAL does not contain negative values or NaN/+Inf', srname)
call assert(size(fval) == n + 1 .and. .not. any(is_nan(fval) .or. is_posinf(fval)), &
& 'SIZE(FVAL) == N+1 and FVAL is not NaN/+Inf', srname)
call assert(size(sim, 1) == n .and. size(sim, 2) == n + 1, 'SIZE(SIM) == [N, N+1]', srname)
call assert(all(is_finite(sim)), 'SIM is finite', srname)
call assert(all(sum(abs(sim(:, 1:n)), dim=1) > 0), 'SIM(:, 1:N) has no zero column', srname)
call assert(size(simi, 1) == n .and. size(simi, 2) == n, 'SIZE(SIMI) == [N, N]', srname)
call assert(all(is_finite(simi)), 'SIMI is finite', srname)
call assert(isinv(sim(:, 1:n), simi, itol) .or. info == DAMAGING_ROUNDING, &
& 'SIMI = SIM(:, 1:N)^{-1} unless the rounding is damaging', srname)
end if
end subroutine updatexfc
subroutine updatepole(cpen, conmat, cval, fval, sim, simi, info)
!--------------------------------------------------------------------------------------------------!
! This subroutine identifies the best vertex of the current simplex with respect to the merit
! function PHI = F + CPEN * CSTRV, and then switch this vertex to SIM(:, N + 1), which Powell called
! the "pole position" in his comments. CONMAT, CVAL, FVAL, and SIMI are updated accordingly.
!
! N.B. 1: In precise arithmetic, the following two procedures produce the same results:
! 1) apply UPDATEPOLE to SIM twice, first with CPEN = CPEN1 and then with CPEN = CPEN2;
! 2) apply UPDATEPOLE to SIM with CPEN = CPEN2.
! In finite-precision arithmetic, however, they may produce different results unless CPEN1 = CPEN2.
!
! N.B. 2: When JOPT == N+1, the best vertex is already at the pole position, so there is nothing to
! switch. However, as in Powell's code, the code below will check whether SIMI is good enough to
! work as the inverse of SIM(:, 1:N) or not. If not, Powell's code would invoke an error return of
! COBYLB; our implementation, however, will try calculating SIMI from scratch; if the recalculated
! SIMI is still of poor quality, then UPDATEPOLE will return with INFO = DAMAGING_ROUNDING,
! informing COBYLB that SIMI is poor due to damaging rounding errors.
!
! N.B. 3: UPDATEPOLE should be called when and only when FINDPOLE can potentially returns a value
! other than N+1. The value of FINDPOLE is determined by CPEN, CVAL, and FVAL, the latter two being
! decided by SIM. Thus UPDATEPOLE should be called after CPEN or SIM changes. COBYLA updates CPEN at
! only two places: the beginning of each trust-region iteration, and when REDRHO is called;
! SIM is updated only by UPDATEXFC, which itself calls UPDATEPOLE internally. Therefore, we only
! need to call UPDATEPOLE after updating CPEN at the beginning of each trust-region iteration and
! after each invocation of REDRHO.
!--------------------------------------------------------------------------------------------------!
! Common modules
use, non_intrinsic :: consts_mod, only : IK, RP, ZERO, ONE, TENTH, DEBUGGING
use, non_intrinsic :: infos_mod, only : DAMAGING_ROUNDING, INFO_DFT
use, non_intrinsic :: debug_mod, only : assert
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf, is_finite
use, non_intrinsic :: linalg_mod, only : matprod, eye, inv, isinv, maximum
implicit none
! Inputs
real(RP), intent(in) :: cpen
! In-outputs
real(RP), intent(inout) :: conmat(:, :) ! CONMAT(M, N+1)
real(RP), intent(inout) :: cval(:) ! CVAL(N+1)
real(RP), intent(inout) :: fval(:) ! FVAL(N+1)
real(RP), intent(inout) :: sim(:, :)! SIM(N, N+1)
real(RP), intent(inout) :: simi(:, :) ! SIMI(N, N)
! Outputs
integer(IK), intent(out) :: info
! Local variables
character(len=*), parameter :: srname = 'UPDATEPOLE'
integer(IK) :: jopt
integer(IK) :: m
integer(IK) :: n
real(RP) :: erri
real(RP) :: erri_test
real(RP) :: sim_jopt(size(sim, 1))
real(RP) :: sim_old(size(sim, 1), size(sim, 2))
real(RP) :: simi_old(size(simi, 1), size(simi, 2))
real(RP) :: simi_test(size(simi, 1), size(simi, 2))
real(RP), parameter :: itol = ONE
! Sizes
m = int(size(conmat, 1), kind(m))
n = int(size(sim, 1), kind(n))
! Preconditions
if (DEBUGGING) then
call assert(m >= 0, 'M >= 0', srname)
call assert(n >= 1, 'N >= 1', srname)
call assert(cpen > 0, 'CPEN > 0', srname)
call assert(size(conmat, 1) == m .and. size(conmat, 2) == n + 1, 'SIZE(CONMAT) = [M, N+1]', srname)
call assert(.not. any(is_nan(conmat) .or. is_posinf(conmat)), 'CONMAT does not contain NaN/+Inf', srname)
call assert(size(cval) == n + 1 .and. .not. any(cval < 0 .or. is_nan(cval) .or. is_posinf(cval)), &
& 'SIZE(CVAL) == N+1 and CVAL does not contain negative values or NaN/+Inf', srname)
call assert(size(fval) == n + 1 .and. .not. any(is_nan(fval) .or. is_posinf(fval)), &
& 'SIZE(FVAL) == N+1 and FVAL is not NaN/+Inf', srname)
call assert(size(sim, 1) == n .and. size(sim, 2) == n + 1, 'SIZE(SIM) == [N, N+1]', srname)
call assert(all(is_finite(sim)), 'SIM is finite', srname)
call assert(all(sum(abs(sim(:, 1:n)), dim=1) > 0), 'SIM(:, 1:N) has no zero column', srname)
call assert(size(simi, 1) == n .and. size(simi, 2) == n, 'SIZE(SIMI) == [N, N]', srname)
call assert(all(is_finite(simi)), 'SIMI is finite', srname)
call assert(isinv(sim(:, 1:n), simi, itol), 'SIMI = SIM(:, 1:N)^{-1}', srname)
end if
!====================!
! Calculation starts !
!====================!
! INFO must be set, as it is an output.
info = INFO_DFT
! Identify the optimal vertex of the current simplex.
jopt = findpole(cpen, cval, fval)
! Switch the best vertex to the pole position SIM(:, N+1) if it is not there already, and update
! SIMI. Before the update, save a copy of SIM and SIMI. If the update is unsuccessful due to
! damaging rounding errors, we restore them and return with INFO = DAMAGING_ROUNDING.
sim_old = sim
simi_old = simi
if (jopt >= 1 .and. jopt <= n) then
! Unless there is a bug in FINDPOLE, it is guaranteed that JOPT >= 1.
! When JOPT == N + 1, there is nothing to switch; in addition, SIMI(JOPT, :) will be illegal.
sim(:, n + 1) = sim(:, n + 1) + sim(:, jopt)
sim_jopt = sim(:, jopt)
sim(:, jopt) = ZERO
sim(:, 1:n) = sim(:, 1:n) - spread(sim_jopt, dim=2, ncopies=n)
!!MATLAB: sim(:, 1:n) = sim(:, 1:n) - sim_jopt; % sim_jopt should be a column! Implicit expansion
! The above update is equivalent to multiply SIM(:, 1:N) from the right side by a matrix whose
! JOPT-th row is [-1, -1, ..., -1], while all the other rows are the same as those of the
! identity matrix. It is easy to check that the inverse of this matrix is itself. Therefore,
! SIMI should be updated by a multiplication with this matrix (i.e., its inverse) from the left
! side, as is done in the following line. The JOPT-th row of the updated SIMI is minus the sum
! of all rows of the original SIMI, whereas all the other rows remain unchanged.
simi(jopt, :) = -sum(simi, dim=1) ! Must ensure that 1 <= JOPT <= N!
end if
! Check whether SIMI is a poor approximation to the inverse of SIM(:, 1:N).
! Calculate SIMI from scratch if the current one is damaged by rounding errors.
erri = maximum(abs(matprod(simi, sim(:, 1:n)) - eye(n))) ! MAXIMUM(X) returns NaN if X contains NaN
if (erri > TENTH * itol .or. is_nan(erri)) then
simi_test = inv(sim(:, 1:n))
erri_test = maximum(abs(matprod(simi_test, sim(:, 1:n)) - eye(n)))
if (erri_test < erri .or. (is_nan(erri) .and. .not. is_nan(erri_test))) then
simi = simi_test
erri = erri_test
end if
end if
! If SIMI is satisfactory, then update FVAL, CONMAT, and CVAL. Otherwise, restore SIM and SIMI, and
! return with INFO = DAMAGING_ROUNDING.
if (erri <= itol) then
if (jopt >= 1 .and. jopt <= n) then
fval([jopt, n + 1_IK]) = fval([n + 1_IK, jopt])
conmat(:, [jopt, n + 1_IK]) = conmat(:, [n + 1_IK, jopt])
cval([jopt, n + 1_IK]) = cval([n + 1_IK, jopt])
end if
else ! ERRI > ITOL or ERRI is NaN
info = DAMAGING_ROUNDING
sim = sim_old
simi = simi_old
end if
!====================!
! Calculation ends !
!====================!
! Postconditions
if (DEBUGGING) then
call assert(findpole(cpen, cval, fval) == n + 1 .or. info == DAMAGING_ROUNDING, &
& 'The best point is SIM(:, N+1) unless the rounding is damaging', srname)
call assert(size(conmat, 1) == m .and. size(conmat, 2) == n + 1, 'SIZE(CONMAT) = [M, N+1]', srname)
call assert(.not. any(is_nan(conmat) .or. is_posinf(conmat)), 'CONMAT does not contain NaN/+Inf', srname)
call assert(size(cval) == n + 1 .and. .not. any(cval < 0 .or. is_nan(cval) .or. is_posinf(cval)), &
& 'SIZE(CVAL) == N+1 and CVAL does not contain negative values or NaN/+Inf', srname)
call assert(size(fval) == n + 1 .and. .not. any(is_nan(fval) .or. is_posinf(fval)), &
& 'SIZE(FVAL) == N+1 and FVAL is not NaN/+Inf', srname)
call assert(size(sim, 1) == n .and. size(sim, 2) == n + 1, 'SIZE(SIM) == [N, N+1]', srname)
call assert(all(is_finite(sim)), 'SIM is finite', srname)
call assert(all(sum(abs(sim(:, 1:n)), dim=1) > 0), 'SIM(:, 1:N) has no zero column', srname)
call assert(size(simi, 1) == n .and. size(simi, 2) == n, 'SIZE(SIMI) == [N, N]', srname)
call assert(all(is_finite(simi)), 'SIMI is finite', srname)
! Do not check SIMI = SIM(:, 1:N)^{-1}, as it may not be true due to damaging rounding.
call assert(isinv(sim(:, 1:n), simi, itol) .or. info == DAMAGING_ROUNDING, &
& 'SIMI = SIM(:, 1:N)^{-1} unless the rounding is damaging', srname)
end if
end subroutine updatepole
function findpole(cpen, cval, fval) result(jopt)
!--------------------------------------------------------------------------------------------------!
! This subroutine identifies the best vertex of the current simplex with respect to the merit
! function PHI = F + CPEN * CSTRV.
!--------------------------------------------------------------------------------------------------!
! Common modules
use, non_intrinsic :: consts_mod, only : IK, RP, DEBUGGING
use, non_intrinsic :: debug_mod, only : assert
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf
implicit none
! Inputs
real(RP), intent(in) :: cpen
real(RP), intent(in) :: cval(:) ! CVAL(N+1)
real(RP), intent(in) :: fval(:) ! FVAL(N+1)
! Outputs
integer(IK) :: jopt
! Local variables
character(len=*), parameter :: srname = 'FINDPOLE'
integer(IK) :: n
real(RP) :: phi(size(cval))
real(RP) :: phimin
! Size
n = int(size(fval) - 1, kind(n))
! Preconditions
if (DEBUGGING) then
call assert(cpen > 0, 'CPEN > 0', srname)
call assert(size(cval) == n + 1 .and. .not. any(cval < 0 .or. is_nan(cval) .or. is_posinf(cval)), &
& 'SIZE(CVAL) == N+1 and CVAL does not contain negative values or NaN/+Inf', srname)
call assert(size(fval) == n + 1 .and. .not. any(is_nan(fval) .or. is_posinf(fval)), &
& 'SIZE(FVAL) == N+1 and FVAL is not NaN/+Inf', srname)
end if
!====================!
! Calculation starts !
!====================!
! Identify the optimal vertex of the current simplex.
jopt = int(size(fval), kind(jopt)) ! We use N + 1 as the default value of JOPT.
phi = fval + cpen * cval
phimin = minval(phi)
! Essentially, JOPT = MINLOC(PHI). However, we keep JOPT = N + 1 unless there is a strictly better
! choice. When there are multiple choices, we choose the JOPT with the smallest value of CVAL.
if (phimin < phi(jopt) .or. any(cval < cval(jopt) .and. phi <= phi(jopt))) then
jopt = int(minloc(cval, mask=(phi <= phimin), dim=1), kind(jopt))
!!MATLAB: cmin = min(cval(phi <= phimin)); jopt = find(phi <= phimin & cval <= cmin, 1, 'first');
end if
!====================!
! Calculation ends !
!====================!
! Postconditions
if (DEBUGGING) then
call assert(jopt >= 1 .and. jopt <= n + 1, '1 <= JOPT <= N+1', srname)
call assert(jopt == n + 1 .or. phi(jopt) < phi(n + 1) .or. (phi(jopt) <= phi(n + 1) .and. cval(jopt) < cval(n + 1)), &
& 'JOPT = N+1 unless PHI(JOPT) < PHI(N+1) or PHI(JOPT) <= PHI(N+1) and CVAL(JOPT) < CVAL(N+1)', srname)
end if
end function findpole
end module update_cobyla_mod