File: test_canonical_constraint.py

package info (click to toggle)
python-scipy 1.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 93,828 kB
  • sloc: python: 156,854; ansic: 82,925; fortran: 80,777; cpp: 7,505; makefile: 427; sh: 294
file content (297 lines) | stat: -rw-r--r-- 9,940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from __future__ import division, print_function, absolute_import
import numpy as np
from numpy.testing import assert_array_equal, assert_equal
from scipy.optimize._constraints import (NonlinearConstraint, Bounds,
                                         PreparedConstraint)
from scipy.optimize._trustregion_constr.canonical_constraint \
    import CanonicalConstraint, initial_constraints_as_canonical


def create_quadratic_function(n, m, rng):
    a = rng.rand(m)
    A = rng.rand(m, n)
    H = rng.rand(m, n, n)
    HT = np.transpose(H, (1, 2, 0))

    def fun(x):
        return a + A.dot(x) + 0.5 * H.dot(x).dot(x)

    def jac(x):
        return A + H.dot(x)

    def hess(x, v):
        return HT.dot(v)

    return fun, jac, hess


def test_bounds_cases():
    # Test 1: no constraints.
    user_constraint = Bounds(-np.inf, np.inf)
    x0 = np.array([-1, 2])
    prepared_constraint = PreparedConstraint(user_constraint, x0, False)
    c = CanonicalConstraint.from_PreparedConstraint(prepared_constraint)

    assert_equal(c.n_eq, 0)
    assert_equal(c.n_ineq, 0)

    c_eq, c_ineq = c.fun(x0)
    assert_array_equal(c_eq, [])
    assert_array_equal(c_ineq, [])

    J_eq, J_ineq = c.jac(x0)
    assert_array_equal(J_eq, np.empty((0, 2)))
    assert_array_equal(J_ineq, np.empty((0, 2)))

    assert_array_equal(c.keep_feasible, [])

    # Test 2: infinite lower bound.
    user_constraint = Bounds(-np.inf, [0, np.inf, 1], [False, True, True])
    x0 = np.array([-1, -2, -3], dtype=float)
    prepared_constraint = PreparedConstraint(user_constraint, x0, False)
    c = CanonicalConstraint.from_PreparedConstraint(prepared_constraint)

    assert_equal(c.n_eq, 0)
    assert_equal(c.n_ineq, 2)

    c_eq, c_ineq = c.fun(x0)
    assert_array_equal(c_eq, [])
    assert_array_equal(c_ineq, [-1, -4])

    J_eq, J_ineq = c.jac(x0)
    assert_array_equal(J_eq, np.empty((0, 3)))
    assert_array_equal(J_ineq, np.array([[1, 0, 0], [0, 0, 1]]))

    assert_array_equal(c.keep_feasible, [False, True])

    # Test 3: infinite upper bound.
    user_constraint = Bounds([0, 1, -np.inf], np.inf, [True, False, True])
    x0 = np.array([1, 2, 3], dtype=float)
    prepared_constraint = PreparedConstraint(user_constraint, x0, False)
    c = CanonicalConstraint.from_PreparedConstraint(prepared_constraint)

    assert_equal(c.n_eq, 0)
    assert_equal(c.n_ineq, 2)

    c_eq, c_ineq = c.fun(x0)
    assert_array_equal(c_eq, [])
    assert_array_equal(c_ineq, [-1, -1])

    J_eq, J_ineq = c.jac(x0)
    assert_array_equal(J_eq, np.empty((0, 3)))
    assert_array_equal(J_ineq, np.array([[-1, 0, 0], [0, -1, 0]]))

    assert_array_equal(c.keep_feasible, [True, False])

    # Test 4: interval constraint.
    user_constraint = Bounds([-1, -np.inf, 2, 3], [1, np.inf, 10, 3],
                             [False, True, True, True])
    x0 = np.array([0, 10, 8, 5])
    prepared_constraint = PreparedConstraint(user_constraint, x0, False)
    c = CanonicalConstraint.from_PreparedConstraint(prepared_constraint)

    assert_equal(c.n_eq, 1)
    assert_equal(c.n_ineq, 4)

    c_eq, c_ineq = c.fun(x0)
    assert_array_equal(c_eq, [2])
    assert_array_equal(c_ineq, [-1, -2, -1, -6])

    J_eq, J_ineq = c.jac(x0)
    assert_array_equal(J_eq, [[0, 0, 0, 1]])
    assert_array_equal(J_ineq, [[1, 0, 0, 0],
                                [0, 0, 1, 0],
                                [-1, 0, 0, 0],
                                [0, 0, -1, 0]])

    assert_array_equal(c.keep_feasible, [False, True, False, True])


