示例#1
0
    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()

        # Objective function/constraints of the NLP
        if not hasattr(self, '_objective'):
            raise ValueError('need to set objective function')
        nlp = CS.MXFunction(CS.nlpIn(x=self._dvMap.vectorize()),
                            CS.nlpOut(f=self._objective, g=g))
        setFXOptions(nlp, constraintFunOpts)
        nlp.init()

        # 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=CS.sp_dense(nd, 1),
                                f=CS.sp_dense(1, 1),
                                lam_x=CS.sp_dense(nd, 1),
                                lam_g=CS.sp_dense(nc, 1),
                                lam_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(nlp)
        #        self.solver = CS.WorhpSolver(nlp)
        #        self.solver = CS.SQPMethod(nlp)

        # Set options
        setFXOptions(self.solver, solverOpts)

        # initialize the solver
        self.solver.init()

        # Bounds on g
        self.solver.setInput(lbg, 'lbg')
        self.solver.setInput(ubg, 'ubg')

        ## Nonlinear constraint function, for debugging
        gfcn = CS.MXFunction([self._dvMap.vectorize()], [g])
        gfcn.init()
        setFXOptions(gfcn, constraintFunOpts)
        self._gfcn = gfcn
示例#2
0
    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()

        # Objective function/constraints of the NLP
        if not hasattr(self,'_objective'):
            raise ValueError('need to set objective function')
        nlp = CS.MXFunction(CS.nlpIn(x=self._dvMap.vectorize()),CS.nlpOut(f=self._objective, g=g))
        setFXOptions(nlp,constraintFunOpts)
        nlp.init()

        # 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 = CS.sp_dense(nd,1),
                                               f = CS.sp_dense(1,1),
                                               lam_x = CS.sp_dense(nd,1),
                                               lam_g = CS.sp_dense(nc,1),
                                               lam_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(nlp)
#        self.solver = CS.WorhpSolver(nlp)
#        self.solver = CS.SQPMethod(nlp)

        # Set options
        setFXOptions(self.solver, solverOpts)

        # initialize the solver
        self.solver.init()

        # Bounds on g
        self.solver.setInput(lbg,'lbg')
        self.solver.setInput(ubg,'ubg')

        ## Nonlinear constraint function, for debugging
        gfcn = CS.MXFunction([self._dvMap.vectorize()],[g])
        gfcn.init()
        setFXOptions(gfcn,constraintFunOpts)
        self._gfcn = gfcn
示例#3
0
                   CT.entry("sref",expr=geom.sref)])

obj = -CL / (CDi + 0.01)

nlp = C.SXFunction(C.nlpIn(x=dvs),C.nlpOut(f=obj,g=g))

# callback
class MyCallback:
    def __init__(self):
        self.iters = []
    def __call__(self,f,*args):
        self.iters.append(numpy.array(f.getInput("x")))
mycallback = MyCallback()
pyfun = C.PyFunction( mycallback, C.nlpSolverOut(x=C.sp_dense(dvs.size,1),
                                                 f=C.sp_dense(1,1),
                                                 lam_x=C.sp_dense(dvs.size,1),
                                                 lam_g = C.sp_dense(g.size,1),
                                                 lam_p = C.sp_dense(0,1),
                                                 g = C.sp_dense(g.size,1) ),
                      [C.sp_dense(1,1)] )
pyfun.init()

solver = C.IpoptSolver(nlp)
solver.setOption('tol',1e-11)
solver.setOption('linear_solver','ma27')
#solver.setOption('linear_solver','ma57')
solver.setOption("iteration_callback",pyfun)
solver.init()

lbg = g(solver.input("lbg"))
ubg = g(solver.input("ubg"))
lbx = dvs(solver.input("lbx"))