def makeCallback(): nd = ocp.getDesignVars().size() nc = ocp._constraints.getG().size() c = C.PyFunction( MyCallback(), C.nlpsolverOut(x_opt=C.sp_dense(nd, 1), cost=C.sp_dense(1, 1), lambda_x=C.sp_dense(nd, 1), lambda_g=C.sp_dense(nc, 1), g=C.sp_dense(nc, 1)), [C.sp_dense(1, 1)]) c.init() return c
def addCallback(): nd = len(boundsVec) nc = g.getLb().size() c = C.PyFunction( MyCallback(), C.nlpsolverOut(x=C.sp_dense(nd, 1), f=C.sp_dense(1, 1), lam_x=C.sp_dense(nd, 1), lam_p=C.sp_dense(0, 1), lam_g=C.sp_dense(nc, 1), g=C.sp_dense(nc, 1)), [C.sp_dense(1, 1)]) c.init() solver.setOption("iteration_callback", c)
def setupSolver(self,solverOpts=[],constraintFunOpts=[],callback=None): if not self.collocationIsSetup: raise ValueError("you forgot to call setupCollocation") g = self._constraints.getG() lbg = self._constraints.getLb() ubg = self._constraints.getUb() # Nonlinear constraint function gfcn = CS.MXFunction([self._dvMap.vectorize()],[g]) setFXOptions(gfcn,constraintFunOpts) # Objective function of the NLP if not hasattr(self,'_objective'): raise ValueError('need to set objective function') ofcn = CS.MXFunction([self._dvMap.vectorize()],[self._objective]) # solver callback (optional) if callback is not None: nd = self._dvMap.vectorize().size() nc = self._constraints.getG().size() c = CS.PyFunction( callback, CS.nlpsolverOut(x_opt = CS.sp_dense(nd,1), cost = CS.sp_dense(1,1), lambda_x = CS.sp_dense(nd,1), lambda_g = CS.sp_dense(nc,1), lambda_p = CS.sp_dense(0,1), g = CS.sp_dense(nc,1) ), [CS.sp_dense(1,1)] ) c.init() solverOpts.append( ("iteration_callback", c) ) # Allocate an NLP solver self.solver = CS.IpoptSolver(ofcn,gfcn) # self.solver = CS.WorhpSolver(ofcn,gfcn) # self.solver = CS.SQPMethod(ofcn,gfcn) # Set options setFXOptions(self.solver, solverOpts) # initialize the solver self.solver.init() # Bounds on g self.solver.setInput(lbg,CS.NLP_LBG) self.solver.setInput(ubg,CS.NLP_UBG) self._gfcn = gfcn
def makeCallback(): nd = ocp.getDesignVars().size() nc = ocp._constraints.getG().size() c = C.PyFunction( MyCallback(), C.nlpsolverOut( x_opt=C.sp_dense(nd, 1), cost=C.sp_dense(1, 1), lambda_x=C.sp_dense(nd, 1), lambda_g=C.sp_dense(nc, 1), g=C.sp_dense(nc, 1), ), [C.sp_dense(1, 1)], ) c.init() return c
def addCallback(): nd = len(boundsVec) nc = g.getLb().size() c = C.PyFunction( MyCallback(), C.nlpsolverOut( x=C.sp_dense(nd, 1), f=C.sp_dense(1, 1), lam_x=C.sp_dense(nd, 1), lam_p=C.sp_dense(0, 1), lam_g=C.sp_dense(nc, 1), g=C.sp_dense(nc, 1), ), [C.sp_dense(1, 1)], ) c.init() solver.setOption("iteration_callback", c)