redrho.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. '''
  2. This module provides a function that calculates RHO when it needs to be reduced.
  3. Translated from Zaikun Zhang's modern-Fortran reference implementation in PRIMA.
  4. Dedicated to late Professor M. J. D. Powell FRS (1936--2015).
  5. Python translation by Nickolai Belakovski.
  6. '''
  7. from .consts import DEBUGGING
  8. import numpy as np
  9. def redrho(rho_in, rhoend):
  10. '''
  11. This function calculates RHO when it needs to be reduced.
  12. The scheme is shared by UOBYQA, NEWUOA, BOBYQA, LINCOA. For COBYLA, Powell's code reduces RHO by
  13. 'RHO *= 0.5; if RHO <= 1.5 * RHOEND: RHO = RHOEND' as specified in (11) of the COBYLA
  14. paper. However, this scheme seems to work better, especially after we introduce DELTA.
  15. '''
  16. # Preconditions
  17. if DEBUGGING:
  18. assert rho_in > rhoend > 0
  19. #====================#
  20. # Calculation starts #
  21. #====================#
  22. rho_ratio = rho_in / rhoend
  23. if rho_ratio > 250:
  24. rho = 0.1 * rho_in
  25. elif rho_ratio <= 16:
  26. rho = rhoend
  27. else:
  28. rho = np.sqrt(rho_ratio) * rhoend # rho = np.sqrt(rho * rhoend)
  29. #==================#
  30. # Calculation ends #
  31. #==================#
  32. # Postconditions
  33. if DEBUGGING:
  34. assert rho_in > rho >= rhoend
  35. return rho