def init_opt_ipopt_get_statistics(self): """ Get statistics from the last optimization run. Returns:: return_status -- The return status from IPOPT. nbr_iter -- The number of iterations. objective -- The final value of the objective function. total_exec_time -- The execution time. """ return_code = ct.c_int() iters = ct.c_int() objective = c_jmi_real_t() exec_time = c_jmi_real_t() if self._nlp_init._jmi_model._dll.jmi_init_opt_ipopt_get_statistics( self._ipopt_init, byref(return_code), byref(iters), byref(objective), byref(exec_time)) is not 0: raise JMIException( "Error when retrieve statistics - optimization problem may not be solved." ) return (return_code.value, iters.value, objective.value, exec_time.value)
def __init__(self, nlp_init): """ Class for solving a DAE initialization problem my means of optimization using IPOPT. Parameters:: nlp_init -- The NLPInitialization object. """ self._nlp_init = nlp_init self._ipopt_init = ct.c_voidp() self._set_initOpt_typedefs() try: assert self._nlp_init._jmi_model._dll.jmi_init_opt_ipopt_new( byref(self._ipopt_init), self._nlp_init._jmi_init_opt) == 0, \ "jmi_init_opt_ipopt_new returned non-zero" except AttributeError as e: raise JMIException( "Can not create InitializationOptimizer object. \ " "Please recompile model with target='ipopt") assert self._ipopt_init.value is not None, \ "jmi struct not returned correctly"
def init_opt_set_initial_from_model(self): """ Set the initial point of the NLP based on values in the JMI model. """ if self._jmi_model._dll.jmi_init_opt_set_initial_from_model( self._jmi_init_opt) is not 0: raise JMIException("Could not set initial point from model.")
def init_opt_f(self, f): """ Get the cost function value at a given point in search space. Parameters:: f -- The value of the cost function. (Return variable) """ if self._jmi_model._dll.jmi_init_opt_f(self._jmi_init_opt, f) is not 0: raise JMIException("Getting the cost function failed.")
def init_opt_set_initial(self, x_init): """ Set the initial point of the NLP. Parameters:: x_init -- The initial guess vector. """ if self._jmi_model._dll.jmi_init_opt_set_initial( self._jmi_init_opt, x_init) is not 0: raise JMIException("Setting the initial point failed.")
def init_opt_h(self, res): """ Get the residual of the equality constraints h. Parameters:: res -- The residual of the equality constraints. (Return variable) """ if self._jmi_model._dll.jmi_init_opt_h(self._jmi_init_opt, res) is not 0: raise JMIException( "Getting the residual of the equality constraints failed.")
def init_opt_ipopt_set_num_option(self, key, val): """ Set an Ipopt double option. Parameters:: key -- The name of the option. val -- The value of the option. """ if self._nlp_init._jmi_model._dll.jmi_init_opt_ipopt_set_num_option( self._ipopt_init, key, val) is not 0: raise JMIException("The Ipopt real option " + key + " is unknown")
def init_opt_dh(self, jac): """ Get the Jacobian of the residual of the equality constraints. Parameters:: jac -- The Jacobian of the residual of the equality constraints. (Return variable) """ if self._jmi_model._dll.jmi_init_opt_dh(self._jmi_init_opt, jac) is not 0: raise JMIException( "Getting the Jacobian of the residual of the equality constraints." )
def init_opt_set_bounds(self, x_lb, x_ub): """ Set the upper and lower bounds of the optimization variables. Parameters:: x_lb -- The lower bounds vector. (Return variable) x_ub -- The upper bounds vector. (Return variable) """ if self._jmi_model._dll.jmi_init_opt_set_bounds( self._jmi_init_opt, x_lb, x_ub) is not 0: raise JMIException( "Getting upper and lower bounds of the optimization variables failed." )
def init_opt_ipopt_solve(self): """ Solve the NLP problem. """ if self._nlp_init._jmi_model._dll.jmi_init_opt_ipopt_solve( self._ipopt_init) > 1: raise JMIException("Solving IPOPT failed.") # Check return status from Ipopt and raise exception if not ok (return_status, nbr_iters, obj_final, tot_exec_time) = self.init_opt_ipopt_get_statistics() # Return code should be one of (taken from IpReturnCodes.inc): # 0: IP_SOLVE_SUCCEEDED # 1: IP_ACCEPTABLE_LEVEL # 6: IP_FEASIBLE_POINT_FOUND if return_status not in (0, 1, 6): raise IpoptException("Ipopt failed with return code: " + str(return_status) + \ " Please see Ipopt documentation for more information.")
def init_opt_dh_nz_indices(self, irow, icol): """ Get the indices of the non-zeros in the equality constraint Jacobian. Parameters:: irow -- The row indices of the non-zero entries in the Jacobian of the residual of the equality constraints. (Return variable) icol -- The column indices of the non-zero entries in the Jacobian of the residual of the equality constraints. (Return variable) """ if self._jmi_model._dll.jmi_init_opt_dh_nz_indices( self._jmi_init_opt, irow, icol) is not 0: raise JMIException( "Getting the indices of the non-zeros in the equality constraint Jacobian failed." )
def init_opt_get_dimensions(self): """ Get the number of variables and the number of constraints in the problem. Returns:: Tuple with the number of variables in the NLP problem, equality constraints, and non-zeros in the Jacobian of the equality constraints respectively. """ n_real_x = ct.c_int() n_h = ct.c_int() dh_n_nz = ct.c_int() if self._jmi_model._dll.jmi_init_opt_get_dimensions( self._jmi_init_opt, byref(n_real_x), byref(n_h), byref(dh_n_nz)) is not 0: raise JMIException( "Getting the number of variables and constraints failed.") return n_real_x.value, n_h.value, dh_n_nz.value