예제 #1
0
    def solve(self,quiet=False,return_status=False):
        """
        Solves optimization program using current
        parameter values.
        """

        # Check DCP
        if not self.is_dcp():
            raise ValueError('Program is not DCP')

        # Create param-value map
        replace_map = {}
        for param in self.parameters:
            if np.isnan(param.value):
                raise ValueError('Invalid parameter value: NaN')
            else:
                replace_map[param] = param.value
        
        # Create new program
        new_p = cvxpy_program(self.action,
                              re_eval(self.objective,replace_map),
                              re_eval(self.constraints,replace_map),
                              [],self.options,'')

        # Solve new program
        obj,valid = solve_prog(new_p,quiet)
        if return_status:
            return obj,valid
        else:
            return obj
예제 #2
0
    def solve(self, quiet=False, return_status=False):
        """
        Solves optimization program using current
        parameter values.
        """

        # Check DCP
        if not self.is_dcp():
            raise ValueError('Program is not DCP')

        # Create param-value map
        replace_map = {}
        for param in self.parameters:
            if np.isnan(param.value):
                raise ValueError('Invalid parameter value: NaN')
            else:
                replace_map[param] = param.value

        # Create new program
        new_p = cvxpy_program(self.action, re_eval(self.objective,
                                                   replace_map),
                              re_eval(self.constraints, replace_map), [],
                              self.options, '')

        # Solve new program
        obj, valid = solve_prog(new_p, quiet)
        if return_status:
            return obj, valid
        else:
            return obj
예제 #3
0
 def solve(self):
     """
     Description
     -----------
     Solve program.
     """
     obj, lagrange_mul_eq = solve_prog(self)
     return obj
예제 #4
0
 def solve(self):
     """
     Description
     -----------
     Solve program.
     """
     obj,lagrange_mul_eq = solve_prog(self)
     return obj
예제 #5
0
    def _solve_isolating(self, *args):
        """
        Description
        -----------
        Isolate parameters and then solve program.
        Used by linearize method to obtain subgradient.
        Arguments must be numbers. A very important 
        point is that solve_isolating introduces
        equality constraints and places them at the
        beginning of the constraint list. This is 
        later used to construct the subgradient.
        """

        # Process input
        if (len(args) != 0 and type(args[0]) is list):
            args = args[0]

        # Create argument list
        arg_list = []
        for arg in args:
            if (np.isscalar(arg)):
                arg_list += [arg]
            elif (type(arg) is cvxpy_matrix):
                (m, n) = arg.shape
                for i in range(0, m, 1):
                    for j in range(0, n, 1):
                        arg_list += [arg[i, j]]
            else:
                raise ValueError('Arguments must be numeric')

        # Check number of arguments
        if (len(arg_list) != len(self.params)):
            raise ValueError('Invalid number of arguments')

        # Isolate parameters
        p1_map = {}
        new_constr = cvxpy_list([])
        for p in self.params:
            v = var('v_' + p.name)
            p1_map[p] = v
            new_constr += cvxpy_list([equal(v, p)])
        new_p1 = prog((self.action, re_eval(self.obj, p1_map)),
                      new_constr + re_eval(self.constr, p1_map), self.params,
                      self.options, self.name)

        # Substitute parameters with arguments
        p2_map = {}
        for k in range(0, len(arg_list), 1):
            p2_map[new_p1.params[k]] = arg_list[k]
        new_p2 = prog((new_p1.action, re_eval(new_p1.obj, p2_map)),
                      re_eval(new_p1.constr, p2_map), [], new_p1.options,
                      new_p1.name)

        # Solve program
        obj, lagrange_mul_eq = solve_prog(new_p2)
        self.lagrange_mul_eq = lagrange_mul_eq
        return obj
예제 #6
0
    def _solve_isolating(self,*args):
        """
        Description
        -----------
        Isolate parameters and then solve program.
        Used by linearize method to obtain subgradient.
        Arguments must be numbers. A very important 
        point is that solve_isolating introduces
        equality constraints and places them at the
        beginning of the constraint list. This is 
        later used to construct the subgradient.
        """

        # Process input
        if(len(args) != 0 and type(args[0]) is list):
            args = args[0]
        
        # Create argument list
        arg_list = []
        for arg in args:
            if(np.isscalar(arg)):
                arg_list += [arg]
            elif(type(arg) is cvxpy_matrix):
                (m,n) = arg.shape
                for i in range(0,m,1):
                    for j in range(0,n,1):
                        arg_list += [arg[i,j]]
            else:
                raise ValueError('Arguments must be numeric')

        # Check number of arguments
        if(len(arg_list) != len(self.params)):
            raise ValueError('Invalid number of arguments')

        # Isolate parameters
        p1_map = {}
        new_constr = cvxpy_list([])
        for p in self.params:
            v = var('v_'+p.name)
            p1_map[p] = v
            new_constr += cvxpy_list([equal(v,p)])
        new_p1 = prog((self.action,re_eval(self.obj,p1_map)),
                      new_constr+re_eval(self.constr,p1_map),
                      self.params, self.options,self.name)

        # Substitute parameters with arguments
        p2_map = {}
        for k in range(0,len(arg_list),1):
            p2_map[new_p1.params[k]] = arg_list[k]
        new_p2 = prog((new_p1.action,re_eval(new_p1.obj,p2_map)),
                      re_eval(new_p1.constr,p2_map),
                      [],new_p1.options,new_p1.name)

        # Solve program
        obj,lagrange_mul_eq = solve_prog(new_p2)
        self.lagrange_mul_eq = lagrange_mul_eq
        return obj
