예제 #1
0
def solve_problem(eigs, filename=None, use_stdout=False, use_tr=False):
    # Get the A matrix
    A = create_random_problem(eigs)

    # Create the other problem data
    b = np.random.uniform(size=len(eigs))
    Acon = np.random.uniform(size=len(eigs))
    bcon = np.random.uniform()

    problem = Quadratic(A, b, Acon, bcon)

    if use_tr:
        # Create the trust region problem
        max_lbfgs = 10
        tr_init_size = 0.05
        tr_min_size = 1e-6
        tr_max_size = 10.0
        tr_eta = 0.25
        tr_penalty_gamma = 10.0

        qn = ParOpt.LBFGS(problem, subspace=max_lbfgs)
        tr = ParOpt.pyTrustRegion(problem, qn, tr_init_size, tr_min_size,
                                  tr_max_size, tr_eta, tr_penalty_gamma)
        tr.setMaxTrustRegionIterations(500)

        # Set up the optimization problem
        tr_opt = ParOpt.pyParOpt(tr, 10, ParOpt.BFGS)
        if filename is not None and use_stdout is False:
            tr_opt.setOutputFile(filename)

        # Set the tolerances
        tr_opt.setAbsOptimalityTol(1e-8)
        tr_opt.setStartingPointStrategy(ParOpt.AFFINE_STEP)
        tr_opt.setStartAffineStepMultiplierMin(0.01)

        # Set optimization parameters
        tr_opt.setArmijoParam(1e-5)
        tr_opt.setMaxMajorIterations(5000)
        tr_opt.setBarrierPower(2.0)
        tr_opt.setBarrierFraction(0.1)

        # optimize
        tr.setOutputFile(filename + '_tr')
        tr.optimize(tr_opt)
    else:
        # Set up the optimization problem
        max_lbfgs = 10
        opt = ParOpt.pyParOpt(problem, max_lbfgs, ParOpt.BFGS)
        if filename is not None and use_stdout is False:
            opt.setOutputFile(filename)

        # Set optimization parameters
        opt.setArmijoParam(1e-5)
        opt.setMaxMajorIterations(5000)
        opt.setBarrierPower(2.0)
        opt.setBarrierFraction(0.1)
        opt.optimize()

    return
예제 #2
0
    problem.setPrefix(args.prefix)

    if use_tr:
        # Create the quasi-Newton Hessian approximation
        qn_subspace_size = 25
        qn = ParOpt.LBFGS(problem, subspace=qn_subspace_size)

        # Trust region problem parameters
        tr_size = 0.01
        tr_max_size = 0.02
        tr_min_size = 1e-6
        eta = 0.25
        tr_penalty = 10.0

        # Create the trust region problem
        tr = ParOpt.pyTrustRegion(problem, qn, tr_size, tr_min_size,
                                  tr_max_size, eta, tr_penalty)
        # Create the ParOpt problem
        opt = ParOpt.pyParOpt(tr, qn_subspace_size, ParOpt.NO_HESSIAN_APPROX)
        # Set the penalty parameter internally in the code. These must be
        # consistent between the trust region object and ParOpt.
        opt.setPenaltyGamma(tr_penalty)

        # Set parameters for ParOpt in the subproblem
        opt.setMaxMajorIterations(500)
        opt.setAbsOptimalityTol(1e-6)

        # Don't update the quasi-Newton method
        opt.setQuasiNewton(qn)
        opt.setUseQuasiNewtonUpdates(0)

        # Set the design variable bounds
