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
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
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
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()
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
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
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
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.pyParOpt(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
# 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 filename = os.path.join(args.prefix, 'paropt%d.out' % (ite)) opt.setOutputFile(filename)
max_mma_iters = 10 problem = Toy(comm) problem.setInequalityOptions(dense_ineq=True, sparse_ineq=False, use_lower=True, use_upper=True) # Set the ParOpt problem into MMA mma = ParOpt.pyMMA(problem, use_mma=True) mma.setInitAsymptoteOffset(0.5) mma.setMinAsymptoteOffset(0.01) mma.setBoundRelax(1e-4) mma.setOutputFile(os.path.join(args.prefix, 'mma_output.out')) # Create the ParOpt problem opt = ParOpt.pyParOpt(mma, args.max_lbfgs, ParOpt.BFGS) # Set parameters opt.setMaxMajorIterations(args.max_opt_iters) opt.setHessianResetFreq(args.hessian_reset) opt.setAbsOptimalityTol(args.opt_abs_tol) opt.setBarrierFraction(args.opt_barrier_frac) opt.setBarrierPower(args.opt_barrier_power) opt.setOutputFrequency(args.output_freq) opt.setAbsOptimalityTol(1e-7) opt.setUseDiagHessian(1) # Set the starting point using the mass fraction x = mma.getOptimizedPoint() print 'Initial x = ', np.array(x) #problem.setInitDesignVars(x)
# Set the constraints funcs = [functions.StructuralMass(assembler)] initial_mass = assembler.evalFunctions(funcs) m_fixed = vol_frac*vol problem.addConstraints(0, funcs, [-m_fixed], [-1.0/m_fixed]) problem.addConstraints(1, [], [], []) problem.setObjective([1.0, 1.0]) # Initialize the problem and set the prefix problem.initialize() problem.setPrefix('./results/') max_bfgs = 20 opt = ParOpt.pyParOpt(problem, max_bfgs, ParOpt.BFGS) opt.setOutputFrequency(1) opt.setOutputFile("paropt_output.out") opt.optimize() print assembler.evalFunctions(funcs)/initial_mass # Output for visualization flag = (TACS.ToFH5.NODES | TACS.ToFH5.DISPLACEMENTS | TACS.ToFH5.STRAINS | TACS.ToFH5.STRESSES | TACS.ToFH5.EXTRAS) f5 = TACS.ToFH5(assembler, TACS.PY_SOLID, flag) f5.writeToFile('bracket.f5')
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
max_lanczos = 100 tol = 1e-16 problem.addBucklingConstraint(sigma, num_eigs, ks_weight, offset, scale, max_lanczos, tol) problem.setObjective(obj_array, funcs) #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 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))) opt.checkGradients(1e-6) opt.setRelFunctionTol(1e-7) # If the old filter exists, we're on the second iteration if old_filtr:
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)))
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()
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()
self.pc.applyFactor(self.svsens, self.adj) self.tacs.evalAdjointResProduct(self.adj, self.adjSensProdArray) A[:] = -1.0 * (self.tempdvsens + self.adjSensProdArray) # Scale the gradient values g[:] *= self.xscale A[:] *= self.xscale return fail # Create the problem object sizing = CRMSizing(tacs, f5) max_lbfgs = 30 opt = ParOpt.pyParOpt(sizing, max_lbfgs, ParOpt.BFGS) # Set the output file opt.setOutputFile('paropt_history.out') # Set optimization parameters opt.setGMRESSubspaceSize(30) opt.setNKSwitchTolerance(1e3) opt.setGMRESTolerances(0.1, 1e-30) opt.setUseHvecProduct(0) opt.setMajorIterStepCheck(45) opt.setMaxMajorIterations(20000) opt.setGradientCheckFrequency(100, 1e-6) opt.setMaxLineSearchIters(5) opt.setBarrierFraction(0.25) opt.setBarrierPower(1.15)