Skip to content

Commit 2a36760

Browse files
committed
250916.124050.CST [skip ci] moderate the linear constraint values in cobyla for consitency
1 parent bd3ec2b commit 2a36760

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

fortran/cobyla/cobyla.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module cobyla_mod
4242
!
4343
! Started: July 2021
4444
!
45-
! Last Modified: Sunday, April 07, 2024 PM04:12:44
45+
! Last Modified: Tue 16 Sep 2025 12:35:02 PM CST
4646
!--------------------------------------------------------------------------------------------------!
4747

4848
implicit none
@@ -499,13 +499,15 @@ subroutine cobyla(calcfc, m_nlcon, x, &
499499
! If NLCONSTR0 is present, then F0 must be present, and we assume that F(X0) = F0 even if F0 is NaN.
500500
! If NLCONSTR0 is absent, then F0 must be either absent or NaN, both of which will be interpreted as
501501
! F(X0) is not provided and we have to evaluate F(X0) and NLCONSTR(X0) now.
502-
constr_loc(1:m - m_nlcon) = moderatec(matprod(x, amat) - bvec)
502+
constr_loc(1:m - m_nlcon) = moderatec(matprod(x, amat) - bvec) ! Linear and bound constraints
503+
! Note that EVALUATE moderates the nonlinear constraint values. Thus we also moderate the
504+
! bound/linear constraint values here to make CSTRV consistent.
503505
if (present(f0) .and. present(nlconstr0) .and. all(is_finite(x))) then
504506
f_loc = moderatef(f0)
505507
constr_loc(m - m_nlcon + 1:m) = moderatec(nlconstr0)
506508
else
507509
x = moderatex(x)
508-
call evaluate(calcfc, x, f_loc, constr_loc(m - m_nlcon + 1:m))
510+
call evaluate(calcfc, x, f_loc, constr_loc(m - m_nlcon + 1:m)) ! Nonlinear constraints
509511
! N.B.: Do NOT call FMSG, SAVEHIST, or SAVEFILT for the function/constraint evaluation at X0.
510512
! They will be called during the initialization, which will read the function/constraint at X0.
511513
end if

fortran/cobyla/cobylb.f90

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module cobylb_mod
1717
!
1818
! Started: July 2021
1919
!
20-
! Last Modified: Tue 12 Aug 2025 04:58:06 PM CST
20+
! Last Modified: Tue 16 Sep 2025 12:36:40 PM CST
2121
!--------------------------------------------------------------------------------------------------!
2222

2323
implicit none
@@ -42,7 +42,7 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
4242
use, non_intrinsic :: checkexit_mod, only : checkexit
4343
use, non_intrinsic :: consts_mod, only : RP, IK, ZERO, ONE, HALF, TENTH, EPS, REALMAX, DEBUGGING, MIN_MAXFILT
4444
use, non_intrinsic :: debug_mod, only : assert
45-
use, non_intrinsic :: evaluate_mod, only : evaluate
45+
use, non_intrinsic :: evaluate_mod, only : evaluate, moderatec
4646
use, non_intrinsic :: history_mod, only : savehist, rangehist
4747
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf, is_finite
4848
use, non_intrinsic :: infos_mod, only : INFO_DFT, MAXTR_REACHED, SMALL_TR_RADIUS, DAMAGING_ROUNDING, CALLBACK_TERMINATE
@@ -393,8 +393,10 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
393393
cstrv = cval(j)
394394
else
395395
! Evaluate the objective and constraints at X, taking care of possible Inf/NaN values.
396-
constr(1:m_lcon) = matprod(x, amat) - bvec
397-
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
396+
constr(1:m_lcon) = moderatec(matprod(x, amat) - bvec) ! Linear constraints
397+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m)) ! Nonlinear constraints
398+
! Note that EVALUATE moderates the nonlinear constraint values. Thus we also moderate the
399+
! linear constraint values here to make CSTRV consistent.
398400
cstrv = maximum([ZERO, constr])
399401
nf = nf + 1_IK
400402
! Save X, F, CONSTR, CSTRV into the history.
@@ -586,8 +588,10 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
586588
cstrv = cval(j)
587589
else
588590
! Evaluate the objective and constraints at X, taking care of possible Inf/NaN values.
589-
constr(1:m_lcon) = matprod(x, amat) - bvec
590-
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
591+
constr(1:m_lcon) = moderatec(matprod(x, amat) - bvec) ! Linear constraints
592+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m)) ! Nonlinear constraints
593+
! Note that EVALUATE moderates the nonlinear constraint values. Thus we also moderate the
594+
! linear constraint values here to make CSTRV consistent.
591595
cstrv = maximum([ZERO, constr])
592596
nf = nf + 1_IK
593597
! Save X, F, CONSTR, CSTRV into the history.
@@ -652,8 +656,10 @@ subroutine cobylb(calcfc, iprint, maxfilt, maxfun, amat, bvec, ctol, cweight, et
652656
! Ensure that D has not been updated after SHORTD == TRUE occurred, or the code below is incorrect.
653657
x = sim(:, n + 1) + d
654658
if (info == SMALL_TR_RADIUS .and. shortd .and. norm(x - sim(:, n + 1)) > 1.0E-3_RP * rhoend .and. nf < maxfun) then
655-
constr(1:m_lcon) = matprod(x, amat) - bvec
656-
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
659+
constr(1:m_lcon) = moderatec(matprod(x, amat) - bvec) ! Linear constraints
660+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m)) ! Nonlinear constraints
661+
! Note that EVALUATE moderates the nonlinear constraint values. Thus we also moderate the linear
662+
! constraint values here to make CSTRV consistent.
657663
cstrv = maximum([ZERO, constr])
658664
nf = nf + 1_IK
659665
! Save X, F, CONSTR, CSTRV into the history.

