Exemplo n.º 1
0
    def _pm_expand(self,constr):
        
        # Get shape
        v = constr.left
        n = v.shape[0]-1
        x = v[0:n,0]
        y = v[n,0]
        z = var()

        # Get power of 2 size
        m = 0
        while (np.log2(n+m) % 1 != 0):
            m = m + 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0,n,1):
            el_list+=[x[i,0]]
            if(not np.isscalar(x[i,0]) and
               type(x[i,0]) is not cvxpy_obj):
                constr_list += [greater(x[i,0],0)]

        # Construct expansion
        z = var()
        for i in range(0,m,1):
            el_list += [z]
        while(len(el_list) > 2):
            new_list = []
            for i in range(0,len(el_list)/2):
                x1 = el_list[2*i]
                x2 = el_list[2*i+1]
                w = var()
                constr_list += [belongs(vstack((hstack((x1,w)),
                                                hstack((w,x2)))),
                                        sdc(2))]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [belongs(vstack((hstack((x1,z)),
                                        hstack((z,x2)))),
                                sdc(2))]
        constr_list += [greater(z,0),greater(z,y)]
        return cvxpy_list(constr_list)
Exemplo n.º 2
0
    def _pm_expand(self, constr):

        # Get shape
        v = constr.left
        n = v.shape[0] - 1
        x = v[0:n, 0]
        y = v[n, 0]
        z = var()

        # Get power of 2 size
        m = 0
        while np.log2(n + m) % 1 != 0:
            m = m + 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0, n, 1):
            el_list += [x[i, 0]]
            if not np.isscalar(x[i, 0]) and type(x[i, 0]) is not cvxpy_obj:
                constr_list += [greater(x[i, 0], 0)]

        # Construct expansion
        z = var()
        for i in range(0, m, 1):
            el_list += [z]
        while len(el_list) > 2:
            new_list = []
            for i in range(0, len(el_list) / 2):
                x1 = el_list[2 * i]
                x2 = el_list[2 * i + 1]
                w = var()
                constr_list += [belongs(vstack((hstack((x1, w)), hstack((w, x2)))), sdc(2))]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [belongs(vstack((hstack((x1, z)), hstack((z, x2)))), sdc(2))]
        constr_list += [greater(z, 0), greater(z, y)]
        return cvxpy_list(constr_list)
Exemplo n.º 3
0
def transform(constr_list):
    """
    Description 
    -----------
    Transforms nonlinear equality constraints
    to equivalent form involving two inequalities.

    Arguments
    ---------
    constr_list: cvxpy_list of constraints in 
    expanded form so that all nonlinear constraints
    are equalities.
    """

    # New list
    new_list = cvxpy_list([])

    # Transform
    for constr in constr_list:

        # Function
        if (constr.left.type == TREE and constr.left.item.type == FUNCTION):

            # Get function
            fn = constr.left.item

            # Get equivalent transformation
            new_constr = cvxpy_list([
                less(constr.left, constr.right),
                greater(constr.left, constr.right)
            ])

            # Append
            new_list += cvxpy_list(new_constr)

        # Not a function
        else:

            # Append
            new_list += cvxpy_list([constr])

    # Return transformed list
    return new_list
Exemplo n.º 4
0
def transform(constr_list):
    """
    Description 
    -----------
    Transforms nonlinear equality constraints
    to equivalent form involving two inequalities.

    Arguments
    ---------
    constr_list: cvxpy_list of constraints in 
    expanded form so that all nonlinear constraints
    are equalities.
    """

    # New list
    new_list = cvxpy_list([])

    # Transform
    for constr in constr_list:

        # Function
        if(constr.left.type == TREE and
           constr.left.item.type == FUNCTION):

            # Get function
            fn = constr.left.item

            # Get equivalent transformation
            new_constr = cvxpy_list([less(constr.left,constr.right),
                                     greater(constr.left,constr.right)])
        
            # Append
            new_list += cvxpy_list(new_constr)
        
        # Not a function
        else:
            
            # Append
            new_list += cvxpy_list([constr])
            
    # Return transformed list
    return new_list
