def getMin(*args): """Solves a constrained optimization problem Basic solve: getMin(X,Y,Z,msg,fns,state) Solve with a state manipulator: getMin(X,Y,Z,msg,smanip,fns,state) """ # Check the number of arguments if len(args)!=6 and len(args)!=7: raise Exception("The getMin function requires either 6 or 7 arguments, " "but %d given." % len(args)) # Extract the arguments X=args[0] Y=args[1] Z=args[2] msg=args[3] fns = args[4] state = args[5] smanip = Optizelle.StateManipulator() if len(args)==6 else args[6] # Check the arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkVectorSpace("Y",Y) Optizelle.checkVectorSpace("Z",Z) Optizelle.checkMessaging("msg",msg) Optizelle.Constrained.Functions.checkT("fns",fns) Optizelle.Constrained.State.checkT("state",state) Optizelle.checkStateManipulator("smanip",smanip) # Call the optimization Optizelle.Utility.ConstrainedAlgorithmsGetMin(X,Y,Z,msg,fns,state,smanip)
def register(self, fn, vector_type): """Extends the current function with fn. This function will only be called if the first argument matches vector_type.""" # Check our arguments Optizelle.checkFunction("(de)serialize", fn) Optizelle.checkType("vector_type", vector_type) # Register the function self.fns[vector_type] = fn
class t(object): """All the functions required by an optimization algorithm""" def __init__(self): self._f = Optizelle.ScalarValuedFunction() self._PH = Optizelle.Operator() # Create all of the properties f = Optizelle.createScalarValuedFunctionProperty("f", "Objective function") PH = Optizelle.createOperatorProperty( "PH", "Preconditioner for the Hessian of the objective")
def register(self,fn,vector_type): """Extends the current function with fn. This function will only be called if the first argument matches vector_type.""" # Check our arguments Optizelle.checkFunction("(de)serialize",fn) Optizelle.checkType("vector_type",vector_type) # Register the function self.fns[vector_type]=fn
def getMin(*args): """Solves an equality constrained optimization problem Basic solve: getMin(X,Y,msg,fns,state) Solve with a state manipulator: getMin(X,Y,msg,fns,state,smanip) """ # Check the number of arguments if len(args) != 5 and len(args) != 6: raise Exception( "The getMin function requires either 5 or 6 arguments, " "but %d given." % len(args)) # Extract the arguments X = args[0] Y = args[1] msg = args[2] fns = args[3] state = args[4] smanip = Optizelle.StateManipulator() if len(args) == 5 else args[5] # Check the arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkVectorSpace("Y", Y) Optizelle.checkMessaging("msg", msg) Optizelle.EqualityConstrained.Functions.checkT("fns", fns) Optizelle.EqualityConstrained.State.checkT("state", state) Optizelle.checkStateManipulator("smanip", smanip) # Call the optimization Optizelle.Utility.EqualityConstrainedAlgorithmsGetMin( X, Y, msg, fns, state, smanip)
def __init__(self, X, msg, x): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkMessaging("msg", msg) # Allocate memory for our vectors allocateVectors(self, X, x) # Create the state Optizelle.Utility.UnconstrainedStateCreate(self, X, msg, x)
def __init__(self,X,msg,x): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkMessaging("msg",msg) # Allocate memory for our vectors allocateVectors(self,X,x) # Create the state Optizelle.Utility.UnconstrainedStateCreate(self,X,msg,x)
def read(X,Y,Z,msg,fname,state): """Read parameters from file""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkVectorSpace("Y",Y) Optizelle.checkVectorSpace("Z",Z) Optizelle.checkMessaging("msg",msg) Optizelle.checkString("fname",fname) Optizelle.Constrained.State.checkT("state",state) # Do the read Optizelle.Utility.ConstrainedStateReadJson(X,Y,Z,msg,fname,state)
def write_restart(X,Y,Z,msg,fname,state): """Writes a json restart file""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkVectorSpace("Y",Y) Optizelle.checkVectorSpace("Z",Z) Optizelle.checkMessaging("msg",msg) Optizelle.checkString("fname",fname) Optizelle.Constrained.State.checkT("state",state) # Do the write Optizelle.Utility.ConstrainedRestartWriteRestart( X,Y,Z,msg,fname,state)
def read_restart(X,Y,Z,msg,fname,x,y,z,state): """Reads a json restart file""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkVectorSpace("Y",Y) Optizelle.checkVectorSpace("Z",Z) Optizelle.checkMessaging("msg",msg) Optizelle.checkString("fname",fname) Optizelle.Constrained.State.checkT("state",state) # Do the read Optizelle.Utility.ConstrainedRestartReadRestart( X,Y,Z,msg,fname,x,y,z,state)
def release(X, msg, state, xs, reals, nats, params): """Release the data into structures controlled by the user""" # Check the arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkMessaging("msg", msg) Optizelle.Unconstrained.State.checkT("state", state) # Release the information from the state Optizelle.Utility.UnconstrainedRestartRelease(X, msg, state, xs, reals, nats, params) # Return nothing. We've modified the passed in lists. return None
def release(X,msg,state,xs,reals,nats,params): """Release the data into structures controlled by the user""" # Check the arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkMessaging("msg",msg) Optizelle.Unconstrained.State.checkT("state",state) # Release the information from the state Optizelle.Utility.UnconstrainedRestartRelease( X,msg,state,xs,reals,nats,params) # Return nothing. We've modified the passed in lists. return None
def solve(self): """Solve the optimization problem and return the optimized parameters.""" if self.problem.constraints is None: num_equality_constraints = 0 num_inequality_constraints = 0 + len( self.bound_inequality_constraints) else: num_equality_constraints = self.problem.constraints.equality_constraints( )._get_constraint_dim() num_inequality_constraints = self.problem.constraints.inequality_constraints( )._get_constraint_dim() + len(self.bound_inequality_constraints) # No constraints if num_equality_constraints == 0 and num_inequality_constraints == 0: Optizelle.Unconstrained.Algorithms.getMin(DolfinVectorSpace, Optizelle.Messaging(), self.fns, self.state) # Equality constraints only elif num_equality_constraints > 0 and num_inequality_constraints == 0: Optizelle.EqualityConstrained.Algorithms.getMin( DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), self.fns, self.state) # Inequality constraints only elif num_equality_constraints == 0 and num_inequality_constraints > 0: Optizelle.InequalityConstrained.Algorithms.getMin( DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), self.fns, self.state) # Inequality and equality constraints else: Optizelle.Constrained.Algorithms.getMin(DolfinVectorSpace, DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), self.fns, self.state) # Print out the reason for convergence # FIXME: Use logging print("The algorithm stopped due to: %s" % (Optizelle.StoppingCondition.to_string(self.state.opt_stop))) # Return the optimal control list_type = self.problem.reduced_functional.controls return delist(self.state.x, list_type)
class t(Optizelle.Unconstrained.Functions.t): """All the functions required by an optimization algorithm""" def __init__(self): super(t,self).__init__() self._h=Optizelle.VectorValuedFunction() # Create all of the properties h = Optizelle.createVectorValuedFunctionProperty( "h", "Inequality constraints")
def __init__(self,X,Z,msg,x,z): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkEuclidean("Z",Z) Optizelle.checkMessaging("msg",msg) # Allocate memory for our vectors Optizelle.Unconstrained.State.allocateVectors(self,X,x) allocateVectors(self,X,Z,x,z) # Create the state Optizelle.Utility.InequalityConstrainedStateCreate(self,X,Z,msg,x,z)
def capture(X,msg,state,xs,reals,nats,params): """Capture data from structures controlled by the user.""" # Check the arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkMessaging("msg",msg) Optizelle.Unconstrained.State.checkT("state",state) Optizelle.checkVectors('xs',xs) Optizelle.checkReals('reals',reals) Optizelle.checkNaturals('nats',nats) Optizelle.checkParams('params',params) # Capture the restart information Optizelle.Utility.UnconstrainedRestartCapture( X,msg,state,xs,reals,nats,params) # Return nothing. The state has been modified. return None
# Read in the name for the input file if len(sys.argv) != 2: sys.exit("simple_equality.py <parameters>") fname = sys.argv[1] #---State0--- # Generate an initial guess x = numpy.array([2.1, 1.1]) # Allocate memory for the equality multiplier y = numpy.array([0.]) # Create an optimization state state = Optizelle.EqualityConstrained.State.t(Optizelle.Rm, Optizelle.Rm, Optizelle.Messaging(), x, y) #---State1--- #---Parameters0--- # Read the parameters from file Optizelle.json.EqualityConstrained.read(Optizelle.Rm, Optizelle.Rm, Optizelle.Messaging(), fname, state) #---Parameters1--- #---Functions0--- # Create a bundle of functions fns = Optizelle.EqualityConstrained.Functions.t() fns.f = MyObj() fns.g = MyEq() fns.PSchur_left = MyPrecon() #---Functions1---
def __init__(self): self._f = Optizelle.ScalarValuedFunction() self._PH = Optizelle.Operator()
def __build_optizelle_state(self): # Optizelle does not support maximization problem directly, # hence we negate the functional instead if isinstance(self.problem, MaximizationProblem): scale = -1 else: scale = +1 bound_inequality_constraints = [] if self.problem.bounds is not None: # We need to process the damn bounds for (control, bound) in zip(self.problem.reduced_functional.controls, self.problem.bounds): (lb, ub) = bound if lb is not None: bound_inequality_constraints.append( OptizelleBoundConstraint(control.data(), lb, 'lower')) if ub is not None: bound_inequality_constraints.append( OptizelleBoundConstraint(control.data(), ub, 'upper')) self.bound_inequality_constraints = bound_inequality_constraints # Create the appropriate Optizelle state, taking into account which # type of constraints we have (unconstrained, (in)-equality constraints). if self.problem.constraints is None: num_equality_constraints = 0 num_inequality_constraints = 0 + len(bound_inequality_constraints) else: num_equality_constraints = self.problem.constraints.equality_constraints( )._get_constraint_dim() num_inequality_constraints = self.problem.constraints.inequality_constraints( )._get_constraint_dim() + len(bound_inequality_constraints) x = [p.data() for p in self.problem.reduced_functional.controls] # Unconstrained case if num_equality_constraints == 0 and num_inequality_constraints == 0: self.state = Optizelle.Unconstrained.State.t( DolfinVectorSpace, Optizelle.Messaging(), x) self.fns = Optizelle.Unconstrained.Functions.t() self.fns.f = OptizelleObjective(self.problem.reduced_functional, scale=scale) log(INFO, "Found no constraints.") # Equality constraints only elif num_equality_constraints > 0 and num_inequality_constraints == 0: # Allocate memory for the equality multiplier equality_constraints = self.problem.constraints.equality_constraints( ) y = equality_constraints.output_workspace() self.state = Optizelle.EqualityConstrained.State.t( DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), x, y) self.fns = Optizelle.Constrained.Functions.t() self.fns.f = OptizelleObjective(self.problem.reduced_functional, scale=scale) self.fns.g = OptizelleConstraints(self.problem, equality_constraints) log( INFO, "Found no equality and %i inequality constraints." % equality_constraints._get_constraint_dim()) # Inequality constraints only elif num_equality_constraints == 0 and num_inequality_constraints > 0: # Allocate memory for the inequality multiplier if self.problem.constraints is not None: inequality_constraints = self.problem.constraints.inequality_constraints( ) all_inequality_constraints = constraints.MergedConstraints( inequality_constraints.constraints + bound_inequality_constraints) else: all_inequality_constraints = constraints.MergedConstraints( bound_inequality_constraints) z = all_inequality_constraints.output_workspace() self.state = Optizelle.InequalityConstrained.State.t( DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), x, z) self.fns = Optizelle.InequalityConstrained.Functions.t() self.fns.f = OptizelleObjective(self.problem.reduced_functional, scale=scale) self.fns.h = OptizelleConstraints(self.problem, all_inequality_constraints) log( INFO, "Found no equality and %i inequality constraints." % all_inequality_constraints._get_constraint_dim()) # Inequality and equality constraints else: # Allocate memory for the equality multiplier equality_constraints = self.problem.constraints.equality_constraints( ) y = equality_constraints.output_workspace() # Allocate memory for the inequality multiplier if self.problem.constraints is not None: inequality_constraints = self.problem.constraints.inequality_constraints( ) all_inequality_constraints = constraints.MergedConstraints( inequality_constraints.constraints + bound_inequality_constraints) else: all_inequality_constraints = constraints.MergedConstraints( bound_inequality_constraints) z = all_inequality_constraints.output_workspace() self.state = Optizelle.Constrained.State.t(DolfinVectorSpace, DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), x, y, z) self.fns = Optizelle.Constrained.Functions.t() self.fns.f = OptizelleObjective(self.problem.reduced_functional, scale=scale) self.fns.g = OptizelleConstraints(self.problem, equality_constraints) self.fns.h = OptizelleConstraints(self.problem, all_inequality_constraints) log( INFO, "Found %i equality and %i inequality constraints." % (equality_constraints._get_constraint_dim(), all_inequality_constraints._get_constraint_dim())) # Set solver parameters self.__set_optizelle_parameters()
def capture(X, Y, Z, msg, state, xs, ys, zs, reals, nats, params): """Capture data from structures controlled by the user.""" # Check the arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkVectorSpace("Y", Y) Optizelle.checkVectorSpace("Z", Z) Optizelle.checkMessaging("msg", msg) Optizelle.Constrained.State.checkT("state", state) Optizelle.checkVectors('xs', xs) Optizelle.checkVectors('ys', ys) Optizelle.checkVectors('zs', zs) Optizelle.checkReals('reals', reals) Optizelle.checkNaturals('nats', nats) Optizelle.checkParams('params', params) # Capture the restart information Optizelle.Utility.ConstrainedRestartCapture(X, Y, Z, msg, state, xs, ys, zs, reals, nats, params) # Return nothing. The state has been modified. return None
def serialize(x): """Converts a vector to a JSON formatted string""" raise Optizelle.Exception( "The serialize function for the vector %s not defined." % str(x))
result[0]=one_over_det*(200.*dx[0]+400.*x[0]*dx[1]) result[1]=(one_over_det* (400.*x[0]*dx[0]+(1200.*x[0]*x[0]-400.*x[1]+2.)*dx[1])) #---Preconditioner1--- # Read in the name for the input file if len(sys.argv)!=2: sys.exit("python rosenbrock.py <parameters>") fname = sys.argv[1] #---State0--- # Generate an initial guess for Rosenbrock x = numpy.array([-1.2,1.0]) # Create an unconstrained state based on this vector state=Optizelle.Unconstrained.State.t(Optizelle.Rm,Optizelle.Messaging(),x) #---State1--- #---Parameters0--- # Read the parameters from file Optizelle.json.Unconstrained.read(Optizelle.Rm,Optizelle.Messaging(), fname,state) #---Parameters1--- #---Functions0--- # Create the bundle of functions fns=Optizelle.Unconstrained.Functions.t() fns.f=Rosenbrock() fns.PH=RosenHInv() #---Functions1---
def deserialize(x, x_json): """Converts a JSON formatted string to a vector""" raise Optizelle.Exception( "The deserialize function for the vector %s not defined." % str(x))
rname = sys.argv[2] if len(sys.argv) == 3 else "" # Generate an initial guess for Rosenbrock x = array.array('d', [-1.2, 1.0]) # Create an unconstrained state based on this vector state = Optizelle.Unconstrained.State.t(MyVS, MyMessaging(), x) #---ReadRestart0--- # If we have a restart file, read in the parameters if len(sys.argv) == 3: Optizelle.json.Unconstrained.read_restart(MyVS, MyMessaging(), rname, x, state) # Read additional parameters from file Optizelle.json.Unconstrained.read(MyVS, Optizelle.Messaging(), pname, state) #---ReadRestart1--- # Create the bundle of functions fns = Optizelle.Unconstrained.Functions.t() fns.f = Rosenbrock() fns.PH = RosenHInv() #---Solver0--- # Solve the optimization problem Optizelle.Unconstrained.Algorithms.getMin(MyVS, MyMessaging(), fns, state, MyRestartManipulator()) #---Solver1--- # Print out the reason for convergence print("The algorithm converged due to: %s" %
class t(Optizelle.Unconstrained.State.t): """Internal state of the optimization""" def __init__(self, X, Y, msg, x, y): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkVectorSpace("Y", Y) Optizelle.checkMessaging("msg", msg) # Allocate memory for our vectors Optizelle.Unconstrained.State.allocateVectors(self, X, x) allocateVectors(self, X, Y, x, y) # Create the state Optizelle.Utility.EqualityConstrainedStateCreate(self, X, Y, msg, x, y) # Create all of the properties y = Optizelle.createVectorProperty( "y", "Equality multiplier (dual variable or Lagrange multiplier)") dy = Optizelle.createVectorProperty("dy", "Step in the equality multiplier") zeta = Optizelle.createFloatProperty( "zeta", "The fraction of the total trust-region used for the quasi-norm step") eta0 = Optizelle.createFloatProperty( "eta0", ("Trust-region parameter that bounds the error in the predicted " "reduction")) rho = Optizelle.createFloatProperty( "rho", "Penalty parameter for the augmented-Lagrangian") rho_old = Optizelle.createFloatProperty( "rho_old", "Penalty parameter from the last iteration") rho_bar = Optizelle.createFloatProperty( "rho_bar", "Fixed increase in the penalty parameter") eps_constr = Optizelle.createFloatProperty( "eps_constr", "Stopping tolerance for the norm of the constraints") xi_qn = Optizelle.createFloatProperty( "xi_qn", "Inexactness tolerance for the quasi-Newton step") xi_pg = Optizelle.createFloatProperty( "xi_pg", "Inexactness tolerance for the projection of the gradient") xi_proj = Optizelle.createFloatProperty( "xi_proj", "Inexactness tolerance for the null-space projection") xi_tang = Optizelle.createFloatProperty( "xi_tang", "Inexactness tolerance for the tangential step") xi_lmh = Optizelle.createFloatProperty( "xi_lmh", "Inexactness tolerance for the equality multiplier") def xi_all(self, value): """Sets all the inexactness tolerances: xi_qn, xi_pg, xi_proj, xi_tang, and xi_lmh""" self.xi_qn = value self.xi_pg = value self.xi_proj = value self.xi_tang = value self.xi_lmh = value xi_lmg = Optizelle.createFloatProperty( "xi_lmg", "Absolute tolerance on the residual of the equality multiplier solve") xi_4 = Optizelle.createFloatProperty( "xi_4", ("Tolerance for how much error is acceptable after computing the " "tangential step given the result from the tangential subproblem")) rpred = Optizelle.createFloatProperty( "rpred", "Residual term in the predicted reduction") PSchur_left_type = Optizelle.createEnumProperty( "PSchur_left_type", Optizelle.Operators, "Left preconditioner for the augmented system") PSchur_right_type = Optizelle.createEnumProperty( "PSchur_right_type", Optizelle.Operators, "Right preconditioner for the augmented system") augsys_iter_max = Optizelle.createNatProperty( "augsys_iter_max", "Maximum number of iterations used when solving the augmented system") augsys_rst_freq = Optizelle.createNatProperty("augsys_rst_freq", ( "Equality constraint evaluated at x. This is used in the quasinormal " "step as well as in the computation of the linear Taylor series at x " "in the direciton dx_n.")) norm_gxtyp = Optizelle.createFloatProperty( "norm_gxtyp", ("A typical norm for norm_gx. Generally, we just take the value at " "the first iteration.")) gpxdxn_p_gx = Optizelle.createVectorProperty( "gpxdxn_p_gx", ("Linear Taylor series at x in the direction dx_n. This is used both " "in the predicted reduction as well as the residual predicted " "reduction.")) gpxdxt = Optizelle.createVectorProperty( "gpxdxt", ("Derivative of the constraint applied to the tangential step this is " "used in the residual predicted reduction.")) norm_gpxdxnpgx = Optizelle.createVectorProperty( "norm_gpxdxnpgx", ("Norm of gpxdxn_p_gx. This is used in the penalty parameter " "computation and predicted reduction.")) dx_n = Optizelle.createVectorProperty("dx_n", "Normal step") dx_ncp = Optizelle.createVectorProperty("dx_ncp", "Cauchy point for normal step") dx_t = Optizelle.createVectorProperty("dx_t", "(Corrected) tangential step") dx_t_uncorrected = Optizelle.createVectorProperty( "dx_t_uncorrected", "Tangential step prior to correction") dx_tcp_uncorrected = Optizelle.createVectorProperty( "dx_tcp_uncorrected", "Cauchy point for tangential step prior to correction") H_dxn = Optizelle.createVectorProperty("H_dxn", ( "Hessian applied to the normal step. This is required by W_gradpHdxn " "as well as the predicted reduction.")) W_gradpHdxn = Optizelle.createVectorProperty("W_gradpHdxn", ( "Quantity grad f(x) + g'(x)*y + H dx_n projected into the null-space ", "of the constraints. This is required in the tangential subproblem " "and the predicted reduction.")) H_dxtuncorrected = Optizelle.createVectorProperty( "H_dxtuncorrected", ("Hessian applied to the uncorrected tangential step. This is needed " "in the predicted reduction.")) g_diag = Optizelle.createEnumProperty("g_diag", Optizelle.FunctionDiagnostics, "Function diagnostics on g")
def __init__(self): super(t,self).__init__() self._h=Optizelle.VectorValuedFunction()
if len(sys.argv) != 2: sys.exit("simple_constrained.py <parameters>") fname = sys.argv[1] # Generate an initial guess x = numpy.array([2.1, 1.1]) # Allocate memory for the equality multiplier y = numpy.array([0.]) # Allocate memory for the inequality multiplier z = numpy.array([0.]) # Create an optimization state state = Optizelle.Constrained.State.t(Optizelle.Rm, Optizelle.Rm, Optizelle.Rm, Optizelle.Messaging(), x, y, z) # Read the parameters from file Optizelle.json.Constrained.read(Optizelle.Rm, Optizelle.Rm, Optizelle.Rm, Optizelle.Messaging(), fname, state) # Create a bundle of functions fns = Optizelle.Constrained.Functions.t() fns.f = MyObj() fns.g = MyEq() fns.h = MyIneq() # Solve the optimization problem Optizelle.Constrained.Algorithms.getMin(Optizelle.Rm, Optizelle.Rm, Optizelle.Rm, Optizelle.Messaging(), fns, state)
Optizelle.json.InequalityConstrained.read(XX,ZZ,fname,state) #---ReadJson1--- # Create a bundle of functions #---Functions0--- fns = Optizelle.InequalityConstrained.Functions.t() #---Functions1--- # Do a null optimization state.f_x = 1.0 #---Solver0--- Optizelle.InequalityConstrained.Algorithms.getMin(XX,ZZ,msg,fns,state) #---Solver1--- # Do a null optimization with a state manipulator smanip = Optizelle.StateManipulator() #---SmanipSolver0--- Optizelle.InequalityConstrained.Algorithms.getMin(XX,ZZ,msg,fns,state,smanip) #---SmanipSolver1--- # Read and write the state to file fname = "restart.json" #---WriteReadRestart0--- Optizelle.json.InequalityConstrained.write_restart(XX,ZZ,fname,state); Optizelle.json.InequalityConstrained.read_restart(XX,ZZ,fname,x,z,state); #---WriteReadRestart1--- # Do a release #---Release0--- xs = Optizelle.InequalityConstrained.Restart.X_Vectors() zs = Optizelle.InequalityConstrained.Restart.Z_Vectors()
# z=(g''(x)dx)*dy def pps(self,x,dx,dy,z): z[0] = ((-cos(x[0])*dx[0]*sin(x[1])-sin(x[0])*cos(x[1])*dx[1])*dy[0] +(6.*dx[0]*x[1] + 6.*x[0]*dx[1])*dy[1] +(-1./sq(x[0])*dx[0])*dy[2]) z[1] = ((-sin(x[0])*dx[0]*cos(x[1])-cos(x[0])*sin(x[1])*dx[1])*dy[0] +(6.*x[0]*dx[0]+6.*x[1]*dx[1])*dy[1] +(60.*cub(x[1])*dx[1])*dy[2]) # Allocate memory for an initial guess and equality multiplier x = numpy.array([1.2,2.3]) y = numpy.zeros(3) # Create an optimization state state=Optizelle.EqualityConstrained.State.t( Optizelle.Rm,Optizelle.Rm,Optizelle.Messaging(),x,y) # Modify the state so that we just run our diagnostics and exit state.dscheme = Optizelle.DiagnosticScheme.DiagnosticsOnly; state.f_diag = Optizelle.FunctionDiagnostics.SecondOrder; state.g_diag = Optizelle.FunctionDiagnostics.SecondOrder; # Create a bundle of functions fns=Optizelle.EqualityConstrained.Functions.t() fns.f=Rosenbrock() fns.g=Utility() # Even though this looks like we're solving an optimization problem, # we're actually just going to run our diagnostics and then exit. Optizelle.EqualityConstrained.Algorithms.getMin( Optizelle.Rm,Optizelle.Rm,Optizelle.Messaging(),fns,state)
class t(object): """Internal state of the optimization""" def __init__(self, X, msg, x): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X", X) Optizelle.checkMessaging("msg", msg) # Allocate memory for our vectors allocateVectors(self, X, x) # Create the state Optizelle.Utility.UnconstrainedStateCreate(self, X, msg, x) # Create all of the properties eps_grad = Optizelle.createFloatProperty( "eps_grad", "Tolerance for the gradient stopping condition") eps_dx = Optizelle.createFloatProperty( "eps_dx", "Tolerance for the step length stopping criteria") algorithm_class = Optizelle.createEnumProperty("algorihm_class", Optizelle.AlgorithmClass, "Algorithm class") stored_history = Optizelle.createNatProperty( "stored_history", "Number of control objects to store in a quasi-Newton method") history_reset = Optizelle.createNatProperty( "history_reset", "Number of failed iterations before we reset the " "history for quasi-Newton methods") iter = Optizelle.createNatProperty("iter", "Current iteration") iter_max = Optizelle.createNatProperty( "iter_max", "Maximum number of optimization iterations") opt_stop = Optizelle.createEnumProperty( "opt_stop", Optizelle.StoppingCondition, "Why we've stopped the optimization") krylov_iter = Optizelle.createNatProperty( "krylov_iter", "Current number of Krylov iterations taken") krylov_iter_max = Optizelle.createNatProperty( "krylov_iter_max", "Maximum number of iterations in the Krylov method") krylov_iter_total = Optizelle.createNatProperty( "krylov_iter_total", "Total number of Krylov iterations taken") krylov_orthog_max = Optizelle.createNatProperty( "krylov_orthog_max", ("The maximum number of vectors we orthogonalize " "against in the Krylov method. For something like " "CG, this is 1.")) krylov_stop = Optizelle.createEnumProperty( "krylov_stop", Optizelle.KrylovStop, "Why the Krylov method was last stopped") krylov_rel_err = Optizelle.createFloatProperty( "krylov_rel_err", "Relative error in the Krylov method") eps_krylov = Optizelle.createFloatProperty( "eps_krylov", "Stopping tolerance for the Krylov method") krylov_solver = Optizelle.createEnumProperty( "krylov_solver", Optizelle.KrylovSolverTruncated, "Truncated Krylov solver") algorithm_class = Optizelle.createEnumProperty("algorithm_class", Optizelle.AlgorithmClass, "Algorithm class") PH_type = Optizelle.createEnumProperty("PH_type", Optizelle.Operators, "Preconditioner for the Hessian") H_type = Optizelle.createEnumProperty("H_type", Optizelle.Operators, "Hessian approximation") norm_gradtyp = Optizelle.createFloatProperty("norm_gradtyp", "Norm of a typical tradient") norm_dxtyp = Optizelle.createFloatProperty("norm_dxtyp", "Norm of a typical trial step") x = Optizelle.createVectorProperty("x", "Optimization variable") grad = Optizelle.createVectorProperty( "grad", ("Gradient, possibly of the objective, possibly of the Lagrangian. " "It depends on the context.")) dx = Optizelle.createVectorProperty("dx", "Trial step") x_old = Optizelle.createVectorProperty("x_old", "Old optimization variable") grad_old = Optizelle.createVectorProperty("grad_old", "Old gradient") dx_old = Optizelle.createVectorProperty("dx_old", "Old trial step") oldY = Optizelle.createVectorListProperty("oldY", "Difference in prior gradients") oldS = Optizelle.createVectorListProperty("oldS", "Difference in prior steps") f_x = Optizelle.createFloatProperty( "f_x", "Current value of the objective function") f_xpdx = Optizelle.createFloatProperty( "f_xpdx", "Objective function at the trial step") msg_level = Optizelle.createNatProperty("msg_level", "Messaging level") delta = Optizelle.createFloatProperty("delta", "Trust region radius") eta1 = Optizelle.createFloatProperty( "eta1", "Trust-region parameter for checking whether a step has been accepted") eta2 = Optizelle.createFloatProperty( "eta2", ("Trust-region parameter for checking whether we enlarge the " "trust-region radius")) ared = Optizelle.createFloatProperty("ared", "Actual reduction") pred = Optizelle.createFloatProperty("pred", "Predicted reduction") rejected_trustregion = Optizelle.createNatProperty( "rejected_trustregion", "Number of rejected trust-region steps") alpha0 = Optizelle.createFloatProperty("alpha0", "Base line-search step length") alpha = Optizelle.createFloatProperty("alpha", "Actual line-search step length") c1 = Optizelle.createFloatProperty( "c1", "Parameter that helps govern the sufficient decrease") linesearch_iter = Optizelle.createNatProperty( "linesearch_iter", "Current number of iterations used in the line-search") linesearch_iter_max = Optizelle.createNatProperty( "linesearch_iter_max", "Maximum number of iterations used in the line-search") linesearch_iter_total = Optizelle.createNatProperty( "linesearch_iter_total", "Total number of line-search iterations computed") eps_ls = Optizelle.createFloatProperty( "eps_ls", "Stopping tolerance for the line-search") dir = Optizelle.createEnumProperty("dir", Optizelle.LineSearchDirection, "Search direction type") kind = Optizelle.createEnumProperty("kind", Optizelle.LineSearchKind, "Type of line-search") f_diag = Optizelle.createEnumProperty("f_diag", Optizelle.FunctionDiagnostics, "Function diagnostics on f") dscheme = Optizelle.createEnumProperty("dscheme", Optizelle.DiagnosticScheme, "Diagnostic scheme")
import Optizelle #---Import0--- import Optizelle.EqualityConstrained.State import Optizelle.EqualityConstrained.Functions import Optizelle.EqualityConstrained.Algorithms import Optizelle.EqualityConstrained.Restart import Optizelle.json.EqualityConstrained #---Import1--- import numpy import math # Create some type shortcuts XX = Optizelle.Rm YY = Optizelle.Rm msg = Optizelle.Messaging() # Create some arbitrary vector in R^2 x = numpy.array([1.2, 2.3]) x0 = numpy.array([2.3, 1.2]) # Create a different arbitrary vector in R^3 y = numpy.array([3.4, 4.5, 5.6]) y0 = numpy.array([5.6, 4.5, 3.4]) # Create a state based on this vector #---State0--- state = Optizelle.EqualityConstrained.State.t(XX, YY, msg, x, y) #---State1--- # Read in some parameters
class t(Optizelle.Unconstrained.State.t): """Internal state of the optimization""" def __init__(self,X,Z,msg,x,z): """Constructor""" # Check our arguments Optizelle.checkVectorSpace("X",X) Optizelle.checkEuclidean("Z",Z) Optizelle.checkMessaging("msg",msg) # Allocate memory for our vectors Optizelle.Unconstrained.State.allocateVectors(self,X,x) allocateVectors(self,X,Z,x,z) # Create the state Optizelle.Utility.InequalityConstrainedStateCreate(self,X,Z,msg,x,z) # Create all of the properties z = Optizelle.createVectorProperty( "z", "Inequality multiplier (dual variable or Lagrange multiplier)") dz = Optizelle.createVectorProperty( "dz", "Step in the inequality multiplier") h_x = Optizelle.createVectorProperty( "h_x", "The inequality constraint evaluated at x.") mu = Optizelle.createFloatProperty( "mu", "Interior point parameter") mu_est = Optizelle.createFloatProperty( "mu_est", "Current interior point estimate") mu_typ = Optizelle.createFloatProperty( "mu_typ", "Typical value for mu. Generally, the first estimated value for mu.") eps_mu = Optizelle.createFloatProperty( "eps_mu", "Relative stopping criteria for the interior point parameter") sigma = Optizelle.createFloatProperty( "sigma", ("The amount that we reduce the interior point parameter by everytime " "we approach the central path")) gamma = Optizelle.createFloatProperty( "gamma", "How close we move to the boundary during a single step") ipm = Optizelle.createEnumProperty( "ipm", Optizelle.InteriorPointMethod, "Type of interior point method") cstrat = Optizelle.createEnumProperty( "cstrat", Optizelle.CentralityStrategy, "Centrality strategy") h_diag = Optizelle.createEnumProperty( "h_diag", Optizelle.FunctionDiagnostics, "Function diagnostics on h")