示例#1
0
    def minimize(
        self,
        fun: Callable[[POINT], float],
        x0: POINT,
        jac: Optional[Callable[[POINT], POINT]] = None,
        bounds: Optional[List[Tuple[float, float]]] = None,
    ) -> OptimizerResult:
        snobfit_settings = {
            "maxmp": self._maxmp,
            "maxfail": self._maxfail,
            "verbose": self._verbose,
        }
        options = optset(optin=snobfit_settings)
        # counters the error when initial point is outside the acceptable bounds
        x0 = np.asarray(x0)
        for idx, theta in enumerate(x0):
            if abs(theta) > bounds[idx][0]:
                x0[idx] = x0[idx] % bounds[idx][0]
            elif abs(theta) > bounds[idx][1]:
                x0[idx] = x0[idx] % bounds[idx][1]

        res, history = skq.minimize(
            fun,
            x0,
            bounds=bounds,
            budget=self._maxiter,
            method="snobfit",
            options=options,
        )

        optimizer_result = OptimizerResult()
        optimizer_result.x = res.optpar
        optimizer_result.fun = res.optval
        optimizer_result.nfev = len(history)
        return optimizer_result
示例#2
0
 def optimize(
     self,
     num_vars,
     objective_function,
     gradient_function=None,
     variable_bounds=None,
     initial_point=None,
 ):
     """Runs the optimization."""
     super().optimize(num_vars, objective_function, gradient_function,
                      variable_bounds, initial_point)
     snobfit_settings = {
         "maxmp": self._maxmp,
         "maxfail": self._maxfail,
         "verbose": self._verbose,
     }
     options = optset(optin=snobfit_settings)
     # counters the error when initial point is outside the acceptable bounds
     for idx, theta in enumerate(initial_point):
         if abs(theta) > variable_bounds[idx][0]:
             initial_point[
                 idx] = initial_point[idx] % variable_bounds[idx][0]
         elif abs(theta) > variable_bounds[idx][1]:
             initial_point[
                 idx] = initial_point[idx] % variable_bounds[idx][1]
     res, history = skq.minimize(
         objective_function,
         np.array(initial_point, dtype=float),
         bounds=variable_bounds,
         budget=self._maxiter,
         method="snobfit",
         options=options,
     )
     return res.optpar, res.optval, len(history)
示例#3
0
    def test02_bra(self):
        """Minimize Branin's function"""

        import SQSnobFit

        def bra(x):
            from math import cos, pi

            a = 1
            b = 5.1/(4*pi*pi)
            c = 5/pi
            d = 6
            h = 10
            ff = 1/(8*pi)

            return a*(x[1]-b*x[0]**2+c*x[0]-d)**2+h*(1-ff)*cos(x[0])+h

        bounds = np.array([[-5, 5], [-5, 5]], dtype=float)
        budget = 80      # larger budget needed for full convergence
        x0 = np.array([0.5, 0.5])

        from SQSnobFit import optset
        options = optset(maxmp=len(x0)+6)

        result, history = SQSnobFit.minimize(bra, x0, bounds, budget, options)
        # LIMIT:
        # fglob = 0.397887357729739
        # xglob = [3.14159265, 2.27500000]
        assert np.round(sum(result.optpar)-sum((3.1416, 2.275)), 8) == 0
 def create_optimizer(self):
     from SQSnobFit import minimize, optset
     options = optset(maxmp=2 + 6)
     _, __ = minimize(self._priv_evaluator,
                      x0=self.init_guess,
                      bounds=self.bounds,
                      budget=self.budget)  #, options=options)
     self.is_converged = True
示例#5
0
        def run_Hartman6(self, initial, expected, budget=250, options=None):
            self.reset()

            bounds = np.array([[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]], dtype=float)
            x0 = np.array(initial)

            from SQSnobFit import optset
            if options is None:
                options = optset(maxmp=6+6)
            result, history = SQSnobFit.minimize(Hartman6, x0, bounds, budget, options)

            assert np.round(sum(result.optpar)-sum(expected), 8) == 0
    def test01_availability(self):
        """Quick API check on available optimizers"""

        import skquant.opt as skqopt

        bounds = np.array([[-1, 1], [-1, 1]], dtype=float)
        budget = 40
        x0 = np.array([0.5, 0.5])

        # interface with incorrect input
        assert raises(RuntimeError,
                      skqopt.minimize,
                      f_easy_simple,
                      x0,
                      bounds,
                      budget,
                      method='does not exist')

        # ImFil
        result, history = \
             skqopt.minimize(f_easy_simple, x0, bounds, budget, method='imfil')
        assert type(result.optpar) == np.ndarray
        assert np.round(
            sum(result.optpar) - sum(ref_results[f_easy_simple]['imfil']),
            8) == 0.0

        # SnobFit
        from SQSnobFit import optset
        options = optset(maxmp=len(x0) + 6)
        result, history = \
             skqopt.minimize(f_easy_simple, [], bounds, budget, method='snobfit', options=options)
        assert type(result.optpar) == np.ndarray
        assert np.round(
            sum(result.optpar) - sum(ref_results[f_easy_simple]['snobfit']),
            8) == 0.0

        # Py-BOBYQA
        result, history = \
             skqopt.minimize(f_easy_simple, x0, bounds, budget, method='bobyqa')
        assert type(result.optpar) == np.ndarray
        assert np.round(sum(result.optpar), 5) == 0

        # ORBIT
        if skqopt._check_orbit_prerequisites():
            randstate = 1
            np.random.seed(randstate)
            result, history = \
                 skqopt.minimize(f_easy_simple, x0, bounds, budget, method='orbit')
            assert type(result.optpar) == np.ndarray
            assert np.round(
                sum(result.optpar) - sum((0.00076624, 0.00060909)), 7) == 0
示例#7
0
        def run_easy(self, initial, expected):
            self.reset()

            bounds = np.array([[-1, 1], [-1, 1]], dtype=float)
            budget = 40
            x0 = np.array(initial)

            from SQSnobFit import optset
            options = optset(maxmp=2+6)

            result, history = SQSnobFit.minimize(f_easy, x0, bounds, budget, options)

          # problem is symmetric, so values may have switched: just check the sum
            assert np.round(sum(result.optpar)-sum(expected), 8) == 0