Skip to content

Commit

Permalink
240421.150115.HKT fix a failure of ftest: "Assertion fails: KNEW >= 1…
Browse files Browse the repository at this point in the history
… unless XIMPROVED = FALSE"
  • Loading branch information
zaikunzhang committed Apr 21, 2024
1 parent 6dad03d commit 7bbde9e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 41 deletions.
19 changes: 10 additions & 9 deletions fortran/bobyqa/geometry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module geometry_bobyqa_mod
!
! Started: February 2022
!
! Last Modified: Wednesday, April 03, 2024 AM12:49:42
! Last Modified: Sunday, April 21, 2024 PM01:57:17
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -139,22 +139,23 @@ function setdrop_tr(kopt, ximproved, bmat, d, delta, rho, xpt, zmat) result(knew
score(kopt) = -ONE
end if

knew = 0
! The following IF works slightly better than `IF (ANY(SCORE > 0))` from Powell's BOBYQA and LINCOA
! code.
if (any(score > 1) .or. (ximproved .and. any(score > 0))) then ! Powell's UOBYQA and NEWUOA code.
! See (6.1) of the BOBYQA paper for the definition of KNEW in this case.
! SCORE(K) = NaN implies DEN(K) = NaN. We exclude such K as we want DEN to be big.
knew = int(maxloc(score, mask=(.not. is_nan(score)), dim=1), kind(knew))
!!MATLAB: [~, knew] = max(score, [], 'omitnan');
elseif (ximproved) then
! Powell's code does not include the following instructions. With Powell's code, if DEN consists
! of only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the
! following value, to make sure that the new trial point is included in the interpolation set.
! However, the updating subroutine will likely need to skip the update of the Lagrange
! polynomials (i.e., H), or they would be destroyed by the NaNs.
end if

! Powell's code does not include the following instructions. With Powell's code, if DEN consists of
! only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the following value,
! to make sure that the new trial point is included in the interpolation set. However, the updating
! subroutine will likely need to skip the update of the Lagrange polynomials (i.e., H), or they
! would be destroyed by the NaNs.
if ((ximproved .and. knew == 0) .or. knew < 0) then ! KNEW < 0 is impossible in theory.
knew = int(maxloc(distsq, dim=1), kind(knew))
else
knew = 0
end if

!====================!
Expand Down
11 changes: 6 additions & 5 deletions fortran/cobyla/geometry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module geometry_cobyla_mod
!
! Started: July 2021
!
! Last Modified: Sunday, March 24, 2024 PM05:13:08
! Last Modified: Sunday, April 21, 2024 PM01:57:52
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -168,15 +168,16 @@ function setdrop_tr(ximproved, d, delta, rho, sim, simi) result(jdrop)
score(n + 1) = -ONE
end if

jdrop = 0
! The following IF works a bit better than `IF (ANY(SCORE > 1) .OR. ANY(SCORE > 0) .AND. XIMPROVED)`
! from Powell's UOBYQA and NEWUOA code.
if (any(score > 0)) then ! Powell's BOBYQA and LINCOA code
jdrop = int(maxloc(score, mask=(.not. is_nan(score)), dim=1), kind(jdrop))
!!MATLAB: [~, jdrop] = max(score, [], 'omitnan');
elseif (ximproved) then
end if

if ((ximproved .and. jdrop == 0) .or. jdrop < 0) then ! JDROP < 0 is impossible in theory.
jdrop = int(maxloc(distsq, dim=1), kind(jdrop))
else
jdrop = 0 ! We arrive here when XIMPROVED = FALSE and no entry of SCORE is positive.
end if

!====================!
Expand All @@ -186,7 +187,7 @@ function setdrop_tr(ximproved, d, delta, rho, sim, simi) result(jdrop)
! Postconditions
if (DEBUGGING) then
call assert(jdrop >= 0 .and. jdrop <= n + 1, '0 <= JDROP <= N+1', srname)
call assert(jdrop <= n .or. ximproved, 'JDROP >= 1 unless IMPROVEX = TRUE', srname)
call assert(jdrop <= n .or. ximproved, 'JDROP <= n unless IMPROVEX = TRUE', srname)
call assert(jdrop >= 1 .or. .not. ximproved, 'JDROP >= 1 unless IMPROVEX = FALSE', srname)
! JDROP >= 1 when XIMPROVED = TRUE unless NaN occurs in DISTSQ, which should not happen if the
! starting point does not contain NaN and the trust-region/geometry steps never contain NaN.
Expand Down
19 changes: 10 additions & 9 deletions fortran/lincoa/geometry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module geometry_lincoa_mod
!
! Started: February 2022
!
! Last Modified: Wednesday, April 03, 2024 AM12:54:36
! Last Modified: Sunday, April 21, 2024 PM01:54:26
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -160,21 +160,22 @@ function setdrop_tr(idz, kopt, ximproved, bmat, d, delta, rho, xpt, zmat) result
score(kopt) = -ONE
end if

knew = 0
! The following IF works a bit better than `IF (ANY(SCORE > 1) .OR. ANY(SCORE > 0) .AND. XIMPROVED)`
! from Powell's UOBYQA and NEWUOA code.
if (any(score > 0)) then ! Powell's BOBYQA and LINCOA code
! SCORE(K) is NaN implies ABS(DEN(K)) is NaN, but we want ABS(DEN) to be big. So we exclude such K.
knew = int(maxloc(score, mask=(.not. is_nan(score)), dim=1), kind(knew))
!!MATLAB: [~, knew] = max(score, [], 'omitnan');
else if (ximproved) then
! Powell's code does not include the following instructions. With Powell's code, if DEN consists
! of only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the
! following value, to make sure that the new trial point is included in the interpolation set.
! However, the updating subroutine will likely need to skip the update of the Lagrange
! polynomials (i.e., H), or they would be destroyed by the NaNs.
end if

! Powell's code does not include the following instructions. With Powell's code, if DEN consists of
! only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the following value,
! to make sure that the new trial point is included in the interpolation set. However, the updating
! subroutine will likely need to skip the update of the Lagrange polynomials (i.e., H), or they
! would be destroyed by the NaNs.
if ((ximproved .and. knew == 0) .or. knew < 0) then ! KNEW < 0 is impossible in theory.
knew = int(maxloc(distsq, dim=1), kind(knew))
else
knew = 0
end if

!====================!
Expand Down
19 changes: 10 additions & 9 deletions fortran/newuoa/geometry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module geometry_newuoa_mod
!
! Started: July 2020
!
! Last Modified: Wednesday, April 03, 2024 AM12:55:24
! Last Modified: Sunday, April 21, 2024 PM02:47:22
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -128,21 +128,22 @@ function setdrop_tr(idz, kopt, ximproved, bmat, d, delta, rho, xpt, zmat) result
score(kopt) = -ONE
end if

knew = 0
! The following IF works a bit better than `IF (ANY(SCORE > 0))` from Powell's BOBYQA and LINCOA code.
if (any(score > 1) .or. (ximproved .and. any(score > 0))) then ! Powell's UOBYQA and NEWUOA code
! See (7.5) of the NEWUOA paper for the definition of KNEW in this case.
! SCORE(K) is NaN implies ABS(DEN(K)) is NaN, but we want ABS(DEN) to be big. So we exclude such K.
knew = int(maxloc(score, mask=(.not. is_nan(score)), dim=1), kind(knew))
!!MATLAB: [~, knew] = max(score, [], 'omitnan');
elseif (ximproved) then
! Powell's code does not include the following instructions. With Powell's code, if DEN consists
! of only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the
! following value, to make sure that the new trial point is included in the interpolation set.
! However, the updating subroutine will likely need to skip the update of the Lagrange
! polynomials (i.e., H), or they would be destroyed by the NaNs.
end if

! Powell's code does not include the following instructions. With Powell's code, if DEN consists of
! only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the following value,
! to make sure that the new trial point is included in the interpolation set. However, the updating
! subroutine will likely need to skip the update of the Lagrange polynomials (i.e., H), or they
! would be destroyed by the NaNs.
if ((ximproved .and. knew == 0) .or. knew < 0) then ! KNEW < 0 is impossible in theory.
knew = int(maxloc(distsq, dim=1), kind(knew))
else
knew = 0
end if

!====================!
Expand Down
1 change: 1 addition & 0 deletions fortran/tests/makefiles/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ $(TESTS_C) $(TESTS_INT_C):
fi ; \
fi) 2>&1 \
| grep -v "^[[:blank:]]*$$" \
| grep -v "Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking" \
| grep -v "ld: warning: no platform load command found in.*assuming: macOS" \
| grep -v "/usr/bin/ld: warning: libpgmath.so.0d, needed by /home/zaikunzhang/local/flang/lib/libflang.so, not found" \
| grep -v "Warning (165): Implicit interface 'calfun' called at (1)" \
Expand Down
19 changes: 10 additions & 9 deletions fortran/uobyqa/geometry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module geometry_uobyqa_mod
!
! Started: February 2022
!
! Last Modified: Wednesday, April 03, 2024 AM12:54:55
! Last Modified: Sunday, April 21, 2024 PM01:52:04
!--------------------------------------------------------------------------------------------------!

