Пример #1
0
    def test_ego_mixed_integer_full_gaussian(self):
        n_iter = 15
        xtypes = [FLOAT, (ENUM, 3), (ENUM, 2), ORD]
        xlimits = np.array([[-5, 5], ["blue", "red", "green"],
                            ["large", "small"], [0, 2]])
        n_doe = 2
        sampling = MixedIntegerSamplingMethod(
            xtypes,
            xlimits,
            LHS,
            criterion="ese",
            random_state=42,
            output_in_folded_space=True,
        )
        xdoe = sampling(n_doe)
        criterion = "EI"  #'EI' or 'SBO' or 'LCB'
        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)

        ego = EGO(
            n_iter=n_iter,
            criterion=criterion,
            xdoe=xdoe,
            xtypes=xtypes,
            xlimits=xlimits,
            surrogate=sm,
            enable_tunneling=False,
            random_state=42,
            categorical_kernel=FULL_GAUSSIAN,
        )
        _, y_opt, _, _, _ = ego.optimize(
            fun=TestEGO.function_test_mixed_integer)
Пример #2
0
    def _setup_optimizer(self, fun):
        """
        Instanciate internal surrogate used for optimization
        and setup function evaluator wrt options

        Parameters
        ----------

        fun: function to optimize: ndarray[n, nx] or ndarray[n] -> ndarray[n, 1]

        Returns
        -------

        ndarray: initial coord-x doe
        ndarray: initial coord-y doe = fun(xdoe)

        """
        # Set the model
        self.gpr = self.options["surrogate"]
        self.xlimits = self.options["xlimits"]

        # Handle mixed integer optimization
        xtypes = self.options["xtypes"]
        if xtypes:
            self.mixint = MixedIntegerContext(xtypes,
                                              self.xlimits,
                                              work_in_folded_space=False)
            self.gpr = self.mixint.build_surrogate_model(self.gpr)
            self._sampling = self.mixint.build_sampling_method(
                LHS,
                criterion="ese",
                random_state=self.options["random_state"])
        else:
            self.mixint = None
            self._sampling = LHS(
                xlimits=self.xlimits,
                criterion="ese",
                random_state=self.options["random_state"],
            )

        # Build DOE
        self._evaluator = self.options["evaluator"]
        xdoe = self.options["xdoe"]
        if xdoe is None:
            self.log("Build initial DOE with LHS")
            n_doe = self.options["n_doe"]
            x_doe = self._sampling(n_doe)
        else:
            self.log("Initial DOE given")
            x_doe = np.atleast_2d(xdoe)
            if self.mixint:
                x_doe = self.mixint.unfold_with_enum_mask(x_doe)

        ydoe = self.options["ydoe"]
        if ydoe is None:
            y_doe = self._evaluator.run(fun, x_doe)
        else:  # to save time if y_doe is already given to EGO
            y_doe = ydoe

        return x_doe, y_doe
Пример #3
0
    def test_branin_2D_mixed(self):
        n_iter = 20
        fun = Branin(ndim=2)
        xtypes = [ORD, FLOAT]
        xlimits = fun.xlimits
        criterion = "EI"  #'EI' or 'SBO' or 'LCB'

        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)
        sampling = MixedIntegerSamplingMethod(xtypes, xlimits, FullFactorial)
        xdoe = sampling(10)

        ego = EGO(
            xdoe=xdoe,
            n_iter=n_iter,
            criterion=criterion,
            xtypes=xtypes,
            xlimits=xlimits,
            surrogate=sm,
            random_state=42,
        )

        x_opt, y_opt, _, _, _ = ego.optimize(fun=fun)
        # 3 optimal points possible: [-pi, 12.275], [pi, 2.275], [9.42478, 2.475]
        self.assertTrue(
            np.allclose([[-3, 12.275]], x_opt, rtol=0.2)
            or np.allclose([[3, 2.275]], x_opt, rtol=0.2)
            or np.allclose([[9, 2.475]], x_opt, rtol=0.2))
        self.assertAlmostEqual(0.494, float(y_opt), delta=1)
Пример #4
0
    def test_ego_mixed_integer(self):
        n_iter = 15
        xtypes = [FLOAT, (ENUM, 3), (ENUM, 2), ORD]
        xlimits = np.array([[-5, 5], ["blue", "red", "green"],
                            ["large", "small"], ["0", "2", "3"]])
        n_doe = 2
        sampling = MixedIntegerSamplingMethod(xtypes,
                                              xlimits,
                                              LHS,
                                              criterion="ese",
                                              random_state=42)
        xdoe = sampling(n_doe)
        criterion = "EI"  #'EI' or 'SBO' or 'LCB'
        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)

        ego = EGO(
            n_iter=n_iter,
            criterion=criterion,
            xdoe=xdoe,
            xtypes=xtypes,
            xlimits=xlimits,
            surrogate=sm,
            enable_tunneling=False,
            random_state=42,
        )
        _, y_opt, _, _, _ = ego.optimize(
            fun=TestEGO.function_test_mixed_integer)

        self.assertAlmostEqual(-15, float(y_opt), delta=5)
