예제 #1
0
    def optimize(self, obj_handle, ctr_handle_ineq, ctr_handle_eq, p0):
        nonl_cons_ineq = NonlinearConstraint(ctr_handle_ineq,
                                             -np.inf,
                                             0,
                                             jac='3-point',
                                             hess=BFGS())
        nonl_cons_eq = NonlinearConstraint(ctr_handle_eq,
                                           0,
                                           0,
                                           jac='3-point',
                                           hess=BFGS())

        logger.info('Optimizing the lyapunov function')
        solution = minimize(obj_handle,
                            np.reshape(p0, [len(p0)]),
                            hess=BFGS(),
                            constraints=[nonl_cons_eq, nonl_cons_ineq],
                            method='trust-constr',
                            options={
                                'disp': True,
                                'initial_constr_penalty': 1.5
                            },
                            callback=self.callback_opt)

        return solution.x, solution.fun
예제 #2
0
    def test_list_of_problems(self):
        list_of_problems = [
            Maratos(),
            Maratos(constr_hess='2-point'),
            Maratos(constr_hess=SR1()),
            Maratos(constr_jac='2-point', constr_hess=SR1()),
            MaratosGradInFunc(),
            HyperbolicIneq(),
            HyperbolicIneq(constr_hess='3-point'),
            HyperbolicIneq(constr_hess=BFGS()),
            HyperbolicIneq(constr_jac='3-point', constr_hess=BFGS()),
            Rosenbrock(),
            IneqRosenbrock(),
            EqIneqRosenbrock(),
            BoundedRosenbrock(),
            Elec(n_electrons=2),
            Elec(n_electrons=2, constr_hess='2-point'),
            Elec(n_electrons=2, constr_hess=SR1()),
            Elec(n_electrons=2, constr_jac='3-point', constr_hess=SR1())
        ]

        for prob in list_of_problems:
            for grad in (prob.grad, '3-point', False):
                for hess in (prob.hess, '3-point', SR1(),
                             BFGS(exception_strategy='damp_update'),
                             BFGS(exception_strategy='skip_update')):

                    # Remove exceptions
                    if grad in ('2-point', '3-point', 'cs', False) and \
                       hess in ('2-point', '3-point', 'cs'):
                        continue
                    if prob.grad is True and grad in ('3-point', False):
                        continue
                    with suppress_warnings() as sup:
                        sup.filter(UserWarning, "delta_grad == 0.0")
                        result = minimize(prob.fun,
                                          prob.x0,
                                          method='trust-constr',
                                          jac=grad,
                                          hess=hess,
                                          bounds=prob.bounds,
                                          constraints=prob.constr)

                    if prob.x_opt is not None:
                        assert_array_almost_equal(result.x,
                                                  prob.x_opt,
                                                  decimal=5)
                        # gtol
                        if result.status == 1:
                            assert_array_less(result.optimality, 1e-8)
                    # xtol
                    if result.status == 2:
                        assert_array_less(result.tr_radius, 1e-8)

                        if result.method == "tr_interior_point":
                            assert_array_less(result.barrier_parameter, 1e-8)
                    # max iter
                    if result.status in (0, 3):
                        raise RuntimeError("Invalid termination condition.")
예제 #3
0
def optimalPump(x, nInc):

    fObjRest, Sensibil, y = Prediction(x, 1)

    def fun_obj(x):
        res, sens, y = Prediction(x, 0)
        cost = res['fObj']
        return cost

    def fun_constr_1(x):
        res, sens, y = Prediction(x, 0)
        g1 = res['g1']
        return g1

    def fun_constr_2(x):
        res, sens, y = Prediction(x, 0)
        g2 = res['g2']
        return g2

    c1 = NonlinearConstraint(fun_constr_1,
                             -9999999,
                             0,
                             jac='2-point',
                             hess=BFGS(),
                             keep_feasible=False)
    c2 = NonlinearConstraint(fun_constr_2,
                             -9999999,
                             0,
                             jac='2-point',
                             hess=BFGS(),
                             keep_feasible=False)
    bounds = Bounds([0 for i in range(nInc)], [1 for i in range(nInc)],
                    keep_feasible=False)
    res = minimize(fun_obj,
                   x,
                   args=(),
                   method='trust-constr',
                   jac='2-point',
                   hess=BFGS(),
                   constraints=[c1, c2],
                   options={'verbose': 3},
                   bounds=bounds)
    #print("res=",res)
    print("Solução final: x=", [round(res.x[i], 3) for i in range(len(res.x))])
    #a=input('')

    fObjRest, Sensibil, yopt = Prediction(res.x, 1)
    print("CustoF=", fObjRest['fObj'], '\n')
    cost = fObjRest['fObj']
    xopt = res.x

    return xopt, yopt, xopt, yopt, cost
예제 #4
0
def minimize_and_plot(X, Y, kernel, C, thresh):
    n = len(Y)
    # arguments to pass to minimize function
    args = (Y, X, kernel)
    # define the constraints (page 20) as instances of scipy.optimize.LinearConstraint
    # constraints each alpha to be from 0 to C
    diag = np.eye(n)
    alpha_constr = LinearConstraint(diag, 0, C)
    # constraints sum of (alpha * y)
    alpha_y_constr = LinearConstraint(np.transpose(Y), 0, 0)

    print("Starting computations...")
    # minimization. we are using ready QP solver 'trust-constr'
    result = minimize(fun=function_to_optimize,
                      method='trust-constr',
                      x0=np.empty(shape=(n, )),
                      jac='2-point',
                      hess=BFGS(exception_strategy='skip_update'),
                      constraints=[alpha_constr, alpha_y_constr],
                      args=args)
    # prints the results. If status==0, then the optimizer failed to find the optimal value
    print("status:", result.status)
    print("message:", result.message)
    alphas = result.x
    # indexes of support vectors
    sv_inds = find_support_vector_inds(alphas, thresh)
    print("alphas of support vectors:", '\n', alphas[sv_inds])

    w, b = find_w_b(alphas, Y, X, sv_inds, kernel, thresh, C)

    # create a mesh to plot points and predictions
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xrange, yrange = np.meshgrid(np.arange(x_min, x_max, 0.02),
                                 np.arange(y_min, y_max, 0.02))
    # form a grid by taking each point point from x and y range
    grid = np.c_[xrange.ravel(), yrange.ravel()]
    grid = grid.astype(float)
    # make predictions for each point of the grid
    grid_predictions = predict(alphas, Y, X, grid, b, sv_inds, kernel)
    grid_predictions = grid_predictions.reshape(xrange.shape)

    # plot color grid points according to the prediction made for each point
    plt.contourf(xrange, yrange, grid_predictions, cmap='copper', alpha=0.8)
    # plot initial data points
    plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='autumn')
    # plot support vectors
    plt.scatter(X[sv_inds, 0], X[sv_inds, 1], s=3, c="black")

    if w is not None:  # print lines on which support vectors should reside
        x_plot = np.linspace(x_min, x_max - 0.02, 1000)
        y_plot_1 = (-w[0] * x_plot - b + 1) / w[1]
        y_plot_2 = (-w[0] * x_plot - b - 1) / w[1]
        plt.plot(x_plot, y_plot_1.T.flatten())
        plt.plot(x_plot, y_plot_2.T.flatten())
    # print(b)
    plt.title('SVM Results ' + kernel.__name__)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()
