File: test_trustregion_krylov.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 (173 lines) | stat: -rw-r--r-- 6,727 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
"""
Unit tests for Krylov space trust-region subproblem solver.

To run it in its simplest form::
  nosetests test_optimize.py

"""
from __future__ import division, print_function, absolute_import

import numpy as np
from scipy.optimize._trlib import (get_trlib_quadratic_subproblem)
from numpy.testing import (assert_, assert_array_equal,
                           assert_almost_equal,
                           assert_equal, assert_array_almost_equal,
                           assert_array_less)

KrylovQP = get_trlib_quadratic_subproblem(tol_rel_i=1e-8, tol_rel_b=1e-6)
KrylovQP_disp = get_trlib_quadratic_subproblem(tol_rel_i=1e-8, tol_rel_b=1e-6, disp=True)

class TestKrylovQuadraticSubproblem(object):

    def test_for_the_easy_case(self):

        # `H` is chosen such that `g` is not orthogonal to the
        # eigenvector associated with the smallest eigenvalue.
        H = np.array([[1.0, 0.0, 4.0],
                      [0.0, 2.0, 0.0],
                      [4.0, 0.0, 3.0]])
        g = np.array([5.0, 0.0, 4.0])

        # Trust Radius
        trust_radius = 1.0

        # Solve Subproblem
        subprob = KrylovQP(x=0,
                           fun=lambda x: 0,
                           jac=lambda x: g,
                           hess=lambda x: None,
                           hessp=lambda x, y: H.dot(y))
        p, hits_boundary = subprob.solve(trust_radius)

        assert_array_almost_equal(p, np.array([-1.0, 0.0, 0.0]))
        assert_equal(hits_boundary, True)
        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)
        # check trust region constraint
        assert_almost_equal(np.linalg.norm(p), trust_radius)

        trust_radius = 0.5
        p, hits_boundary = subprob.solve(trust_radius)

        assert_array_almost_equal(p,
                np.array([-0.46125446, 0., -0.19298788]))
        assert_equal(hits_boundary, True)
        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)
        # check trust region constraint
        assert_almost_equal(np.linalg.norm(p), trust_radius)

    def test_for_the_hard_case(self):

        # `H` is chosen such that `g` is orthogonal to the
        # eigenvector associated with the smallest eigenvalue.
        H = np.array([[1.0, 0.0, 4.0],
                      [0.0, 2.0, 0.0],
                      [4.0, 0.0, 3.0]])
        g = np.array([0.0, 2.0, 0.0])

        # Trust Radius
        trust_radius = 1.0

        # Solve Subproblem
        subprob = KrylovQP(x=0,
                           fun=lambda x: 0,
                           jac=lambda x: g,
                           hess=lambda x: None,
                           hessp=lambda x, y: H.dot(y))
        p, hits_boundary = subprob.solve(trust_radius)

        assert_array_almost_equal(p, np.array([0.0, -1.0, 0.0]))
        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)
        # check trust region constraint
        assert_almost_equal(np.linalg.norm(p), trust_radius)

        trust_radius = 0.5
        p, hits_boundary = subprob.solve(trust_radius)

        assert_array_almost_equal(p, np.array([0.0, -0.5, 0.0]))
        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)
        # check trust region constraint
        assert_almost_equal(np.linalg.norm(p), trust_radius)

    def test_for_interior_convergence(self):

        H = np.array([[1.812159, 0.82687265, 0.21838879, -0.52487006, 0.25436988],
                      [0.82687265, 2.66380283, 0.31508988, -0.40144163, 0.08811588],
                      [0.21838879, 0.31508988, 2.38020726, -0.3166346, 0.27363867],
                      [-0.52487006, -0.40144163, -0.3166346, 1.61927182, -0.42140166],
                      [0.25436988, 0.08811588, 0.27363867, -0.42140166, 1.33243101]])
        g = np.array([0.75798952, 0.01421945, 0.33847612, 0.83725004, -0.47909534])
        trust_radius = 1.1

        # Solve Subproblem
        subprob = KrylovQP(x=0,
                           fun=lambda x: 0,
                           jac=lambda x: g,
                           hess=lambda x: None,
                           hessp=lambda x, y: H.dot(y))
        p, hits_boundary = subprob.solve(trust_radius)

        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)

        assert_array_almost_equal(p, [-0.68585435, 0.1222621, -0.22090999,
                                      -0.67005053, 0.31586769])
        assert_array_almost_equal(hits_boundary, False)

    def test_for_very_close_to_zero(self):

        H = np.array([[0.88547534, 2.90692271, 0.98440885, -0.78911503, -0.28035809],
                      [2.90692271, -0.04618819, 0.32867263, -0.83737945, 0.17116396],
                      [0.98440885, 0.32867263, -0.87355957, -0.06521957, -1.43030957],
                      [-0.78911503, -0.83737945, -0.06521957, -1.645709, -0.33887298],
                      [-0.28035809, 0.17116396, -1.43030957, -0.33887298, -1.68586978]])
        g = np.array([0, 0, 0, 0, 1e-6])
        trust_radius = 1.1

        # Solve Subproblem
        subprob = KrylovQP(x=0,
                           fun=lambda x: 0,
                           jac=lambda x: g,
                           hess=lambda x: None,
                           hessp=lambda x, y: H.dot(y))
        p, hits_boundary = subprob.solve(trust_radius)

        # check kkt satisfaction
        assert_almost_equal(
                np.linalg.norm(H.dot(p) + subprob.lam * p + g),
                0.0)
        # check trust region constraint
        assert_almost_equal(np.linalg.norm(p), trust_radius)

        assert_array_almost_equal(p, [0.06910534, -0.01432721,
                                      -0.65311947, -0.23815972,
                                      -0.84954934])
        assert_array_almost_equal(hits_boundary, True)

    def test_disp(self, capsys):
        H = -np.eye(5)
        g = np.array([0, 0, 0, 0, 1e-6])
        trust_radius = 1.1

        subprob = KrylovQP_disp(x=0,
                                fun=lambda x: 0,
                                jac=lambda x: g,
                                hess=lambda x: None,
                                hessp=lambda x, y: H.dot(y))
        p, hits_boundary = subprob.solve(trust_radius)
        out, err = capsys.readouterr()
        assert_(out.startswith(' TR Solving trust region problem'), repr(out))