fortran/cobyla/initialize.f90

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module initialize_cobyla_mod
88
!
99
! Started: July 2021
1010
!
11-
! Last Modified: Tue 12 Aug 2025 04:57:48 PM CST
11+
! Last Modified: Tue 16 Sep 2025 12:35:23 PM CST
1212
!--------------------------------------------------------------------------------------------------!
1313

1414
implicit none
@@ -29,7 +29,7 @@ subroutine initxfc(calcfc, iprint, maxfun, amat, bvec, constr0, ctol, f0, ftarge
2929
use, non_intrinsic :: checkexit_mod, only : checkexit
3030
use, non_intrinsic :: consts_mod, only : RP, IK, ZERO, TENTH, REALMAX, DEBUGGING
3131
use, non_intrinsic :: debug_mod, only : assert
32-
use, non_intrinsic :: evaluate_mod, only : evaluate
32+
use, non_intrinsic :: evaluate_mod, only : evaluate, moderatec
3333
use, non_intrinsic :: history_mod, only : savehist
3434
use, non_intrinsic :: infnan_mod, only : is_nan, is_posinf, is_finite
3535
use, non_intrinsic :: infos_mod, only : INFO_DFT
@@ -161,8 +161,10 @@ subroutine initxfc(calcfc, iprint, maxfun, amat, bvec, constr0, ctol, f0, ftarge
161161
else
162162
j = k - 1_IK
163163
x(j) = x(j) + rhobeg
164-
constr(1:m_lcon) = matprod(x, amat) - bvec
165-
call evaluate(calcfc, x, f, constr(m_lcon + 1:m))
164+
constr(1:m_lcon) = moderatec(matprod(x, amat) - bvec) ! Linear constraints.
165+
call evaluate(calcfc, x, f, constr(m_lcon + 1:m)) ! Nonlinear constraints.
166+
! Note that EVALUATE moderates the nonlinear constraint values. Thus we also moderate the
167+
! linear constraint values here to make CSTRV consistent.
166168
end if
167169
cstrv = maximum([ZERO, constr])
168170

0 commit comments

Comments
 (0)