implicit none
Expand Down Expand Up @@ -132,21 +132,22 @@ function setdrop_tr(kopt, ximproved, d, pl, rho, xpt) result(knew)
score(kopt) = -ONE
end if

knew = 0
! It makes almost no difference if we change the IF below to `IF (ANY(SCORE>0))`, which is used
! in Powell's BOBYQA and LINCOA code.
if (any(score > 1) .or. (ximproved .and. any(score > 0))) then ! Powell's UOBYQA and NEWUOA code
! SCORE(K) is NaN implies VLAG(K) is NaN, but we want ABS(VLAG) to be big. So we exclude such K.
knew = int(maxloc(score, mask=(.not. is_nan(score)), dim=1), kind(knew))
!!MATLAB: [~, knew] = max(score, [], 'omitnan');
elseif (ximproved) then
! Powell's code does not include the following instructions. With Powell's code, if VLAG
! consists of only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the
! following value, to make sure that the new trial point is included in the interpolation set.
! However, the updating subroutine will likely need to skip the update of the Lagrange
! polynomial, or they would be destroyed by the NaNs.
end if

! Powell's code does not include the following instructions. With Powell's code, if VLAG consists of
! only NaN, then KNEW can be 0 even when XIMPROVED is TRUE. Here, we set KNEW to the following value,
! to make sure that the new trial point is included in the interpolation set. However, the updating
! subroutine will likely need to skip the update of the Lagrange polynomial, or they would be
! destroyed by the NaNs.
if ((ximproved .and. knew == 0) .or. knew < 0) then ! KNEW < 0 is impossible in theory.
knew = int(maxloc(distsq, dim=1), kind(knew))
else
knew = 0
end if

!====================!
Expand Down
1 change: 1 addition & 0 deletions matlab/mex_gateways/tests/makefiles/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ $(TESTS) $(TESTS_INT):
$(eval EXTRA_FLAGS := -DPRIMA_DEBUGGING=$(DBG) -DPRIMA_INTEGER_KIND=$(IK) -DPRIMA_REAL_PRECISION=$(RP) -DPRIMA_QP_AVAILABLE=$(QPAVLB))
@($(FC1) $(EXTRA_FLAGS) -c $(SRC) && $(FC2) $(EXTRA_FLAGS) -c $(SRC)) 2>&1 \
| grep -v "^[[:blank:]]*$$" \
| grep -v "Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking" \
| grep -v "Absoft ANSI 1610:" \
| grep -v "Absoft Pro Fortran.*: 0 Errors, 0 Warnings, 0 Other messages," \
| grep -v "after the END INTERFACE keywords is only legal in Fortran 95 and beyond" \
Expand Down

0 comments on commit 7bbde9e

Please sign in to comment.