def run_HS13(Vec, params_dict): obj = HS13_Obj() x = Vec(2) d = Vec(2) HS13_initial_guess(x) l = Vec(1) l[0] = 1.0 icon = HS13_Icon() ilower = Vec(1) HS13_Ibnds(ilower) ibnd = ROL.Bounds(ilower, isLower=True) lower = Vec(2) HS13_Bnd(lower) bnd = ROL.Bounds(lower, isLower=True) params = ROL.ParameterList(params_dict, "Parameters") problem = ROL.OptimizationProblem(obj, x, bnd=bnd, icons=[icon], imuls=[l], ibnds=[ibnd]) solver = ROL.OptimizationSolver(problem, params) solver.solve() print(x[0], x[1]) assert HS13_minimum(x)
def get_rol_bounds(py_lb, py_ub): nvars = len(py_lb) # if np.all(py_lb == -np.inf) and np.all(py_ub == np.inf): # raise Exception('Bounds not needed lb and ub are -inf, +inf') if np.any(py_lb == np.inf): raise Exception('A lower bound was set to +inf') if np.any(py_ub == -np.inf): raise Exception('An upper bound was set to -inf') lb, ub = RolVector(nvars), RolVector(nvars) for ii in range(nvars): lb[ii], ub[ii] = py_lb[ii], py_ub[ii] # if np.all(py_lb == -np.inf) and not np.all(py_ub == np.inf): # return ROL.Bounds(ub, False, 1.0) # elif np.all(py_ub == np.inf) and not np.all(py_lb == -np.inf): # return ROL.Bounds(lb, True, 1.0) # avoid overflow warnings created by numpy_vector.py I = np.where(~np.isfinite(py_lb))[0] J = np.where(~np.isfinite(py_ub))[0] for ii in I: lb[ii] = -1e6 # -np.finfo(float).max/100 for jj in J: ub[jj] = 1e6 # np.finfo(float).max/100 # print(lb.data, ub.data) return ROL.Bounds(lb, ub, 1.0)
def solve(self): """ Solve the optimization problem and return the optimized parameters. """ bnd = self.bounds econs = self.constraints[0][0] emuls = self.constraints[0][1] icons = self.constraints[1][0] imuls = self.constraints[1][1] if len(icons) > 0: zeros = [i.clone() for i in imuls] ibnds = [ROL.Bounds(z, isLower=True) for z in zeros] else: ibnds = [] rolproblem = ROL.OptimizationProblem(self.rolobjective, self.rolvector, bnd=bnd, econs=econs, emuls=emuls, icons=icons, imuls=imuls, ibnds=ibnds) x = self.rolvector params = ROL.ParameterList(self.params_dict, "Parameters") self.solver = ROL.OptimizationSolver(rolproblem, params) self.solver.solve() return self.problem.reduced_functional.controls.delist(x.dat)
def __get_bounds(self): bounds = self.problem.bounds if bounds is None: return None controlvec = self.rolvector lowervec = controlvec.clone() uppervec = controlvec.clone() for i in range(len(controlvec.dat)): general_lb, general_ub = bounds[i] if isinstance(general_lb, (int, float)): lowervec.dat[i]._applyUnary(lambda x: general_lb) else: lowervec.dat[i].assign(general_lb) if isinstance(general_ub, (int, float)): uppervec.dat[i]._applyUnary(lambda x: general_ub) else: uppervec.dat[i].assign(general_ub) res = ROL.Bounds(lowervec, uppervec, 1.0) # FIXME: without this the lowervec and uppervec get cleaned up too # early. This is a bug in PyROL and we'll hopefully figure that out # soon self.lowervec = lowervec self.uppervec = uppervec return res
def createBounds(): x_lo = NumpyVector(2) x_lo[0] = -1 x_lo[1] = -1 x_up = NumpyVector(2) x_up[0] = +0.7 x_up[1] = +0.7 bnd = ROL.Bounds(x_lo, x_up, 1.0) bnd.test() return bnd
def get_problem(): obj = MyObj() x = NumpyVector(2) x_lo = NumpyVector(2) x_lo[0] = -1 x_lo[1] = -1 x_up = NumpyVector(2) x_up[0] = +0.7 x_up[1] = +0.7 bnd = ROL.Bounds(x_lo, x_up, 1.0) optimProblem = ROL.OptimizationProblem(obj, x, bnd=bnd) return optimProblem
def run_HS4(Vec, params_dict): obj = HS4_Obj() x = Vec(2) HS4_initial_guess(x) lower = Vec(2) upper = Vec(2) HS4_Bnd(lower, upper) bnd = ROL.Bounds(lower, upper, 1.0) params = ROL.ParameterList(params_dict, "Parameters") problem = ROL.OptimizationProblem(obj, x, bnd=bnd) solver = ROL.OptimizationSolver(problem, params) solver.solve() print(x[0], x[1]) assert HS4_minimum(x)
def run_B(algo): obj = MyObj() paramsDict["Step"]["Type"] = algo params = ROL.ParameterList(paramsDict, "Parameters") x = NumpyVector(2) x_lo = NumpyVector(2) x_lo[0] = -1 x_lo[1] = -1 x_up = NumpyVector(2) x_up[0] = +0.7 x_up[1] = +0.7 bnd = ROL.Bounds(x_lo, x_up, 1.0) optimProblem = ROL.OptimizationProblem(obj, x, bnd=bnd) solver = ROL.OptimizationSolver(optimProblem, params) solver.solve() assert round(x[0] - 0.7, 6) == 0.0 assert round(x[1], 6) == 0.0
def run_HS29(Vec, params_dict): params = ROL.ParameterList(params_dict, "Parameters") obj = HS29_Obj() x = Vec(3) x[0] = 1. x[1] = 2. x[2] = .1 d = Vec(3) d[0] = 1 d[1] = -1 d[2] = 1. v = Vec(3) v[0] = 1 v[1] = -1 v[2] = 1. # obj.checkGradient(x) # obj.checkHessVec(x, d, 4, 1) HS29_initial_guess(x) l = Vec(1) l[0] = 0.0 con = HS29_Icon() jv = Vec(1) jv[0] = 1. # con.checkApplyJacobian(x, d, jv, 4, 1) # con.checkAdjointConsistencyJacobian(jv, d, x) # con.checkApplyAdjointHessian(x, jv, d, v, 5, 1) ilower = Vec(1) HS29_Ibnds(ilower) ibnd = ROL.Bounds(ilower, isLower=True) problem = ROL.OptimizationProblem(obj, x, icons=[con], imuls=[l], ibnds=[ibnd]) solver = ROL.OptimizationSolver(problem, params) solver.solve() print(x[0], x[1], x[2]) assert HS29_minimum(x)
inner_product = L2Inner() obj = Objective(inner_product) u = Function(V, name="velocity").assign(vp) opt = FeVector(u.vector(), inner_product) # Add control bounds to the problem (uses more RAM) xlo = Function(V) xlo.interpolate(Constant(1.0)) x_lo = FeVector(xlo.vector(), inner_product) xup = Function(V) xup.interpolate(Constant(5.0)) x_up = FeVector(xup.vector(), inner_product) bnd = ROL.Bounds(x_lo, x_up, 1.0) algo = ROL.Algorithm("Line Search", params) algo.run(opt, obj, bnd) if comm.ensemble_comm.rank == 0: File("res.pvd", comm=comm.comm).write(vp) if COMM_WORLD.rank == 0: func.close() mem.close()
'Feasibility Tolerance Decrease Exponent': 0.9, 'Print Intermediate Optimization History': True, 'Subproblem Step Type': 'Line Search', 'Subproblem Iteration Limit': 3 } }, 'Status Test': { 'Gradient Tolerance': 1e-15, 'Relative Gradient Tolerance': 1e-10, 'Step Tolerance': 1e-16, 'Relative Step Tolerance': 1e-10, 'Iteration Limit': 30 } } params = ROL.ParameterList(paramsDict, "Parameters") bound_constraint = ROL.Bounds(lower, upper, 1.0) optimProblem = ROL.OptimizationProblem(obj, x, bnd=bound_constraint, econ=volConstr, emul=l_initializacao) solver = ROL.OptimizationSolver(optimProblem, params) solver.solve() print("aqui mudouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu") q.assign(0.1) interm = obj.resposta() del x interm = FeVector(interm.vector(), dot_product)
obj = Objective() x = ROL.StdVector(2) x[0] = -1.0 x[1] = 2.0 lower = ROL.StdVector(2) lower[0] = -10 lower[1] = -10 upper = ROL.StdVector(2) upper[0] = 0.5 upper[1] = 0.5 bnd = ROL.Bounds(lower, upper, 1.0) params = ROL.ParameterList(params_dict, "Parameters") problem = ROL.OptimizationProblem(obj, x) solver = ROL.OptimizationSolver(problem, params) solver.solve() # algo = ROL.Algorithm("Line Search", params) # algo.run(x, obj) print(x[0]) print(x[1]) problem = ROL.OptimizationProblem(obj, x, bnd=bnd) solver = ROL.OptimizationSolver(problem, params) solver.solve() print(x[0])