Пример #5
0
    def test_branin_2D_mixed_parallel(self):
        n_parallel = 5
        n_iter = 20
        fun = Branin(ndim=2)
        xlimits = fun.xlimits
        criterion = "EI"  #'EI' or 'SBO' or 'UCB'
        qEI = "KB"
        xtypes = [INT, FLOAT]

        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)
        sampling = mixint.build_sampling_method(FullFactorial)
        xdoe = sampling(10)

        ego = EGO(
            xdoe=xdoe,
            n_iter=n_iter,
            criterion=criterion,
            xtypes=[INT, FLOAT],
            xlimits=xlimits,
            n_parallel=n_parallel,
            qEI=qEI,
            evaluator=ParallelEvaluator(),
            surrogate=sm,
            random_state=42,
        )

        x_opt, y_opt, _, _, _ = ego.optimize(fun=fun)
        # 3 optimal points possible: [-pi, 12.275], [pi, 2.275], [9.42478, 2.475]
        self.assertTrue(
            np.allclose([[-3, 12.275]], x_opt, rtol=0.2)
            or np.allclose([[3, 2.275]], x_opt, rtol=0.2)
            or np.allclose([[9, 2.475]], x_opt, rtol=0.2))
        self.assertAlmostEqual(0.494, float(y_opt), delta=1)
Пример #6
0
 def test_krg_mixed_3D_bad_regr(self):
     xtypes = [FLOAT, (ENUM, 3), ORD]
     xlimits = [[-10, 10], ["blue", "red", "green"], [-10, 10]]
     mixint = MixedIntegerContext(xtypes, xlimits)
     with self.assertRaises(ValueError):
         sm = mixint.build_surrogate_model(
             KRG(print_prediction=False, poly="linear")
         )
Пример #7
0
    def run_mixed_integer_context_example(self):
        import numpy as np
        import matplotlib.pyplot as plt
        from matplotlib import colors
        from mpl_toolkits.mplot3d import Axes3D

        from smt.surrogate_models import KRG
        from smt.sampling_methods import LHS, Random
        from smt.applications.mixed_integer import MixedIntegerContext, FLOAT, INT, ENUM

        xtypes = [INT, FLOAT, (ENUM, 4)]
        xlimits = [[0, 5], [0.0, 4.0], ["blue", "red", "green", "yellow"]]

        def ftest(x):
            return (x[:, 0] * x[:, 0] + x[:, 1] * x[:, 1]) * (x[:, 2] + 1)

        # context to create consistent DOEs and surrogate
        mixint = MixedIntegerContext(xtypes, xlimits)

        # DOE for training
        lhs = mixint.build_sampling_method(LHS, criterion="ese")

        num = mixint.get_unfolded_dimension() * 5
        print("DOE point nb = {}".format(num))
        xt = lhs(num)
        yt = ftest(xt)

        # Surrogate
        sm = mixint.build_surrogate_model(KRG())
        print(xt)
        sm.set_training_values(xt, yt)
        sm.train()

        # DOE for validation
        rand = mixint.build_sampling_method(Random)
        xv = rand(50)
        yv = ftest(xv)
        yp = sm.predict_values(xv)

        plt.plot(yv, yv)
        plt.plot(yv, yp, "o")
        plt.xlabel("actual")
        plt.ylabel("prediction")

        plt.show()
Пример #8
0
    def test_qp_mixed_2D_INT(self):
        xtypes = [FLOAT, INT]
        xlimits = [[-10, 10], [-10, 10]]
        mixint = MixedIntegerContext(xtypes, xlimits)

        sm = mixint.build_surrogate_model(QP(print_prediction=False))
        sampling = mixint.build_sampling_method(LHS, criterion="m")

        fun = Sphere(ndim=2)
        xt = sampling(10)
        yt = fun(xt)
        sm.set_training_values(xt, yt)
        sm.train()

        eq_check = True
        for i in range(xt.shape[0]):
            if abs(float(xt[i, :][1]) - int(float(xt[i, :][1]))) > 10e-8:
                eq_check = False
        self.assertTrue(eq_check)