예제 #5
0
def exercise_3(vector: []):
    linear_constraint = LinearConstraint([[1, 2], [1, -1]], [-np.inf, -np.inf],
                                         [1, 4])

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

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

    # def cons_H(x, v):
    # return v[0]*np.array([[2, 0], [0, 0]])

    # nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1, jac=cons_J, hess=cons_H)
    nonlinear_constraint = NonlinearConstraint(cons_f,
                                               -np.inf,
                                               1,
                                               jac=cons_J,
                                               hess=BFGS())

    # x0 = np.array([0.5, 0])
    x0 = np.array(vector)

    res = minimize(rosen,
                   x0,
                   method='trust-constr',
                   jac=rosen_der,
                   hess=rosen_hess,
                   constraints=[linear_constraint, nonlinear_constraint],
                   options={'verbose': 1})

    print(res.x)
예제 #6
0
 def test_BFGS_skip_update(self):
     # Define auxiliar problem
     prob = Rosenbrock(n=5)
     # Define iteration points
     x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
               [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
               [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
               [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
               [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
               [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
               [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184]]
     # Get iteration points
     grad_list = [prob.grad(x) for x in x_list]
     delta_x = [
         np.array(x_list[i + 1]) - np.array(x_list[i])
         for i in range(len(x_list) - 1)
     ]
     delta_grad = [
         grad_list[i + 1] - grad_list[i] for i in range(len(grad_list) - 1)
     ]
     hess = BFGS(init_scale=1, min_curvature=10)
     hess.initialize(len(x_list[0]), 'hess')
     # Compare the hessian and its inverse
     for i in range(len(delta_x) - 1):
         s = delta_x[i]
         y = delta_grad[i]
         hess.update(s, y)
     # Test skip update
     B = np.copy(hess.get_matrix())
     s = delta_x[5]
     y = delta_grad[5]
     hess.update(s, y)
     B_updated = np.copy(hess.get_matrix())
     assert_array_equal(B, B_updated)
예제 #7
0
def _get_steadystate_dualcontrol_init(dis: float, r11: float, r22: float,
                                      K11: float, K22: float, BB12: float,
                                      BB21: float, P: float, C11: float,
                                      C12: float, c2: float, CB: float,
                                      umax: float) -> np.array:
    ## Used as initial guess for fully specified problem
    x01 = np.array([1, 1, .1, .1]).T
    cons = NonlinearConstraint(
        fun=lambda x: const01_ineq_init(x, r11, r22, K11, K22, BB12, BB21),
        lb=0,
        ub=np.inf,
    )
    OptimizeResult01_init = minimize(
        fun=obj1,
        x0=x01,
        args=(r11, r22, K11, K22, BB12, BB21, P, c2, C11, C12, CB),
        method='trust-constr',
        jac='cs',
        hess=BFGS(),
        constraints=cons,
        bounds=[
            (0, K11),
            (0, K22),
            (0, umax),
            (0, umax),
        ],
        tol=10e-14,
    )
    print(OptimizeResult01_init.status)
    print(OptimizeResult01_init.message)
    print(OptimizeResult01_init.fun)
    print(OptimizeResult01_init.method)
    print(*OptimizeResult01_init.x, sep='\n ')
    return OptimizeResult01_init.x
예제 #8
0
def  BFGS_algorithm(obj_fun, theta0, max_iter=2e04, epsilon=0):
    print("Starting BFGS algorithm.")
    #Initialization of object: bfgs
    bfgs = BFGS()
    bfgs.initialize(6, "inv_hess")
    #Lists to store results for theta (th) and cost(c)
    th,c = [],[]
    th.append(theta0)
    c.append(obj_fun(theta0))
    niter = max_iter
    success = (False, "max_iter reached.")
    #Iteration
    for n in range(max_iter):
        th_0 = th[n]
        g0 = gradient(th_0)
        #If loss<epsilon, converged
        #If epsilon=0, no check for convergence
        if (epsilon > 0) and (obj_fun(th_0) < epsilon):
            niter = n
            success = (True, "Loss = {}".format(obj_fun(th_0)))
            break
        #Compute search direction
        d = bfgs.dot(g0)
        #Compute step size through line search
        alpha = line_search(obj_fun,gradient,th_0,-g0)[0]
        #Update theta and gradient
        th_1 = th_0 - alpha*d
        g1 = gradient(th_1)
        #Update theta history and cost history
        th.append(th_1)
        c.append(obj_fun(th_1))
        #Update inverse hessian
        bfgs.update(th_1-th_0, g1-g0)
    print("Exiting.")
    return th,c,niter,success
예제 #9
0
    def test_hessian_initialization(self):
        quasi_newton = (BFGS(), SR1())

        for qn in quasi_newton:
            qn.initialize(5, 'hess')
            B = qn.get_matrix()

            assert_array_equal(B, np.eye(5))
 def wrap_in_constraint(first_or_second):
     function_loc, function_jac, func_hessian = one_saddlenode_problem(
         SN_loc, first_or_second, paramIndex)
     constraint = scipy.optimize.NonlinearConstraint(fun=function_loc,
                                                     lb=0,
                                                     ub=0,
                                                     jac='cs',
                                                     hess=BFGS())
     return constraint
예제 #11
0
    def __init__(self):

        # To keep track of the iterations
        self.counter = 0

        # Parameters
        self.name = 'Trust-Constr'
        self.refl = True
        self.save_fig = False
        self.save_log = False
        self.constrained = True

        if self.save_log:
            self.data = []

        if self.constrained:
            self.constraints = (LinearConstraint(MP.CONSTRAINT_MAT_EXT,
                                                 MP.LOWER_BOUND_EXT,
                                                 MP.UPPER_BOUND_EXT),
                                NonlinearConstraint(functional_constraint,
                                                    -np.inf,
                                                    0,
                                                    jac='cs',
                                                    hess=BFGS()))
        else:
            self.constraints = ()

        print("Welcome! You are using a trust-constr optimiser.")
        print("Reflections: ", self.refl)
        print("Constraints: ", self.constrained)

        time.sleep(1)
        # 360 = 5000?
        # Objective function. We want to maximise this
        self.result = minimize(self.obj_fun,
                               MP.INITIAL_SOLUTION,
                               method='trust-constr',
                               jac='3-point',
                               constraints=self.constraints,
                               hess=BFGS(exception_strategy='damp_update'))

        # What is the result of the optimisation?
        print(self.result)
예제 #12
0
    def test_warn_ignored_options(self):
        # warns about constraint options being ignored
        fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2
        x0 = (2, 0, 1)

        if self.method == "slsqp":
            bnds = ((0, None), (0, None), (0, None))
        else:
            bnds = None

        cons = NonlinearConstraint(lambda x: x[0], 2, np.inf)
        res = minimize(fun,
                       x0,
                       method=self.method,
                       bounds=bnds,
                       constraints=cons)
        # no warnings without constraint options
        assert_allclose(res.fun, 1)

        cons = LinearConstraint([1, 0, 0], 2, np.inf)
        res = minimize(fun,
                       x0,
                       method=self.method,
                       bounds=bnds,
                       constraints=cons)
        # no warnings without constraint options
        assert_allclose(res.fun, 1)

        cons = []
        cons.append(
            NonlinearConstraint(lambda x: x[0]**2,
                                2,
                                np.inf,
                                keep_feasible=True))
        cons.append(
            NonlinearConstraint(lambda x: x[0]**2, 2, np.inf, hess=BFGS()))
        cons.append(
            NonlinearConstraint(lambda x: x[0]**2,
                                2,
                                np.inf,
                                finite_diff_jac_sparsity=42))
        cons.append(
            NonlinearConstraint(lambda x: x[0]**2,
                                2,
                                np.inf,
                                finite_diff_rel_step=42))
        cons.append(LinearConstraint([1, 0, 0], 2, np.inf, keep_feasible=True))
        for con in cons:
            _assert_warns(OptimizeWarning,
                          minimize,
                          fun,
                          x0,
                          method=self.method,
                          bounds=bnds,
                          constraints=cons)
예제 #13
0
def optimizeWeights(per_weights, tickers, start_date, end_date):
    n = len(per_weights)
    bounds = Bounds([0 for i in range(n)], [1 for i in range(n)])
    lc = LinearConstraint([[1 for i in range(n)]], [1], [1])
    res = minimize(portfolio_risk,
                   per_weights,
                   args=(tickers, start_date, end_date),
                   method='trust-constr',
                   jac='2-point',
                   hess=BFGS(),
                   constraints=[lc],
                   bounds=bounds)
    return res.x
예제 #14
0
    def optimize_N(N_init, N_SUM_MAX=45, max_iter=6):
        """Value function of N optimization as per Algorithm 1 of the paper.
        Uses trust-region-constrained from scipy

        Parameters
        ----------
        N_init : np.array of shape (M,), where M is the number of modalities
            Initial array of fractions
        N_SUM_MAX : float
            Max treatment course length, the bound on the sum of N
        max_iter : int
            Max number of iterations

        Returns
        -------
        x : np.array of shape (M,)
            Optimal fractionation schedule N
        res : OptimizationResult from scipy
            Metadata of the algorithm
        Nk_hist : list
            Iterates N history 
        """
        Nk_hist = []

        def callback(xk, OptRes):
            Nk_hist.append(xk)
            return 0

        bounds = scipy.optimize.Bounds([1, 1], [np.inf, np.inf])  #or 0,0
        linear_constraint = scipy.optimize.LinearConstraint(
            np.array([1, 1]), -np.inf, N_SUM_MAX)

        x0 = np.array(N_init)
        res = scipy.optimize.minimize(objective_N,
                                      x0,
                                      method='trust-constr',
                                      jac="2-point",
                                      hess=BFGS(),
                                      constraints=[linear_constraint],
                                      options={
                                          'verbose': 4,
                                          'maxiter': max_iter
                                      },
                                      bounds=bounds,
                                      callback=callback)
        x = res.x
        print('NEW N rounded:', np.rint(x), 'EXACT new N:', x)
        return x, res, Nk_hist
예제 #15
0
    def get_scalar(self):
        """
        This function does constrained minimization and calculates scalars
        """
        num_spend_var = self.spend_matrix.shape[1]
        beta_init = np.ones(self.spend_matrix.shape[1] * 2 +
                            self.non_spend_matrix.shape[1] + 1)
        res = minimize(self.objective,
                       beta_init,
                       method='trust-constr',
                       jac='3-point',
                       hess=BFGS(),
                       constraints=self.constraints)
        a_tilda, b_tilda, control_beta = res.x[:num_spend_var], res.x[
            num_spend_var:num_spend_var * 2], res.x[num_spend_var * 2:]
        scalars = 1.0 / ((self.a / self.b) * (b_tilda / a_tilda))
        beta = a_tilda / a / scalars

        return scalars, beta
예제 #16
0
def find_path(ex,ey,a0,b0):
    
    def calc_int(pars):
        a1,a2,a3,b1,b2,b3=pars
        a4 = ex - a0 - (a1+a2+a3)
        b4 = ey - b0 - (b1+b2+b3)
        def gfunc(t):
            t = t[0]
            x = a0 + a1*t + a2*t**2 + a3*t**3 + a4*t**4 
            y = b0 + b1*t + b2*t**2 + b3*t**3 + b4*t**4
            s1 = 2.2; x1 = 2.0; y1 = 2.0
            g1 = np.exp( -4 *np.log(2) * ((x-x1)**2+(y-y1)**2) / s1**2)
            return g1*10.0    
        ts = np.linspace(0.0,1.0,100)
        dzs = np.array([util._approx_fprime_helper([t],gfunc)[0] for t in ts])
        tmp = np.sqrt(1.0+(dzs**2.0))
        Iv = trapz(tmp, 1/100.)
        tmp = np.array([b1 + 2*b2*t + 3*b3*t**2 - 112.0*t**3 + (a1 + 2*a2*t + 3*a3*t**2 - 65.2*t**3)**2 for t in ts])
        tmp = tmp[tmp>0.0]
        tmp = np.sqrt(tmp)
        Ih = trapz(tmp, 1/100.)
        res = Iv*5 + Ih*1
        return res 
    
    LIM = 5.0
    # rasgele secilmis baslangic degerleri
    a1,a2,a3 = 0,0,0
    b1,b2,b3 = 0,0,0
    x0 = a1,a2,a3,b1,b2,b3

    opts = {'maxiter': 300, 'verbose': 0}
    res = minimize (fun=calc_int,
                    x0=x0,
                    method='trust-constr',
                    hess = BFGS (),
                    bounds=Bounds([-LIM, -LIM, -LIM, -LIM, -LIM, -LIM],
                                  [LIM, LIM, LIM, LIM, LIM, LIM]),
                    options=opts)
    

    return res
예제 #17
0
def get_steadystate_dualcontrol(dis: float, r11: float, r22: float, K11: float,
                                K22: float, BB12: float, BB21: float, P: float,
                                C11: float, C12: float, c2: float, CB: float,
                                umax: float) -> Tuple[float]:
    x01 = _get_steadystate_dualcontrol_init(dis, r11, r22, K11, K22, BB12,
                                            BB21, P, C11, C12, c2, CB, umax)

    cons = get_Nonlinear_Constraints(dis, umax, r11, r22, K11, K22, BB12, BB21,
                                     P, c2, C11, C12, CB)

    OptimizeResult01 = minimize(
        fun=obj1,
        x0=np.append(x01, np.array([.1, .1, 0, 0, 0, 0, 0, 0]).T, axis=0),
        args=(r11, r22, K11, K22, BB12, BB21, P, c2, C11, C12, CB),
        method='trust-constr',
        jac='cs',
        hess=BFGS(),
        constraints=cons,
        bounds=[
            (0, K11),
            (0, K22),
            (0, umax),
            (0, umax),
            (0, None),
            (0, None),
            (0, None),
            (0, None),
            (0, None),
            (0, None),
            (0, None),
            (0, None),
        ],
        options={
            'xtol': 10e-12,
            'gtol': 10e-12,
            'disp': True
        },
    )
    X1T = OptimizeResult01.x[0]
    X2T = OptimizeResult01.x[1]
    return X1T, X2T
예제 #18
0
    def test_hessian_initialization(self):
        ndims = 5
        rnd_matrix = np.random.randint(1, 50, size=(ndims, ndims))
        init_scales = {
            None:
            np.eye(ndims),
            2:
            np.eye(ndims) * 2,
            np.array(range(1, ndims + 1)):
            np.eye(ndims) * np.array(range(1, ndims + 1)),
            rnd_matrix:
            rnd_matrix
        }
        for init_scale, true_matrix in init_scales.items():
            quasi_newton = (BFGS(init_scale=init_scale),
                            SR1(init_scale=init_scale))

            for qn in quasi_newton:
                qn.initialize(ndims, 'hess')
                B = qn.get_matrix()

                assert_array_equal(B, true_matrix)
예제 #19
0
def find_path(ex, ey, a0, b0):
    def calc_int(pars):
        a1, a2, a3, b1, b2, b3 = pars

        def sigx(t):
            t = t[0]
            x = a0 + \
                a1*sig(t,1) + \
                a2*sig(t,2) + \
                a3*sig(t,3)
            return x

        def sigy(t):
            t = t[0]
            y = b0 + \
                b1*sig(t,1) + \
                b2*sig(t,2) + \
                b3*sig(t,3)
            return y

        def gfunc(t):
            t = t[0]
            x = sigx([t])
            y = sigy([t])
            s1 = 2.2
            x1 = 2.0
            y1 = 2.0
            g1 = np.exp(-4 * np.log(2) * ((x - x1)**2 + (y - y1)**2) / s1**2)
            s2 = 1.2
            x2 = 4.0
            y2 = 1.0
            g2 = np.exp(-4 * np.log(2) * ((x - x2)**2 + (y - y2)**2) / s2**2)
            return g1 * 10.0 + g2 * 10.0

        ts = np.linspace(0.0, 5.0, 100)
        dzs = np.array([util._approx_fprime_helper([t], gfunc)[0] for t in ts])
        tmp = np.sqrt(1.0 + (dzs**2.0))
        Iv = trapz(tmp, 5. / 100)
        dxs = np.array([util._approx_fprime_helper([t], sigx)[0] for t in ts])
        dys = np.array([util._approx_fprime_helper([t], sigy)[0] for t in ts])
        tmp = np.power(dxs, 2) + np.power(dys, 2)
        tmp = tmp[tmp > 0.0]
        tmp = np.sqrt(tmp)
        Ih = trapz(tmp, 5. / 100)
        res = Iv * 5.0 + Ih * 1.0
        #print (res)
        return res

    LIM = 2.0

    a1, a2, a3, b1, b2, b3 = 0.1, 0.1, 0.1, 0.1, 0.1, 0.1
    x0 = a1, a2, a3, b1, b2, b3

    opts = {'maxiter': 50, 'verbose': 0}

    def conx(x):
        aa1, aa2, aa3, bb1, bb2, bb3 = x
        a = a0 + aa1 * (5.0 - 1.0) + aa2 * (5.0 - 2.0) + aa3 * (5.0 - 3.0) - ex
        return a

    def cony(x):
        aa1, aa2, aa3, bb1, bb2, bb3 = x
        b = b0 + bb1 * (5.0 - 1.0) + bb2 * (5.0 - 2.0) + bb3 * (5.0 - 3.0) - ey
        return b

    cons = [{'type': 'eq', 'fun': conx}, {'type': 'eq', 'fun': cony}]

    res = minimize(fun=calc_int,
                   x0=x0,
                   method='trust-constr',
                   hess=BFGS(),
                   bounds=Bounds([-LIM, -LIM, -LIM, -LIM, -LIM, -LIM],
                                 [LIM, LIM, LIM, LIM, LIM, LIM]),
                   constraints=cons,
                   options=opts)

    return res
예제 #20
0
 def test_rosenbrock_with_no_exception(self):
     # Define auxiliar problem
     prob = Rosenbrock(n=5)
     # Define iteration points
     x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
               [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
               [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
               [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
               [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
               [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
               [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184],
               [0.2354930, 0.0443711, 0.0173959, 0.0041872, 0.00794563],
               [0.4168118, 0.1433867, 0.0111714, 0.0126265, -0.00658537],
               [0.4681972, 0.2153273, 0.0225249, 0.0152704, -0.00463809],
               [0.6023068, 0.3346815, 0.0731108, 0.0186618, -0.00371541],
               [0.6415743, 0.3985468, 0.1324422, 0.0214160, -0.00062401],
               [0.7503690, 0.5447616, 0.2804541, 0.0539851, 0.00242230],
               [0.7452626, 0.5644594, 0.3324679, 0.0865153, 0.00454960],
               [0.8059782, 0.6586838, 0.4229577, 0.1452990, 0.00976702],
               [0.8549542, 0.7226562, 0.4991309, 0.2420093, 0.02772661],
               [0.8571332, 0.7285741, 0.5279076, 0.2824549, 0.06030276],
               [0.8835633, 0.7727077, 0.5957984, 0.3411303, 0.09652185],
               [0.9071558, 0.8299587, 0.6771400, 0.4402896, 0.17469338],
               [0.9190793, 0.8486480, 0.7163332, 0.5083780, 0.26107691],
               [0.9371223, 0.8762177, 0.7653702, 0.5773109, 0.32181041],
               [0.9554613, 0.9119893, 0.8282687, 0.6776178, 0.43162744],
               [0.9545744, 0.9099264, 0.8270244, 0.6822220, 0.45237623],
               [0.9688112, 0.9351710, 0.8730961, 0.7546601, 0.56622448],
               [0.9743227, 0.9491953, 0.9005150, 0.8086497, 0.64505437],
               [0.9807345, 0.9638853, 0.9283012, 0.8631675, 0.73812581],
               [0.9886746, 0.9777760, 0.9558950, 0.9123417, 0.82726553],
               [0.9899096, 0.9803828, 0.9615592, 0.9255600, 0.85822149],
               [0.9969510, 0.9935441, 0.9864657, 0.9726775, 0.94358663],
               [0.9979533, 0.9960274, 0.9921724, 0.9837415, 0.96626288],
               [0.9995981, 0.9989171, 0.9974178, 0.9949954, 0.99023356],
               [1.0002640, 1.0005088, 1.0010594, 1.0021161, 1.00386912],
               [0.9998903, 0.9998459, 0.9997795, 0.9995484, 0.99916305],
               [1.0000008, 0.9999905, 0.9999481, 0.9998903, 0.99978047],
               [1.0000004, 0.9999983, 1.0000001, 1.0000031, 1.00000297],
               [0.9999995, 1.0000003, 1.0000005, 1.0000001, 1.00000032],
               [0.9999999, 0.9999997, 0.9999994, 0.9999989, 0.99999786],
               [0.9999999, 0.9999999, 0.9999999, 0.9999999, 0.99999991]]
     # Get iteration points
     grad_list = [prob.grad(x) for x in x_list]
     delta_x = [
         np.array(x_list[i + 1]) - np.array(x_list[i])
         for i in range(len(x_list) - 1)
     ]
     delta_grad = [
         grad_list[i + 1] - grad_list[i] for i in range(len(grad_list) - 1)
     ]
     # Check curvature condition
     for i in range(len(delta_x)):
         s = delta_x[i]
         y = delta_grad[i]
         if np.dot(s, y) <= 0:
             raise ArithmeticError()
     # Define QuasiNewton update
     for quasi_newton in (BFGS(init_scale=1,
                               min_curvature=1e-4), SR1(init_scale=1)):
         hess = deepcopy(quasi_newton)
         inv_hess = deepcopy(quasi_newton)
         hess.initialize(len(x_list[0]), 'hess')
         inv_hess.initialize(len(x_list[0]), 'inv_hess')
         # Compare the hessian and its inverse
         for i in range(len(delta_x)):
             s = delta_x[i]
             y = delta_grad[i]
             hess.update(s, y)
             inv_hess.update(s, y)
             B = hess.get_matrix()
             H = inv_hess.get_matrix()
             assert_array_almost_equal(np.linalg.inv(B), H, decimal=10)
         B_true = prob.hess(x_list[i + 1])
         assert_array_less(norm(B - B_true) / norm(B_true), 0.1)
예제 #21
0
def main():
    parser = optparse.OptionParser()

    parser.add_option(
        '-p',
        type='string',
        action='store',
        dest='path_chessboard',
        help='path to object point, image point and pantilt pos.')

    parser.add_option('-l',
                      type='string',
                      action='store',
                      dest='path_line',
                      help='path to object point on field boundary.')

    parser.add_option(
        '-s',
        type='string',
        action='store',
        dest='savePath',
        help='path for save intrinsic camera matrix and distortion coeff.')

    parser.add_option('--camMat',
                      type='string',
                      action='store',
                      dest='camMatPath',
                      help='path to camera matrix.')

    parser.add_option('--resultPath',
                      type='string',
                      action='store',
                      dest='resultPath',
                      help='path for saving result.')

    parser.add_option('--use_cammat', action='store_true', dest='use_cammat')

    parser.add_option('--visualize',
                      action='store_true',
                      dest='visualize',
                      help='To only visualize error.')

    (options, args) = parser.parse_args()

    ## Load image points, object point and joint state.
    data = np.load(options.path_chessboard)

    objPointList_chessboard = np.array(data['objectPoints'])

    objPointList_chessboard = objPointList_chessboard.astype(np.float32)
    imgPointList_chessboard = np.array(data['imagePoints'])

    pantiltPosList_chessboard = np.array(data['pantilt'])
    rpy_chessboard = np.array(data['rpy'])

    if options.camMatPath is not None:
        ## Load camera matrix.
        camera_prop = np.load(options.camMatPath)
        cameraMatrix = camera_prop['cameraMatrix']
        distCoeffs = camera_prop['distCoeffs']
        # roi = camera_prop[ 'roi' ]

    elif not options.use_cammat:
        ## Re-calculate camera matrix
        retval, cameraMatrix, distCoeffs, rvecs, tvecs = cv2.calibrateCamera(
            objPointList_chessboard, imgPointList_chessboard, (640, 480), None,
            None)
        # cameraMatrix = np.eye( 3 )
        # distCoeffs = np.array([[0, 0, 0, 0, 0]])
        # newCameraMatrix, roi = cv2.getOptimalNewCameraMatrix( cameraMatrix,
        # 													distCoeffs,
        # 													(640,480),
        # 													0,
        # 													(640,480))

        distCoeffs = distCoeffs[0]

        if options.savePath is not None:
            np.savez(
                options.savePath,
                cameraMatrix=cameraMatrix,
                distCoeffs=distCoeffs,
            )

    if options.resultPath is not None:

        if options.use_cammat:
            config = configobj.ConfigObj(options.resultPath)
            cameraMatrix = np.zeros((3, 3), dtype=np.float64)
            distCoeffs = []
            cameraMatrix[0, 0] = config['CameraParameters']['fx']
            cameraMatrix[1, 1] = config['CameraParameters']['fy']
            cameraMatrix[0, 2] = config['CameraParameters']['cx']
            cameraMatrix[1, 2] = config['CameraParameters']['cy']
            cameraMatrix[2, 2] = 1
            distCoeffs.append(config['CameraParameters']['k1'])
            distCoeffs.append(config['CameraParameters']['k2'])
            distCoeffs.append(config['CameraParameters']['k3'])
            distCoeffs.append(config['CameraParameters']['p1'])
            distCoeffs.append(config['CameraParameters']['p2'])

        else:

            try:
                config = configobj.ConfigObj(options.resultPath)
            except Exception:
                config = configobj.ConfigObj()
                config.filename = options.resultPath
            print distCoeffs
            config['CameraParameters']['fx'] = cameraMatrix[0, 0]
            config['CameraParameters']['fy'] = cameraMatrix[1, 1]
            config['CameraParameters']['cx'] = cameraMatrix[0, 2]
            config['CameraParameters']['cy'] = cameraMatrix[1, 2]
            config['CameraParameters']['k1'] = distCoeffs[0]
            config['CameraParameters']['k2'] = distCoeffs[1]
            config['CameraParameters']['k3'] = distCoeffs[4] if len(
                distCoeffs) >= 5 else 0.0
            config['CameraParameters']['p1'] = distCoeffs[2]
            config['CameraParameters']['p2'] = distCoeffs[3]

            config.write()

    if options.visualize:
        loadDimensionFromConfig(options.resultPath)
        x = getRobotConfiguration()
        print x
        dataChessboard = {
            'objPointList': objPointList_chessboard,
            'imgPointList': imgPointList_chessboard,
            'jsList': pantiltPosList_chessboard,
            'rpy': rpy_chessboard
        }

        args = (dataChessboard, cameraMatrix, distCoeffs)

        lossFunction_chessboard(x, *args, vis=True)

        return

    # cameraMatrix[0,0] = 625.44622803
    # cameraMatrix[1,1] = 579.21398926
    # cameraMatrix[0,2] = 330.76631757
    # cameraMatrix[1,2] = 161.34627042

    ###########################################################################
    ## Minimizer Part

    ## Our initial guess.
    initialGuess = np.array([
        0.46,
        0.0,
        0.02,
        0.03,
        0.07,
        0.02,
        0.0,
        0.0,
        5.0,
        5.0,
    ])
    # objPointList = objPointList[ : len( objPointList )/2]
    # imgPointList = imgPointList[ : len( imgPointList )/2]
    # pantiltPosList = pantiltPosList[ : len( pantiltPosList )/2]

    dataChessboard = {
        'objPointList': objPointList_chessboard,
        'imgPointList': imgPointList_chessboard,
        'jsList': pantiltPosList_chessboard,
        'rpy': rpy_chessboard
    }

    args = (dataChessboard, cameraMatrix, distCoeffs)
    method = "trust-constr"
    jac = "3-point"
    hess = BFGS()
    bounds = [(0.2, 0.6), (-0.1, 0.5), (0.005, 0.05), (0.005, 0.05),
              (0.035, 0.2), (0.0, 0.05), (-0.02, 0.02), (-90, 90), (-90, 90),
              (-90, 90)]

    constr = LinearConstraint(np.eye(len(bounds)), [i[0] for i in bounds],
                              [i[1] for i in bounds])

    resultTrustConstr = minimize(lossFunction_chessboard,
                                 initialGuess,
                                 args=args,
                                 method=method,
                                 jac=jac,
                                 hess=hess,
                                 bounds=bounds,
                                 constraints=constr)

    # resultEvo = differential_evolution(lossFunction, bounds,
    # 								args = args,
    # 								)
    args = (dataChessboard, cameraMatrix, distCoeffs)
    x = resultTrustConstr.x
    lossFunction_chessboard(x.copy(), *args, vis=True)

    print "Loss Trust-constr: ", resultTrustConstr.fun
    print "Result Trust-constr: ", resultTrustConstr.x
    print cameraMatrix

    # print "Loss Evo: ", resultEvo.fun
    # print "Result Evo: ", resultEvo.x

    # finalConfig = resultEvo.x if resultEvo.fun < resultTrustConstr.fun else resultTrustConstr.x
    finalConfig = x

    # finalConfig = list(finalConfig[:2]) + list(ORIGINAL_CONFIG[2:-1]) + list(finalConfig[2:])

    setNewRobotConfiguration(*finalConfig)

    if options.resultPath is not None:
        saveDimension(options.resultPath)
예제 #22
0
    def next(self):
        # Parse parameters from GUI.
        try:
            s = self.entryXtol.get().strip()
            xtol = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid xtol value "' + s + '".')
            return
        try:
            s = self.entryGtol.get().strip()
            gtol = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid gtol value "' + s + '".')
            return
        try:
            s = self.entryBtol.get().strip()
            btol = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid barrier tolerance value "' + s + '".')
            return
        try:
            s = self.entryMaxiter.get().strip()
            maxiter = int(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid maximum iterations value "' + s + '".')
            return
        if maxiter < 1:
            messagebox.showerror('ERROR', 'Maximum iterations must be positive.')
            return
        try:
            s = self.entryInitConstrPen.get().strip()
            init_constr_pen = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid initial constrains penalty value "' + s + '".')
            return
        try:
            s = self.entryInitTrustRad.get().strip()
            init_trust_rad = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid initial trust radius value "' + s + '".')
            return
        try:
            s = self.entryInitBarrPar.get().strip()
            init_barr_par = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid initial barrier parameter value "' + s + '".')
            return
        try:
            s = self.entryInitBarrTol.get().strip()
            init_barr_tol = float(s)
        except ValueError:
            messagebox.showerror('ERROR', 'Invalid initial barrier tolerance value "' + s + '".')
            return

        # Set trust-constr specific parameters.
        method = self.controller.method
        weighted_error = self.controller.weighted_error
        if self.calcJac.get():
            method.calcJac = True
            method.jac = weighted_error.jac
        else:
            method.calcJac = False
            method.jac = '2-point'
        if self.calcHess.get():
            method.calcHess = True
            method.hess = weighted_error.hess
        else:
            method.calcHess = False
            method.hess = BFGS()
        method.xtol = xtol
        method.gtol = gtol
        method.barrier_tol = btol
        method.maxiter = maxiter
        method.initial_constr_penalty = init_constr_pen
        method.initial_tr_radius = init_trust_rad
        method.initial_barrier_parameter = init_barr_par
        method.initial_barrier_tolerance = init_barr_tol

        self.controller.close()
예제 #23
0
def test_traj_min_v3(x0, x1, v0):

    dx = x1 - x0

    #dx[np.where(dx==0.0)] = 1e-6
    dx_n = np.linalg.norm(dx)

    # come up with an initial guess for v1
    # have to careful as some solution are non-physical

    sign = np.sign(dx)

    # condition x0==x1 falls under x1 - x0 >= 0
    sign[sign == 0] = 1

    v1 = (v0 * -1) + sign * 1

    # performance constraints
    v_max = 5  # max velocity
    a_max = 1e10  # max acceleration

    def obj(v1):
        # objective function is the time to travel between two points
        v_av = (v0 + v1) / 2
        t = np.linalg.norm(dx) / np.linalg.norm(v_av)
        return t
        #return 2 * (x1[0] - x0[0] + x1[1] - x0[1] + x1[2] - x0[2]) / (v0[0] + v1[0] + v0[1] + v1[1] + v0[2] + v1[2])

    def cons_a(v1):
        "acceleration constraint"

        #v0 = np.array((1, 0, 0))
        #v1 = np.array((0, 1, 1))
        # where there is no position change the acceleration calculation is infinite.
        # however no change in position is also just 0 acceleration
        #a = (v1 - v0) / obj(v1)
        #print('a', a)
        a_n = (np.linalg.norm(v1)**2 -
               np.linalg.norm(v0)**2) / 2 / np.linalg.norm(dx)
        #a = np.nan_to_num(a, copy=True, nan=0.0, posinf=0.0, neginf=0.0)
        #return np.linalg.norm(a)
        return a_n
        #return np.linalg.norm(a)

    def cons_v(v1):
        # contraint on velocity
        #return v1
        return np.linalg.norm(v1)

    def cons_dx(v1):
        #a_n = (np.linalg.norm(v1)**2 - np.linalg.norm(v0)**2) / 2 / cons_a(v1)
        #return a_n
        return ((v0 + v1) / 2) * obj(v1)

    def cons_x1(v1):
        xf = x0 + ((v0 + v1) / 2) * obj(v1)
        return xf

    def callback(v1, srate):
        #print('dx min', np.linalg.norm(cons_dx(v1)), 'dx', np.linalg.norm(dx))
        pass

    # accleration performance constraint
    acceleration_cons = NonlinearConstraint(cons_a,
                                            0,
                                            5,
                                            jac='2-point',
                                            hess=BFGS())
    # velocity performance constraint
    velocity_cons = NonlinearConstraint(cons_v,
                                        0,
                                        5,
                                        jac='2-point',
                                        hess=BFGS())
    # final position constraint
    dx_cons = NonlinearConstraint(cons_dx, dx, dx, jac='3-point', hess=BFGS())

    x1_cons = NonlinearConstraint(cons_x1, x1, x1, jac='3-point', hess=BFGS())

    res = minimize(
        obj,
        x0=v1,
        #callback=callback,
        method='trust-constr',
        options={
            #'gtol': 1e-2,
            'maxiter': 20000,
            'verbose': 0,
        },
        #jac=jacobian_flux,
        constraints=[
            # sides must have some minimum length
            acceleration_cons,
            velocity_cons,
            dx_cons,
            #x1_cons
        ])

    dt = res.fun
    v1 = res.x
    a = (v1 - v0) / dt

    x1_p = x0 + (v0 * dt) + 0.5 * a * dt * dt
    dx_p = x1_p - x0
    dx_p_n = np.linalg.norm(dx_p)

    print('results')
    print('dt', dt)
    print('x0', x0)
    print('x1', x1)
    print('v0', v0)
    print('v1', v1)
    print('v av', (v0 + v1) / 2)
    print('v mag', np.linalg.norm(v1))
    print('a mag', cons_a(v1))
    print('a', a)
    print('x1', x1, x1_p)
    print('dx norm', np.linalg.norm(dx), dx_p_n)

    print('\n')

    return dt, v1
예제 #24
0
    def run(self):
        """
        Optimize the problem using selected Scipy optimizer.

        Returns
        -------
        boolean
            Failure flag; True if failed to converge, False is successful.
        """
        problem = self._problem
        opt = self.options['optimizer']
        model = problem.model
        self.iter_count = 0
        self._total_jac = None

        self._check_for_missing_objective()

        # Initial Run
        with RecordingDebugging(self._get_name(), self.iter_count,
                                self) as rec:
            model.run_solve_nonlinear()
            self.iter_count += 1

        self._con_cache = self.get_constraint_values()
        desvar_vals = self.get_design_var_values()
        self._dvlist = list(self._designvars)

        # maxiter and disp get passsed into scipy with all the other options.
        self.opt_settings['maxiter'] = self.options['maxiter']
        self.opt_settings['disp'] = self.options['disp']

        # Size Problem
        nparam = 0
        for param in itervalues(self._designvars):
            nparam += param['size']
        x_init = np.empty(nparam)

        # Initial Design Vars
        i = 0
        use_bounds = (opt in _bounds_optimizers)
        if use_bounds:
            bounds = []
        else:
            bounds = None

        for name, meta in iteritems(self._designvars):
            size = meta['size']
            x_init[i:i + size] = desvar_vals[name]
            i += size

            # Bounds if our optimizer supports them
            if use_bounds:
                meta_low = meta['lower']
                meta_high = meta['upper']
                for j in range(size):

                    if isinstance(meta_low, np.ndarray):
                        p_low = meta_low[j]
                    else:
                        p_low = meta_low

                    if isinstance(meta_high, np.ndarray):
                        p_high = meta_high[j]
                    else:
                        p_high = meta_high

                    bounds.append((p_low, p_high))

        if use_bounds and (opt in _supports_new_style) and _use_new_style:
            # For 'trust-constr' it is better to use the new type bounds, because it seems to work
            # better (for the current examples in the tests) with the "keep_feasible" option
            try:
                from scipy.optimize import Bounds
                from scipy.optimize._constraints import old_bound_to_new
            except ImportError:
                msg = (
                    'The "trust-constr" optimizer is supported for SciPy 1.1.0 and above. '
                    'The installed version is {}')
                raise ImportError(msg.format(scipy_version))

            # Convert "old-style" bounds to "new_style" bounds
            lower, upper = old_bound_to_new(bounds)  # tuple, tuple
            keep_feasible = self.opt_settings.get('keep_feasible_bounds', True)
            bounds = Bounds(lb=lower, ub=upper, keep_feasible=keep_feasible)

        # Constraints
        constraints = []
        i = 1  # start at 1 since row 0 is the objective.  Constraints start at row 1.
        lin_i = 0  # counter for linear constraint jacobian
        lincons = []  # list of linear constraints
        self._obj_and_nlcons = list(self._objs)

        if opt in _constraint_optimizers:
            for name, meta in iteritems(self._cons):
                size = meta['size']
                upper = meta['upper']
                lower = meta['lower']
                equals = meta['equals']
                if 'linear' in meta and meta['linear']:
                    lincons.append(name)
                    self._con_idx[name] = lin_i
                    lin_i += size
                else:
                    self._obj_and_nlcons.append(name)
                    self._con_idx[name] = i
                    i += size

                # In scipy constraint optimizers take constraints in two separate formats

                # Type of constraints is list of NonlinearConstraint
                if opt in _supports_new_style and _use_new_style:
                    try:
                        from scipy.optimize import NonlinearConstraint
                    except ImportError:
                        msg = (
                            'The "trust-constr" optimizer is supported for SciPy 1.1.0 and'
                            'above. The installed version is {}')
                        raise ImportError(msg.format(scipy_version))

                    if equals is not None:
                        lb = ub = equals
                    else:
                        lb = lower
                        ub = upper
                    # Loop over every index separately,
                    # because scipy calls each constraint by index.
                    for j in range(size):
                        # Double-sided constraints are accepted by the algorithm
                        args = [name, False, j]
                        # TODO linear constraint if meta['linear']
                        # TODO add option for Hessian
                        con = NonlinearConstraint(
                            fun=signature_extender(self._con_val_func, args),
                            lb=lb,
                            ub=ub,
                            jac=signature_extender(self._congradfunc, args))
                        constraints.append(con)
                else:  # Type of constraints is list of dict
                    # Loop over every index separately,
                    # because scipy calls each constraint by index.
                    for j in range(size):
                        con_dict = {}
                        if meta['equals'] is not None:
                            con_dict['type'] = 'eq'
                        else:
                            con_dict['type'] = 'ineq'
                        con_dict['fun'] = self._confunc
                        if opt in _constraint_grad_optimizers:
                            con_dict['jac'] = self._congradfunc
                        con_dict['args'] = [name, False, j]
                        constraints.append(con_dict)

                        if isinstance(upper, np.ndarray):
                            upper = upper[j]

                        if isinstance(lower, np.ndarray):
                            lower = lower[j]

                        dblcon = (upper < openmdao.INF_BOUND) and (
                            lower > -openmdao.INF_BOUND)

                        # Add extra constraint if double-sided
                        if dblcon:
                            dcon_dict = {}
                            dcon_dict['type'] = 'ineq'
                            dcon_dict['fun'] = self._confunc
                            if opt in _constraint_grad_optimizers:
                                dcon_dict['jac'] = self._congradfunc
                            dcon_dict['args'] = [name, True, j]
                            constraints.append(dcon_dict)

            # precalculate gradients of linear constraints
            if lincons:
                self._lincongrad_cache = self._compute_totals(
                    of=lincons, wrt=self._dvlist, return_format='array')
            else:
                self._lincongrad_cache = None

        # Provide gradients for optimizers that support it
        if opt in _gradient_optimizers:
            jac = self._gradfunc
        else:
            jac = None

        # Hessian calculation method for optimizers, which require it
        if opt in _hessian_optimizers:
            if 'hess' in self.opt_settings:
                hess = self.opt_settings.pop('hess')
            else:
                # Defaults to BFGS, if not in opt_settings
                from scipy.optimize import BFGS
                hess = BFGS()
        else:
            hess = None

        # compute dynamic simul deriv coloring if option is set
        if coloring_mod._use_total_sparsity:
            if self._coloring_info['coloring'] is coloring_mod._DYN_COLORING:
                coloring_mod.dynamic_total_coloring(
                    self,
                    run_model=False,
                    fname=self._get_total_coloring_fname())
            elif self.options['dynamic_simul_derivs']:
                warn_deprecation(
                    "The 'dynamic_simul_derivs' option has been deprecated. Call "
                    "the 'declare_coloring' function instead.")
                coloring_mod.dynamic_total_coloring(
                    self,
                    run_model=False,
                    fname=self._get_total_coloring_fname())

        # optimize
        try:
            if opt in _optimizers:
                result = minimize(
                    self._objfunc,
                    x_init,
                    # args=(),
                    method=opt,
                    jac=jac,
                    hess=hess,
                    # hessp=None,
                    bounds=bounds,
                    constraints=constraints,
                    tol=self.options['tol'],
                    # callback=None,
                    options=self.opt_settings)
            elif opt == 'basinhopping':
                from scipy.optimize import basinhopping

                def fun(x):
                    return self._objfunc(x), jac(x)

                if 'minimizer_kwargs' not in self.opt_settings:
                    self.opt_settings['minimizer_kwargs'] = {
                        "method": "L-BFGS-B",
                        "jac": True
                    }
                self.opt_settings.pop(
                    'maxiter')  # It does not have this argument

                def accept_test(f_new, x_new, f_old, x_old):
                    # Used to implement bounds besides the original functionality
                    if bounds is not None:
                        bound_check = all([
                            b[0] <= xi <= b[1] for xi, b in zip(x_new, bounds)
                        ])
                        user_test = self.opt_settings.pop('accept_test',
                                                          None)  # callable
                        # has to satisfy both the bounds and the acceptance test defined by the
                        # user
                        if user_test is not None:
                            test_res = user_test(f_new, x_new, f_old, x_old)
                            if test_res == 'force accept':
                                return test_res
                            else:  # result is boolean
                                return bound_check and test_res
                        else:  # no user acceptance test, check only the bounds
                            return bound_check
                    else:
                        return True

                result = basinhopping(fun,
                                      x_init,
                                      accept_test=accept_test,
                                      **self.opt_settings)
            elif opt == 'dual_annealing':
                from scipy.optimize import dual_annealing
                self.opt_settings.pop('disp')  # It does not have this argument
                # There is no "options" param, so "opt_settings" can be used to set the (many)
                # keyword arguments
                result = dual_annealing(self._objfunc,
                                        bounds=bounds,
                                        **self.opt_settings)
            elif opt == 'differential_evolution':
                from scipy.optimize import differential_evolution
                # There is no "options" param, so "opt_settings" can be used to set the (many)
                # keyword arguments
                result = differential_evolution(self._objfunc,
                                                bounds=bounds,
                                                **self.opt_settings)
            elif opt == 'shgo':
                from scipy.optimize import shgo
                kwargs = dict()
                for param in ('minimizer_kwargs', 'sampling_method ', 'n',
                              'iters'):
                    if param in self.opt_settings:
                        kwargs[param] = self.opt_settings[param]
                # Set the Jacobian and the Hessian to the value calculated in OpenMDAO
                if 'minimizer_kwargs' not in kwargs or kwargs[
                        'minimizer_kwargs'] is None:
                    kwargs['minimizer_kwargs'] = {}
                kwargs['minimizer_kwargs'].setdefault('jac', jac)
                kwargs['minimizer_kwargs'].setdefault('hess', hess)
                # Objective function tolerance
                self.opt_settings['f_tol'] = self.options['tol']
                result = shgo(self._objfunc,
                              bounds=bounds,
                              constraints=constraints,
                              options=self.opt_settings,
                              **kwargs)
            else:
                msg = 'Optimizer "{}" is not implemented yet. Choose from: {}'
                raise NotImplementedError(msg.format(opt, _all_optimizers))

        # If an exception was swallowed in one of our callbacks, we want to raise it
        # rather than the cryptic message from scipy.
        except Exception as msg:
            if self._exc_info is not None:
                self._reraise()
            else:
                raise

        if self._exc_info is not None:
            self._reraise()

        self.result = result

        if hasattr(result, 'success'):
            self.fail = False if result.success else True
            if self.fail:
                print('Optimization FAILED.')
                print(result.message)
                print('-' * 35)

            elif self.options['disp']:
                print('Optimization Complete')
                print('-' * 35)
        else:
            self.fail = True  # It is not known, so the worst option is assumed
            print('Optimization Complete (success not known)')
            print(result.message)
            print('-' * 35)

        return self.fail
예제 #25
0
# Calculate the initial sequencing distribution
# In this case we use the base method of allocating to each sample the same
#  amount of sequencing relative to the total redundancy budget allocated
#  for that category.
v_rho_t = np.repeat(rho_t_T/s, s)/eta
v_rho_n = np.repeat(rho_n_T/s, s)
x0 = np.append(v_rho_t, v_rho_n)
# Results of the initial distribution
print(beta_opt(x0), alpha_opt(x0))

# Define the total cost constraint
linear_constraint = LinearConstraint(np.append(eta, np.repeat(1,s)), 
                                     0, rho_t_T+rho_n_T)
# Define the type I error constraint
nonlinear_constraint = NonlinearConstraint(alpha_opt, 0, 0.051, jac='2-point', hess=BFGS())
# Define bounds on the sequencing redundancy (i.e. make sure it is not negative)
bounds = Bounds(np.repeat(0, 2*s), np.repeat(np.inf, 2*s))

# This runs the optimization
res = minimize(beta_opt, x0, method='trust-constr', 
               jac='2-point', hess=BFGS(),
               constraints=[linear_constraint, 
                            nonlinear_constraint],
               options={'verbose': 2}, bounds=bounds)


# Optimisation restults
print(beta_opt(res.x), alpha_opt(res.x))

# Outputs the number of samples that were included in the sequencing experiment
예제 #26
0
def test_traj_min():

    # change in position
    x1 = 1
    x2 = 2

    # initial velocity
    v1 = 0
    # guess
    v2 = 1

    dx = x2 - x1

    # performance constraints
    v_max = 5  # max velocity
    a_max = 5  # max acceleration

    def obj(v2):
        t = (2 * (x2 - x1)) / (v1 + v2)
        return t

    def cons_a(x):
        # contraint on acceleration
        a = (x[0]**2 - v1**2) / 2 / dx

        return [np.abs(a)]

    def cons_v(x):
        # contraint on velocity
        return np.abs(x)

    def callback(xk, state):
        pass

    nonlinear_constraint_1 = NonlinearConstraint(cons_a,
                                                 0,
                                                 a_max,
                                                 jac='2-point',
                                                 hess=BFGS())
    nonlinear_constraint_2 = NonlinearConstraint(cons_v,
                                                 0,
                                                 v_max,
                                                 jac='2-point',
                                                 hess=BFGS())

    res = minimize(
        obj,
        x0=v2,
        method='trust-constr',
        options={
            #'xtol': 1e-12,
            'maxiter': 20000,
            'disp': True,
            'verbose': 0,
        },
        #jac=jacobian_flux,
        callback=callback,
        constraints=[
            # sides must have some minimum length
            nonlinear_constraint_1,
            nonlinear_constraint_2
        ])

    dt = res.fun[0]
    v2 = res.x[0]

    # results

    # minimum dt
    print('dt', dt)
    # predicted final velocity
    print('v2', v2)
    # acceleration over the step
    print('acceleration', (v2 - v1) / dt)
    # distance travelled
    print('dx', ((v2 + v1) / 2) * dt)
예제 #27
0
        ax.plot(sets[i][0],
                sets[i][1],
                colores[i],
                label='Order ' + str(i + 1))
        if len(sets) > 1:
            plt.legend()
        plt.savefig(str(i) + '.eps')
    plt.show()


#compare(solver(t_months, t_pas, 1), solver(t_months, t_pas, 2), solver(t_months, t_pas, 3), solver(t_months, t_pas, 5))
th = sp.array([12.0, pi2 / 12, 4.9, 0.01])
th1 = sp.array([1.0, 1.0, 1.0, 0.01])
#compare(sp.array([all_months, sinusoide(th, all_months)]))

fit = BFGS(logpost, x0=th, args=(t_months, t_pas), approx_grad=True)[0]
#fit1 = BFGS(logpost, x0=th1, args=(t_months, t_pas), approx_grad=True)[0]
compare(sp.array([all_months, sinusoide(fit, all_months)]))
#approx_grad=1

print '#############################'
th2 = sp.array([30, 0.8, 3, 0.01])
th0 = sp.zeros(4)

A0, B0, C0, D0 = fit


def REsinusoide(theta, x, polorder=3):
    A, B, C, D = theta
    w = solver(t_months, t_pas, polorder)[3]
    pol = 0
    # definindo restrições lineares
    # x_0 + 2*x_1 <= 1.0
    # 2*x_0 + x_1 = 1.0
    lin_cons = LinearConstraint([[1.0, 2.0], [2.0, 1.0]], [-np.inf, 1.0],
                                [1.0, 1.0])

    # definindo restrições não-lineares
    # x_0**2 + x_1 <= 1.0
    # x_0**2 - x_1 <= 1.0
    # argumentos: função, limites, jacobiana e hessiana (usando aproximação
    # numérica - BFGS ou SR1 - ver documentação).
    nlin_cons = NonlinearConstraint(nl_cons,
                                    -np.inf,
                                    1.0,
                                    jac=nl_Jcons,
                                    hess=BFGS())
    #  nlin_cons = NonlinearConstraint(nl_cons, -np.inf, 1.0, jac='2-point',
    #                                  hess=BFGS())

    # minimizacao usando trust-constr com aproximações de jacobiano e hessiana
    # da função objetivo
    x0 = np.array([0.5, 0.0])
    res = minimize(fa,
                   x0,
                   method='trust-constr',
                   jac=Jfa,
                   hess='2-point',
                   constraints=[lin_cons, nlin_cons],
                   bounds=bnds,
                   options={'disp': True})
    print("x_0 = %5.5e" % (res.x[0]))
예제 #29
0
    return [1 - x[0]**2 - x[1]]


def cons_J(x):
    return [[-2 * x[0], -1.0]]


from scipy.optimize import LinearConstraint
linear_constraint = LinearConstraint([[2, 1]], [1], [1])

from scipy.optimize import NonlinearConstraint
nonlinear_constraint = NonlinearConstraint(cons_f,
                                           -np.inf,
                                           1,
                                           jac=cons_J,
                                           hess=BFGS())

x0 = np.array([0.5, 0])

res_cons = minimize(rosen,
                    x0,
                    method='trust-constr',
                    jac=rosen_der,
                    hess=BFGS(),
                    constraints=[linear_constraint, nonlinear_constraint],
                    options={'verbose': 1},
                    bounds=None)

res_uncons = minimize(rosen,
                      x0,
                      method='trust-constr',
예제 #30
0
    def matvec(p):
        return np.array([p[0]*2*(v[0]+v[1]), 0])
    return LinearOperator((2, 2), matvec=matvec)


# nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1, jac=cons_J, hess=cons_H)

# nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1,
#                                            jac=cons_J, hess=cons_H_sparse)

# nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1,
#                                           jac=cons_J, hess=cons_H_linear_operator)

#nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1, jac=cons_J, hess=BFGS())
#nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1, jac=cons_J, hess='2-point')
nonlinear_constraint = NonlinearConstraint(cons_f, -np.inf, 1, jac='2-point', hess=BFGS())

# %% rosenbrock

def rosen(x):
    """The Rosenbrock function"""
    return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

def rosen_der(x):
    xm = x[1:-1]
    xm_m1 = x[:-2]
    xm_p1 = x[2:]
    der = np.zeros_like(x)
    der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm)
    der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0])
    der[-1] = 200*(x[-1]-x[-2]**2)