File: test__linprog_ip_clean_inputs.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 (365 lines) | stat: -rw-r--r-- 10,865 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Unit test for Linear Programming via Simplex Algorithm.
"""
from __future__ import division, print_function, absolute_import

import numpy as np
from numpy.testing import assert_, assert_allclose
from pytest import raises as assert_raises
from scipy.optimize._linprog_ip import _clean_inputs
from copy import deepcopy


def test_aliasing():
    c = 1
    A_ub = [[1]]
    b_ub = [1]
    A_eq = [[1]]
    b_eq = [1]
    bounds = (-np.inf, np.inf)

    c_copy = deepcopy(c)
    A_ub_copy = deepcopy(A_ub)
    b_ub_copy = deepcopy(b_ub)
    A_eq_copy = deepcopy(A_eq)
    b_eq_copy = deepcopy(b_eq)
    bounds_copy = deepcopy(bounds)

    _clean_inputs(c, A_ub, b_ub, A_eq, b_eq, bounds)

    assert_(c == c_copy, "c modified by _clean_inputs")
    assert_(A_ub == A_ub_copy, "A_ub modified by _clean_inputs")
    assert_(b_ub == b_ub_copy, "b_ub modified by _clean_inputs")
    assert_(A_eq == A_eq_copy, "A_eq modified by _clean_inputs")
    assert_(b_eq == b_eq_copy, "b_eq modified by _clean_inputs")
    assert_(bounds == bounds_copy, "bounds modified by _clean_inputs")


def test_aliasing2():
    c = np.array([1, 1])
    A_ub = np.array([[1, 1], [2, 2]])
    b_ub = np.array([[1], [1]])
    A_eq = np.array([[1, 1]])
    b_eq = np.array([1])
    bounds = [(-np.inf, np.inf), (None, 1)]

    c_copy = c.copy()
    A_ub_copy = A_ub.copy()
    b_ub_copy = b_ub.copy()
    A_eq_copy = A_eq.copy()
    b_eq_copy = b_eq.copy()
    bounds_copy = deepcopy(bounds)

    _clean_inputs(c, A_ub, b_ub, A_eq, b_eq, bounds)

    assert_allclose(c, c_copy, err_msg="c modified by _clean_inputs")
    assert_allclose(A_ub, A_ub_copy, err_msg="A_ub modified by _clean_inputs")
    assert_allclose(b_ub, b_ub_copy, err_msg="b_ub modified by _clean_inputs")
    assert_allclose(A_eq, A_eq_copy, err_msg="A_eq modified by _clean_inputs")
    assert_allclose(b_eq, b_eq_copy, err_msg="b_eq modified by _clean_inputs")
    assert_(bounds == bounds_copy, "bounds modified by _clean_inputs")


def test_missing_inputs():
    c = [1, 2]
    A_ub = np.array([[1, 1], [2, 2]])
    b_ub = np.array([1, 1])
    A_eq = np.array([[1, 1], [2, 2]])
    b_eq = np.array([1, 1])

    assert_raises(TypeError, _clean_inputs)
    assert_raises(TypeError, _clean_inputs, c=None)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=A_ub)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=A_ub, b_ub=None)
    assert_raises(ValueError, _clean_inputs, c=c, b_ub=b_ub)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=None, b_ub=b_ub)
    assert_raises(ValueError, _clean_inputs, c=c, A_eq=A_eq)
    assert_raises(ValueError, _clean_inputs, c=c, A_eq=A_eq, b_eq=None)
    assert_raises(ValueError, _clean_inputs, c=c, b_eq=b_eq)
    assert_raises(ValueError, _clean_inputs, c=c, A_eq=None, b_eq=b_eq)


def test_too_many_dimensions():
    cb = [1, 2, 3, 4]
    A = np.random.rand(4, 4)
    bad2D = [[1, 2], [3, 4]]
    bad3D = np.random.rand(4, 4, 4)
    assert_raises(ValueError, _clean_inputs, c=bad2D, A_ub=A, b_ub=cb)
    assert_raises(ValueError, _clean_inputs, c=cb, A_ub=bad3D, b_ub=cb)
    assert_raises(ValueError, _clean_inputs, c=cb, A_ub=A, b_ub=bad2D)
    assert_raises(ValueError, _clean_inputs, c=cb, A_eq=bad3D, b_eq=cb)
    assert_raises(ValueError, _clean_inputs, c=cb, A_eq=A, b_eq=bad2D)


def test_too_few_dimensions():
    bad = np.random.rand(4, 4).ravel()
    cb = np.random.rand(4)
    assert_raises(ValueError, _clean_inputs, c=cb, A_ub=bad, b_ub=cb)
    assert_raises(ValueError, _clean_inputs, c=cb, A_eq=bad, b_eq=cb)


def test_inconsistent_dimensions():
    m = 2
    n = 4
    c = [1, 2, 3, 4]

    Agood = np.random.rand(m, n)
    Abad = np.random.rand(m, n + 1)
    bgood = np.random.rand(m)
    bbad = np.random.rand(m + 1)
    boundsbad = [(0, 1)] * (n + 1)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=Abad, b_ub=bgood)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=Agood, b_ub=bbad)
    assert_raises(ValueError, _clean_inputs, c=c, A_eq=Abad, b_eq=bgood)
    assert_raises(ValueError, _clean_inputs, c=c, A_eq=Agood, b_eq=bbad)
    assert_raises(ValueError, _clean_inputs, c=c, bounds=boundsbad)


def test_type_errors():
    bad = "hello"
    c = [1, 2]
    A_ub = np.array([[1, 1], [2, 2]])
    b_ub = np.array([1, 1])
    A_eq = np.array([[1, 1], [2, 2]])
    b_eq = np.array([1, 1])
    bounds = [(0, 1)]
    assert_raises(
        TypeError,
        _clean_inputs,
        c=bad,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=bad,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=bad,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=bad,
        b_eq=b_eq,
        bounds=bounds)

    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bad)
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds="hi")
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=["hi"])
    assert_raises(
        TypeError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=[
            ("hi")])
    assert_raises(TypeError, _clean_inputs, c=c, A_ub=A_ub,
                  b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=[(1, "")])
    assert_raises(TypeError, _clean_inputs, c=c, A_ub=A_ub,
                  b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=[(1, 2), (1, "")])


def test_non_finite_errors():
    c = [1, 2]
    A_ub = np.array([[1, 1], [2, 2]])
    b_ub = np.array([1, 1])
    A_eq = np.array([[1, 1], [2, 2]])
    b_eq = np.array([1, 1])
    bounds = [(0, 1)]
    assert_raises(
        ValueError, _clean_inputs, c=[0, None], A_ub=A_ub, b_ub=b_ub,
        A_eq=A_eq, b_eq=b_eq, bounds=bounds)
    assert_raises(
        ValueError, _clean_inputs, c=[np.inf, 0], A_ub=A_ub, b_ub=b_ub,
        A_eq=A_eq, b_eq=b_eq, bounds=bounds)
    assert_raises(
        ValueError, _clean_inputs, c=[0, -np.inf], A_ub=A_ub, b_ub=b_ub,
        A_eq=A_eq, b_eq=b_eq, bounds=bounds)
    assert_raises(
        ValueError, _clean_inputs, c=[np.nan, 0], A_ub=A_ub, b_ub=b_ub,
        A_eq=A_eq, b_eq=b_eq, bounds=bounds)

    assert_raises(ValueError, _clean_inputs, c=c, A_ub=[[1, 2], [None, 1]],
                  b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds)
    assert_raises(
        ValueError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=[
            np.inf,
            1],
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_raises(ValueError, _clean_inputs, c=c, A_ub=A_ub, b_ub=b_ub, A_eq=[
                  [1, 2], [1, -np.inf]], b_eq=b_eq, bounds=bounds)
    assert_raises(
        ValueError,
        _clean_inputs,
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=[
            1,
            np.nan],
        bounds=bounds)


def test__clean_inputs1():
    c = [1, 2]
    A_ub = [[1, 1], [2, 2]]
    b_ub = [1, 1]
    A_eq = [[1, 1], [2, 2]]
    b_eq = [1, 1]
    bounds = None
    outputs = _clean_inputs(
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_allclose(outputs[0], np.array(c))
    assert_allclose(outputs[1], np.array(A_ub))
    assert_allclose(outputs[2], np.array(b_ub))
    assert_allclose(outputs[3], np.array(A_eq))
    assert_allclose(outputs[4], np.array(b_eq))
    assert_(outputs[5] == [(0, None)] * 2, "")

    assert_(outputs[0].shape == (2,), "")
    assert_(outputs[1].shape == (2, 2), "")
    assert_(outputs[2].shape == (2,), "")
    assert_(outputs[3].shape == (2, 2), "")
    assert_(outputs[4].shape == (2,), "")


def test__clean_inputs2():
    c = 1
    A_ub = [[1]]
    b_ub = 1
    A_eq = [[1]]
    b_eq = 1
    bounds = (0, 1)
    outputs = _clean_inputs(
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_allclose(outputs[0], np.array(c))
    assert_allclose(outputs[1], np.array(A_ub))
    assert_allclose(outputs[2], np.array(b_ub))
    assert_allclose(outputs[3], np.array(A_eq))
    assert_allclose(outputs[4], np.array(b_eq))
    assert_(outputs[5] == [(0, 1)], "")

    assert_(outputs[0].shape == (1,), "")
    assert_(outputs[1].shape == (1, 1), "")
    assert_(outputs[2].shape == (1,), "")
    assert_(outputs[3].shape == (1, 1), "")
    assert_(outputs[4].shape == (1,), "")


def test__clean_inputs3():
    c = [[1, 2]]
    A_ub = np.random.rand(2, 2)
    b_ub = [[1], [2]]
    A_eq = np.random.rand(2, 2)
    b_eq = [[1], [2]]
    bounds = [(0, 1)]
    outputs = _clean_inputs(
        c=c,
        A_ub=A_ub,
        b_ub=b_ub,
        A_eq=A_eq,
        b_eq=b_eq,
        bounds=bounds)
    assert_allclose(outputs[0], np.array([1, 2]))
    assert_allclose(outputs[2], np.array([1, 2]))
    assert_allclose(outputs[4], np.array([1, 2]))
    assert_(outputs[5] == [(0, 1)] * 2, "")

    assert_(outputs[0].shape == (2,), "")
    assert_(outputs[2].shape == (2,), "")
    assert_(outputs[4].shape == (2,), "")


def test_bad_bounds():
    c = [1, 2]
    assert_raises(ValueError, _clean_inputs, c=c, bounds=(1, -2))
    assert_raises(ValueError, _clean_inputs, c=c, bounds=[(1, -2)])
    assert_raises(ValueError, _clean_inputs, c=c, bounds=[(1, -2), (1, 2)])

    assert_raises(ValueError, _clean_inputs, c=c, bounds=(1, 2, 2))
    assert_raises(ValueError, _clean_inputs, c=c, bounds=[(1, 2, 2)])
    assert_raises(ValueError, _clean_inputs, c=c, bounds=[(1, 2), (1, 2, 2)])
    assert_raises(ValueError, _clean_inputs, c=c,
                  bounds=[(1, 2), (1, 2), (1, 2)])


def test_good_bounds():
    c = [1, 2]
    outputs = _clean_inputs(c=c, bounds=None)
    assert_(outputs[5] == [(0, None)] * 2, "")

    outputs = _clean_inputs(c=c, bounds=(1, 2))
    assert_(outputs[5] == [(1, 2)] * 2, "")

    outputs = _clean_inputs(c=c, bounds=[(1, 2)])
    assert_(outputs[5] == [(1, 2)] * 2, "")

    outputs = _clean_inputs(c=c, bounds=[(1, np.inf)])
    assert_(outputs[5] == [(1, None)] * 2, "")

    outputs = _clean_inputs(c=c, bounds=[(-np.inf, 1)])
    assert_(outputs[5] == [(None, 1)] * 2, "")

    outputs = _clean_inputs(c=c, bounds=[(-np.inf, np.inf), (-np.inf, np.inf)])
    assert_(outputs[5] == [(None, None)] * 2, "")