22Unit test of create_trsp_solver()
33"""
44
5+ import numbers
56import warnings
67import unittest
78
9+ import numpy as np
10+
811import ibcdfo
912
1013
@@ -27,7 +30,28 @@ def testWarnings(self):
2730 for each in words :
2831 self .assertTrue (each in str (w [0 ].message ))
2932
30- def testSuccessful (self ):
33+ def test1D (self ):
34+ EPS = np .finfo (float ).eps
35+
36+ # Specify problem
37+ G = np .array ([- 1.1 ])
38+ H = np .atleast_2d ([2.2 ])
39+ Low = np .array ([- 1.9 ])
40+ Upp = np .array ([0.9 ])
41+ self .assertTrue (H [0 ] > 0.0 )
42+
43+ # Known solutions
44+ s_expected = 0.5
45+ f_expected = - 0.275
46+
47+ too_small = np .array ([0.25 ])
48+ s_small = too_small [0 ]
49+ f_small = - 0.20625
50+
51+ too_large = np .array ([0.8 ])
52+ s_large = too_large [0 ]
53+ f_large = - 0.176
54+
3155 for idx in self .__solvers :
3256 # Expected emission of warnings tested in testWarnings. Ignore only those.
3357 warnings .simplefilter ("default" )
@@ -38,3 +62,33 @@ def testSuccessful(self):
3862
3963 solve_trsp = ibcdfo .pounders .create_trsp_solver (idx )
4064 self .assertTrue (callable (solve_trsp ))
65+
66+ # Unconstrained solution in bounds
67+ s_0 , f_0 , was_successful = solve_trsp (H , G , Low , Upp )
68+ self .assertTrue (was_successful )
69+ self .assertTrue (isinstance (s_0 , np .ndarray ))
70+ self .assertEqual (s_0 .ndim , 1 )
71+ self .assertEqual (len (s_0 ), 1 )
72+ self .assertTrue (isinstance (f_0 , numbers .Real ))
73+ self .assertTrue (np .fabs (1.0 - s_0 [0 ] / s_expected ) <= 110.0 * EPS )
74+ self .assertTrue (np .fabs (1.0 - f_0 / f_expected ) <= 110.0 * EPS )
75+
76+ # Unconstrained solution outside bounds
77+ s_0 , f_0 , was_successful = solve_trsp (H , G , Low , too_small )
78+ self .assertTrue (was_successful )
79+ self .assertTrue (isinstance (s_0 , np .ndarray ))
80+ self .assertEqual (s_0 .ndim , 1 )
81+ self .assertEqual (len (s_0 ), 1 )
82+ self .assertTrue (isinstance (f_0 , numbers .Real ))
83+ self .assertTrue (np .fabs (1.0 - s_0 [0 ] / s_small ) <= 110.0 * EPS )
84+ self .assertTrue (np .fabs (1.0 - f_0 / f_small ) <= 110.0 * EPS )
85+
86+ if idx != ibcdfo .pounders .constants .TRSP_SOLVER_SIMPLE :
87+ # The simple sampler requires that Low <= 0 <= Upp
88+ s_0 , f_0 , was_successful = solve_trsp (H , G , too_large , Upp )
89+ self .assertTrue (was_successful )
90+ self .assertTrue (isinstance (s_0 , np .ndarray ))
91+ self .assertEqual (s_0 .ndim , 1 )
92+ self .assertTrue (isinstance (f_0 , numbers .Real ))
93+ self .assertTrue (np .fabs (1.0 - s_0 / s_large ) <= 110.0 * EPS )
94+ self .assertTrue (np .fabs (1.0 - f_0 / f_large ) <= 1500.0 * EPS )
0 commit comments