예제 #7
0
    def __call__(self, *args):
        """
        Description
        -----------
        Call program with specified arguments.
        Parameters are substituted with arguments.
        If all arguments are numeric, the resulting
        optimization program is solved. If some
        arguments are object, a tree is returned.
        
        Arguments
        ---------
        args: List of arguments.
        (Can be numbers or objects)
        """

        # Process input
        if (len(args) != 0 and type(args[0]) is list):
            args = args[0]

        # Create argument list
        arg_list = []
        for arg in args:
            if (np.isscalar(arg) or type(arg).__name__ in SCALAR_OBJS):
                arg_list += [arg]
            elif (type(arg) is cvxpy_matrix
                  or type(arg).__name__ in ARRAY_OBJS):
                (m, n) = arg.shape
                for i in range(0, m, 1):
                    for j in range(0, n, 1):
                        arg_list += [arg[i, j]]
            else:
                raise ValueError('Invalid argument type')

        # Check number of arguments
        if (len(arg_list) != len(self.params)):
            raise ValueError('Invalid argument syntax')

        # Solve if numeric
        if (len(arg_list) == 0
                or reduce(lambda x, y: x and y,
                          map(lambda x: np.isscalar(x), arg_list))):

            # Substitute parameters with arguments
            p1_map = {}
            for k in range(0, len(arg_list), 1):
                p1_map[self.params[k]] = arg_list[k]
            new_p = prog((self.action, re_eval(self.obj, p1_map)),
                         re_eval(self.constr, p1_map), [], self.options,
                         self.name)

            # Solve program
            obj, lagrange_mul_eq = solve_prog(new_p)
            return obj

        # Upgrade numbers to objects
        for i in range(0, len(arg_list), 1):
            if (np.isscalar(arg_list[i])):
                arg_list[i] = cvxpy_obj(CONSTANT, arg_list[i],
                                        str(arg_list[i]))

        # Return tree
        return cvxpy_tree(self, arg_list)
예제 #8
0
    def __call__(self,*args):
        """
        Description
        -----------
        Call program with specified arguments.
        Parameters are substituted with arguments.
        If all arguments are numeric, the resulting
        optimization program is solved. If some
        arguments are object, a tree is returned.
        
        Arguments
        ---------
        args: List of arguments.
        (Can be numbers or objects)
        """

        # Process input
        if(len(args) != 0 and type(args[0]) is list):
            args = args[0]
        
        # Create argument list
        arg_list = []
        for arg in args:
            if(np.isscalar(arg) or 
               type(arg).__name__ in SCALAR_OBJS):
                arg_list += [arg]
            elif(type(arg) is cvxpy_matrix or 
                 type(arg).__name__ in ARRAY_OBJS):
                (m,n) = arg.shape
                for i in range(0,m,1):
                    for j in range(0,n,1):
                        arg_list += [arg[i,j]]
            else:
                raise ValueError('Invalid argument type')

        # Check number of arguments
        if(len(arg_list) != len(self.params)):
            raise ValueError('Invalid argument syntax')
    
        # Solve if numeric
        if(len(arg_list) == 0 or
           reduce(lambda x,y: x and y,
                  map(lambda x: np.isscalar(x),arg_list))):

            # Substitute parameters with arguments
            p1_map = {}
            for k in range(0,len(arg_list),1):
                p1_map[self.params[k]] = arg_list[k]
            new_p = prog((self.action,re_eval(self.obj,p1_map)),
                         re_eval(self.constr,p1_map),[],
                         self.options,self.name)
            
            # Solve program
            obj,lagrange_mul_eq = solve_prog(new_p)
            return obj        

        # Upgrade numbers to objects
        for i in range(0,len(arg_list),1):
            if(np.isscalar(arg_list[i])):
                arg_list[i] = cvxpy_obj(CONSTANT,
                                        arg_list[i],
                                        str(arg_list[i]))

        # Return tree
        return cvxpy_tree(self,arg_list)