@@ -117,24 +117,28 @@ def test_snopt_setDVFromHist(self):
117117 # since we restarted from the optimum
118118 self .assertEqual (second ["xvars" ].shape , (1 , 4 ))
119119
120- def test_slsqp_scaling_offset_optProb (self ):
120+ @parameterized .expand (["SLSQP" , "IPOPT" ])
121+ def test_scaling_offset_optProb (self , optName ):
121122 """
122123 Test that scaling and offset works as expected
123124 Also test optProb stored in the history file is correct
124125 """
125- self .optName = "SLSQP"
126- histFileName = "hs071_SLSQP_scaling_offset .hst"
126+ self .optName = optName
127+ histFileName = f"hs071_ { optName } _scaling_offset .hst"
127128 objScale = 4.2
128129 xScale = [2 , 3 , 4 , 5 ]
129130 conScale = [0.6 , 1.7 ]
130131 offset = [1 , - 2 , 40 , 2.5 ]
131132 self .setup_optProb (objScale = objScale , xScale = xScale , conScale = conScale , offset = offset )
132133 sol = self .optimize (storeHistory = histFileName )
133- self .assert_solution_allclose (sol , self .tol ["SLSQP" ])
134+ lambda_sign = - 1.0 if optName == "IPOPT" else 1.0
135+ self .assert_solution_allclose (sol , self .tol [optName ], lambda_sign = lambda_sign )
134136 # now we retrieve the history file, and check the scale=True option is indeed
135137 # scaling things correctly
138+ # IPOPT calls gradient and jacobian at first, so set callCounter = 2 for the first non-derivative call
139+ callCounter = "2" if optName == "IPOPT" else "0"
136140 hist = History (histFileName , flag = "r" )
137- orig_values = hist .getValues (callCounters = "0" , scale = False )
141+ orig_values = hist .getValues (callCounters = callCounter , scale = False )
138142 optProb = hist .getOptProb ()
139143
140144 # check that the scales are stored properly
@@ -147,13 +151,13 @@ def test_slsqp_scaling_offset_optProb(self):
147151 assert_allclose (objScale , optProb .objectives [obj ].scale , atol = 1e-12 , rtol = 1e-12 )
148152
149153 # verify the scale option in getValues
150- scaled_values = hist .getValues (callCounters = "0" , scale = True , stack = False )
154+ scaled_values = hist .getValues (callCounters = callCounter , scale = True , stack = False )
151155 x = orig_values ["xvars" ][0 ]
152156 x_scaled = scaled_values ["xvars" ][0 ]
153157 assert_allclose (x_scaled , (x - offset ) * xScale , atol = 1e-12 , rtol = 1e-12 )
154158
155159 # now do the same but with stack=True
156- stacked_values = hist .getValues (callCounters = "0" , scale = True , stack = True )
160+ stacked_values = hist .getValues (callCounters = callCounter , scale = True , stack = True )
157161 x_scaled = stacked_values ["xuser" ][0 ]
158162 assert_allclose (x_scaled , (x - offset ) * xScale , atol = 1e-12 , rtol = 1e-12 )
159163
0 commit comments