blob: 9a35ebeece2efb69bba12540fa0109799cd6d0c2 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#!/usr/bin/python
2
3import numpy
4from numpy.testing import *
5import polytope
6import unittest
7
8__author__ = 'Austin Schuh (austin.linux@gmail.com)'
9
10def MakePoint(*args):
11 """Makes a point from a set of arguments."""
12 return numpy.matrix([[arg] for arg in args])
13
14class TestHPolytope(unittest.TestCase):
15 def setUp(self):
16 """Builds a simple box polytope."""
17 self.H = numpy.matrix([[1, 0],
18 [-1, 0],
19 [0, 1],
20 [0, -1]])
21 self.k = numpy.matrix([[12],
22 [12],
23 [12],
24 [12]])
25 self.p = polytope.HPolytope(self.H, self.k)
26
27 def test_Hk(self):
28 """Tests that H and k are saved correctly."""
29 assert_array_equal(self.p.H, self.H)
30 assert_array_equal(self.p.k, self.k)
31
32 def test_IsInside(self):
33 """Tests IsInside for various points."""
34 inside_points = [
35 MakePoint(0, 0),
36 MakePoint(6, 6),
37 MakePoint(12, 6),
38 MakePoint(-6, 10)]
39 outside_points = [
40 MakePoint(14, 0),
41 MakePoint(-14, 0),
42 MakePoint(0, 14),
43 MakePoint(0, -14),
44 MakePoint(14, -14)]
45
46 for inside_point in inside_points:
47 self.assertTrue(self.p.IsInside(inside_point),
48 msg='Point is' + str(inside_point))
49
50 for outside_point in outside_points:
51 self.assertFalse(self.p.IsInside(outside_point),
52 msg='Point is' + str(outside_point))
53
54 def AreVertices(self, p, vertices):
55 """Checks that all the vertices are on corners of the set."""
56 for i in xrange(vertices.shape[0]):
57 # Check that all the vertices have the correct number of active
58 # constraints.
59 lmda = p.H * vertices[i,:].T - p.k
60 num_active_constraints = 0
61 for j in xrange(lmda.shape[0]):
62 # Verify that the constraints are either active, or not violated.
63 if numpy.abs(lmda[j, 0]) <= 1e-9:
64 num_active_constraints += 1
65 else:
66 self.assertLessEqual(lmda[j, 0], 0.0)
67
68 self.assertEqual(p.ndim, num_active_constraints)
69
70 def HasSamePoints(self, expected, actual):
71 """Verifies that the points in expected are in actual."""
72 found_points = set()
73 self.assertEqual(expected.shape, actual.shape)
74 for index in xrange(expected.shape[0]):
75 expected_point = expected[index, :]
76 for actual_index in xrange(actual.shape[0]):
77 actual_point = actual[actual_index, :]
78 if numpy.abs(expected_point - actual_point).max() <= 1e-4:
79 found_points.add(actual_index)
80 break
81
82 self.assertEqual(len(found_points), actual.shape[0],
83 msg="Expected:\n" + str(expected) + "\nActual:\n" + str(actual))
84
85 def test_Skewed_Nonsym_Vertices(self):
86 """Tests the vertices of a severely skewed space."""
87 self.H = numpy.matrix([[10, -1],
88 [-1, -1],
89 [-1, 10],
90 [10, 10]])
91 self.k = numpy.matrix([[2],
92 [2],
93 [2],
94 [2]])
95 self.p = polytope.HPolytope(self.H, self.k)
96 vertices = self.p.Vertices()
97 self.AreVertices(self.p, vertices)
98
99 self.HasSamePoints(
100 numpy.matrix([[0., 0.2],
101 [0.2, 0.],
102 [-2., 0.],
103 [0., -2.]]),
104 vertices)
105
106 def test_Vertices_Nonsym(self):
107 """Tests the vertices of a nonsymetric space."""
108 self.k = numpy.matrix([[6],
109 [12],
110 [2],
111 [10]])
112 self.p = polytope.HPolytope(self.H, self.k)
113 vertices = self.p.Vertices()
114 self.AreVertices(self.p, vertices)
115
116 self.HasSamePoints(
117 numpy.matrix([[6., 2.],
118 [6., -10.],
119 [-12., -10.],
120 [-12., 2.]]),
121 vertices)
122
123 def test_Vertices(self):
124 """Tests the vertices of a nonsymetric space."""
125 self.HasSamePoints(self.p.Vertices(),
126 numpy.matrix([[12., 12.],
127 [12., -12.],
128 [-12., -12.],
129 [-12., 12.]]))
130
131 def test_concat(self):
132 """Tests that the concat function works for simple inputs."""
133 self.assertEqual(["asd", "qwe"],
134 polytope._PiecewiseConcat(["a", "q"],
135 ["s", "w"],
136 ["d", "e"]))
137
138 def test_str(self):
139 """Verifies that the str method works for the provided p."""
140 self.assertEqual('[[ 1 0] [[12] \n'
141 ' [-1 0] [[x0] <= [12] \n'
142 ' [ 0 1] [x1]] [12] \n'
143 ' [ 0 -1]] [12]] ',
144 str(self.p))
145
146 def MakePWithDims(self, num_constraints, num_dims):
147 """Makes a zeroed out polytope with the correct size."""
148 self.p = polytope.HPolytope(
149 numpy.matrix(numpy.zeros((num_constraints, num_dims))),
150 numpy.matrix(numpy.zeros((num_constraints, 1))))
151
152 def test_few_constraints_odd_constraint_even_dims_str(self):
153 """Tests printing out the set with odd constraints and even dimensions."""
154 self.MakePWithDims(num_constraints=5, num_dims=2)
155 self.assertEqual('[[ 0. 0.] [[ 0.] \n'
156 ' [ 0. 0.] [[x0] [ 0.] \n'
157 ' [ 0. 0.] [x1]] <= [ 0.] \n'
158 ' [ 0. 0.] [ 0.] \n'
159 ' [ 0. 0.]] [ 0.]] ',
160 str(self.p))
161
162 def test_few_constraints_odd_constraint_small_dims_str(self):
163 """Tests printing out the set with odd constraints and odd dimensions."""
164 self.MakePWithDims(num_constraints=5, num_dims=1)
165 self.assertEqual('[[ 0.] [[ 0.] \n'
166 ' [ 0.] [ 0.] \n'
167 ' [ 0.] [[x0]] <= [ 0.] \n'
168 ' [ 0.] [ 0.] \n'
169 ' [ 0.]] [ 0.]] ',
170 str(self.p))
171
172 def test_few_constraints_odd_constraint_odd_dims_str(self):
173 """Tests printing out the set with odd constraints and odd dimensions."""
174 self.MakePWithDims(num_constraints=5, num_dims=3)
175 self.assertEqual('[[ 0. 0. 0.] [[ 0.] \n'
176 ' [ 0. 0. 0.] [[x0] [ 0.] \n'
177 ' [ 0. 0. 0.] [x1] <= [ 0.] \n'
178 ' [ 0. 0. 0.] [x2]] [ 0.] \n'
179 ' [ 0. 0. 0.]] [ 0.]] ',
180 str(self.p))
181
182 def test_many_constraints_even_constraint_odd_dims_str(self):
183 """Tests printing out the set with even constraints and odd dimensions."""
184 self.MakePWithDims(num_constraints=2, num_dims=3)
185 self.assertEqual('[[ 0. 0. 0.] [[x0] [[ 0.] \n'
186 ' [ 0. 0. 0.]] [x1] <= [ 0.]] \n'
187 ' [x2]] ',
188 str(self.p))
189
190
191if __name__ == '__main__':
192 unittest.main()