예제 #3
0
def plot_it_all(problem, use_tr=False):
    '''
    Plot a carpet plot with the search histories for steepest descent,
    conjugate gradient and BFGS from the same starting point.
    '''

    # Set up the optimization problem
    max_lbfgs = 20
    opt = ParOpt.pyParOpt(problem, max_lbfgs, ParOpt.BFGS)
    opt.checkGradients(1e-6)

    # Create the data for the carpet plot
    n = 150
    xlow = -4.0
    xhigh = 4.0
    x1 = np.linspace(xlow, xhigh, n)

    ylow = -3.0
    yhigh = 3.0
    x2 = np.linspace(ylow, yhigh, n)
    r = np.zeros((n, n))

    for j in range(n):
        for i in range(n):
            fail, fobj, con = problem.evalObjCon([x1[i], x2[j]])
            r[j, i] = fobj

    # Assign the contour levels
    levels = np.min(r) + np.linspace(0, 1.0, 75)**2 * (np.max(r) - np.min(r))

    # Create the plot
    fig = plt.figure(facecolor='w')
    plt.contour(x1, x2, r, levels)
    plt.plot([0.5 - yhigh, 0.5 - ylow], [yhigh, ylow], '-k')

    colours = [
        '-bo', '-ko', '-co', '-mo', '-yo', '-bx', '-kx', '-cx', '-mx', '-yx'
    ]

    for k in range(len(colours)):
        # Optimize the problem
        problem.x_hist = []

        if use_tr:
            # Create the quasi-Newton Hessian approximation
            qn = ParOpt.LBFGS(problem, subspace=2)

            # Create the trust region problem
            tr_init_size = 0.05
            tr_min_size = 1e-6
            tr_max_size = 10.0
            tr_eta = 0.25
            tr_penalty_gamma = 10.0
            tr = ParOpt.pyTrustRegion(problem, qn, tr_init_size, tr_min_size,
                                      tr_max_size, tr_eta, tr_penalty_gamma)

            # Set up the optimization problem
            tr_opt = ParOpt.pyParOpt(tr, 2, ParOpt.BFGS)

            # Optimize
            tr.optimize(tr_opt)

            # Get the optimized point
            x, z, zw, zl, zu = tr_opt.getOptimizedPoint()
        else:
            opt.resetQuasiNewtonHessian()
            opt.setInitBarrierParameter(0.1)
            opt.setUseLineSearch(1)
            opt.optimize()

            # Get the optimized point and print out the data
            x, z, zw, zl, zu = opt.getOptimizedPoint()

        # Copy out the steepest descent points
        popt = np.zeros((2, len(problem.x_hist)))
        for i in range(len(problem.x_hist)):
            popt[0, i] = problem.x_hist[i][0]
            popt[1, i] = problem.x_hist[i][1]

        plt.plot(popt[0, :],
                 popt[1, :],
                 colours[k],
                 label='ParOpt %d' % (popt.shape[1]))
        plt.plot(popt[0, -1], popt[1, -1], '-ro')

        # Print the data to the screen
        g = np.zeros(2)
        A = np.zeros((1, 2))
        problem.evalObjConGradient(x, g, A)

        print('The design variables:    ', x[:])
        print('The multipliers:         ', z[:])
        print('The objective gradient:  ', g[:])
        print('The constraint gradient: ', A[:])

    ax = fig.axes[0]
    ax.set_aspect('equal', 'box')
    plt.legend()
    plt.show()
예제 #4
0
def solve_problem(eigs, filename=None, data_type='orthogonal', use_tr=False):
    # Create a random orthogonal Q vector
    if data_type == 'orthogonal':
        B = np.random.uniform(size=(n, n))
        Q, s, v = np.linalg.svd(B)

        # Create a random Affine matrix
        Affine = create_random_spd(eigs)
    else:
        Q = np.random.uniform(size=(n, n))
        Affine = np.diag(1e-3 * np.ones(n))

    # Create the random right-hand-side
    b = np.random.uniform(size=n)

    # Create the constraint data
    Acon = np.random.uniform(size=n)
    bcon = 0.25 * np.sum(Acon)

    # Create the convex problem
    problem = ConvexProblem(Q, Affine, b, Acon, bcon)

    if use_tr:
        # Create the trust region problem
        max_lbfgs = 10
        tr_init_size = 0.05
        tr_min_size = 1e-6
        tr_max_size = 10.0
        tr_eta = 0.25
        tr_penalty_gamma = 10.0

        qn = ParOpt.LBFGS(problem, subspace=max_lbfgs)
        tr = ParOpt.pyTrustRegion(problem, qn, tr_init_size, tr_min_size,
                                  tr_max_size, tr_eta, tr_penalty_gamma)
        tr.setMaxTrustRegionIterations(500)
        tr.setTrustRegionTolerances(1e-5, 1e-4, 0.0)

        # Set up the optimization problem
        tr_opt = ParOpt.pyParOpt(tr, 10, ParOpt.BFGS)
        if filename is not None:
            tr_opt.setOutputFile(filename)

        # Set the tolerances
        tr_opt.setAbsOptimalityTol(1e-8)
        tr_opt.setStartingPointStrategy(ParOpt.AFFINE_STEP)
        tr_opt.setStartAffineStepMultiplierMin(0.01)

        # Set optimization parameters
        tr_opt.setArmijoParam(1e-5)
        tr_opt.setMaxMajorIterations(5000)
        tr_opt.setBarrierPower(2.0)
        tr_opt.setBarrierFraction(0.1)

        # optimize
        tr.setOutputFile(filename + '_tr')
        tr.optimize(tr_opt)

        # Get the optimized point from the trust-region subproblem
        x, z, zw, zl, zu = tr_opt.getOptimizedPoint()
    else:
        # Set up the optimization problem
        max_lbfgs = 50
        opt = ParOpt.pyParOpt(problem, max_lbfgs, ParOpt.BFGS)
        if filename is not None:
            opt.setOutputFile(filename)

        # Set optimization parameters
        opt.setArmijoParam(1e-5)
        opt.setMaxMajorIterations(5000)
        opt.setBarrierPower(2.0)
        opt.setBarrierFraction(0.1)
        opt.optimize()

        # Get the optimized point
        x, z, zw, zl, zu = opt.getOptimizedPoint()

    return x