blob: e5b8d5f9b09d873e62c52b43d04485a77bae36ca [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
brians343bc112013-02-10 01:53:46 +00002
3import numpy
4from numpy.testing import *
brians343bc112013-02-10 01:53:46 +00005import unittest
6
Brian Silverman9c89c0a2016-01-08 01:04:57 -08007import frc971.control_loops.python.polytope as polytope
8
brians343bc112013-02-10 01:53:46 +00009__author__ = 'Austin Schuh (austin.linux@gmail.com)'
10
Ravago Jones26f7ad02021-02-05 15:45:59 -080011
brians343bc112013-02-10 01:53:46 +000012def MakePoint(*args):
Austin Schuh5ea48472021-02-02 20:46:41 -080013 """Makes a point from a set of arguments."""
14 return numpy.matrix([[arg] for arg in args])
brians343bc112013-02-10 01:53:46 +000015
Ravago Jones26f7ad02021-02-05 15:45:59 -080016
brians343bc112013-02-10 01:53:46 +000017class TestHPolytope(unittest.TestCase):
Austin Schuh5ea48472021-02-02 20:46:41 -080018 def setUp(self):
19 """Builds a simple box polytope."""
Ravago Jones26f7ad02021-02-05 15:45:59 -080020 self.H = numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]])
21 self.k = numpy.matrix([[12], [12], [12], [12]])
Austin Schuh5ea48472021-02-02 20:46:41 -080022 self.p = polytope.HPolytope(self.H, self.k)
brians343bc112013-02-10 01:53:46 +000023
Austin Schuh5ea48472021-02-02 20:46:41 -080024 def test_Hk(self):
25 """Tests that H and k are saved correctly."""
26 assert_array_equal(self.p.H, self.H)
27 assert_array_equal(self.p.k, self.k)
brians343bc112013-02-10 01:53:46 +000028
Austin Schuh5ea48472021-02-02 20:46:41 -080029 def test_IsInside(self):
30 """Tests IsInside for various points."""
31 inside_points = [
Ravago Jones26f7ad02021-02-05 15:45:59 -080032 MakePoint(0, 0),
33 MakePoint(6, 6),
34 MakePoint(12, 6),
35 MakePoint(-6, 10)
36 ]
Austin Schuh5ea48472021-02-02 20:46:41 -080037 outside_points = [
Ravago Jones26f7ad02021-02-05 15:45:59 -080038 MakePoint(14, 0),
39 MakePoint(-14, 0),
40 MakePoint(0, 14),
41 MakePoint(0, -14),
42 MakePoint(14, -14)
43 ]
brians343bc112013-02-10 01:53:46 +000044
Austin Schuh5ea48472021-02-02 20:46:41 -080045 for inside_point in inside_points:
Ravago Jones26f7ad02021-02-05 15:45:59 -080046 self.assertTrue(
47 self.p.IsInside(inside_point),
48 msg='Point is' + str(inside_point))
brians343bc112013-02-10 01:53:46 +000049
Austin Schuh5ea48472021-02-02 20:46:41 -080050 for outside_point in outside_points:
Ravago Jones26f7ad02021-02-05 15:45:59 -080051 self.assertFalse(
52 self.p.IsInside(outside_point),
53 msg='Point is' + str(outside_point))
brians343bc112013-02-10 01:53:46 +000054
Austin Schuh5ea48472021-02-02 20:46:41 -080055 def AreVertices(self, p, vertices):
56 """Checks that all the vertices are on corners of the set."""
57 for i in range(vertices.shape[0]):
58 # Check that all the vertices have the correct number of active
59 # constraints.
Ravago Jones26f7ad02021-02-05 15:45:59 -080060 lmda = p.H * vertices[i, :].T - p.k
Austin Schuh5ea48472021-02-02 20:46:41 -080061 num_active_constraints = 0
62 for j in range(lmda.shape[0]):
63 # Verify that the constraints are either active, or not violated.
64 if numpy.abs(lmda[j, 0]) <= 1e-9:
65 num_active_constraints += 1
66 else:
67 self.assertLessEqual(lmda[j, 0], 0.0)
brians343bc112013-02-10 01:53:46 +000068
Austin Schuh5ea48472021-02-02 20:46:41 -080069 self.assertEqual(p.ndim, num_active_constraints)
brians343bc112013-02-10 01:53:46 +000070
Austin Schuh5ea48472021-02-02 20:46:41 -080071 def HasSamePoints(self, expected, actual):
72 """Verifies that the points in expected are in actual."""
73 found_points = set()
74 self.assertEqual(expected.shape, actual.shape)
75 for index in range(expected.shape[0]):
76 expected_point = expected[index, :]
77 for actual_index in range(actual.shape[0]):
78 actual_point = actual[actual_index, :]
79 if numpy.abs(expected_point - actual_point).max() <= 1e-4:
80 found_points.add(actual_index)
81 break
brians343bc112013-02-10 01:53:46 +000082
Ravago Jones26f7ad02021-02-05 15:45:59 -080083 self.assertEqual(
84 len(found_points),
85 actual.shape[0],
Austin Schuh5ea48472021-02-02 20:46:41 -080086 msg="Expected:\n" + str(expected) + "\nActual:\n" + str(actual))
brians343bc112013-02-10 01:53:46 +000087
Austin Schuh5ea48472021-02-02 20:46:41 -080088 def test_Skewed_Nonsym_Vertices(self):
89 """Tests the vertices of a severely skewed space."""
Ravago Jones26f7ad02021-02-05 15:45:59 -080090 self.H = numpy.matrix([[10, -1], [-1, -1], [-1, 10], [10, 10]])
91 self.k = numpy.matrix([[2], [2], [2], [2]])
Austin Schuh5ea48472021-02-02 20:46:41 -080092 self.p = polytope.HPolytope(self.H, self.k)
93 vertices = self.p.Vertices()
94 self.AreVertices(self.p, vertices)
brians343bc112013-02-10 01:53:46 +000095
Austin Schuh5ea48472021-02-02 20:46:41 -080096 self.HasSamePoints(
Ravago Jones26f7ad02021-02-05 15:45:59 -080097 numpy.matrix([[0., 0.2], [0.2, 0.], [-2., 0.], [0., -2.]]),
Austin Schuh5ea48472021-02-02 20:46:41 -080098 vertices)
brians343bc112013-02-10 01:53:46 +000099
Austin Schuh5ea48472021-02-02 20:46:41 -0800100 def test_Vertices_Nonsym(self):
101 """Tests the vertices of a nonsymetric space."""
Ravago Jones26f7ad02021-02-05 15:45:59 -0800102 self.k = numpy.matrix([[6], [12], [2], [10]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800103 self.p = polytope.HPolytope(self.H, self.k)
104 vertices = self.p.Vertices()
105 self.AreVertices(self.p, vertices)
brians343bc112013-02-10 01:53:46 +0000106
Austin Schuh5ea48472021-02-02 20:46:41 -0800107 self.HasSamePoints(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800108 numpy.matrix([[6., 2.], [6., -10.], [-12., -10.], [-12., 2.]]),
Austin Schuh5ea48472021-02-02 20:46:41 -0800109 vertices)
brians343bc112013-02-10 01:53:46 +0000110
Austin Schuh5ea48472021-02-02 20:46:41 -0800111 def test_Vertices(self):
112 """Tests the vertices of a nonsymetric space."""
Ravago Jones26f7ad02021-02-05 15:45:59 -0800113 self.HasSamePoints(
114 self.p.Vertices(),
115 numpy.matrix([[12., 12.], [12., -12.], [-12., -12.], [-12., 12.]]))
brians343bc112013-02-10 01:53:46 +0000116
Austin Schuh5ea48472021-02-02 20:46:41 -0800117 def test_concat(self):
118 """Tests that the concat function works for simple inputs."""
119 self.assertEqual(["asd", "qwe"],
120 list(
121 polytope._PiecewiseConcat(["a", "q"], ["s", "w"],
122 ["d", "e"])))
brians343bc112013-02-10 01:53:46 +0000123
Austin Schuh5ea48472021-02-02 20:46:41 -0800124 def test_str(self):
125 """Verifies that the str method works for the provided p."""
Ravago Jones26f7ad02021-02-05 15:45:59 -0800126 self.assertEqual(
127 '[[ 1 0] [[12] \n'
128 ' [-1 0] [[x0] <= [12] \n'
129 ' [ 0 1] [x1]] [12] \n'
130 ' [ 0 -1]] [12]] ', str(self.p))
brians343bc112013-02-10 01:53:46 +0000131
Austin Schuh5ea48472021-02-02 20:46:41 -0800132 def MakePWithDims(self, num_constraints, num_dims):
133 """Makes a zeroed out polytope with the correct size."""
134 self.p = polytope.HPolytope(
135 numpy.matrix(numpy.zeros((num_constraints, num_dims))),
136 numpy.matrix(numpy.zeros((num_constraints, 1))))
brians343bc112013-02-10 01:53:46 +0000137
Austin Schuh5ea48472021-02-02 20:46:41 -0800138 def test_few_constraints_odd_constraint_even_dims_str(self):
139 """Tests printing out the set with odd constraints and even dimensions."""
140 self.MakePWithDims(num_constraints=5, num_dims=2)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800141 self.assertEqual(
142 '[[0. 0.] [[0.] \n'
143 ' [0. 0.] [[x0] [0.] \n'
144 ' [0. 0.] [x1]] <= [0.] \n'
145 ' [0. 0.] [0.] \n'
146 ' [0. 0.]] [0.]] ', str(self.p))
brians343bc112013-02-10 01:53:46 +0000147
Austin Schuh5ea48472021-02-02 20:46:41 -0800148 def test_few_constraints_odd_constraint_small_dims_str(self):
149 """Tests printing out the set with odd constraints and odd dimensions."""
150 self.MakePWithDims(num_constraints=5, num_dims=1)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800151 self.assertEqual(
152 '[[0.] [[0.] \n'
153 ' [0.] [0.] \n'
154 ' [0.] [[x0]] <= [0.] \n'
155 ' [0.] [0.] \n'
156 ' [0.]] [0.]] ', str(self.p))
brians343bc112013-02-10 01:53:46 +0000157
Austin Schuh5ea48472021-02-02 20:46:41 -0800158 def test_few_constraints_odd_constraint_odd_dims_str(self):
159 """Tests printing out the set with odd constraints and odd dimensions."""
160 self.MakePWithDims(num_constraints=5, num_dims=3)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800161 self.assertEqual(
162 '[[0. 0. 0.] [[0.] \n'
163 ' [0. 0. 0.] [[x0] [0.] \n'
164 ' [0. 0. 0.] [x1] <= [0.] \n'
165 ' [0. 0. 0.] [x2]] [0.] \n'
166 ' [0. 0. 0.]] [0.]] ', str(self.p))
brians343bc112013-02-10 01:53:46 +0000167
Austin Schuh5ea48472021-02-02 20:46:41 -0800168 def test_many_constraints_even_constraint_odd_dims_str(self):
169 """Tests printing out the set with even constraints and odd dimensions."""
170 self.MakePWithDims(num_constraints=2, num_dims=3)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800171 self.assertEqual(
172 '[[0. 0. 0.] [[x0] [[0.] \n'
173 ' [0. 0. 0.]] [x1] <= [0.]] \n'
174 ' [x2]] ', str(self.p))
brians343bc112013-02-10 01:53:46 +0000175
176
177if __name__ == '__main__':
Austin Schuh5ea48472021-02-02 20:46:41 -0800178 unittest.main()