def test_nonlinear_constraint():
    n = 3
    m = 5
    rng = np.random.RandomState(0)
    x0 = rng.rand(n)

    fun, jac, hess = create_quadratic_function(n, m, rng)
    f = fun(x0)
    J = jac(x0)

    lb = [-10, 3, -np.inf, -np.inf, -5]
    ub = [10, 3, np.inf, 3, np.inf]
    user_constraint = NonlinearConstraint(
        fun, lb, ub, jac, hess, [True, False, False, True, False])

    for sparse_jacobian in [False, True]:
        prepared_constraint = PreparedConstraint(user_constraint, x0,
                                                 sparse_jacobian)
        c = CanonicalConstraint.from_PreparedConstraint(prepared_constraint)

        assert_array_equal(c.n_eq, 1)
        assert_array_equal(c.n_ineq, 4)

        c_eq, c_ineq = c.fun(x0)
        assert_array_equal(c_eq, [f[1] - lb[1]])
        assert_array_equal(c_ineq, [f[3] - ub[3], lb[4] - f[4],
                                    f[0] - ub[0], lb[0] - f[0]])

        J_eq, J_ineq = c.jac(x0)
        if sparse_jacobian:
            J_eq = J_eq.toarray()
            J_ineq = J_ineq.toarray()

        assert_array_equal(J_eq, J[1, None])
        assert_array_equal(J_ineq, np.vstack((J[3], -J[4], J[0], -J[0])))

        v_eq = rng.rand(c.n_eq)
        v_ineq = rng.rand(c.n_ineq)
        v = np.zeros(m)
        v[1] = v_eq[0]
        v[3] = v_ineq[0]
        v[4] = -v_ineq[1]
        v[0] = v_ineq[2] - v_ineq[3]
        assert_array_equal(c.hess(x0, v_eq, v_ineq), hess(x0, v))

        assert_array_equal(c.keep_feasible, [True, False, True, True])


def test_concatenation():
    rng = np.random.RandomState(0)
    n = 4
    x0 = np.random.rand(n)

    f1 = x0
    J1 = np.eye(n)
    lb1 = [-1, -np.inf, -2, 3]
    ub1 = [1, np.inf, np.inf, 3]
    bounds = Bounds(lb1, ub1, [False, False, True, False])

    fun, jac, hess = create_quadratic_function(n, 5, rng)
    f2 = fun(x0)
    J2 = jac(x0)
    lb2 = [-10, 3, -np.inf, -np.inf, -5]
    ub2 = [10, 3, np.inf, 5, np.inf]
    nonlinear = NonlinearConstraint(
        fun, lb2, ub2, jac, hess, [True, False, False, True, False])

    for sparse_jacobian in [False, True]:
        bounds_prepared = PreparedConstraint(bounds, x0, sparse_jacobian)
        nonlinear_prepared = PreparedConstraint(nonlinear, x0, sparse_jacobian)

        c1 = CanonicalConstraint.from_PreparedConstraint(bounds_prepared)
        c2 = CanonicalConstraint.from_PreparedConstraint(nonlinear_prepared)
        c = CanonicalConstraint.concatenate([c1, c2], sparse_jacobian)

        assert_equal(c.n_eq, 2)
        assert_equal(c.n_ineq, 7)

        c_eq, c_ineq = c.fun(x0)
        assert_array_equal(c_eq, [f1[3] - lb1[3], f2[1] - lb2[1]])
        assert_array_equal(c_ineq, [lb1[2] - f1[2], f1[0] - ub1[0],
                                    lb1[0] - f1[0], f2[3] - ub2[3],
                                    lb2[4] - f2[4], f2[0] - ub2[0],
                                    lb2[0] - f2[0]])

        J_eq, J_ineq = c.jac(x0)
        if sparse_jacobian:
            J_eq = J_eq.toarray()
            J_ineq = J_ineq.toarray()

        assert_array_equal(J_eq, np.vstack((J1[3], J2[1])))
        assert_array_equal(J_ineq, np.vstack((-J1[2], J1[0], -J1[0], J2[3],
                                              -J2[4], J2[0], -J2[0])))

        v_eq = rng.rand(c.n_eq)
        v_ineq = rng.rand(c.n_ineq)
        v = np.zeros(5)
        v[1] = v_eq[1]
        v[3] = v_ineq[3]
        v[4] = -v_ineq[4]
        v[0] = v_ineq[5] - v_ineq[6]
        H = c.hess(x0, v_eq, v_ineq).dot(np.eye(n))
        assert_array_equal(H, hess(x0, v))

        assert_array_equal(c.keep_feasible,
                           [True, False, False, True, False, True, True])