Пример #9
0
    def test_krg_mixed_3D(self):
        xtypes = [FLOAT, (ENUM, 3), INT]
        xlimits = [[-10, 10], ["blue", "red", "green"], [-10, 10]]
        mixint = MixedIntegerContext(xtypes, xlimits)

        sm = mixint.build_surrogate_model(KRG(print_prediction=False))
        sampling = mixint.build_sampling_method(LHS, criterion="m")

        fun = Sphere(ndim=3)
        xt = sampling(20)
        yt = fun(xt)
        sm.set_training_values(xt, yt)
        sm.train()

        eq_check = True
        for i in range(xt.shape[0]):
            if abs(float(xt[i, :][2]) - int(float(xt[i, :][2]))) > 10e-8:
                eq_check = False
            if not (xt[i, :][1] == 0 or xt[i, :][1] == 1 or xt[i, :][1] == 2):
                eq_check = False
        self.assertTrue(eq_check)
Пример #10
0
    def test_ego_mixed_integer(self):
        n_iter = 15
        xlimits = np.array([[-5, 5], ["1", "2", "3"], ["4", "5"]],
                           dtype="object")
        xdoe = np.array([[5, 0, 0], [4, 0, 0]])
        criterion = "EI"  #'EI' or 'SBO' or 'UCB'
        xtypes = [INT, (ENUM, 3), (ENUM, 2)]
        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)

        ego = EGO(
            n_iter=n_iter,
            criterion=criterion,
            xdoe=xdoe,
            xtypes=xtypes,
            xlimits=xlimits,
            surrogate=sm,
            enable_tunneling=False,
            random_state=42,
        )
        _, y_opt, _, _, _ = ego.optimize(
            fun=TestEGO.function_test_mixed_integer)

        self.assertAlmostEqual(-15, float(y_opt), delta=5)
Пример #11
0
    def run_ego_mixed_integer_example():
        import numpy as np
        from smt.applications import EGO
        from smt.applications.mixed_integer import (
            MixedIntegerContext,
            FLOAT,
            ENUM,
            ORD,
        )
        import matplotlib.pyplot as plt
        from smt.surrogate_models import KRG
        from smt.sampling_methods import LHS

        # Regarding the interface, the function to be optimized should handle
        # categorical values as index values in the enumeration type specification.
        # For instance, here "blue" will be passed to the function as the index value 2.
        # This allows to keep the numpy ndarray X handling numerical values.
        def function_test_mixed_integer(X):
            # float
            x1 = X[:, 0]
            #  enum 1
            c1 = X[:, 1]
            x2 = c1 == 0
            x3 = c1 == 1
            x4 = c1 == 2
            #  enum 2
            c2 = X[:, 2]
            x5 = c2 == 0
            x6 = c2 == 1
            # int
            i = X[:, 3]

            y = ((x2 + 2 * x3 + 3 * x4) * x5 * x1 +
                 (x2 + 2 * x3 + 3 * x4) * x6 * 0.95 * x1 + i)
            return y

        n_iter = 15
        xtypes = [FLOAT, (ENUM, 3), (ENUM, 2), ORD]
        xlimits = np.array([[-5, 5], ["red", "green", "blue"],
                            ["square", "circle"], [0, 2]])
        criterion = "EI"  #'EI' or 'SBO' or 'LCB'
        qEI = "KB"
        sm = KRG(print_global=False)
        mixint = MixedIntegerContext(xtypes, xlimits)
        n_doe = 3
        sampling = mixint.build_sampling_method(LHS,
                                                criterion="ese",
                                                random_state=42)
        xdoe = sampling(n_doe)
        ydoe = function_test_mixed_integer(xdoe)

        ego = EGO(
            n_iter=n_iter,
            criterion=criterion,
            xdoe=xdoe,
            ydoe=ydoe,
            xtypes=xtypes,
            xlimits=xlimits,
            surrogate=sm,
            qEI=qEI,
            random_state=42,
        )

        x_opt, y_opt, _, _, y_data = ego.optimize(
            fun=function_test_mixed_integer)
        print("Minimum in x={} with f(x)={:.1f}".format(x_opt, float(y_opt)))
        print("Minimum in typed x={}".format(
            ego.mixint.cast_to_mixed_integer(x_opt)))

        min_ref = -15
        mini = np.zeros(n_iter)
        for k in range(n_iter):
            mini[k] = np.log(np.abs(np.min(y_data[0:k + n_doe - 1]) - min_ref))
        x_plot = np.linspace(1, n_iter + 0.5, n_iter)
        u = max(np.floor(max(mini)) + 1, -100)
        l = max(np.floor(min(mini)) - 0.2, -10)
        fig = plt.figure()
        axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
        axes.plot(x_plot, mini, color="r")
        axes.set_ylim([l, u])
        plt.title("minimum convergence plot", loc="center")
        plt.xlabel("number of iterations")
        plt.ylabel("log of the difference w.r.t the best")
        plt.show()