diff --git a/open-codegen/test/test.py b/open-codegen/test/test.py index 2b55ad89..21a7d13f 100644 --- a/open-codegen/test/test.py +++ b/open-codegen/test/test.py @@ -4,6 +4,7 @@ import opengen as og import subprocess import logging +import numpy as np class RustBuildTestCase(unittest.TestCase): @@ -527,6 +528,26 @@ def test_tcp_manager_remote_port_no_ip(self): with self.assertRaises(Exception) as __context: _remote_tcp_manager = og.tcp.OptimizerTcpManager(port=8888) + def test_set_y(self): + c = og.constraints.Ball2(radius=1) + y_calc = og.builder.SetYCalculator(c) + y = y_calc.obtain() + + def test_squared_norm(self): + u = np.array([3, 4]) + y = og.functions.norm2_squared(u) + self.assertAlmostEqual(25., y, places=12) + + u = [3, 4] + y = og.functions.norm2_squared(u) + self.assertAlmostEqual(25., y, places=12) + + u = cs.SX.sym("u", 2) + f = og.functions.norm2_squared(u) + fun = cs.Function('fun', [u], [f]) + y = fun([3, 4]) + self.assertAlmostEqual(25., y, places=12) + if __name__ == '__main__': logging.getLogger('retry').setLevel(logging.ERROR) diff --git a/open-codegen/test/test_constraints.py b/open-codegen/test/test_constraints.py index 4421cbd8..8cff3f6c 100644 --- a/open-codegen/test/test_constraints.py +++ b/open-codegen/test/test_constraints.py @@ -315,6 +315,8 @@ def test_cartesian_sx(self): _sqd_sx = cartesian.distance_squared(u_sx) u_mx = cs.SX.sym("u", 9, 1) _sqd_mx = cartesian.distance_squared(u_mx) + self.assertEqual(2, cartesian.segment_dimension(0)) + self.assertEqual(3, cartesian.segment_dimension(1)) def test_cartesian_segments_not_increasing(self): no_constraints = og.constraints.NoConstraints() @@ -343,6 +345,24 @@ def test_cartesian_segments_empty_args(self): with self.assertRaises(ValueError) as __context: og.constraints.CartesianProduct([], sets) + def test_cartesian_convex(self): + ball_inf = og.constraints.BallInf(None, 1) + ball_eucl = og.constraints.Ball2(None, 1) + cartesian = og.constraints.CartesianProduct( + [5, 10], [ball_inf, ball_eucl]) + self.assertTrue(cartesian.is_convex()) + self.assertTrue(cartesian.is_compact()) + + finite_set = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]]) + cartesian = og.constraints.CartesianProduct( + [5, 10, 13], [ball_inf, ball_eucl, finite_set]) + self.assertFalse(cartesian.is_convex()) + + free = og.constraints.NoConstraints() + cartesian = og.constraints.CartesianProduct( + [5, 10, 11], [ball_inf, ball_eucl, free]) + self.assertFalse(cartesian.is_compact()) + # ----------------------------------------------------------------------- # Finite Set # ----------------------------------------------------------------------- @@ -375,6 +395,10 @@ def test_finite_set_compact(self): c = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]]) self.assertTrue(c.is_compact()) + def test_finite_set_dimension(self): + c = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]]) + self.assertEqual(3, c.dimension()) + # ----------------------------------------------------------------------- # Halfspaces # ----------------------------------------------------------------------- @@ -513,6 +537,12 @@ def test_sphere2_sq_distance_symbolic(self): z = fun([1, 1, 0, 0]) self.assertAlmostEqual(0.835786437626905, z, places=12) + def test_sphere2_no_center(self): + sphere = og.constraints.Sphere2(radius=0.5) + u = [0, 0, 0, 0] + dist = sphere.distance_squared(u) + self.assertAlmostEqual(0.25, dist, places=12) + if __name__ == '__main__': unittest.main()