示例#1
0
    def _setup_driver(self, problem):
        """
        Prepare the driver for execution.

        This is the final thing to run during setup.

        Parameters
        ----------
        paropt_problem : <Problem>
            Pointer
        """
        # TODO:
        # - logic for different opt algorithms
        # - treat equality constraints

        super(ParOptDriver, self)._setup_driver(problem)

        # Raise error if multiple objectives are provided
        if len(self._objs) > 1:
            msg = 'ParOpt currently does not support multiple objectives.'
            raise RuntimeError(msg.format(self.__class__.__name__))

        # Create the ParOptProblem from the OpenMDAO problem
        self.paropt_problem = ParOptProblem(problem)

        # Take only the options declared from ParOpt
        info = ParOpt.getOptionsInfo()
        paropt_options = {}
        for key in self.options:
            if key in info.keys():
                paropt_options[key] = self.options[key]

        self.opt = ParOpt.Optimizer(self.paropt_problem, paropt_options)

        return
示例#2
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.TrustRegion(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.InteriorPoint(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.setPrintLevel(1)
        tr.optimize(tr_opt)
    else:
        # Set up the optimization problem
        max_lbfgs = 10
        opt = ParOpt.InteriorPoint(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
示例#3
0
def solve_problem(eigs, filename=None, use_stdout=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)

    # Set up the optimization problem
    max_lbfgs = 40
    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
示例#4
0
文件: dmo_opt.py 项目: afcarl/paropt
def paropt_truss(truss,
                 use_hessian=False,
                 max_qn_subspace=50,
                 qn_type=ParOpt.BFGS):
    '''
    Optimize the given truss structure using ParOpt
    '''

    # Create the optimizer
    opt = ParOpt.pyParOpt(truss, max_qn_subspace, qn_type)

    # Set the optimality tolerance
    opt.setAbsOptimalityTol(1e-5)

    # Set the Hessian-vector product iterations
    if use_hessian:
        opt.setUseLineSearch(0)
        opt.setUseHvecProduct(1)
        opt.setGMRESSubspaceSize(100)
        opt.setNKSwitchTolerance(1.0)
        opt.setEisenstatWalkerParameters(0.5, 0.0)
        opt.setGMRESTolerances(1.0, 1e-30)
    else:
        opt.setUseHvecProduct(0)

    # Set optimization parameters
    opt.setArmijioParam(1e-5)
    opt.setMaxMajorIterations(2500)

    # Perform a quick check of the gradient (and Hessian)
    opt.checkGradients(1e-6)

    return opt
示例#5
0
def paropt_truss(truss, use_hessian=False):
    '''
    Optimize the given truss structure using ParOpt
    '''

    fname = os.path.join(prefix, 'truss_paropt%dx%d.out' % (N, M))
    options = {
        'algorithm': 'ip',
        'qn_subspace_size': 10,
        'abs_res_tol': 1e-6,
        'barrier_strategy': 'complementarity_fraction',
        'use_hvec_product': True,
        'gmres_subspace_size': 25,
        'nk_switch_tol': 1.0,
        'eisenstat_walker_gamma': 0.01,
        'eisenstat_walker_alpha': 0.0,
        'max_gmres_rtol': 1.0,
        'output_level': 1,
        'armijo_constant': 1e-5,
        'output_file': fname
    }

    if use_hessian is False:
        options['use_hvec_product'] = False

    opt = ParOpt.Optimizer(truss, options)
    opt.optimize()

    return opt
示例#6
0
    def __init__(self, *args, **kwargs):
        name = "ParOpt"
        category = "Local Optimizer"
        if _ParOpt is None:
            raise Error("There was an error importing ParOpt")

        # Create and fill-in the dictionary of default option values
        defOpts = {}
        options = _ParOpt.getOptionsInfo()
        for option_name in options:
            # Get the type and default value of the named argument
            _type = None
            if options[option_name].option_type == "bool":
                _type = bool
            elif options[option_name].option_type == "int":
                _type = int
            elif options[option_name].option_type == "float":
                _type = float
            else:
                _type = str
            default_value = options[option_name].default

            # Set the entry into the dictionary
            defOpts[option_name] = [_type, default_value]

        self.set_options = {}
        informs = {}
        Optimizer.__init__(self, name, category, defOpts, informs, *args,
                           **kwargs)

        # ParOpt requires a dense Jacobian format
        self.jacType = "dense2d"

        return
示例#7
0
def plot_it_all(problem):
    """
    Plot a carpet plot with the search histories for steepest descent,
    conjugate gradient and BFGS from the same starting point.
    """

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

    for j in range(n):
        for i in range(n):
            fail, fobj, con = problem.evalObjCon([x1[i], x1[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, x1, r, levels)

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

    options = {
        'algorithm': 'tr',
        'tr_init_size': 0.05,
        'tr_min_size': 1e-6,
        'tr_max_size': 10.0,
        'tr_eta': 0.1,
        'tr_adaptive_gamma_update': True,
        'tr_max_iterations': 200
    }

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

        opt = ParOpt.Optimizer(rosen, options)
        opt.optimize()

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

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

    plt.legend()
    plt.axis([xlow, xhigh, xlow, xhigh])
    plt.show()
示例#8
0
文件: examples.py 项目: afcarl/paropt
def plot_it_all(problem):
    '''
    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)
    #opt.setGradientCheckFrequency(10, 1e-6)

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

    for j in range(n):
        for i in range(n):
            fail, fobj, con = problem.evalObjCon([x1[i], x1[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, x1, r, levels)

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

    for k in range(len(colours)):
        # Optimize the problem
        problem.x_hist = []
        opt.resetQuasiNewtonHessian()
        opt.setInitBarrierParameter(0.1)
        opt.setUseLineSearch(1)
        opt.optimize()

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

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

    plt.legend()
    plt.axis([xlow, xhigh, xlow, xhigh])
    plt.show()
示例#9
0
def create_paropt(analysis,
                  use_hessian=False,
                  tol=1e-5,
                  max_qn_subspace=50,
                  qn_type=ParOpt.BFGS):
    '''
    Optimize the given structure using ParOpt
    '''

    # Set the inequality options
    analysis.setInequalityOptions(dense_ineq=True,
                                  sparse_ineq=False,
                                  use_lower=True,
                                  use_upper=True)

    # Create the optimizer
    opt = ParOpt.pyParOpt(analysis, max_qn_subspace, qn_type)

    # Set the optimality tolerance
    opt.setAbsOptimalityTol(tol)

    # Set the Hessian-vector product iterations
    if use_hessian:
        opt.setUseLineSearch(1)
        opt.setUseHvecProduct(1)
        opt.setGMRESSubspaceSize(100)
        opt.setNKSwitchTolerance(1e3)
        opt.setEisenstatWalkerParameters(0.25, 1e-3)
        opt.setGMRESTolerances(0.25, 1e-30)
    else:
        opt.setUseHvecProduct(0)

    # Set the starting point strategy
    opt.setStartingPointStrategy(ParOpt.AFFINE_STEP)

    # Set the barrier strategy to use
    opt.setBarrierStrategy(ParOpt.COMPLEMENTARITY_FRACTION)

    # Set the norm to use
    opt.setNormType(ParOpt.L1_NORM)

    # Set optimization parameters
    opt.setArmijoParam(1e-5)
    opt.setMaxMajorIterations(5000)

    # Perform a quick check of the gradient (and Hessian)
    opt.checkGradients(1e-8)

    # Set the output level to understand what is going on
    opt.setOutputLevel(2)

    return opt
示例#10
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)

    options = {
        'algorithm': 'ip',
        'abs_res_tol': 1e-8,
        'starting_point_strategy': 'affine_step',
        'barrier_strategy': 'monotone',
        'start_affine_multiplier_min': 0.01,
        'penalty_gamma': 1000.0,
        'qn_subspace_size': 10,
        'qn_type': 'bfgs',
        'output_file': filename}

    if use_tr:
        options = {
            'algorithm': 'tr',
            'tr_init_size': 0.05,
            'tr_min_size': 1e-6,
            'tr_max_size': 10.0,
            'tr_eta': 0.25,
            'tr_adaptive_gamma_update': True,
            'tr_max_iterations': 200,
            'penalty_gamma': 10.0,
            'qn_subspace_size': 10,
            'qn_type': 'bfgs',
            'abs_res_tol': 1e-8,
            'output_file': filename,
            'tr_output_file': os.path.splitext(filename)[0] + '.tr',
            'starting_point_strategy': 'affine_step',
            'barrier_strategy': 'monotone',
            'start_affine_multiplier_min': 0.01}

    opt = ParOpt.Optimizer(problem, options)

    # Set a new starting point
    opt.optimize()
    x, z, zw, zl, zu = opt.getOptimizedPoint()

    return
示例#11
0
    def _declare_options(self):
        """
        Declare options before kwargs are processed in the init method.
        """

        info = ParOpt.getOptionsInfo()

        for name in info:
            default = info[name].default
            descript = info[name].descript
            values = info[name].values
            if info[name].option_type == 'bool':
                self.options.declare(name, default, types=bool, desc=descript)
            elif info[name].option_type == 'int':
                self.options.declare(name,
                                     default,
                                     types=int,
                                     lower=values[0],
                                     upper=values[1],
                                     desc=descript)
            elif info[name].option_type == 'float':
                self.options.declare(name,
                                     default,
                                     types=float,
                                     lower=values[0],
                                     upper=values[1],
                                     desc=descript)
            elif info[name].option_type == 'str':
                if default is None:
                    self.options.declare(name,
                                         default,
                                         types=str,
                                         allow_none=True,
                                         desc=descript)
                else:
                    self.options.declare(name,
                                         default,
                                         types=str,
                                         desc=descript)
            elif info[name].option_type == 'enum':
                self.options.declare(name,
                                     default,
                                     values=values,
                                     desc=descript)

        return
示例#12
0
文件: truss_opt.py 项目: detu/paropt
def paropt_truss(truss, use_hessian=False, use_tr=False, prefix='results'):
    """
    Optimize the given truss structure using ParOpt
    """

    fname = os.path.join(prefix, 'truss_paropt%dx%d.out' % (N, M))
    options = {
        'algorithm': 'ip',
        'qn_subspace_size': 10,
        'abs_res_tol': 1e-5,
        'norm_type': 'l1',
        'init_barrier_param': 10.0,
        'monotone_barrier_fraction': 0.75,
        'barrier_strategy': 'complementarity_fraction',
        'starting_point_strategy': 'least_squares_multipliers',
        'use_hvec_product': True,
        'gmres_subspace_size': 50,
        'nk_switch_tol': 1e3,
        'eisenstat_walker_gamma': 0.01,
        'eisenstat_walker_alpha': 0.0,
        'max_gmres_rtol': 1.0,
        'output_level': 1,
        'armijo_constant': 1e-5,
        'output_file': fname
    }

    if use_tr:
        options['algorithm'] = 'tr'
        options['abs_res_tol'] = 1e-8
        options['barrier_strategy'] = 'monotone'
        options['tr_max_size'] = 100.0
        options['tr_linfty_tol'] = 1e-5
        options['tr_l1_tol'] = 0.0
        options['tr_max_iterations'] = 2000
        options['tr_penalty_gamma_max'] = 1e6
        options['tr_adaptive_gamma_update'] = True
        options['tr_output_file'] = fname.split('.')[0] + '.tr'

    if use_hessian is False:
        options['use_hvec_product'] = False

    opt = ParOpt.Optimizer(truss, options)
    opt.optimize()

    return opt
示例#13
0
def create_paropt(analysis, use_hessian=False,
                  max_qn_subspace=50, qn_type=ParOpt.BFGS):
    '''
    Optimize the given structure using ParOpt
    '''

    # Set the inequality options
    analysis.setInequalityOptions(dense_ineq=True, 
                                  sparse_ineq=False,
                                  use_lower=True,
                                  use_upper=True)
    
    # Create the optimizer
    opt = ParOpt.pyParOpt(analysis, max_qn_subspace, qn_type)

    # Set the optimality tolerance
    opt.setAbsOptimalityTol(1e-6)

    # Set the Hessian-vector product iterations
    if use_hessian:
        opt.setUseLineSearch(1)
        opt.setUseHvecProduct(1)
        opt.setGMRESSubspaceSize(100)
        opt.setNKSwitchTolerance(1.0)
        opt.setEisenstatWalkerParameters(0.5, 0.0)
        opt.setGMRESTolerances(0.1, 1e-30)
    else:
        opt.setUseHvecProduct(0)

    # Set the barrier strategy to use
    opt.setBarrierStrategy(ParOpt.MONOTONE)

    # Set the norm to use
    opt.setNormType(ParOpt.L1_NORM)

    # Set optimization parameters
    opt.setArmijoParam(1e-5)
    opt.setMaxMajorIterations(5000)

    # Perform a quick check of the gradient (and Hessian)
    opt.checkGradients(1e-8)

    return opt
示例#14
0
def solve_problem(eigs, filename=None, data_type='orthogonal'):
    # 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)

    # 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.checkGradients(1e-6)

    # Set optimization parameters
    opt.setArmijioParam(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
示例#15
0
    def __init__(self, raiseError=True, options={}):
        name = "ParOpt"
        category = "Local Optimizer"
        if _ParOpt is None:
            if raiseError:
                raise Error("There was an error importing ParOpt")

        # Create and fill-in the dictionary of default option values
        self.defOpts = {}
        paropt_default_options = _ParOpt.getOptionsInfo()
        # Manually override the options with missing default values
        paropt_default_options["ip_checkpoint_file"].default = "default.out"
        paropt_default_options["problem_name"].default = "problem"
        for option_name in paropt_default_options:
            # Get the type and default value of the named argument
            _type = None
            if paropt_default_options[option_name].option_type == "bool":
                _type = bool
            elif paropt_default_options[option_name].option_type == "int":
                _type = int
            elif paropt_default_options[option_name].option_type == "float":
                _type = float
            else:
                _type = str
            default_value = paropt_default_options[option_name].default

            # Set the entry into the dictionary
            self.defOpts[option_name] = [_type, default_value]

        self.set_options = {}
        self.informs = {}
        super().__init__(name,
                         category,
                         defaultOptions=self.defOpts,
                         informs=self.informs,
                         options=options)

        # ParOpt requires a dense Jacobian format
        self.jacType = "dense2d"

        return
示例#16
0
def paropt_truss(truss, use_hessian=False,
                 prefix='results'):
    '''
    Optimize the given truss structure using ParOpt
    '''

    # Create the optimizer
    max_qn_subspace = 10
    opt = ParOpt.InteriorPoint(truss, max_qn_subspace, ParOpt.BFGS)

    # Set the optimality tolerance
    opt.setAbsOptimalityTol(1e-6)
    opt.setBarrierStrategy(ParOpt.COMPLEMENTARITY_FRACTION)

    # Set the Hessian-vector product iterations
    if use_hessian:
        # opt.setUseLineSearch(0)
        opt.setUseHvecProduct(1)
        opt.setGMRESSubspaceSize(25)
        opt.setNKSwitchTolerance(1.0)
        opt.setEisenstatWalkerParameters(0.01, 0.0)
        opt.setGMRESTolerances(1.0, 1e-30)
    else:
        opt.setUseHvecProduct(0)

    # Set the output level
    opt.setOutputLevel(1)

    # Set optimization parameters
    opt.setArmijoParam(1e-5)
    opt.setMaxMajorIterations(2500)

    # Set the output file to use
    fname = os.path.join(prefix, 'truss_paropt%dx%d.out'%(N, M))
    opt.setOutputFile(fname)

    # Optimize the truss
    opt.optimize()

    return opt
示例#17
0
def paropt_truss(truss, use_hessian=False, prefix='results'):
    '''
    Optimize the given truss structure using ParOpt
    '''

    # Create the optimizer
    max_qn_subspace = 30
    opt = ParOpt.pyParOpt(truss, max_qn_subspace, ParOpt.BFGS)

    # Set the optimality tolerance
    opt.setAbsOptimalityTol(1e-6)

    # Set the Hessian-vector product iterations
    if use_hessian:
        opt.setUseLineSearch(0)
        opt.setUseHvecProduct(1)
        opt.setGMRESSubspaceSize(100)
        opt.setNKSwitchTolerance(1.0)
        opt.setEisenstatWalkerParameters(0.5, 0.0)
        opt.setGMRESTolerances(1.0, 1e-30)
    else:
        opt.setUseHvecProduct(0)
        opt.setHessianResetFreq(max_qn_subspace)

    # Set optimization parameters
    opt.setArmijioParam(1e-5)
    opt.setMaxMajorIterations(2500)

    # Set the output file to use
    fname = os.path.join(prefix, 'truss_paropt%dx%d.out' % (N, M))
    opt.setOutputFile(fname)

    # Optimize the truss
    opt.optimize()

    return opt
示例#18
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.1
        tr_penalty_gamma = 10.0

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

        infeas_tol = 1e-6
        l1_tol = 1e-5
        linfty_tol = 1e-5
        tr.setTrustRegionTolerances(infeas_tol, l1_tol, linfty_tol)

        # Set up the optimization problem
        tr_opt = ParOpt.InteriorPoint(subproblem, 10, ParOpt.BFGS)
        if filename is not None:
            tr_opt.setOutputFile(filename)
            tr.setOutputFile(os.path.splitext(filename)[0] + '.tr')

        # 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.setPrintLevel(1)
        tr.optimize(tr_opt)

        # Get the optimized point from the trust-region subproblem
        x = tr.getOptimizedPoint()
    else:
        # Set up the optimization problem
        max_lbfgs = 50
        opt = ParOpt.InteriorPoint(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
示例#19
0
    def __call__(self,
                 optProb,
                 sens=None,
                 sensStep=None,
                 sensMode=None,
                 storeHistory=None,
                 hotStart=None,
                 storeSens=True):
        """
        This is the main routine used to solve the optimization
        problem.

        Parameters
        ----------
        optProb : Optimization or Solution class instance
            This is the complete description of the optimization problem
            to be solved by the optimizer

        sens : str or python Function.
            Specifiy method to compute sensitivities. To
            explictly use pyOptSparse gradient class to do the
            derivatives with finite differenes use \'FD\'. \'sens\'
            may also be \'CS\' which will cause pyOptSpare to compute
            the derivatives using the complex step method. Finally,
            \'sens\' may be a python function handle which is expected
            to compute the sensitivities directly. For expensive
            function evaluations and/or problems with large numbers of
            design variables this is the preferred method.

        sensStep : float
            Set the step size to use for design variables. Defaults to
            1e-6 when sens is \'FD\' and 1e-40j when sens is \'CS\'.

        sensMode : str
            Use \'pgc\' for parallel gradient computations. Only
            available with mpi4py and each objective evaluation is
            otherwise serial

        storeHistory : str
            File name of the history file into which the history of
            this optimization will be stored

        hotStart : str
            File name of the history file to "replay" for the
            optimziation.  The optimization problem used to generate
            the history file specified in \'hotStart\' must be
            **IDENTICAL** to the currently supplied \'optProb\'. By
            identical we mean, **EVERY SINGLE PARAMETER MUST BE
            IDENTICAL**. As soon as he requested evaluation point
            from ParOpt does not match the history, function and
            gradient evaluations revert back to normal evaluations.

        storeSens : bool
            Flag sepcifying if sensitivities are to be stored in hist.
            This is necessay for hot-starting only.
            """

        self.callCounter = 0
        self.storeSens = storeSens

        if len(optProb.constraints) == 0:
            # If the problem is unconstrained, add a dummy constraint.
            self.unconstrained = True
            optProb.dummyConstraint = True

        # Save the optimization problem and finalize constraint
        # jacobian, in general can only do on root proc
        self.optProb = optProb
        self.optProb.finalizeDesignVariables()
        self.optProb.finalizeConstraints()
        self._setInitialCacheValues()
        self._setSens(sens, sensStep, sensMode)
        blx, bux, xs = self._assembleContinuousVariables()
        xs = np.maximum(xs, blx)
        xs = np.minimum(xs, bux)

        # The number of design variables
        n = len(xs)

        oneSided = True

        if self.unconstrained:
            m = 0
        else:
            indices, blc, buc, fact = self.optProb.getOrdering(
                ['ne', 'le', 'ni', 'li'], oneSided=oneSided)
            m = len(indices)
            self.optProb.jacIndices = indices
            self.optProb.fact = fact
            self.optProb.offset = buc

        if self.optProb.comm.rank == 0:
            # Set history/hotstart
            self._setHistory(storeHistory, hotStart)

            class Problem(_ParOpt.Problem):
                def __init__(self, ptr, n, m, xs, blx, bux):
                    super(Problem, self).__init__(MPI.COMM_SELF, n, m)
                    self.ptr = ptr
                    self.n = n
                    self.m = m
                    self.xs = xs
                    self.blx = blx
                    self.bux = bux
                    self.fobj = 0.0
                    return

                def getVarsAndBounds(self, x, lb, ub):
                    '''Get the variable values and bounds'''
                    lb[:] = self.blx
                    ub[:] = self.bux
                    x[:] = self.xs
                    return

                def evalObjCon(self, x):
                    '''Evaluate the objective and constraint values'''
                    fobj, fcon, fail = self.ptr._masterFunc(
                        x[:], ['fobj', 'fcon'])
                    self.fobj = fobj
                    return fail, fobj, -fcon

                def evalObjConGradient(self, x, g, A):
                    gobj, gcon, fail = self.ptr._masterFunc(
                        x[:], ['gobj', 'gcon'])
                    g[:] = gobj[:]
                    for i in range(self.m):
                        A[i][:] = -gcon[i][:]
                    return fail

            # Create the ParOpt problem class
            problem = Problem(self, n, m, xs, blx, bux)

            # Get the algorithm/subspace size parameters
            algorithm = self.getOption('algorithm').lower()
            qn_subspace_size = self.getOption('qn_subspace_size')
            filename = self.getOption('filename')

            optTime = MPI.Wtime()
            if algorithm == 'ip':
                # Create the optimizer
                opt = _ParOpt.InteriorPoint(problem, qn_subspace_size,
                                            _ParOpt.BFGS)

                # Set the ParOpt options
                self._set_paropt_options(opt)

                # Optimize!
                opt.setOutputFile(filename)
                opt.optimize()
            else:
                norm_type = self.getOption('norm_type').lower()

                # Optimality tolerance
                opt_tol = self.getOption('abs_optimality_tol')

                # Trust region algorithm options
                tr_init_size = self.getOption('tr_init_size')
                tr_max_size = self.getOption('tr_max_size')
                tr_min_size = self.getOption('tr_min_size')
                tr_eta = self.getOption('tr_eta')
                tr_penalty_gamma = self.getOption('tr_penalty_gamma')
                tr_opt_abs_tol = self.getOption('tr_abs_optimality_tol')
                tr_max_iterations = self.getOption('tr_max_iterations')

                # Create the quasi-Newton Hessian approximation
                qn = _ParOpt.LBFGS(problem, subspace=qn_subspace_size)

                # Create the trust region problem
                tr = _ParOpt.TrustRegion(problem, qn, tr_init_size,
                                         tr_min_size, tr_max_size, tr_eta,
                                         tr_penalty_gamma)

                # Create the ParOpt problem
                opt = _ParOpt.InteriorPoint(tr, qn_subspace_size,
                                            _ParOpt.NO_HESSIAN_APPROX)

                # Set the ParOpt options
                self._set_paropt_options(opt)

                # Set the output file name
                opt.setOutputFile(filename)

                # Set the penalty parameter internally in the
                # code. These must be consistent between the trust
                # region object and ParOpt.
                opt.setPenaltyGamma(tr_penalty_gamma)

                # Set parameters for ParOpt in the subproblem
                opt.setMaxMajorIterations(tr_max_iterations)
                opt.setAbsOptimalityTol(tr_opt_abs_tol)

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

                # Check the norm type
                if norm_type == 'l1':
                    opt.setNormType(_ParOpt.L1_NORM)
                elif norm_type == 'linfty':
                    opt.setNormType(_ParOpt.INFTY_NORM)
                else:
                    opt.setNormType(_ParOpt.L2_NORM)

                # Initialize the problem
                tr.initialize()

                # Iterate
                max_iterations = self.getOption('max_iterations')
                for i in range(max_iterations):
                    opt.setInitBarrierParameter(100.0)
                    opt.resetDesignAndBounds()
                    opt.optimize()

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

                    # Update the trust region method
                    infeas, l1, linfty = tr.update(x, z, zw)

                    if norm_type == 'l1':
                        opt_criteria = (l1 < opt_tol)
                    else:
                        opt_criteria = (linfty < opt_tol)
                    if ((infeas < opt_tol) and opt_criteria):
                        break

            # Set the total opt time
            optTime = MPI.Wtime() - optTime

            # Get the obective function value
            fobj = problem.fobj

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

            # Create the optimization solution
            sol_inform = {}
            sol = self._createSolution(optTime, sol_inform, fobj, x[:])

            # Indicate solution finished
            self.optProb.comm.bcast(-1, root=0)
        else:  # We are not on the root process so go into waiting loop:
            self._waitLoop()
            sol = None

        # Communication solution and return
        sol = self._communicateSolution(sol)

        return sol
示例#20
0
    options = {
        'algorithm': 'tr',
        'tr_init_size': 0.05,
        'tr_min_size': 1e-6,
        'tr_max_size': 10.0,
        'tr_eta': 0.25,
        'tr_infeas_tol': 1e-6,
        'tr_l1_tol': 1e-3,
        'tr_linfty_tol': 0.0,
        'tr_adaptive_gamma_update': True,
        'tr_max_iterations': 1000,
        'penalty_gamma': 10.0,
        'qn_subspace_size': 10,
        'qn_type': 'bfgs',
        'abs_res_tol': 1e-8,
        'starting_point_strategy': 'affine_step',
        'barrier_strategy': 'mehrotra_predictor_corrector',
        'tr_steering_barrier_strategy':
            'mehrotra_predictor_corrector',
        'tr_steering_starting_point_strategy': 'affine_step',
        'use_line_search': False}

    # Set up the optimizer
    opt = ParOpt.Optimizer(problem, options)

    # Set a new starting point
    opt.optimize()
    x, z, zw, zl, zu = opt.getOptimizedPoint()

示例#21
0
        A[0][:] = -(dfdx - product) / self.thickness_scale

        # Write out the solution file every 10 iterations
        if self.iter_count % 10 == 0:
            self.f5.writeToFile('ucrm_iter%d.f5' % (self.iter_count))
        self.iter_count += 1

        return fail


# Load structural mesh from BDF file
tacs_comm = MPI.COMM_WORLD
bdf_name = 'CRM_box_2nd.bdf'

crm_opt = uCRM_VonMisesMassMin(tacs_comm, bdf_name)

# Set up the optimization problem
max_lbfgs = 5
opt = ParOpt.pyParOpt(crm_opt, max_lbfgs, ParOpt.BFGS)
opt.setOutputFile('crm_opt.out')

# Set optimization parameters
opt.checkGradients(1e-6)

# Set optimization parameters
opt.setArmijoParam(1e-5)
opt.optimize()

# Get the optimized point
x, z, zw, zl, zu = opt.getOptimizedPoint()
示例#22
0
    force1.scale(-1.0)
    # Set the load cases
    forces = [force1]
    problem.setLoadCases(forces)

    # Set the mass constraint
    problem.addConstraints(0, funcs, [-m_fixed], [-1.0 / m_fixed])
    problem.setObjective(obj_array)

    # Initialize the problem and set the prefix
    problem.initialize()
    problem.setPrefix(args.prefix)

    if use_paropt:
        # Create the ParOpt problem
        opt = ParOpt.pyParOpt(problem, args.max_lbfgs, ParOpt.BFGS)

        # Set the norm type to use
        opt.setNormType(ParOpt.L1_NORM)

        # Set parameters
        opt.setMaxMajorIterations(args.max_opt_iters)
        opt.setHessianResetFreq(args.hessian_reset)
        problem.setIterationCounter(args.max_opt_iters * ite)
        opt.setAbsOptimalityTol(args.opt_abs_tol)
        opt.setBarrierFraction(args.opt_barrier_frac)
        opt.setBarrierPower(args.opt_barrier_power)
        opt.setOutputFrequency(args.output_freq)
        opt.setOutputFile(
            os.path.join(args.prefix, 'paropt_output%d.out' % (ite)))
示例#23
0
import matplotlib.pylab as plt
import numpy as np
import argparse

# Import ParOpt so that we can read the ParOpt output file
from paropt import ParOpt

p = argparse.ArgumentParser('Plot values from a paropt output file')
p.add_argument('filename',
               metavar='paropt.out',
               type=str,
               help='ParOpt output file name')
args = p.parse_args()

# Unpack the output file
header, values = ParOpt.unpack_output(args.filename)

# Set font info
font = {'family': 'sans-serif', 'weight': 'normal', 'size': 17}
matplotlib.rc('font', **font)

# You can get more stuff out of this array
iteration = np.linspace(1, len(values[0]), len(values[0]))
objective = values[7]
opt = values[8]
infeas = values[9]
barrier = values[11]

# Just make the iteration linear
iteration = np.linspace(1, len(iteration), len(iteration))
示例#24
0
        plt.draw()
        plt.pause(0.001)

        return


if __name__ == '__main__':
    nxelems = 3 * 48
    nyelems = 48
    Lx = 15.0
    Ly = 5.0
    problem = TopoAnalysis(nxelems, nyelems, Lx, Ly, E0=70e3, r0=3)
    problem.checkGradients()

    # Create the quasi-Newton Hessian approximation
    qn = ParOpt.LBFGS(problem, subspace=10)

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

    # Set the tolerances
    infeas_tol = 1e-4
    l1_tol = 1e-3
    linfty_tol = 1e-3
    tr.setTrustRegionTolerances(infeas_tol, l1_tol, linfty_tol)
示例#25
0
    def __call__(self,
                 optProb,
                 sens=None,
                 sensStep=None,
                 sensMode=None,
                 storeHistory=None,
                 hotStart=None,
                 storeSens=True):
        """
        This is the main routine used to solve the optimization
        problem.

        Parameters
        ----------
        optProb : Optimization or Solution class instance
            This is the complete description of the optimization problem
            to be solved by the optimizer

        sens : str or python Function.
            Specifiy method to compute sensitivities. To
            explictly use pyOptSparse gradient class to do the
            derivatives with finite differenes use \'FD\'. \'sens\'
            may also be \'CS\' which will cause pyOptSpare to compute
            the derivatives using the complex step method. Finally,
            \'sens\' may be a python function handle which is expected
            to compute the sensitivities directly. For expensive
            function evaluations and/or problems with large numbers of
            design variables this is the preferred method.

        sensStep : float
            Set the step size to use for design variables. Defaults to
            1e-6 when sens is \'FD\' and 1e-40j when sens is \'CS\'.

        sensMode : str
            Use \'pgc\' for parallel gradient computations. Only
            available with mpi4py and each objective evaluation is
            otherwise serial

        storeHistory : str
            File name of the history file into which the history of
            this optimization will be stored

        hotStart : str
            File name of the history file to "replay" for the
            optimziation.  The optimization problem used to generate
            the history file specified in \'hotStart\' must be
            **IDENTICAL** to the currently supplied \'optProb\'. By
            identical we mean, **EVERY SINGLE PARAMETER MUST BE
            IDENTICAL**. As soon as he requested evaluation point
            from ParOpt does not match the history, function and
            gradient evaluations revert back to normal evaluations.

        storeSens : bool
            Flag sepcifying if sensitivities are to be stored in hist.
            This is necessay for hot-starting only.
            """

        self.callCounter = 0
        self.storeSens = storeSens

        if len(optProb.constraints) == 0:
            # If the problem is unconstrained, add a dummy constraint.
            self.unconstrained = True
            optProb.dummyConstraint = True

        # Save the optimization problem and finalize constraint
        # jacobian, in general can only do on root proc
        self.optProb = optProb
        self.optProb.finalizeDesignVariables()
        self.optProb.finalizeConstraints()
        self._setInitialCacheValues()
        self._setSens(sens, sensStep, sensMode)
        blx, bux, xs = self._assembleContinuousVariables()
        xs = np.maximum(xs, blx)
        xs = np.minimum(xs, bux)

        # The number of design variables
        n = len(xs)

        oneSided = True

        if self.unconstrained:
            m = 0
        else:
            indices, blc, buc, fact = self.optProb.getOrdering(
                ["ne", "le", "ni", "li"], oneSided=oneSided)
            m = len(indices)
            self.optProb.jacIndices = indices
            self.optProb.fact = fact
            self.optProb.offset = buc

        if self.optProb.comm.rank == 0:
            # Set history/hotstart
            self._setHistory(storeHistory, hotStart)

            class Problem(_ParOpt.Problem):
                def __init__(self, ptr, n, m, xs, blx, bux):
                    super(Problem, self).__init__(MPI.COMM_SELF, n, m)
                    self.ptr = ptr
                    self.n = n
                    self.m = m
                    self.xs = xs
                    self.blx = blx
                    self.bux = bux
                    self.fobj = 0.0
                    return

                def getVarsAndBounds(self, x, lb, ub):
                    """Get the variable values and bounds"""
                    # Find the average distance between lower and upper bound
                    bound_sum = 0.0
                    for i in range(len(x)):
                        if self.blx[i] <= -1e20 or self.bux[i] >= 1e20:
                            bound_sum += 1.0
                        else:
                            bound_sum += self.bux[i] - self.blx[i]
                    bound_sum = bound_sum / len(x)

                    for i in range(len(x)):
                        x[i] = self.xs[i]
                        lb[i] = self.blx[i]
                        ub[i] = self.bux[i]
                        if self.xs[i] <= self.blx[i]:
                            x[i] = self.blx[i] + 0.5 * np.min(
                                (bound_sum, self.bux[i] - self.blx[i]))
                        elif self.xs[i] >= self.bux[i]:
                            x[i] = self.bux[i] - 0.5 * np.min(
                                (bound_sum, self.bux[i] - self.blx[i]))

                    return

                def evalObjCon(self, x):
                    """Evaluate the objective and constraint values"""
                    fobj, fcon, fail = self.ptr._masterFunc(
                        x[:], ["fobj", "fcon"])
                    self.fobj = fobj
                    return fail, fobj, -fcon

                def evalObjConGradient(self, x, g, A):
                    """Evaluate the objective and constraint gradients"""
                    gobj, gcon, fail = self.ptr._masterFunc(
                        x[:], ["gobj", "gcon"])
                    g[:] = gobj[:]
                    for i in range(self.m):
                        A[i][:] = -gcon[i][:]
                    return fail

            optTime = MPI.Wtime()

            # Optimize the problem
            problem = Problem(self, n, m, xs, blx, bux)
            optimizer = _ParOpt.Optimizer(problem, self.set_options)
            optimizer.optimize()
            x, z, zw, zl, zu = optimizer.getOptimizedPoint()

            # Set the total opt time
            optTime = MPI.Wtime() - optTime

            # Get the obective function value
            fobj = problem.fobj

            if self.storeHistory:
                self.metadata["endTime"] = datetime.datetime.now().strftime(
                    "%Y-%m-%d %H:%M:%S")
                self.metadata["optTime"] = optTime
                self.hist.writeData("metadata", self.metadata)
                self.hist.close()

            # Create the optimization solution. Note that the signs on the multipliers
            # are switch since ParOpt uses a formulation with c(x) >= 0, while pyOpt
            # uses g(x) = -c(x) <= 0. Therefore the multipliers are reversed.
            sol_inform = {}

            # If number of constraints is zero, ParOpt returns z as None.
            # Thus if there is no constraints, should pass an empty list
            # to multipliers instead of z.
            if z is not None:
                sol = self._createSolution(optTime,
                                           sol_inform,
                                           fobj,
                                           x[:],
                                           multipliers=-z)
            else:
                sol = self._createSolution(optTime,
                                           sol_inform,
                                           fobj,
                                           x[:],
                                           multipliers=[])

            # Indicate solution finished
            self.optProb.comm.bcast(-1, root=0)
        else:  # We are not on the root process so go into waiting loop:
            self._waitLoop()
            sol = None

        # Communication solution and return
        sol = self._communicateSolution(sol)

        return sol
示例#26
0

# Parse the arguments
parser = argparse.ArgumentParser()
parser.add_argument('--qn_type', type=str, default='sr1')
args = parser.parse_args()
qn_type = args.qn_type

# This test compares a limited-memory Hessian with their dense counterparts
n = 50
eigs = np.linspace(1, 1 + n, n)
problem = Quadratic(eigs)

if qn_type == 'sr1':
    # Create the LSR1 object
    qn = ParOpt.LSR1(problem, subspace=n)
else:
    # Create the limited-memory BFGS object
    qn = ParOpt.LBFGS(problem, subspace=n)

# Create a random set of steps and their corresponding vectors
S = np.random.uniform(size=(n, n))
Y = np.dot(problem.A, S)

# Create paropt vectors
ps = problem.createDesignVec()
py = problem.createDesignVec()

# Compute the update to the
y0 = Y[:, -1]
s0 = S[:, -1]
示例#27
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.
    """

    # Check the problem gradients
    problem.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 = []

        # Set the trust region parameters
        filename = 'paropt.out'

        options = {
            'algorithm': 'ip',
            'abs_res_tol': 1e-8,
            'starting_point_strategy': 'affine_step',
            'barrier_strategy': 'monotone',
            'start_affine_multiplier_min': 0.01,
            'penalty_gamma': 1000.0,
            'qn_subspace_size': 10,
            'qn_type': 'bfgs'
        }

        if use_tr:
            options = {
                'algorithm': 'tr',
                'tr_init_size': 0.05,
                'tr_min_size': 1e-6,
                'tr_max_size': 10.0,
                'tr_eta': 0.25,
                'penalty_gamma': 10.0,
                'qn_subspace_size': 10,
                'qn_type': 'bfgs',
                'abs_res_tol': 1e-8,
                'output_file': filename,
                'tr_output_file': os.path.splitext(filename)[0] + '.tr',
                'starting_point_strategy': 'affine_step',
                'barrier_strategy': 'monotone',
                'start_affine_multiplier_min': 0.01
            }

        opt = ParOpt.Optimizer(problem, options)

        # Set a new starting point
        opt.optimize()
        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()
示例#28
0
文件: polygon.py 项目: detu/paropt
        'use_backtracking_alpha': True,
        'output_level': 1,
        'max_major_iters': 1000
    }

    # use trust region algorithm
    if use_tr:
        options = {
            'algorithm': 'tr',
            'qn_type': 'bfgs',
            'abs_res_tol': 1e-8,
            'output_level': 0,
            'use_backtracking_alpha': True,
            'max_major_iters': 100,
            'tr_init_size': 0.1,
            'tr_min_size': 1e-6,
            'tr_max_size': 1.0,
            'tr_eta': 0.25,
            'penalty_gamma': 1.0,
            'tr_adaptive_gamma_update': True,
            'tr_penalty_gamma_max': 1e5,
            'tr_penalty_gamma_min': 1e-5,
            'tr_max_iterations': 500,
            'use_line_search': False
        }

    polygon = Polygon(args.n)
    polygon.checkGradients()
    opt = ParOpt.Optimizer(polygon, options)
    opt.optimize()
示例#29
0
文件: sellar.py 项目: nwu63/paropt
        '''Evaluate the objective and constraint'''
        fail = 0
        fobj = x[1] * x[1] + x[0] + x[2] + np.exp(-x[3])
        cons = np.array([x[0] + x[1] - 1.0])
        return fail, fobj, cons

    def evalObjConGradient(self, x, g, A):
        '''Evaluate the objective and constraint gradient'''
        fail = 0

        g[0] = 1.0
        g[1] = 2.0 * x[1]
        g[2] = 1.0
        g[3] = -np.exp(-x[3])

        A[0][0] = 1.0
        A[0][1] = 1.0

        return fail


# Allocate the optimization problem
problem = Sellar()

# Set up the optimization problem
max_lbfgs = 50
opt = ParOpt.InteriorPoint(problem, max_lbfgs, ParOpt.BFGS)

# Optimize
opt.optimize()
示例#30
0
        plt.pause(0.001)

        return

if __name__ == '__main__':
    nxelems = 128
    nyelems = 128
    Lx = 8.0
    Ly = 8.0
    problem = TopoAnalysis(nxelems, nyelems,
                           Lx, Ly, E0=70e3, r0=3, kappa=70e3,
                           thermal_problem=True)
    problem.checkGradients()

    # Create the quasi-Newton Hessian approximation
    qn = ParOpt.LBFGS(problem, subspace=10)

    # Create the trust region problem
    tr_init_size = 0.02
    tr_min_size = 1e-6
    tr_max_size = 0.05
    tr_eta = 0.2
    tr_penalty_gamma = 10.0
    subproblem = ParOpt.QuadraticSubproblem(problem, qn)
    tr = ParOpt.TrustRegion(subproblem, tr_init_size,
                            tr_min_size, tr_max_size,
                            tr_eta, tr_penalty_gamma)

    # Set the tolerances
    infeas_tol = 1e-4
    l1_tol = 1e-3