Skip to content

Commit 32e5a64

Browse files
committed
250820.185147.CST [skip ci] revise test_SOLVER.f90 to reduce testing time: test random dimensions instead of prescribed ones
1 parent 23f94e0 commit 32e5a64

File tree

6 files changed

+415
-449
lines changed

6 files changed

+415
-449
lines changed

fortran/tests/test_bobyqa.f90

Lines changed: 74 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module test_solver_mod
5757
!
5858
! Started: September 2021
5959
!
60-
! Last Modified: Wed 20 Aug 2025 05:07:51 PM CST
60+
! Last Modified: Wed 20 Aug 2025 06:42:49 PM CST
6161
!--------------------------------------------------------------------------------------------------!
6262

6363
implicit none
@@ -68,15 +68,15 @@ module test_solver_mod
6868
contains
6969

7070

71-
subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdim)
71+
subroutine test_solver(probs, mindim, maxdim, nrand, randseed, testdim)
7272

7373
use, non_intrinsic :: bobyqa_mod, only : bobyqa
7474
use, non_intrinsic :: consts_mod, only : RP, IK, TWO, TEN, ZERO, REALMAX
7575
use, non_intrinsic :: debug_mod, only : validate
7676
use, non_intrinsic :: infnan_mod, only : is_neginf
7777
use, non_intrinsic :: memory_mod, only : safealloc
7878
use, non_intrinsic :: noise_mod, only : noisy, noisy_calfun, orig_calfun
79-
use, non_intrinsic :: param_mod, only : MINDIM_DFT, MAXDIM_DFT, DIMSTRIDE_DFT, NRAND_DFT, RANDSEED_DFT
79+
use, non_intrinsic :: param_mod, only : MINDIM_DFT, MAXDIM_DFT, NRAND_DFT, RANDSEED_DFT
8080
use, non_intrinsic :: prob_mod, only : PNLEN, PROB_T, construct, destruct
8181
use, non_intrinsic :: rand_mod, only : setseed, rand, randn
8282
use, non_intrinsic :: recursive_mod, only : recursive_fun2
@@ -87,7 +87,6 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
8787
character(len=PNLEN), intent(in), optional :: probs(:)
8888
integer(IK), intent(in), optional :: mindim
8989
integer(IK), intent(in), optional :: maxdim
90-
integer(IK), intent(in), optional :: dimstride
9190
integer(IK), intent(in), optional :: nrand
9291
integer, intent(in), optional :: randseed
9392
character(len=*), intent(in), optional :: testdim
@@ -100,9 +99,6 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
10099
character(len=PNLEN) :: probs_loc(100) ! Maximal number of problems to test: 100
101100
integer :: randseed_loc
102101
integer :: rseed
103-
integer(IK) :: dim_list(100) ! Maximal number of dimensions to test: 100
104-
integer(IK) :: dimstride_loc
105-
integer(IK) :: idim
106102
integer(IK) :: iprint
107103
integer(IK) :: iprob
108104
integer(IK) :: irand
@@ -111,7 +107,6 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
111107
integer(IK) :: maxhist
112108
integer(IK) :: mindim_loc
113109
integer(IK) :: n
114-
integer(IK) :: ndim
115110
integer(IK) :: nnpt
116111
integer(IK) :: nprobs
117112
integer(IK) :: npt
@@ -153,12 +148,6 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
153148
maxdim_loc = MAXDIM_DFT
154149
end if
155150

156-
if (present(dimstride)) then
157-
dimstride_loc = dimstride
158-
else
159-
dimstride_loc = DIMSTRIDE_DFT
160-
end if
161-
162151
if (present(nrand)) then
163152
nrand_loc = nrand
164153
else
@@ -212,15 +201,23 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
212201
else
213202

