def test_constraint_wrapper(self):
        lb = np.array([0, 20, 30])
        ub = np.array([0.5, np.inf, 70])
        x0 = np.array([1, 2, 3])
        pc = _ConstraintWrapper(Bounds(lb, ub), x0)
        assert (pc.violation(x0) > 0).any()
        assert (pc.violation([0.25, 21, 31]) == 0).all()

        x0 = np.array([1, 2, 3, 4])
        A = np.array([[1, 2, 3, 4], [5, 0, 0, 6], [7, 0, 8, 0]])
        pc = _ConstraintWrapper(LinearConstraint(A, -np.inf, 0), x0)
        assert (pc.violation(x0) > 0).any()
        assert (pc.violation([-10, 2, -10, 4]) == 0).all()

        pc = _ConstraintWrapper(LinearConstraint(csr_matrix(A), -np.inf, 0),
                                x0)
        assert (pc.violation(x0) > 0).any()
        assert (pc.violation([-10, 2, -10, 4]) == 0).all()

        def fun(x):
            return A.dot(x)

        nonlinear = NonlinearConstraint(fun, -np.inf, 0)
        pc = _ConstraintWrapper(nonlinear, [-10, 2, -10, 4])
        assert (pc.violation(x0) > 0).any()
        assert (pc.violation([-10, 2, -10, 4]) == 0).all()
예제 #2
0
    def test_input_validation(self):
        A = np.eye(4)
        message = "`lb`, `ub`, and `keep_feasible` must be broadcastable"
        with pytest.raises(ValueError, match=message):
            LinearConstraint(A, [1, 2], [1, 2, 3])

        A = np.empty((4, 3, 5))
        message = "`A` must have exactly two dimensions."
        with pytest.raises(ValueError, match=message):
            LinearConstraint(A)
예제 #3
0
def test_prepare_constraint_infeasible_x0():
    lb = np.array([0, 20, 30])
    ub = np.array([0.5, np.inf, 70])
    x0 = np.array([1, 2, 3])
    enforce_feasibility = np.array([False, True, True], dtype=bool)
    bounds = Bounds(lb, ub, enforce_feasibility)
    pytest.raises(ValueError, PreparedConstraint, bounds, x0)

    x0 = np.array([1, 2, 3, 4])
    A = np.array([[1, 2, 3, 4], [5, 0, 0, 6], [7, 0, 8, 0]])
    enforce_feasibility = np.array([True, True, True], dtype=bool)
    linear = LinearConstraint(A, -np.inf, 0, enforce_feasibility)
    pytest.raises(ValueError, PreparedConstraint, linear, x0)

    def fun(x):
        return A.dot(x)

    def jac(x):
        return A

    def hess(x, v):
        return sps.csr_matrix((4, 4))

    nonlinear = NonlinearConstraint(fun, -np.inf, 0, jac, hess,
                                    enforce_feasibility)
    pytest.raises(ValueError, PreparedConstraint, nonlinear, x0)
