Esempio n. 1
0
    def __round_optim(self, obj, x0, cns):
        """
        Solves the iteration round's local optimization
        :param obj: the approximate objective
        :param x0: initial point of the optimization, i.e., the round's sampled point
        :param cns: constraints
        :return: result of the optimization
        """
        res = None
        if self.optimizer == "scipy":
            from scipy.optimize import minimize

            if (len(cns) > 0) and (self.scipy_solver
                                   not in ["COBYLA", "SLSQP", "L-BFGS-B"]):
                raise ValueError("%s does not support arbitrary constraints" %
                                 self.scipy_solver)
            res = minimize(obj,
                           x0,
                           method=self.scipy_solver,
                           constraints=cns,
                           bounds=self.bounds)
        else:
            try:
                from Optimithon import Base, QuasiNewton
                from numpy import inf

                cns_ = [_["fun"] for _ in cns]
                if self.bounds is not None:
                    for i in range(len(self.bounds)):
                        if self.bounds[i][0] != -inf:
                            cns_.append(
                                lambda x, i_=i: x[i_] - self.bounds[i_][0])
                        if self.bounds[i][1] != inf:
                            cns_.append(
                                lambda x, i_=i: self.bounds[i_][1] - x[i_])
                optm = Base(
                    obj,
                    ineq=cns_,
                    x0=x0,
                    br_func=self.optimithon_br_func,
                    penalty=self.optimithon_penalty,
                    method=QuasiNewton,
                    t_method=self.optimithon_t_method,
                    dd_method=self.optimithon_dd_method,
                    ls_method=self.optimithon_ls_method,
                    ls_bt_method=self.optimithon_ls_bt_method,
                    max_iter=self.optimithon_max_iter,
                    difftool=self.optimithon_difftool,
                )
                optm.Verbose = False
                optm()
                res = optm.solution
                res.fun = res.objective
            except ModuleNotFoundError:
                pass
        return res
Esempio n. 2
0
from scipy.optimize import minimize

NumVars = 9
fun = lambda x: sum([100 * (x[i + 1] - x[i]**2)**2 +
                     (1 - x[i])**2 for i in range(NumVars - 1)])
cons = [
    {'type': 'ineq', 'fun': lambda x: 9 - x[i]**2} for i in range(NumVars)]
x0 = array([0 for _ in range(NumVars)])
sol1 = minimize(fun, x0, method='COBYLA', constraints=cons)
sol2 = minimize(fun, x0, method='SLSQP', constraints=cons)
print("solution according to 'COBYLA':")
print(sol1)
print("solution according to 'SLSQP':")
print(sol2)

OPTIM = Base(fun, ineq=[lambda x: 9 - x[i]**2 for i in range(NumVars)],
             br_func='Carrol',
             penalty=1.e6,
             method=QuasiNewton, x0=x0,  # max_lngth=100.,
             t_method='Cauchy_x',  # 'Cauchy_x', 'ZeroGradient',
             dd_method='BFGS',
             # 'Newton', 'SR1', 'HestenesStiefel', 'PolakRibiere', 'FletcherReeves', 'Gradient', 'DFP', 'BFGS', 'Broyden', 'DaiYuan'
             ls_method='Backtrack',  # 'BarzilaiBorwein', 'Backtrack',
             ls_bt_method='Armijo',  # 'Armijo', 'Goldstein', 'Wolfe', 'BinarySearch'
             )
OPTIM.Verbose = False
OPTIM.MaxIteration = 1500
OPTIM()
print("================================================="*2)
print(OPTIM.solution)
Esempio n. 3
0
from Optimithon import Base
from Optimithon import QuasiNewton
from numpy import array, sin, pi
from scipy.optimize import minimize

fun = lambda x: sin(x[0] + x[1]) + (x[0] - x[1]) ** 2 - 1.5 * x[0] + 2.5 * x[1] + 1.
x0 = array((0., 0.))
print(fun(x0))
sol1 = minimize(fun, x0, method='COBYLA')
sol2 = minimize(fun, x0, method='SLSQP')
print("solution according to 'COBYLA':")
print(sol1)
print("solution according to 'SLSQP':")
print(sol2)

OPTIM = Base(fun,
             method=QuasiNewton, x0=x0,  # max_lngth=100.,
             t_method='Cauchy_x',  # 'Cauchy_x', 'ZeroGradient',
             dd_method='BFGS',
             # 'Newton', 'SR1', 'HestenesStiefel', 'PolakRibiere', 'FletcherReeves', 'Gradient', 'DFP', 'BFGS', 'Broyden', 'DaiYuan'
             ls_method='Backtrack',  # 'BarzilaiBorwein', 'Backtrack',
             ls_bt_method='Armijo',  # 'Armijo', 'Goldstein', 'Wolfe', 'BinarySearch'
             )
OPTIM.Verbose = False
OPTIM.MaxIteration = 1500
OPTIM()
print("==========================" * 4)
print(OPTIM.solution)