preproc.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. '''
  2. This is a module that preprocesses the inputs.
  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, EPS, IPRINT_DEFAULT, FTARGET_DEFAULT, \
  8. MIN_MAXFILT, MAXFILT_DEFAULT, MAXHISTMEM, ETA1_DEFAULT, ETA2_DEFAULT, \
  9. GAMMA1_DEFAULT, GAMMA2_DEFAULT, RHOBEG_DEFAULT, RHOEND_DEFAULT, \
  10. CTOL_DEFAULT, CWEIGHT_DEFAULT
  11. from .present import present
  12. from warnings import warn
  13. import numpy as np
  14. def preproc(solver, num_vars, iprint, maxfun, maxhist, ftarget, rhobeg, rhoend,
  15. num_constraints=None, npt=None, maxfilt=None, ctol=None, cweight=None,
  16. eta1=None, eta2=None, gamma1=None, gamma2=None, is_constrained=None, has_rhobeg=None,
  17. honour_x0=None, xl=None, xu=None, x0=None):
  18. '''
  19. This subroutine preprocesses the inputs. It does nothing to the inputs that are valid.
  20. '''
  21. # Preconditions
  22. if DEBUGGING:
  23. assert num_vars >= 1
  24. if present(num_constraints):
  25. assert num_constraints >= 0
  26. assert num_constraints == 0 or solver.lower() == 'cobyla'
  27. if solver.lower() == 'cobyla' and present(num_constraints) and present(is_constrained):
  28. assert num_constraints == 0 or is_constrained
  29. if solver.lower() == 'bobyqa':
  30. assert present(xl) and present(xu)
  31. assert all(xu - xl >= 2 * EPS)
  32. if present(honour_x0):
  33. assert solver.lower() == 'bobyqa' and present(has_rhobeg) and present(xl) and present(xu) and present(x0)
  34. #====================#
  35. # Calculation starts #
  36. #====================#
  37. # Read num_constraints, if necessary
  38. num_constraints = num_constraints if (present(num_constraints) and solver.lower() == 'cobyla') else 0
  39. # Decide whether the problem is truly constrained
  40. is_constrained = is_constrained if (present(is_constrained)) else num_constraints > 0
  41. # Validate IPRINT
  42. if np.abs(iprint) > 3:
  43. iprint = IPRINT_DEFAULT
  44. warn(f'{solver}: Invalid IPRINT; it should be 0, 1, -1, 2, -2, 3, or -3; it is set to {iprint}')
  45. # Validate MAXFUN
  46. if solver.lower() == 'uobyqa':
  47. min_maxfun = (num_vars + 1) * (num_vars + 2) / 2 + 1
  48. min_maxfun_str = '(N+1)(N+2)/2 + 1'
  49. elif solver.lower() == 'cobyla':
  50. min_maxfun = num_vars + 2
  51. min_maxfun_str = 'num_vars + 2'
  52. else: # CASE ('NEWUOA', 'BOBYQA', 'LINCOA')
  53. min_maxfun = num_vars + 3
  54. min_maxfun_str = 'num_vars + 3'
  55. if maxfun < min_maxfun:
  56. maxfun = min_maxfun
  57. warn(f'{solver}: Invalid MAXFUN; it should be at least {min_maxfun_str}; it is set to {maxfun}')
  58. # Validate MAXHIST
  59. if maxhist < 0:
  60. maxhist = maxfun
  61. warn(f'{solver}: Invalid MAXHIST; it should be a nonnegative integer; it is set to {maxhist}')
  62. maxhist = min(maxhist, maxfun) # MAXHIST > MAXFUN is never needed.
  63. # Validate FTARGET
  64. if np.isnan(ftarget):
  65. ftarget = FTARGET_DEFAULT
  66. warn(f'{solver}: Invalid FTARGET; it should be a real number; it is set to {ftarget}')
  67. # Validate NPT
  68. if (solver.lower() == 'newuoa' or solver.lower() == 'bobyqa' or solver.lower() == 'lincoa') and present(npt):
  69. if (npt < num_vars + 2 or npt > min(maxfun - 1, ((num_vars + 2) * (num_vars + 1)) / 2)):
  70. npt = int(min(maxfun - 1, 2 * num_vars + 1))
  71. warn(f'{solver}: Invalid NPT; it should be an integer in the interval [N+2, (N+1)(N+2)/2] and less than MAXFUN; it is set to {npt}')
  72. # Validate MAXFILT
  73. if present(maxfilt) and (solver.lower() == 'lincoa' or solver.lower() == 'cobyla'):
  74. maxfilt_in = maxfilt
  75. if maxfilt < 1:
  76. maxfilt = MAXFILT_DEFAULT # The inputted MAXFILT is obviously wrong.
  77. else:
  78. maxfilt = max(MIN_MAXFILT, maxfilt) # The inputted MAXFILT is too small.
  79. # Further revise MAXFILT according to MAXHISTMEM.
  80. if solver.lower() == 'lincoa':
  81. unit_memo = (num_vars + 2) * np.dtype(float).itemsize
  82. elif solver.lower() == 'cobyla':
  83. unit_memo = (num_constraints + num_vars + 2) * np.dtype(float).itemsize
  84. else:
  85. unit_memo = 1
  86. # We cannot simply set MAXFILT = MIN(MAXFILT, MAXHISTMEM/...), as they may not have
  87. # the same kind, and compilers may complain. We may convert them, but overflow may occur.
  88. if maxfilt > MAXHISTMEM / unit_memo:
  89. maxfilt = int(MAXHISTMEM / unit_memo) # Integer division.
  90. maxfilt = min(maxfun, max(MIN_MAXFILT, maxfilt))
  91. if is_constrained:
  92. if maxfilt_in < 1:
  93. warn(f'{solver}: Invalid MAXFILT; it should be a positive integer; it is set to {maxfilt}')
  94. elif maxfilt_in < min(maxfun, MIN_MAXFILT):
  95. warn(f'{solver}: MAXFILT is too small; it is set to {maxfilt}')
  96. elif maxfilt < min(maxfilt_in, maxfun):
  97. warn(f'{solver}: MAXFILT is set to {maxfilt} due to memory limit')
  98. # Validate ETA1 and ETA2
  99. eta1_local = eta1 if present(eta1) else ETA1_DEFAULT
  100. eta2_local = eta2 if present(eta2) else ETA2_DEFAULT
  101. # When the difference between ETA1 and ETA2 is tiny, we force them to equal.
  102. # See the explanation around RHOBEG and RHOEND for the reason.
  103. if present(eta1) and present(eta2):
  104. if np.abs(eta1 - eta2) < 1.0E2 * EPS * max(np.abs(eta1), 1):
  105. eta2 = eta1
  106. if present(eta1):
  107. if np.isnan(eta1):
  108. # In this case, we take the value hard coded in Powell's original code
  109. # without any warning. It is useful when interfacing with MATLAB/Python.
  110. eta1 = ETA1_DEFAULT
  111. elif eta1 < 0 or eta1 >= 1:
  112. # Take ETA2 into account if it has a valid value.
  113. if present(eta2) and eta2_local > 0 and eta2_local <= 1:
  114. eta1 = max(EPS, eta2 / 7.0)
  115. else:
  116. eta1 = ETA1_DEFAULT
  117. warn(f'{solver}: Invalid ETA1; it should be in the interval [0, 1) and not more than ETA2; it is set to {eta1}')
  118. if present(eta2):
  119. if np.isnan(eta2):
  120. # In this case, we take the value hard coded in Powell's original code
  121. # without any warning. It is useful when interfacing with MATLAB/Python.
  122. eta2 = ETA2_DEFAULT
  123. elif present(eta1) and (eta2 < eta1_local or eta2 > 1):
  124. eta2 = (eta1 + 2) / 3.0
  125. warn(f'{solver}: Invalid ETA2; it should be in the interval [0, 1) and not less than ETA1; it is set to {eta2}')
  126. # Validate GAMMA1 and GAMMA2
  127. if present(gamma1):
  128. if np.isnan(gamma1):
  129. # In this case, we take the value hard coded in Powell's original code
  130. # without any warning. It is useful when interfacing with MATLAB/Python.
  131. gamma1 = GAMMA1_DEFAULT
  132. elif gamma1 <= 0 or gamma1 >= 1:
  133. gamma1 = GAMMA1_DEFAULT
  134. warn(f'{solver}: Invalid GAMMA1; it should in the interval (0, 1); it is set to {gamma1}')
  135. if present(gamma2):
  136. if np.isnan(gamma2):
  137. # In this case, we take the value hard coded in Powell's original code
  138. # without any warning. It is useful when interfacing with MATLAB/Python.
  139. gamma2 = GAMMA2_DEFAULT
  140. elif gamma2 < 1 or np.isinf(gamma2):
  141. gamma2 = GAMMA2_DEFAULT
  142. warn(f'{solver}: Invalid GAMMA2; it should be a real number not less than 1; it is set to {gamma2}')
  143. # Validate RHOBEG and RHOEND
  144. if np.abs(rhobeg - rhoend) < 1.0e2 * EPS * np.maximum(np.abs(rhobeg), 1):
  145. # When the data is passed from the interfaces (e.g., MEX) to the Fortran code, RHOBEG, and RHOEND
  146. # may change a bit. It was observed in a MATLAB test that MEX passed 1 to Fortran as
  147. # 0.99999999999999978. Therefore, if we set RHOEND = RHOBEG in the interfaces, then it may happen
  148. # that RHOEND > RHOBEG, which is considered as an invalid input. To avoid this situation, we
  149. # force RHOBEG and RHOEND to equal when the difference is tiny.
  150. rhoend = rhobeg
  151. # Revise the default values for RHOBEG/RHOEND according to the solver.
  152. if solver.lower() == 'bobyqa':
  153. rhobeg_default = np.maximum(EPS, np.min(RHOBEG_DEFAULT, np.min(xu - xl) / 4.0))
  154. rhoend_default = np.maximum(EPS, np.min(0.1 * rhobeg_default, RHOEND_DEFAULT))
  155. else:
  156. rhobeg_default = RHOBEG_DEFAULT
  157. rhoend_default = RHOEND_DEFAULT
  158. if solver.lower() == 'bobyqa':
  159. # Do NOT merge the IF below into the ELIF above! Otherwise, XU and XL may be accessed even if
  160. # the solver is not BOBYQA, because the logical evaluation is not short-circuit.
  161. if rhobeg > np.min(xu - xl) / 2:
  162. # Do NOT make this revision if RHOBEG not positive or not finite, because otherwise RHOBEG
  163. # will get a huge value when XU or XL contains huge values that indicate unbounded variables.
  164. rhobeg = np.min(xu - xl) / 4.0 # Here, we do not take RHOBEG_DEFAULT.
  165. warn(f'{solver}: Invalid RHOBEG; {solver} requires 0 < RHOBEG <= np.min(XU-XL)/2; it is set to np.min(XU-XL)/4')
  166. if rhobeg <= 0 or np.isnan(rhobeg) or np.isinf(rhobeg):
  167. # Take RHOEND into account if it has a valid value. We do not do this if the solver is BOBYQA,
  168. # which requires that RHOBEG <= (XU-XL)/2.
  169. if np.isfinite(rhoend) and rhoend > 0 and solver.lower() != 'bobyqa':
  170. rhobeg = max(10 * rhoend, rhobeg_default)
  171. else:
  172. rhobeg = rhobeg_default
  173. warn(f'{solver}: Invalid RHOBEG; it should be a positive number; it is set to {rhobeg}')
  174. if rhoend <= 0 or rhobeg < rhoend or np.isnan(rhoend) or np.isinf(rhoend):
  175. rhoend = max(EPS, min(0.1 * rhobeg, rhoend_default))
  176. warn(f'{solver}: Invalid RHOEND; it should be a positive number and RHOEND <= RHOBEG; it is set to {rhoend}')
  177. # For BOBYQA, revise X0 or RHOBEG so that the distance between X0 and the inactive bounds is at
  178. # least RHOBEG. If HONOUR_X0 == TRUE, revise RHOBEG if needed; otherwise, revise HONOUR_X0 if needed.
  179. if present(honour_x0):
  180. if honour_x0:
  181. rhobeg_old = rhobeg;
  182. lbx = np.isfinite(xl) & (x0 - xl <= EPS * np.maximum(1, np.abs(xl))) # X0 essentially equals XL
  183. ubx = np.isfinite(xu) & (x0 - xu >= -EPS * np.maximum(1, np.abs(xu))) # X0 essentially equals XU
  184. x0[lbx] = xl[lbx]
  185. x0[ubx] = xu[ubx]
  186. rhobeg = max(EPS, np.min([rhobeg, x0[~lbx] - xl[~lbx], xu[~ubx] - x0[~ubx]]))
  187. if rhobeg_old - rhobeg > EPS * max(1, rhobeg_old):
  188. rhoend = max(EPS, min(0.1 * rhobeg, rhoend)) # We do not revise RHOEND unless RHOBEG is truly revised.
  189. if has_rhobeg:
  190. warn(f'{solver}: RHOBEG is revised to {rhobeg} and RHOEND to at most 0.1*RHOBEG so that the distance between X0 and the inactive bounds is at least RHOBEG')
  191. else:
  192. rhoend = np.minimum(rhoend, rhobeg) # This may update RHOEND slightly.
  193. else:
  194. x0_old = x0 # Recorded to see whether X0 is really revised.
  195. # N.B.: The following revision is valid only if XL <= X0 <= XU and RHOBEG <= MINVAL(XU-XL)/2,
  196. # which should hold at this point due to the revision of RHOBEG and moderation of X0.
  197. # The cases below are mutually exclusive in precise arithmetic as MINVAL(XU-XL) >= 2*RHOBEG.
  198. lbx = x0 <= xl + 0.5 * rhobeg
  199. lbx_plus = (x0 > xl + 0.5 * rhobeg) & (x0 < xl + rhobeg)
  200. ubx = x0 >= xu - 0.5 * rhobeg
  201. ubx_minus = (x0 < xu - 0.5 * rhobeg) & (x0 > xu - rhobeg)
  202. x0[lbx] = xl[lbx]
  203. x0[lbx_plus] = xl[lbx_plus] + rhobeg
  204. x0[ubx] = xu[ubx]
  205. x0[ubx_minus] = xu[ubx_minus] - rhobeg
  206. if (any(np.abs(x0_old - x0) > 0)):
  207. warn(f'{solver}: X0 is revised so that the distance between X0 and the inactive bounds is at least RHOBEG set HONOUR_X0 to .TRUE. if you prefer to keep X0 unchanged')
  208. # Validate CTOL (it can be 0)
  209. if (present(ctol)):
  210. if (np.isnan(ctol) or ctol < 0):
  211. ctol = CTOL_DEFAULT
  212. if (is_constrained):
  213. warn(f'{solver}: Invalid CTOL; it should be a nonnegative number; it is set to {ctol}')
  214. # Validate CWEIGHT (it can be +Inf)
  215. if (present(cweight)):
  216. if (np.isnan(cweight) or cweight < 0):
  217. cweight = CWEIGHT_DEFAULT
  218. if (is_constrained):
  219. warn(f'{solver}: Invalid CWEIGHT; it should be a nonnegative number; it is set to {cweight}')
  220. #====================#
  221. # Calculation ends #
  222. #====================#
  223. # Postconditions
  224. if DEBUGGING:
  225. assert abs(iprint) <= 3
  226. assert maxhist >= 0 and maxhist <= maxfun
  227. if present(npt):
  228. assert maxfun >= npt + 1
  229. assert npt >= 3
  230. if present(maxfilt):
  231. assert maxfilt >= np.minimum(MIN_MAXFILT, maxfun) and maxfilt <= maxfun
  232. if present(eta1) and present(eta2):
  233. assert eta1 >= 0 and eta1 <= eta2 and eta2 < 1
  234. if present(gamma1) and present(gamma2):
  235. assert gamma1 > 0 and gamma1 < 1 and gamma2 > 1
  236. assert rhobeg >= rhoend and rhoend > 0
  237. if solver.lower() == 'bobyqa':
  238. assert all(rhobeg <= (xu - xl) / 2)
  239. assert all(np.isfinite(x0))
  240. assert all(x0 >= xl and (x0 <= xl or x0 >= xl + rhobeg))
  241. assert all(x0 <= xu and (x0 >= xu or x0 <= xu - rhobeg))
  242. if present(ctol):
  243. assert ctol >= 0
  244. return iprint, maxfun, maxhist, ftarget, rhobeg, rhoend, npt, maxfilt, ctol, cweight, eta1, eta2, gamma1, gamma2, x0