Exemplo n.º 5
0
    def _pm_expand(self,constr):
        """
        Description
        -----------
        Given the constraint, which must be in the form
        self(args) operator variable, the parameters
        are replaced with arguments and then the 
        partial minimization description of the program
        is merged with the constraint.

        Argument
        --------
        constr: cvxpy_constr of the form self(args) 
        operator variable.
        """

        # Get arguments
        args = constr.left.children

        # Create arg-param map by position
        p_map = {}
        for k in range(0,len(args),1):
            p_map[self.params[k]] = args[k]

        # Create new program
        new_p = prog((self.action,re_eval(self.obj,p_map)),
                     re_eval(self.constr,p_map),[],
                     self.options,self.name)

        # Expand partial minimization
        right = constr.right
        new_constr = []
        if(self.curvature == CONVEX):
            new_constr += [less(new_p.obj,right)]
        else:
            new_constr += [greater(new_p.obj,right)]
        new_constr += new_p.constr
        
        # Return constraints
        return cvxpy_list(new_constr)
Exemplo n.º 6
0
    def _pm_expand(self, constr):
        """
        Description
        -----------
        Given the constraint, which must be in the form
        self(args) operator variable, the parameters
        are replaced with arguments and then the 
        partial minimization description of the program
        is merged with the constraint.

        Argument
        --------
        constr: cvxpy_constr of the form self(args) 
        operator variable.
        """

        # Get arguments
        args = constr.left.children

        # Create arg-param map by position
        p_map = {}
        for k in range(0, len(args), 1):
            p_map[self.params[k]] = args[k]

        # Create new program
        new_p = prog((self.action, re_eval(self.obj, p_map)),
                     re_eval(self.constr, p_map), [], self.options, self.name)

        # Expand partial minimization
        right = constr.right
        new_constr = []
        if (self.curvature == CONVEX):
            new_constr += [less(new_p.obj, right)]
        else:
            new_constr += [greater(new_p.obj, right)]
        new_constr += new_p.constr

        # Return constraints
        return cvxpy_list(new_constr)
Exemplo n.º 7
0
 def _range_constr(self, v):
     return cvxpy_list([greater(v,0)])
Exemplo n.º 8
0
def re_eval(arg,param_map):
    """
    Description
    -----------
    Replaces parameters found in arg using the param_map
    and re-evaluates the resulting object.
    
    Arguments
    ---------
    arg: Argument to be re-evaluated.
    param_map: Dictionery that maps the parameters 
    to objects.
    """

    # Number
    if(np.isscalar(arg)):
        return arg
    
    # Constant object
    elif(type(arg) is cvxpy_obj):
        return arg.get_value()
    
    # Scalar variable
    elif(type(arg) is cvxpy_scalar_var):
        return arg
    
    # Scalar param
    elif(type(arg) is cvxpy_scalar_param):
        return re_eval(param_map[arg],param_map)

    # Summation
    elif(type(arg) is cvxpy_tree and arg.item.name == '+'):
        new_children = map(lambda x:re_eval(x,param_map),arg.children)
        return sum(new_children)

    # Multiplication
    elif(type(arg) is cvxpy_tree and arg.item.name == '*'):
        child1 = re_eval(arg.children[0],param_map)
        child2 = re_eval(arg.children[1],param_map)
        return child1*child2

    # Function
    elif(type(arg) is cvxpy_tree and arg.item.type == FUNCTION):
        new_children = map(lambda x:re_eval(x,param_map),arg.children)
        return arg.item(new_children)

    # Constraint
    elif(type(arg) is cvxpy_constr):
        
        # Not set membership
        if(arg.op != 'in'):
            left = re_eval(arg.left ,param_map)
            right= re_eval(arg.right,param_map)
            if(arg.op == '=='):
                return equal(left,right)
            elif(arg.op == '<='):
                return less(left,right)
            else:
                return greater(left,right)

        # Set membership
        else:
            left = re_eval(arg.left,param_map)
            return belongs(left,arg.right)

    # Array
    elif(type(arg) is cvxpy_expression or
         type(arg) is cvxpy_var or
         type(arg) is cvxpy_param):
        (m,n) = arg.shape
        new_exp = cvxpy_expression(m,n)
        for i in range(0,m,1):
            for j in range(0,n,1):
                new_exp[i,j] = re_eval(arg[i,j],param_map)
        return new_exp

    # List
    elif(type(arg) is cvxpy_list):
        new_list = cvxpy_list([])
        for c in arg:
            new_list += cvxpy_list([re_eval(c,param_map)])
        return new_list

    # Invalid
    else:
        raise ValueError('Invalid argument')
Exemplo n.º 9
0
 def _range_constr(self, v):
     return cvxpy_list([greater(v, 0)])
Exemplo n.º 10
0
 def _dom_constr(self,args):
     arg = args[0]
     return cvxpy_list([greater(arg,0)])
Exemplo n.º 11
0
 def _dom_constr(self, args):
     arg = args[0]
     return cvxpy_list([greater(arg, 0)])