From 61527ff8526824d8a0ebe10bb14b0b3eabf75e7f Mon Sep 17 00:00:00 2001 From: Dario Izzo Date: Mon, 30 Sep 2024 13:47:58 +0200 Subject: [PATCH] python tests done for cassini and mga --- pykep/test.py | 7 ++++--- pykep/test_trajopt.py | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pykep/test.py b/pykep/test.py index 77d2ec3c..88605aae 100644 --- a/pykep/test.py +++ b/pykep/test.py @@ -8,7 +8,7 @@ ## with this file, You can obtain one at http://mozilla.org/MPL/2.0/. import unittest as _ut -from .test_trajopt import mga_tests +from .test_trajopt import * def float_abs_error(a: float, b: float): return abs(a - b) @@ -313,8 +313,9 @@ def run_test_suite(): suite.addTest(propagate_test("test_stark")) suite.addTest(py_udplas_test("test_tle")) suite.addTest(py_udplas_test("test_spice")) - suite.addTest(mga_tests("test_construction")) - suite.addTest(mga_tests("test_encoding_to_encoding")) + suite.addTest(trajopt_mga_tests("test_construction")) + suite.addTest(trajopt_mga_tests("test_encoding_to_encoding")) + suite.addTest(gym_cassini1_tests("test_fitness")) suite.addTest(tl.loadTestsFromTestCase(vsop2013_test)) diff --git a/pykep/test_trajopt.py b/pykep/test_trajopt.py index 6852efee..3dcc413d 100644 --- a/pykep/test_trajopt.py +++ b/pykep/test_trajopt.py @@ -12,13 +12,10 @@ import unittest as _ut -def float_abs_error(a: float, b: float): - return abs(a - b) - def float_rel_error(a: float, b: float): return abs(a - b) / abs(a) -class mga_tests(_ut.TestCase): +class trajopt_mga_tests(_ut.TestCase): def test_construction(self): import pykep as pk earth = pk.planet(pk.udpla.jpl_lp("earth")) @@ -54,3 +51,20 @@ def test_encoding_to_encoding(self): self.assertTrue(float_rel_error(gt, udp_eta.fitness(x_eta)[0]) < 1e-14) self.assertTrue(float_rel_error(gt, udp_direct.fitness(udp_direct.alpha2direct(x_alpha))[0]) < 1e-14) self.assertTrue(float_rel_error(gt, udp_direct.fitness(udp_eta.eta2direct(x_eta))[0]) < 1e-14) + +class gym_cassini1_tests(_ut.TestCase): + def test_fitness(self): + import pykep as pk + udp = pk.trajopt.gym.cassini1 + # Three random values. Ground truth provided by the old pykep code + x = [-554.5189290104555, 103.27184879471751, 335.41655259663474, 80.50258543604521, 862.0950563689543, 2865.018040480413 ] + f = udp.fitness(x)[0] + self.assertTrue(float_rel_error(f,80400.08897584089) < 1e-14) + + x = [-932.0532394941108, 37.534681289972674, 162.5093144821548, 336.970139545233, 1743.2915882004586, 2527.8785180526465 ] + f = udp.fitness(x)[0] + self.assertTrue(float_rel_error(f,217105.87501757513) < 1e-14) + + x = [-583.0776694390058, 388.65047998036107, 334.9959782156864, 65.57508619540917, 1520.2982946551908, 2132.7771932619144 ] + f = udp.fitness(x)[0] + self.assertTrue(float_rel_error(f,107218.0849582104) < 1e-14) \ No newline at end of file