Beispiel #1
0
    def __init__(self,*args):
        shape = 1
        if len(args)==1:
           shape = args[0]
        elif len(args)>1:
           shape = [args[0],args[1]]

        if len(args)==3:
           name = args[2]
        else:
           name = 'p'
               
        OptimizationObject.__init__(self,shape,name)
Beispiel #2
0
def optival( *args ):
    if len(args)==1 and isinstance(args[0],OptimizationObject):
      return np.array(args[0].optival())
    symbols_struct = OptimizationObject.get_primitives(args)
    
    symbols = []
    hassymbols = False;
    symbolsx = [C.MX.sym('dummy')]; # bug in casadi typemaps: [] does not work
    if 'x' in symbols_struct:
       symbols+=symbols_struct["x"]
       hassymbols = True
    if 'p' in symbols_struct:
       symbols+=symbols_struct["p"]
       hassymbols = True
    if not hassymbols:
      symbols = [MX.sym("dummy")]
       
    f = C.Function('f',symbols,args);
    f_inputs = [];
    
    if hassymbols:
        for i,e in enumerate(symbols):
           f_inputs.append(optival(e))
    
    out = f.call(f_inputs)
    
    if len(args)==1:
      return np.array(out[0])
    else:
      return [np.array(out[i]) for i in range(f.nOut())]
Beispiel #3
0
def optival(*args):
    if len(args) == 1 and isinstance(args[0], OptimizationObject):
        return np.array(args[0].optival())
    symbols_struct = OptimizationObject.get_primitives(args)

    symbols = []
    hassymbols = False
    symbolsx = [C.MX.sym('dummy')]
    # bug in casadi typemaps: [] does not work
    if 'x' in symbols_struct:
        symbols += symbols_struct["x"]
        hassymbols = True
    if 'p' in symbols_struct:
        symbols += symbols_struct["p"]
        hassymbols = True
    if not hassymbols:
        symbols = [C.MX.sym("dummy")]

    f = C.Function('f', symbols, args)
    f_inputs = []

    if hassymbols:
        for i, e in enumerate(symbols):
            f_inputs.append(optival(e))

    out = f.call(f_inputs)

    if len(args) == 1:
        return np.array(out[0])
    else:
        return [np.array(out[i]) for i in range(f.nOut())]
Beispiel #4
0
    def __init__(self, *args):
        shape = 1
        if len(args) == 1:
            shape = args[0]
        elif len(args) > 1:
            shape = [args[0], args[1]]

        if len(args) == 3:
            name = args[2]
        else:
            name = 'x'

        import casadi
        OptimizationObject.__init__(self, shape, name)
        self.lb = -casadi.inf * casadi.DM.ones(self.sparsity())
        self.ub = casadi.inf * casadi.DM.ones(self.sparsity())
        self.init = casadi.DM.zeros(self.sparsity())
Beispiel #5
0
 def __init__(self,*args):
    shape = 1
    if len(args)==1:
        shape = args[0]
    elif len(args)>1:
        shape = [args[0],args[1]]
    
    if len(args)==3:
        name = args[2]
    else:
        name = 'x'
    
    import casadi
    OptimizationObject.__init__(self,shape,name)
    self.lb = -casadi.inf*casadi.DM.ones(self.sparsity())
    self.ub = casadi.inf*casadi.DM.ones(self.sparsity())
    self.init = casadi.DM.zeros(self.sparsity())
Beispiel #6
0
    def  __init__(self,objective,*args):
        """
               optisolve(objective)
               optisolve(objective,constraints)
        """
        if len(args)>=1:
            constraints = args[0]
        else:
            constraints = []
        options = dict()
        if len(args)>=2:
            options = args[1]
        
        
        if not isinstance(constraints,list):
            raise Exception("Constraints must be given as list: [x>=0,y<=0]")
            

        [ gl_pure, gl_equality] = sort_constraints( constraints )
        symbols = OptimizationObject.get_primitives([objective]+gl_pure)

        # helper functions for 'x'
        X = C.veccat(*symbols["x"])
        helper = C.Function('helper',[X],symbols["x"])

        helper_inv = C.Function('helper_inv',symbols["x"],[X])

        # helper functions for 'p' if applicable
        if 'p' in symbols:
          P = C.veccat(*symbols["p"])

          self.Phelper_inv = C.Function('Phelper_inv',symbols["p"],[P])
          
        else:
          P = C.MX.sym('p',0,1)

        if len(gl_pure)>0:
            g_helpers = [];
            for p in gl_pure:
               g_helpers.append(C.MX.sym('g',p.sparsity())) 
            
            G_helpers = C.veccat(*g_helpers)

            self.Ghelper = C.Function('Ghelper',[G_helpers],g_helpers)

            self.Ghelper_inv = C.Function('Ghelper_inv',g_helpers,[G_helpers])
        
        codegen = False;
        if 'codegen' in options:
            codegen = options["codegen"]
            del options["codegen"]
        
        opt = {}
        if codegen:
            options["jit"] = True
            opt["jit"] = True
        
        gl_pure_v = C.MX()
        if len(gl_pure)>0:
           gl_pure_v = C.veccat(*gl_pure)

        if objective.is_vector() and objective.numel()>1:
            F = C.vec(objective)
            objective = 0.5*C.dot(F,F)
            FF = C.Function('nlp',[X,P], [F])
            JF = FF.jacobian()
            J_out = JF.call([X,P])
            J = J_out[0].T;
            H = C.mtimes(J,J.T)
            sigma = C.MX.sym('sigma')
            Hf = C.Function('H',dict(x=X,p=P,lam_f=sigma,hess_gamma_x_x=sigma*C.triu(H)),['x', 'p', 'lam_f', 'lam_g'],['hess_gamma_x_x'],opt)
            if "expand" in options and options["expand"]:
               Hf = Hf.expand()
            options["hess_lag"] = Hf
        
        nlp = {"x":X,"p":P,"f":objective,"g":gl_pure_v}

        self.solver = C.nlpsol('solver','ipopt', nlp, options)

        # Save to class properties
        self.symbols      = symbols
        self.helper       = helper
        self.helper_inv   = helper_inv
        self.gl_equality  = gl_equality
        
        self.resolve()