Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def check_hist_file(self, optimizer):
        """
        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)

        # 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)

        # iterKey checks
        iterKeys = hist.getIterKeys()
        for key in ['xuser', 'fail', 'isMajor']:
            self.assertIn(key, iterKeys)

        # getValues check
        hist.getValues()
        hist.getValues(stack=True, major=False, scale=True)
        # 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'])
            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
Ejemplo n.º 3
0
    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)