def check_hist_file(self, optimizer, tol): """ We check the history file here along with the API """ hist = History(self.histFileName, flag="r") # Metadata checks metadata = hist.getMetadata() self.assertEqual(metadata["optimizer"], optimizer) metadata_def_keys = ["optName", "optOptions", "nprocs", "startTime", "endTime", "optTime", "version"] for key in metadata_def_keys: self.assertIn(key, metadata) hist.getOptProb() # Info checks self.assertEqual(hist.getDVNames(), ["xvars"]) self.assertEqual(hist.getConNames(), ["con"]) self.assertEqual(hist.getObjNames(), ["obj"]) dvInfo = hist.getDVInfo() self.assertEqual(len(dvInfo), 1) self.assertEqual(dvInfo["xvars"], hist.getDVInfo(key="xvars")) conInfo = hist.getConInfo() self.assertEqual(len(conInfo), 1) self.assertEqual(conInfo["con"], hist.getConInfo(key="con")) objInfo = hist.getObjInfo() self.assertEqual(len(objInfo), 1) self.assertEqual(objInfo["obj"], hist.getObjInfo(key="obj")) for key in ["lower", "upper", "scale"]: self.assertIn(key, dvInfo["xvars"]) self.assertIn(key, conInfo["con"]) self.assertIn("scale", objInfo["obj"]) # callCounter checks callCounters = hist.getCallCounters() last = hist.read("last") # 'last' key should be present self.assertIn(last, callCounters) # iterKeys checks iterKeys = hist.getIterKeys() for key in ["xuser", "fail", "isMajor"]: self.assertIn(key, iterKeys) # extraFuncsNames checks extraFuncsNames = hist.getExtraFuncsNames() for key in ["extra1", "extra2"]: self.assertIn(key, extraFuncsNames) # getValues checks val = hist.getValues() # this check is only used for optimizers that guarantee '0' and 'last' contain funcs if optimizer in ["SNOPT", "SLSQP", "PSQP"]: val = hist.getValues(callCounters=["0", "last"], stack=True) self.assertEqual(val["isMajor"].size, 2) self.assertTrue(val["isMajor"][0]) # the first callCounter must be a major iteration self.assertTrue(val["isMajor"][-1]) # the last callCounter must be a major iteration # check optimum stored in history file against xstar assert_allclose(val["xuser"][-1], self.xStar1, atol=tol, rtol=tol)
def test_slsqp_scaling_offset_optProb(self): """ Test that scaling and offset works as expected Also test optProb stored in the history file is correct """ test_name = "hs071_SLSQP_scaling_offset" histFileName = "{}.hst".format(test_name) optOptions = {"IFILE": "{}.out".format(test_name)} objScale = 4.2 xScale = [2, 3, 4, 5] conScale = [0.6, 1.7] offset = [1, -2, 40, 2.5] self.optimize( "slsqp", 1e-6, objScale=objScale, xScale=xScale, conScale=conScale, storeHistory=histFileName, offset=offset, optOptions=optOptions, ) # now we retrieve the history file, and check the scale=True option is indeed # scaling things correctly hist = History(histFileName, flag="r") orig_values = hist.getValues(callCounters="0", scale=False) optProb = hist.getOptProb() # check that the scales are stored properly for i, var in enumerate(optProb.variables["xvars"]): assert_allclose(xScale[i], var.scale, atol=1e-12, rtol=1e-12) assert_allclose(offset[i], var.offset, atol=1e-12, rtol=1e-12) for con in optProb.constraints: assert_allclose(conScale, optProb.constraints[con].scale, atol=1e-12, rtol=1e-12) for obj in optProb.objectives: assert_allclose(objScale, optProb.objectives[obj].scale, atol=1e-12, rtol=1e-12) # verify the scale option in getValues scaled_values = hist.getValues(callCounters="0", scale=True, stack=False) x = orig_values["xvars"][0] x_scaled = scaled_values["xvars"][0] assert_allclose(x_scaled, (x - offset) * xScale, atol=1e-12, rtol=1e-12) # now do the same but with stack=True stacked_values = hist.getValues(callCounters="0", scale=True, stack=True) x_scaled = stacked_values["xuser"][0] assert_allclose(x_scaled, (x - offset) * xScale, atol=1e-12, rtol=1e-12) # now we test objective and constraint scaling in getValues obj_orig = orig_values["obj"][0] obj_scaled = scaled_values["obj"][0] assert_allclose(obj_scaled, obj_orig * objScale, atol=1e-12, rtol=1e-12) con_orig = orig_values["con"][0] con_scaled = scaled_values["con"][0] assert_allclose(con_scaled, con_orig * conScale, atol=1e-12, rtol=1e-12)
def check_hist_file(self, tol): """ We check the history file here along with the API """ hist = History(self.histFileName, flag="r") # Metadata checks metadata = hist.getMetadata() self.assertEqual(metadata["optimizer"], self.optName) metadata_def_keys = [ "optName", "optOptions", "nprocs", "startTime", "endTime", "optTime", "version", "optVersion", ] for key in metadata_def_keys: self.assertIn(key, metadata) # we test that SNOPT version is stored correctly if self.optName == "SNOPT" and key == "optVersion": self.assertNotEqual(metadata[key], None) hist.getOptProb() # Info checks self.assertEqual(set(hist.getDVNames()), self.DVs) self.assertEqual(set(hist.getConNames()), self.cons) self.assertEqual(set(hist.getObjNames()), self.objs) dvInfo = hist.getDVInfo() self.assertEqual(len(dvInfo), len(self.DVs)) for var in self.DVs: self.assertEqual(dvInfo[var], hist.getDVInfo(key=var)) conInfo = hist.getConInfo() self.assertEqual(len(conInfo), len(self.cons)) objInfo = hist.getObjInfo() self.assertEqual(len(objInfo), len(self.objs)) for obj in self.objs: self.assertEqual(objInfo[obj], hist.getObjInfo(key=obj)) for key in ["lower", "upper", "scale"]: for dvName in self.DVs: self.assertIn(key, dvInfo[dvName]) for con in self.cons: self.assertIn(key, conInfo[con]) for obj in self.objs: self.assertIn("scale", objInfo[obj]) # callCounter checks callCounters = hist.getCallCounters() last = hist.read("last") # 'last' key should be present self.assertIn(last, callCounters) # iterKey checks iterKeys = hist.getIterKeys() for key in ["xuser", "fail", "isMajor"]: self.assertIn(key, iterKeys) # extraFuncsNames checks extraFuncsNames = hist.getExtraFuncsNames() if hasattr(self, "extras"): for key in self.extras: self.assertIn(key, extraFuncsNames) # getValues checks val = hist.getValues() # timing checks times = hist.getValues(names="time", major=False)["time"] # the times should be monotonically increasing self.assertTrue(np.all(np.diff(times) > 0)) # the final time should be close to the metadata time # we only specify a relatively loose atol because of variations in overhead cost between machines assert_allclose(times[-1], metadata["optTime"], atol=1.0) # this check is only used for optimizers that guarantee '0' and 'last' contain funcs if self.optName in ["SNOPT", "PSQP"]: val = hist.getValues(callCounters=["0", "last"], stack=True) self.assertEqual(val["isMajor"].size, 2) self.assertTrue( val["isMajor"] [0]) # the first callCounter must be a major iteration self.assertTrue( val["isMajor"] [-1]) # the last callCounter must be a major iteration # check optimum stored in history file against xstar val = hist.getValues(callCounters="last", stack=False) for varName in self.DVs: assert_allclose(val[varName].flatten(), self.xStar[self.sol_index][varName], atol=tol, rtol=tol)