def setIdasIntegrator(self, integratorOptions=[]): # make dae input fun daeSXFun = self.dae.sxFun() assert self.nStates()==daeSXFun.inputSX(C.DAE_X).size() assert self.nActions()+self.nParams()==daeSXFun.inputSX(C.DAE_P).size() # make integrator self.integrator = C.IdasIntegrator(daeSXFun) setFXOptions(self.integrator, integratorOptions) self.integrator.init() # set up dynamics constraints for k in range(0,self.nSteps-1): uk = self.actions[:,k] ukp1 = self.actions[:,k+1] xk = self.states[:,k] xkp1 = self.states[:,k+1] p = self.params upk = C.veccat([uk,p]) self.addConstraint(self.integrator.call([xk,upk])[C.INTEGRATOR_XF],'==',xkp1)
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._V],[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._V],[self._objective]) # solver callback (optional) if callback is not None: nd = self._V.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), 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) # 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)
def setSolver(self, solver, solverOptions=[], objFunOptions=[], constraintFunOptions=[]): if hasattr(self, '_solver'): raise ValueError("You've already set a solver and you can't change it") if not hasattr(self, '_objective'): raise ValueError("You need to set an objective") # make objective function f = C.MXFunction([self.getDesignVars()], [self._objective]) setFXOptions(f, objFunOptions) f.init() # make constraint function g = C.MXFunction([self.getDesignVars()], [self._constraints.getG()]) setFXOptions(g, constraintFunOptions) g.init() def mkParallelG(): gs = [C.MXFunction([self.getDesignVars()],[gg]) for gg in self._constraints._g] for gg in gs: gg.init() pg = C.Parallelizer(gs) # pg.setOption("parallelization","openmp") pg.setOption("parallelization","serial") # pg.setOption("parallelization","expand") pg.init() dvsDummy = C.msym('dvs',(self.nStates()+self.nActions())*self.nSteps+self.nParams()) g_ = C.MXFunction([dvsDummy],[C.veccat(pg.call([dvsDummy]*len(gs)))]) g_.init() return g_ # parallelG = mkParallelG() # guess = self._initialGuess.vectorize() # parallelG.setInput([x*1.1 for x in guess]) # g.setInput([x*1.1 for x in guess]) # # g.evaluate() # parallelG.evaluate() # print parallelG.output()-g.output() # exit(0) # make solver function # self._solver = solver(f, mkParallelG()) self._solver = solver(f, g) setFXOptions(self._solver, solverOptions) self._solver.init() # set constraints self._solver.setInput(self._constraints.getLb(), C.NLP_LBG) self._solver.setInput(self._constraints.getUb(), C.NLP_UBG) self.setBounds()