214203
do iprob = 1, nprobs
204+
215205
probname = probs_loc(iprob)
216-
ndim = (maxdim_loc - mindim_loc) / dimstride_loc + 1_IK
217-
dim_list(1:ndim) = mindim_loc + dimstride_loc*[(idim - 1_IK, idim=1_IK, ndim)]
218-
if (strip(probname) == 'ptinsq') then
219-
dim_list(1:ndim) = int(ceiling(real(dim_list(1:ndim)) / 2.0) * 2, IK) ! Must be even
220-
end if
221-
do idim = 1, ndim
222-
n = dim_list(idim)
223-
call construct(prob, probname, n) ! Construct the testing problem.
206+
207+
do irand = 1, max(0_IK, nrand_loc) + 1_IK
208+
! Initialize the random seed using IRAND, RP, and RANDSEED_LOC. Do not include IK so
209+
! that the results for different IK are the same.
210+
rseed = int(sum(istr(solname)) + sum(istr(probname)) + irand + RP + randseed_loc)
211+
call setseed(rseed)
212+
213+
! Set the problem dimension N to a random value in the range [MINDIM, MAXDIM].
214+
n = mindim_loc + floor(rand() * real(maxdim_loc - mindim_loc + 1_IK, RP), kind(n))
215+
if (strip(probname) == 'ptinsq' .and. modulo(n, 2_IK) == 1) then
216+
n = n + 1_IK ! Must be even
217+
end if
218+
219+
! Construct the testing problem.
220+
call construct(prob, probname, n)
224221

