def _pm_expand(self, constr): """ Description: Return the partial minimization expansion of the function. Argument constr: The constraint to be replaced. It is assumed to be in expanded format so the right hand side is a variable. """ arg = constr.left.children[0] right = constr.right return cvxpy_list([less(-right, arg), less(arg, right)])
def _pm_expand(self,constr): """ Description: Return the partial minimization expansion of the function. Argument constr: The constraint to be replaced. It is assumed to be in expanded format so the right hand side is a variable. """ arg = constr.left.children[0] right = constr.right return cvxpy_list([less(-right,arg), less(arg,right)])
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
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
def _pm_expand(self, constr): """ Description ----------- Return the partial minimization expansion of the function. Argument -------- constr: The constraint to be replaced. It is assumed the constraint was expanded and transformed so that the right hand side is a variable. """ new_list = [] for arg in constr.left.children: if type(arg) is cvxpy_obj: arg = arg.get_value() new_list += [less(arg, constr.right)] return cvxpy_list(new_list)
def _pm_expand(self, constr): """ Description ----------- Return the partial minimization expansion of the function. Argument -------- constr: The constraint to be replaced. It is assumed the constraint was expanded and transformed so that the right hand side is a variable. """ new_list = [] for arg in constr.left.children: if (type(arg) is cvxpy_obj): arg = arg.get_value() new_list += [less(arg, constr.right)] return cvxpy_list(new_list)
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)
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)
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')