def test_empty():
    x = np.array([1, 2, 3])
    c = CanonicalConstraint.empty(3)
    assert_equal(c.n_eq, 0)
    assert_equal(c.n_ineq, 0)

    c_eq, c_ineq = c.fun(x)
    assert_array_equal(c_eq, [])
    assert_array_equal(c_ineq, [])

    J_eq, J_ineq = c.jac(x)
    assert_array_equal(J_eq, np.empty((0, 3)))
    assert_array_equal(J_ineq, np.empty((0, 3)))

    H = c.hess(x, None, None).toarray()
    assert_array_equal(H, np.zeros((3, 3)))


def test_initial_constraints_as_canonical():
    # rng is only used to generate the coefficients of the quadratic
    # function that is used by the nonlinear constraint.
    rng = np.random.RandomState(0)

    x0 = np.array([0.5, 0.4, 0.3, 0.2])
    n = len(x0)

    lb1 = [-1, -np.inf, -2, 3]
    ub1 = [1, np.inf, np.inf, 3]
    bounds = Bounds(lb1, ub1, [False, False, True, False])

    fun, jac, hess = create_quadratic_function(n, 5, rng)
    lb2 = [-10, 3, -np.inf, -np.inf, -5]
    ub2 = [10, 3, np.inf, 5, np.inf]
    nonlinear = NonlinearConstraint(
        fun, lb2, ub2, jac, hess, [True, False, False, True, False])

    for sparse_jacobian in [False, True]:
        bounds_prepared = PreparedConstraint(bounds, x0, sparse_jacobian)
        nonlinear_prepared = PreparedConstraint(nonlinear, x0, sparse_jacobian)

        f1 = bounds_prepared.fun.f
        J1 = bounds_prepared.fun.J
        f2 = nonlinear_prepared.fun.f
        J2 = nonlinear_prepared.fun.J

        c_eq, c_ineq, J_eq, J_ineq = initial_constraints_as_canonical(
            n, [bounds_prepared, nonlinear_prepared], sparse_jacobian)

        assert_array_equal(c_eq, [f1[3] - lb1[3], f2[1] - lb2[1]])
        assert_array_equal(c_ineq, [lb1[2] - f1[2], f1[0] - ub1[0],
                                    lb1[0] - f1[0], f2[3] - ub2[3],
                                    lb2[4] - f2[4], f2[0] - ub2[0],
                                    lb2[0] - f2[0]])

        if sparse_jacobian:
            J1 = J1.toarray()
            J2 = J2.toarray()
            J_eq = J_eq.toarray()
            J_ineq = J_ineq.toarray()

        assert_array_equal(J_eq, np.vstack((J1[3], J2[1])))
        assert_array_equal(J_ineq, np.vstack((-J1[2], J1[0], -J1[0], J2[3],
                                              -J2[4], J2[0], -J2[0])))


def test_initial_constraints_as_canonical_empty():
    n = 3
    for sparse_jacobian in [False, True]:
        c_eq, c_ineq, J_eq, J_ineq = initial_constraints_as_canonical(
            n, [], sparse_jacobian)

        assert_array_equal(c_eq, [])
        assert_array_equal(c_ineq, [])

        if sparse_jacobian:
            J_eq = J_eq.toarray()
            J_ineq = J_ineq.toarray()

        assert_array_equal(J_eq, np.empty((0, n)))
        assert_array_equal(J_ineq, np.empty((0, n)))