@@ -12,9 +12,14 @@ class Square (roboptim.core.PyDifferentiableFunction):
1212 def __init__ (self ):
1313 roboptim .core .PyDifferentiableFunction .__init__ \
1414 (self , 1 , 1 , "square function" )
15+ self .compute_counter = 0
16+
17+ def reset (self ):
18+ self .compute_counter = 0
1519
1620 def impl_compute (self , result , x ):
1721 result [0 ] = x [0 ] * x [0 ]
22+ self .compute_counter += 1
1823
1924 def impl_gradient (self , result , x , f_id ):
2025 raise NotImplementedError
@@ -33,16 +38,47 @@ def impl_gradient (self, result, x, functionId):
3338
3439class TestFiniteDifferences (unittest .TestCase ):
3540
36- def test_jacobian (self ):
41+ def test_jacobian_simple (self ):
42+
43+ fd_rule = roboptim .core .FiniteDifferenceRule .SIMPLE
44+ square = Square ()
45+ f = roboptim .core .PyFiniteDifference (square , rule = fd_rule )
46+
47+ x = numpy .array ([4. ])
48+ res = f (x )
49+ numpy .testing .assert_almost_equal (res , [x [0 ] * x [0 ]], 5 )
50+ assert square .compute_counter == 1
51+ square .reset ()
52+
53+ grad = f .gradient (x , 0 )
54+ numpy .testing .assert_almost_equal (grad , [2. * x [0 ]], 5 )
55+ assert square .compute_counter == 2
56+ square .reset ()
57+
58+ jac = f .jacobian (x )
59+ numpy .testing .assert_almost_equal (jac , [[2. * x [0 ]]], 5 )
60+ assert square .compute_counter == 2
61+
62+ def test_jacobian_fivepoints (self ):
3763
3864 fd_rule = roboptim .core .FiniteDifferenceRule .FIVE_POINTS
39- f = roboptim .core .PyFiniteDifference (Square (), rule = fd_rule )
65+ square = Square ()
66+ f = roboptim .core .PyFiniteDifference (square , rule = fd_rule )
4067
4168 x = numpy .array ([4. ])
4269 res = f (x )
4370 numpy .testing .assert_almost_equal (res , [x [0 ] * x [0 ]], 5 )
71+ assert square .compute_counter == 1
72+ square .reset ()
73+
74+ grad = f .gradient (x , 0 )
75+ numpy .testing .assert_almost_equal (grad , [2. * x [0 ]], 5 )
76+ assert square .compute_counter == 4
77+ square .reset ()
78+
4479 jac = f .jacobian (x )
4580 numpy .testing .assert_almost_equal (jac , [[2. * x [0 ]]], 5 )
81+ assert square .compute_counter == 4
4682
4783 def test_problem_1 (self ):
4884 """
@@ -58,17 +94,13 @@ def test_problem_1(self):
5894 # Check starting value
5995 self .assertEqual (cost (problem .startingPoint ), 909 )
6096
61- # Let the test fail if the solver does not exist.
62- try :
63- solver = roboptim .core .PySolver ("ipopt" , problem )
64- print (solver )
65- solver .solve ()
66- r = solver .minimum ()
67- print (r )
68- numpy .testing .assert_almost_equal (r .value , [0. ], 5 )
69- numpy .testing .assert_almost_equal (r .x , [1. , 1. ], 5 )
70- except Exception as e :
71- print ("Error: %s" % e )
97+ solver = roboptim .core .PySolver ("ipopt" , problem )
98+ print (solver )
99+ solver .solve ()
100+ r = solver .minimum ()
101+ print (r )
102+ numpy .testing .assert_almost_equal (r .value , [0. ], 5 )
103+ numpy .testing .assert_almost_equal (r .x , [1. , 1. ], 5 )
72104
73105if __name__ == '__main__' :
74106 unittest .main ()
0 commit comments