test_constraints.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from __future__ import division, print_function, absolute_import
  2. import pytest
  3. import numpy as np
  4. from numpy.testing import TestCase, assert_array_equal
  5. import scipy.sparse as sps
  6. from scipy.optimize._constraints import (
  7. Bounds, LinearConstraint, NonlinearConstraint, PreparedConstraint,
  8. new_bounds_to_old, old_bound_to_new, strict_bounds)
  9. class TestStrictBounds(TestCase):
  10. def test_scalarvalue_unique_enforce_feasibility(self):
  11. m = 3
  12. lb = 2
  13. ub = 4
  14. enforce_feasibility = False
  15. strict_lb, strict_ub = strict_bounds(lb, ub,
  16. enforce_feasibility,
  17. m)
  18. assert_array_equal(strict_lb, [-np.inf, -np.inf, -np.inf])
  19. assert_array_equal(strict_ub, [np.inf, np.inf, np.inf])
  20. enforce_feasibility = True
  21. strict_lb, strict_ub = strict_bounds(lb, ub,
  22. enforce_feasibility,
  23. m)
  24. assert_array_equal(strict_lb, [2, 2, 2])
  25. assert_array_equal(strict_ub, [4, 4, 4])
  26. def test_vectorvalue_unique_enforce_feasibility(self):
  27. m = 3
  28. lb = [1, 2, 3]
  29. ub = [4, 5, 6]
  30. enforce_feasibility = False
  31. strict_lb, strict_ub = strict_bounds(lb, ub,
  32. enforce_feasibility,
  33. m)
  34. assert_array_equal(strict_lb, [-np.inf, -np.inf, -np.inf])
  35. assert_array_equal(strict_ub, [np.inf, np.inf, np.inf])
  36. enforce_feasibility = True
  37. strict_lb, strict_ub = strict_bounds(lb, ub,
  38. enforce_feasibility,
  39. m)
  40. assert_array_equal(strict_lb, [1, 2, 3])
  41. assert_array_equal(strict_ub, [4, 5, 6])
  42. def test_scalarvalue_vector_enforce_feasibility(self):
  43. m = 3
  44. lb = 2
  45. ub = 4
  46. enforce_feasibility = [False, True, False]
  47. strict_lb, strict_ub = strict_bounds(lb, ub,
  48. enforce_feasibility,
  49. m)
  50. assert_array_equal(strict_lb, [-np.inf, 2, -np.inf])
  51. assert_array_equal(strict_ub, [np.inf, 4, np.inf])
  52. def test_vectorvalue_vector_enforce_feasibility(self):
  53. m = 3
  54. lb = [1, 2, 3]
  55. ub = [4, 6, np.inf]
  56. enforce_feasibility = [True, False, True]
  57. strict_lb, strict_ub = strict_bounds(lb, ub,
  58. enforce_feasibility,
  59. m)
  60. assert_array_equal(strict_lb, [1, -np.inf, 3])
  61. assert_array_equal(strict_ub, [4, np.inf, np.inf])
  62. def test_prepare_constraint_infeasible_x0():
  63. lb = np.array([0, 20, 30])
  64. ub = np.array([0.5, np.inf, 70])
  65. x0 = np.array([1, 2, 3])
  66. enforce_feasibility = np.array([False, True, True], dtype=bool)
  67. bounds = Bounds(lb, ub, enforce_feasibility)
  68. pytest.raises(ValueError, PreparedConstraint, bounds, x0)
  69. x0 = np.array([1, 2, 3, 4])
  70. A = np.array([[1, 2, 3, 4], [5, 0, 0, 6], [7, 0, 8, 0]])
  71. enforce_feasibility = np.array([True, True, True], dtype=bool)
  72. linear = LinearConstraint(A, -np.inf, 0, enforce_feasibility)
  73. pytest.raises(ValueError, PreparedConstraint, linear, x0)
  74. def fun(x):
  75. return A.dot(x)
  76. def jac(x):
  77. return A
  78. def hess(x, v):
  79. return sps.csr_matrix((4, 4))
  80. nonlinear = NonlinearConstraint(fun, -np.inf, 0, jac, hess,
  81. enforce_feasibility)
  82. pytest.raises(ValueError, PreparedConstraint, nonlinear, x0)
  83. def test_new_bounds_to_old():
  84. lb = np.array([-np.inf, 2, 3])
  85. ub = np.array([3, np.inf, 10])
  86. bounds = [(None, 3), (2, None), (3, 10)]
  87. assert_array_equal(new_bounds_to_old(lb, ub, 3), bounds)
  88. bounds_single_lb = [(-1, 3), (-1, None), (-1, 10)]
  89. assert_array_equal(new_bounds_to_old(-1, ub, 3), bounds_single_lb)
  90. bounds_no_lb = [(None, 3), (None, None), (None, 10)]
  91. assert_array_equal(new_bounds_to_old(-np.inf, ub, 3), bounds_no_lb)
  92. bounds_single_ub = [(None, 20), (2, 20), (3, 20)]
  93. assert_array_equal(new_bounds_to_old(lb, 20, 3), bounds_single_ub)
  94. bounds_no_ub = [(None, None), (2, None), (3, None)]
  95. assert_array_equal(new_bounds_to_old(lb, np.inf, 3), bounds_no_ub)
  96. bounds_single_both = [(1, 2), (1, 2), (1, 2)]
  97. assert_array_equal(new_bounds_to_old(1, 2, 3), bounds_single_both)
  98. bounds_no_both = [(None, None), (None, None), (None, None)]
  99. assert_array_equal(new_bounds_to_old(-np.inf, np.inf, 3), bounds_no_both)
  100. def test_old_bounds_to_new():
  101. bounds = ([1, 2], (None, 3), (-1, None))
  102. lb_true = np.array([1, -np.inf, -1])
  103. ub_true = np.array([2, 3, np.inf])
  104. lb, ub = old_bound_to_new(bounds)
  105. assert_array_equal(lb, lb_true)
  106. assert_array_equal(ub, ub_true)