예제 #4
0
    def test_L4(self):
        # Lampinen ([5]) test problem 4
        def f(x):
            return np.sum(x[:3])

        A = np.zeros((4, 9))
        A[1, [4, 6]] = 0.0025, 0.0025
        A[2, [5, 7, 4]] = 0.0025, 0.0025, -0.0025
        A[3, [8, 5]] = 0.01, -0.01
        A = A[1:, 1:]
        b = np.array([1, 1, 1])

        def c1(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            return [
                x[1] * x[6] - 833.33252 * x[4] - 100 * x[1] + 83333.333,
                x[2] * x[7] - 1250 * x[5] - x[2] * x[4] + 1250 * x[4],
                x[3] * x[8] - 1250000 - x[3] * x[5] + 2500 * x[5]
            ]

        L = LinearConstraint(A, -np.inf, 1)
        N = NonlinearConstraint(c1, 0, np.inf)

        bounds = [(100, 10000)] + [(1000, 10000)] * 2 + [(10, 1000)] * 5
        constraints = (L, N)

        with suppress_warnings() as sup:
            sup.filter(UserWarning)
            res = differential_evolution(f,
                                         bounds,
                                         strategy='rand1bin',
                                         seed=1234,
                                         constraints=constraints,
                                         popsize=3)

        f_opt = 7049.248

        x_opt = [
            579.306692, 1359.97063, 5109.9707, 182.0177, 295.601172, 217.9823,
            286.416528, 395.601172
        ]

        assert_allclose(f(x_opt), f_opt, atol=0.001)
        assert_allclose(res.fun, f_opt, atol=0.001)

        # selectively use higher tol here for 32-bit
        # Windows based on gh-11693
        if (platform.system() == 'Windows' and np.dtype(np.intp).itemsize < 8):
            assert_allclose(res.x, x_opt, rtol=2.4e-6, atol=0.0035)
        else:
            assert_allclose(res.x, x_opt, atol=0.002)

        assert res.success
        assert_(np.all(A @ res.x <= b))
        assert_(np.all(np.array(c1(res.x)) >= 0))
        assert_(np.all(res.x >= np.array(bounds)[:, 0]))
        assert_(np.all(res.x <= np.array(bounds)[:, 1]))
예제 #5
0
    def test_L3(self):
        # Lampinen ([5]) test problem 3

        def f(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            fun = (x[1]**2 + x[2]**2 + x[1] * x[2] - 14 * x[1] - 16 * x[2] +
                   (x[3] - 10)**2 + 4 * (x[4] - 5)**2 + (x[5] - 3)**2 + 2 *
                   (x[6] - 1)**2 + 5 * x[7]**2 + 7 * (x[8] - 11)**2 + 2 *
                   (x[9] - 10)**2 + (x[10] - 7)**2 + 45)
            return fun  # maximize

        A = np.zeros((4, 11))
        A[1, [1, 2, 7, 8]] = -4, -5, 3, -9
        A[2, [1, 2, 7, 8]] = -10, 8, 17, -2
        A[3, [1, 2, 9, 10]] = 8, -2, -5, 2
        A = A[1:, 1:]
        b = np.array([-105, 0, -12])

        def c1(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            return [
                3 * x[1] - 6 * x[2] - 12 * (x[9] - 8)**2 + 7 * x[10],
                -3 * (x[1] - 2)**2 - 4 * (x[2] - 3)**2 - 2 * x[3]**2 +
                7 * x[4] + 120, -x[1]**2 - 2 * (x[2] - 2)**2 +
                2 * x[1] * x[2] - 14 * x[5] + 6 * x[6],
                -5 * x[1]**2 - 8 * x[2] - (x[3] - 6)**2 + 2 * x[4] + 40, -0.5 *
                (x[1] - 8)**2 - 2 * (x[2] - 4)**2 - 3 * x[5]**2 + x[6] + 30
            ]

        L = LinearConstraint(A, b, np.inf)
        N = NonlinearConstraint(c1, 0, np.inf)
        bounds = [(-10, 10)] * 10
        constraints = (L, N)

        with suppress_warnings() as sup:
            sup.filter(UserWarning)
            res = differential_evolution(f,
                                         bounds,
                                         seed=1234,
                                         constraints=constraints,
                                         popsize=3)

        x_opt = (2.171996, 2.363683, 8.773926, 5.095984, 0.9906548, 1.430574,
                 1.321644, 9.828726, 8.280092, 8.375927)
        f_opt = 24.3062091

        assert_allclose(f(x_opt), f_opt, atol=1e-5)
        assert_allclose(res.x, x_opt, atol=1e-6)
        assert_allclose(res.fun, f_opt, atol=1e-5)
        assert res.success
        assert_(np.all(A @ res.x >= b))
        assert_(np.all(np.array(c1(res.x)) >= 0))
        assert_(np.all(res.x >= np.array(bounds)[:, 0]))
        assert_(np.all(res.x <= np.array(bounds)[:, 1]))
예제 #6
0
    def test_L8(self):
        def f(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            fun = 3 * x[1] + 0.000001 * x[1]**3 + 2 * x[2] + 0.000002 / 3 * x[
                2]**3
            return fun

        A = np.zeros((3, 5))
        A[1, [4, 3]] = 1, -1
        A[2, [3, 4]] = 1, -1
        A = A[1:, 1:]
        b = np.array([-.55, -.55])

        def c1(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            return [
                1000 * np.sin(-x[3] - 0.25) + 1000 * np.sin(-x[4] - 0.25) +
                894.8 - x[1], 1000 * np.sin(x[3] - 0.25) +
                1000 * np.sin(x[3] - x[4] - 0.25) + 894.8 - x[2],
                1000 * np.sin(x[4] - 0.25) +
                1000 * np.sin(x[4] - x[3] - 0.25) + 1294.8
            ]

        L = LinearConstraint(A, b, np.inf)
        N = NonlinearConstraint(c1, np.full(3, -0.001), np.full(3, 0.001))

        bounds = [(0, 1200)] * 2 + [(-.55, .55)] * 2
        constraints = (L, N)

        with suppress_warnings() as sup:
            sup.filter(UserWarning)
            # original Lampinen test was with rand1bin, but that takes a
            # huge amount of CPU time. Changing strategy to best1bin speeds
            # things up a lot
            res = differential_evolution(f,
                                         bounds,
                                         strategy='best1bin',
                                         seed=1234,
                                         constraints=constraints,
                                         maxiter=5000)

        x_opt = (679.9453, 1026.067, 0.1188764, -0.3962336)
        f_opt = 5126.4981

        assert_allclose(f(x_opt), f_opt, atol=1e-3)
        assert_allclose(res.x[:2], x_opt[:2], atol=2e-3)
        assert_allclose(res.x[2:], x_opt[2:], atol=2e-3)
        assert_allclose(res.fun, f_opt, atol=2e-2)
        assert res.success
        assert_(np.all(A @ res.x >= b))
        assert_(np.all(np.array(c1(res.x)) >= -0.001))
        assert_(np.all(np.array(c1(res.x)) <= 0.001))
        assert_(np.all(res.x >= np.array(bounds)[:, 0]))
        assert_(np.all(res.x <= np.array(bounds)[:, 1]))
예제 #7
0
    def test_L1(self):
        # Lampinen ([5]) test problem 1

        def f(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            fun = np.sum(5 * x[1:5]) - 5 * x[1:5] @ x[1:5] - np.sum(x[5:])
            return fun

        A = np.zeros((10, 14))  # 1-indexed to match reference
        A[1, [1, 2, 10, 11]] = 2, 2, 1, 1
        A[2, [1, 10]] = -8, 1
        A[3, [4, 5, 10]] = -2, -1, 1
        A[4, [1, 3, 10, 11]] = 2, 2, 1, 1
        A[5, [2, 11]] = -8, 1
        A[6, [6, 7, 11]] = -2, -1, 1
        A[7, [2, 3, 11, 12]] = 2, 2, 1, 1
        A[8, [3, 12]] = -8, 1
        A[9, [8, 9, 12]] = -2, -1, 1
        A = A[1:, 1:]

        b = np.array([10, 0, 0, 10, 0, 0, 10, 0, 0])

        L = LinearConstraint(A, -np.inf, b)

        bounds = [(0, 1)] * 9 + [(0, 100)] * 3 + [(0, 1)]

        # using a lower popsize to speed the test up
        res = differential_evolution(f,
                                     bounds,
                                     strategy='best1bin',
                                     seed=1234,
                                     constraints=(L),
                                     popsize=2)

        x_opt = (1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1)
        f_opt = -15

        assert_allclose(f(x_opt), f_opt)
        assert res.success
        assert_allclose(res.x, x_opt, atol=5e-4)
        assert_allclose(res.fun, f_opt, atol=5e-3)
        assert_(np.all(A @ res.x <= b))
        assert_(np.all(res.x >= np.array(bounds)[:, 0]))
        assert_(np.all(res.x <= np.array(bounds)[:, 1]))

        # now repeat the same solve, using the same overall constraints,
        # but specify half the constraints in terms of LinearConstraint,
        # and the other half by NonlinearConstraint
        def c1(x):
            x = np.hstack(([0], x))
            return [2 * x[2] + 2 * x[3] + x[11] + x[12], -8 * x[3] + x[12]]

        def c2(x):
            x = np.hstack(([0], x))
            return -2 * x[8] - x[9] + x[12]

        L = LinearConstraint(A[:5, :], -np.inf, b[:5])
        L2 = LinearConstraint(A[5:6, :], -np.inf, b[5:6])
        N = NonlinearConstraint(c1, -np.inf, b[6:8])
        N2 = NonlinearConstraint(c2, -np.inf, b[8:9])
        constraints = (L, N, L2, N2)

        with suppress_warnings() as sup:
            sup.filter(UserWarning)
            res = differential_evolution(f,
                                         bounds,
                                         strategy='rand1bin',
                                         seed=1234,
                                         constraints=constraints,
                                         popsize=2)

        assert_allclose(res.x, x_opt, atol=5e-4)
        assert_allclose(res.fun, f_opt, atol=5e-3)
        assert_(np.all(A @ res.x <= b))
        assert_(np.all(res.x >= np.array(bounds)[:, 0]))
        assert_(np.all(res.x <= np.array(bounds)[:, 1]))
예제 #8
0
 def test_residual(self):
     A = np.eye(2)
     lc = LinearConstraint(A, -2, 4)
     x0 = [-1, 2]
     np.testing.assert_allclose(lc.residual(x0), ([1, 4], [5, 2]))
예제 #9
0
 def test_defaults(self):
     A = np.eye(4)
     lc = LinearConstraint(A)
     lc2 = LinearConstraint(A, -np.inf, np.inf)
     assert_array_equal(lc.lb, lc2.lb)
     assert_array_equal(lc.ub, lc2.ub)