225222
! NPT_LIST defines some extreme values of NPT.
226223
nnpt = 11
@@ -229,65 +226,62 @@ subroutine test_solver(probs, mindim, maxdim, dimstride, nrand, randseed, testdi
229226
& 2_IK * n, 2_IK * n + 1_IK, 2_IK * n + 2_IK, &
230227
& (n + 1_IK) * (n + 2_IK) / 2_IK - 1_IK, (n + 1_IK) * (n + 2_IK) / 2_IK, &
231228
& (n + 1_IK) * (n + 2_IK) / 2_IK + 1_IK]
232-
do irand = 1, max(0_IK, nrand_loc) + 1_IK
233-
! Initialize the random seed using N, IRAND, RP, and RANDSEED_LOC. Do not include IK so
234-
! that the results for different IK are the same.
235-
rseed = int(sum(istr(solname)) + sum(istr(probname)) + n + irand + RP + randseed_loc)
236-
call setseed(rseed)
237-
if (irand <= 1) then
238-
npt = npt_list(ceiling(real(nnpt, RP)*rand())) ! Randomly select NPT from NPT_LIST
239-
else
240-
npt = int(TEN * rand() * real(n, RP), kind(npt))
241-
end if
242-
iprint = int(randn(), kind(iprint))
243-
maxfun = int(1.0E2_RP * rand() * real(n, RP), kind(maxfun))
244-
maxhist = int(TWO * rand() * real(max(10_IK * n, maxfun), RP), kind(maxhist))
245-
if (rand() <= 0.1) then
246-
maxhist = -maxhist
247-
end if
248-
if (rand() <= 0.8) then
249-
ftarget = -TEN**abs(real(min(range(ftarget), 12), RP) * rand())
250-
elseif (rand() <= 0.5) then ! Note that the value of rand() changes.
251-
ftarget = REALMAX
252-
else
253-
ftarget = -REALMAX
254-
end if
255-
256-
rhobeg = noisy(prob % Delta0)
257-
rhoend = max(1.0E-5_RP, rhobeg * 10.0_RP**(6.0_RP * rand() - 5.0_RP))
258-
if (rand() <= 0.1) then
259-
rhoend = rhobeg
260-
elseif (rand() <= 0.1) then ! Note that the value of rand() changes.
261-
rhobeg = ZERO
262-
end if
263-
call safealloc(x0, n) ! Not all compilers support automatic allocation yet, e.g., Absoft.
264-
x0 = noisy(prob % x0)
265-
orig_calfun => prob % calfun
266-
267-
print '(/A, I0, A, I0, A, I0)', strip(probname)//': N = ', n, ' NPT = ', npt, ', Random test ', irand
268-
269-
call safealloc(x, n)
270-
x = x0
271-
call bobyqa(noisy_calfun, x, f, xl=prob % xl, xu=prob % xu, npt=npt, &
272-
& rhobeg=rhobeg, rhoend=rhoend, maxfun=maxfun, maxhist=maxhist, fhist=fhist, &
273-
& xhist=xhist, ftarget=ftarget, iprint=iprint)
274-
275-
if (prob % probtype == 'u') then ! Run the test without constraints
276-
call safealloc(x_unc, n)
277-
x_unc = x0
278-
call bobyqa(noisy_calfun, x_unc, f_unc, npt=npt, rhobeg=rhobeg, rhoend=rhoend, maxfun=maxfun, &
279-
& maxhist=maxhist, fhist=fhist, xhist=xhist, ftarget=ftarget, iprint=iprint)
280-
call validate(all(abs(x - x_unc) <= 0), 'X == X_UNC', srname)
281-
call validate(abs(f - f_unc) <= 0 .or. (is_neginf(f) .and. is_neginf(f_unc)), 'F == F_UNC', srname)
282-
end if
283-
284-
deallocate (x)
285-
nullify (orig_calfun)
286-
end do
287-
288-
! DESTRUCT deallocates allocated arrays/pointers and nullify the pointers. Must be called.
289-
call destruct(prob) ! Destruct the testing problem.
229+
230+
if (irand <= 1) then
231+
npt = npt_list(ceiling(real(nnpt, RP)*rand())) ! Randomly select NPT from NPT_LIST
232+
else
233+
npt = int(TEN * rand() * real(n, RP), kind(npt))
234+
end if
235+
236+
iprint = int(randn(), kind(iprint))
237+
maxfun = int(1.0E2_RP * rand() * real(n, RP), kind(maxfun))
238+
maxhist = int(TWO * rand() * real(max(10_IK * n, maxfun), RP), kind(maxhist))
239+
if (rand() <= 0.1) then
240+
maxhist = -maxhist
241+
end if
242+
if (rand() <= 0.8) then
243+
ftarget = -TEN**abs(real(min(range(ftarget), 12), RP) * rand())
244+
elseif (rand() <= 0.5) then ! Note that the value of rand() changes.
245+
ftarget = REALMAX
246+
else
247+
ftarget = -REALMAX
248+
end if
249+
250+
rhobeg = noisy(prob % Delta0)
251+
rhoend = max(1.0E-5_RP, rhobeg * 10.0_RP**(6.0_RP * rand() - 5.0_RP))
252+
if (rand() <= 0.1) then
253+
rhoend = rhobeg
254+
elseif (rand() <= 0.1) then ! Note that the value of rand() changes.
255+
rhobeg = ZERO
256+
end if
257+
258+
call safealloc(x0, n) ! Not all compilers support automatic allocation yet, e.g., Absoft.
259+
x0 = noisy(prob % x0)
260+
orig_calfun => prob % calfun
261+
262+
print '(/A, I0, A, I0, A, I0)', strip(probname)//': N = ', n, ' NPT = ', npt, ', Random test ', irand
263+
264+
call safealloc(x, n)
265+
x = x0
266+
call bobyqa(noisy_calfun, x, f, xl=prob % xl, xu=prob % xu, npt=npt, &
267+
& rhobeg=rhobeg, rhoend=rhoend, maxfun=maxfun, maxhist=maxhist, fhist=fhist, &
268+
& xhist=xhist, ftarget=ftarget, iprint=iprint)
269+
270+
if (prob % probtype == 'u') then ! Run the test without constraints
271+
call safealloc(x_unc, n)
272+
x_unc = x0
273+
call bobyqa(noisy_calfun, x_unc, f_unc, npt=npt, rhobeg=rhobeg, rhoend=rhoend, maxfun=maxfun, &
274+
& maxhist=maxhist, fhist=fhist, xhist=xhist, ftarget=ftarget, iprint=iprint)
275+
call validate(all(abs(x - x_unc) <= 0), 'X == X_UNC', srname)
276+
call validate(abs(f - f_unc) <= 0 .or. (is_neginf(f) .and. is_neginf(f_unc)), 'F == F_UNC', srname)
277+
end if
278+
279+
deallocate (x)
280+
nullify (orig_calfun)
290281
end do
282+
283+
! DESTRUCT deallocates allocated arrays/pointers and nullify the pointers. Must be called.
284+
call destruct(prob) ! Destruct the testing problem.
291285
end do
292286
end if
293287

0 commit comments

Comments
 (0)