test_lbfgsb_setulb.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import numpy as np
  2. from scipy.optimize import _lbfgsb
  3. def objfun(x):
  4. """simplified objective func to test lbfgsb bound violation"""
  5. x0 = [0.8750000000000278,
  6. 0.7500000000000153,
  7. 0.9499999999999722,
  8. 0.8214285714285992,
  9. 0.6363636363636085]
  10. x1 = [1.0, 0.0, 1.0, 0.0, 0.0]
  11. x2 = [1.0,
  12. 0.0,
  13. 0.9889733043149325,
  14. 0.0,
  15. 0.026353554421041155]
  16. x3 = [1.0,
  17. 0.0,
  18. 0.9889917442915558,
  19. 0.0,
  20. 0.020341986743231205]
  21. f0 = 5163.647901211178
  22. f1 = 5149.8181642072905
  23. f2 = 5149.379332309634
  24. f3 = 5149.374490771297
  25. g0 = np.array([-0.5934820547965749,
  26. 1.6251549718258351,
  27. -71.99168459202559,
  28. 5.346636965797545,
  29. 37.10732723092604])
  30. g1 = np.array([-0.43295349282641515,
  31. 1.008607936794592,
  32. 18.223666726602975,
  33. 31.927010036981997,
  34. -19.667512518739386])
  35. g2 = np.array([-0.4699874455100256,
  36. 0.9466285353668347,
  37. -0.016874360242016825,
  38. 48.44999161133457,
  39. 5.819631620590712])
  40. g3 = np.array([-0.46970678696829116,
  41. 0.9612719312174818,
  42. 0.006129809488833699,
  43. 48.43557729419473,
  44. 6.005481418498221])
  45. if np.allclose(x, x0):
  46. f = f0
  47. g = g0
  48. elif np.allclose(x, x1):
  49. f = f1
  50. g = g1
  51. elif np.allclose(x, x2):
  52. f = f2
  53. g = g2
  54. elif np.allclose(x, x3):
  55. f = f3
  56. g = g3
  57. else:
  58. raise ValueError(
  59. 'Simplified objective function not defined '
  60. 'at requested point')
  61. return (np.copy(f), np.copy(g))
  62. def test_setulb_floatround():
  63. """test if setulb() violates bounds
  64. checks for violation due to floating point rounding error
  65. """
  66. n = 5
  67. m = 10
  68. factr = 1e7
  69. pgtol = 1e-5
  70. maxls = 20
  71. iprint = -1
  72. nbd = np.full((n,), 2)
  73. low_bnd = np.zeros(n, np.float64)
  74. upper_bnd = np.ones(n, np.float64)
  75. x0 = np.array(
  76. [0.8750000000000278,
  77. 0.7500000000000153,
  78. 0.9499999999999722,
  79. 0.8214285714285992,
  80. 0.6363636363636085])
  81. x = np.copy(x0)
  82. f = np.array(0.0, np.float64)
  83. g = np.zeros(n, np.float64)
  84. fortran_int = _lbfgsb.types.intvar.dtype
  85. wa = np.zeros(2*m*n + 5*n + 11*m*m + 8*m, np.float64)
  86. iwa = np.zeros(3*n, fortran_int)
  87. task = np.zeros(1, 'S60')
  88. csave = np.zeros(1, 'S60')
  89. lsave = np.zeros(4, fortran_int)
  90. isave = np.zeros(44, fortran_int)
  91. dsave = np.zeros(29, np.float64)
  92. task[:] = b'START'
  93. for n_iter in range(7): # 7 steps required to reproduce error
  94. f, g = objfun(x)
  95. _lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr,
  96. pgtol, wa, iwa, task, iprint, csave, lsave,
  97. isave, dsave, maxls)
  98. assert (x <= upper_bnd).all() and (x >= low_bnd).all(), (
  99. "_lbfgsb.setulb() stepped to a point outside of the bounds")