def belongs(x, A): """ Form set membership constraint (x in A) :param x: :class:`cvxpy_matrix` or array object. :param A: Set atom. :rtype: :class:`cvxpy.constr` """ # Element is not array-like if (type(x).__name__ not in ARRAY_OBJS and type(x) is not cvxpy_matrix): raise ValueError('First argument must be array-like') # Set is not valid if (A.type != SET): raise ValueError('Second argument must be a set') # Verify dimensions (m1, n1) = A.shape (m2, n2) = x.shape # Dimensions don't match if ((m1, n1) != (m2, n2)): raise ValueError('Invalid dimensions') # Construct and return constraint return cvxpy_constr(x, 'in', A)
def belongs(x,A): """ Form set membership constraint (x in A) :param x: :class:`cvxpy_matrix` or array object. :param A: Set atom. :rtype: :class:`cvxpy.constr` """ # Element is not array-like if(type(x).__name__ not in ARRAY_OBJS and type(x) is not cvxpy_matrix): raise ValueError('First argument must be array-like') # Set is not valid if(A.type != SET): raise ValueError('Second argument must be a set') # Verify dimensions (m1,n1) = A.shape (m2,n2) = x.shape # Dimensions don't match if((m1,n1) != (m2,n2)): raise ValueError('Invalid dimensions') # Construct and return constraint return cvxpy_constr(x,'in',A)
def compare(obj1, op, obj2): # Both scalars if ((np.isscalar(obj1) or type(obj1).__name__ in SCALAR_OBJS) and (np.isscalar(obj2) or type(obj2).__name__ in SCALAR_OBJS)): # Upgrade scalars to cvxpy_obj if (np.isscalar(obj1)): obj1 = cvxpy_obj(CONSTANT, obj1, str(obj1)) if (np.isscalar(obj2)): obj2 = cvxpy_obj(CONSTANT, obj2, str(obj2)) # Construct and return constraint return cvxpy_constr(obj1, op, obj2) # Upgrate scalars to arrays if ((type(obj1) is cvxpy_matrix or type(obj1).__name__ in ARRAY_OBJS) and (np.isscalar(obj2) or type(obj2).__name__ in SCALAR_OBJS)): (m, n) = obj1.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] = obj2 obj2 = new_exp if ((type(obj2) is cvxpy_matrix or type(obj2).__name__ in ARRAY_OBJS) and (np.isscalar(obj1) or type(obj1).__name__ in SCALAR_OBJS)): (m, n) = obj2.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] = obj1 obj1 = new_exp # Both arrays if ((type(obj1) is cvxpy_matrix or type(obj1).__name__ in ARRAY_OBJS) and (type(obj2) is cvxpy_matrix or type(obj2).__name__ in ARRAY_OBJS)): constr = [] if (obj1.shape != obj2.shape): raise ValueError('Invalid dimensions') (m, n) = obj1.shape for i in range(0, m, 1): for j in range(0, n, 1): constr += [compare(obj1[i, j], op, obj2[i, j])] return cvxpy_list(constr) # Invalid arguments raise ValueError('Objects not comparable')
def compare(obj1,op,obj2): # Both scalars if((np.isscalar(obj1) or type(obj1).__name__ in SCALAR_OBJS) and (np.isscalar(obj2) or type(obj2).__name__ in SCALAR_OBJS)): # Upgrade scalars to cvxpy_obj if(np.isscalar(obj1)): obj1 = cvxpy_obj(CONSTANT,obj1,str(obj1)) if(np.isscalar(obj2)): obj2 = cvxpy_obj(CONSTANT,obj2,str(obj2)) # Construct and return constraint return cvxpy_constr(obj1,op,obj2) # Upgrate scalars to arrays if((type(obj1) is cvxpy_matrix or type(obj1).__name__ in ARRAY_OBJS) and (np.isscalar(obj2) or type(obj2).__name__ in SCALAR_OBJS)): (m,n) = obj1.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] = obj2 obj2 = new_exp if((type(obj2) is cvxpy_matrix or type(obj2).__name__ in ARRAY_OBJS) and (np.isscalar(obj1) or type(obj1).__name__ in SCALAR_OBJS)): (m,n) = obj2.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] = obj1 obj1 = new_exp # Both arrays if((type(obj1) is cvxpy_matrix or type(obj1).__name__ in ARRAY_OBJS) and (type(obj2) is cvxpy_matrix or type(obj2).__name__ in ARRAY_OBJS)): constr = [] if(obj1.shape != obj2.shape): raise ValueError('Invalid dimensions') (m,n) = obj1.shape for i in range(0,m,1): for j in range(0,n,1): constr += [compare(obj1[i,j],op,obj2[i,j])] return cvxpy_list(constr) # Invalid arguments raise ValueError('Objects not comparable')
def expand(arg): # Constant if(type(arg) is cvxpy_obj): return arg,cvxpy_list([]) # Scalar variable elif(type(arg) is cvxpy_scalar_var): return arg,cvxpy_list([]) # Summation elif(type(arg) is cvxpy_tree and arg.item.name == '+'): # Get item and children item = arg.item children = arg.children # New var v = var() # Expand children new_children = [] new_constr = cvxpy_list([]) for child in children: # Multiplication if(child.type == TREE and child.item.name == '*'): child_var,child_constr = expand(child.children[1]) new_children += [child.children[0].data*child_var] new_constr += child_constr # Else else: child_var,child_constr = expand(child) new_children += [child_var] new_constr += child_constr # Return (Always right side is the new variable) new_tree = cvxpy_tree(item,new_children) return v,cvxpy_list([equal(new_tree,v)])+new_constr # Multiplication elif(type(arg) is cvxpy_tree and arg.item.name == '*'): # Get item and children item = arg.item children = arg.children # New var v = var() # Apply expand to second operand (first is a constant) child_var,child_constr = expand(children[1]) # Return result (Always right side is the new variable) new_tree = cvxpy_tree(item,[children[0],child_var]) new_eq = cvxpy_list([equal(new_tree,v)]) new_eq += child_constr return v,new_eq # Function elif(type(arg) is cvxpy_tree and arg.item.type == FUNCTION): # Get item and children item = arg.item children = arg.children # New var v = var() # Analyze children new_children = [] new_constr = cvxpy_list([]) for child in children: child_var,child_constr = expand(child) new_children += [child_var] new_constr += child_constr # Return (Always right side is the new variable) new_tree = cvxpy_tree(item,new_children) new_constr += item._range_constr(v) new_constr += item._dom_constr(new_children) return v,cvxpy_list([equal(new_tree,v)])+new_constr # Constraint elif(type(arg) is cvxpy_constr): # Not set membership if(arg.op != 'in'): # Apply expand to left and right side obj1,constr_list1 = expand(arg.left) obj2,constr_list2 = expand(arg.right) # Return new constraints new_constr = cvxpy_constr(obj1,arg.op,obj2) new_list = cvxpy_list([new_constr]) new_list += constr_list1 new_list += constr_list2 return new_list # Set membership else: obj, constr_list = expand(arg.left) new_constr = cvxpy_constr(obj,arg.op,arg.right) return cvxpy_list([new_constr])+constr_list # Array elif(type(arg) is cvxpy_expression or type(arg) is cvxpy_var): (m,n) = arg.shape new_list = cvxpy_list([]) new_exp = cvxpy_expression(m,n) for i in range(0,m,1): for j in range(0,n,1): # Number: Upgrade if(np.isscalar(arg[i,j])): new_exp[i,j] = cvxpy_obj(CONSTANT,arg[i,j],str(arg[i,j])) # Not a number else: obj,constr_list = expand(arg[i,j]) new_exp[i,j] = obj new_list += constr_list return new_exp,new_list # List of constraints elif(type(arg) is cvxpy_list): # Empty list if(len(arg) == 0): return cvxpy_list([]) else: new_list = map(expand,arg) return reduce(lambda x,y:x+y,new_list) # Invalid else: raise ValueError('Invalid argument')
def expand(arg): # Constant if (type(arg) is cvxpy_obj): return arg, cvxpy_list([]) # Scalar variable elif (type(arg) is cvxpy_scalar_var): return arg, cvxpy_list([]) # Summation elif (type(arg) is cvxpy_tree and arg.item.name == '+'): # Get item and children item = arg.item children = arg.children # New var v = var() # Expand children new_children = [] new_constr = cvxpy_list([]) for child in children: # Multiplication if (child.type == TREE and child.item.name == '*'): child_var, child_constr = expand(child.children[1]) new_children += [child.children[0].data * child_var] new_constr += child_constr # Else else: child_var, child_constr = expand(child) new_children += [child_var] new_constr += child_constr # Return (Always right side is the new variable) new_tree = cvxpy_tree(item, new_children) return v, cvxpy_list([equal(new_tree, v)]) + new_constr # Multiplication elif (type(arg) is cvxpy_tree and arg.item.name == '*'): # Get item and children item = arg.item children = arg.children # New var v = var() # Apply expand to second operand (first is a constant) child_var, child_constr = expand(children[1]) # Return result (Always right side is the new variable) new_tree = cvxpy_tree(item, [children[0], child_var]) new_eq = cvxpy_list([equal(new_tree, v)]) new_eq += child_constr return v, new_eq # Function elif (type(arg) is cvxpy_tree and arg.item.type == FUNCTION): # Get item and children item = arg.item children = arg.children # New var v = var() # Analyze children new_children = [] new_constr = cvxpy_list([]) for child in children: child_var, child_constr = expand(child) new_children += [child_var] new_constr += child_constr # Return (Always right side is the new variable) new_tree = cvxpy_tree(item, new_children) new_constr += item._range_constr(v) new_constr += item._dom_constr(new_children) return v, cvxpy_list([equal(new_tree, v)]) + new_constr # Constraint elif (type(arg) is cvxpy_constr): # Not set membership if (arg.op != 'in'): # Apply expand to left and right side obj1, constr_list1 = expand(arg.left) obj2, constr_list2 = expand(arg.right) # Return new constraints new_constr = cvxpy_constr(obj1, arg.op, obj2) new_list = cvxpy_list([new_constr]) new_list += constr_list1 new_list += constr_list2 return new_list # Set membership else: obj, constr_list = expand(arg.left) new_constr = cvxpy_constr(obj, arg.op, arg.right) return cvxpy_list([new_constr]) + constr_list # Array elif (type(arg) is cvxpy_expression or type(arg) is cvxpy_var): (m, n) = arg.shape new_list = cvxpy_list([]) new_exp = cvxpy_expression(m, n) for i in range(0, m, 1): for j in range(0, n, 1): # Number: Upgrade if (np.isscalar(arg[i, j])): new_exp[i, j] = cvxpy_obj(CONSTANT, arg[i, j], str(arg[i, j])) # Not a number else: obj, constr_list = expand(arg[i, j]) new_exp[i, j] = obj new_list += constr_list return new_exp, new_list # List of constraints elif (type(arg) is cvxpy_list): # Empty list if (len(arg) == 0): return cvxpy_list([]) else: new_list = map(expand, arg) return reduce(lambda x, y: x + y, new_list) # Invalid else: raise ValueError('Invalid argument')