コード例 #1
0
ファイル: semidefinite.py プロジェクト: lfwin/CvxAlgGeo
    def __init__(
        self,
        Prog,
        Rng,
        Settings={
            'Order': 0,
            'Solver': 'cvxopt',
            'Iterations': 100,
            'detail': True,
            'tryKKT': 0
        }):
        """
        Initiates a SosTools object by input data, mainly polynomials
        f and g1,..., gm
        
        Arguments:
	    Prog:
	        is the list of polynomials f, g_1,..., gm, in the following
	        format:
	                [f, [g1,..., gm]]
	
	    Rng:
	        is the polynomial ring were f, g1,..., gm are coming from.
	        A typical format is:
	               PolynomialRing(base, 'x', n)
	        where 
	        base is the base ring, such as 
	            RR: Real numbers,
	            QQ: Rational numbers
	            ZZ: Integers
	            etc,
	        'x' is a generic name for indeterminate, and
	         n is the number of indeterminates.
	
	    Settings:
	        is a dictionary of options to configure the behavior of the
	        object as well as sdp solver.
	        The options are:
	
	        Order:
	            The order of sdp relaxation is (the ceiling of
	            half degree of f, g1,..., gm) + Order. 
	        Solver:
	            'cvxopt',
	            'csdp',
	            'sdpa'.
	            by default is set to 'cvxopt', but based on available
	            sdp solvers can be chaned to 'csdp' and 'sdpa'.
	        Iterations:
	            The maximum number of iterations of the solver. The
	            default value is 100, and it currently just effects 
	            'cvxopt' solver.
	        detail:
	            Boolean value, default is True. Set to False to hide
                    the sdp solver's progress output. Only works for 'cvxopt'.
	        tryKKT:
	            A positive integer number. If the solver encounters a
	            singular KKT matrix, it continues to iterate for this 
	            given number of iterations. Only works for 'cvxopt'.
        """

        self.MainPolynomial = Prog[0]
        self.Field = self.MainPolynomial.base_ring()
        self.Ring = Rng
        self.vars = self.Ring.gens()
        self.NumVars = len(self.vars)
        ###
        f_tot_deg = self.MainPolynomial.total_degree()
        if (f_tot_deg % 2) == 1:
            f_half_deg = (f_tot_deg + 1) / 2
        else:
            f_half_deg = f_tot_deg / 2

        self.MainPolyHlfDeg = f_half_deg
        self.MainPolyTotDeg = f_tot_deg
        ###
        self.Constraints = []
        if len(Prog) > 1:
            self.Constraints = Prog[1]
        self.Constraints.append(self.vars[0]**0)
        self.NumberOfConstraints = len(self.Constraints)
        ###
        for g in self.Constraints:
            tmp_deg = g.total_degree()
            self.Degs.append(tmp_deg)
            if (tmp_deg % 2) == 1:
                self.HalfDegs.append((tmp_deg + 1) / 2)
            else:
                self.HalfDegs.append(tmp_deg / 2)
        ###
        if 'Order' in Settings:
            self.Ord = Settings['Order']
        else:
            self.Ord = 0
        ###
        if 'Solver' in Settings:
            self.solver = Settings['Solver']
        else:
            self.solver = 'cvxopt'
        ###
        if 'detail' in Settings:
            self.detail = Settings['detail']
        else:
            self.detail = True
        ###
        if len(self.HalfDegs) > 0:
            cns_half_deg_max = max(self.HalfDegs)
        else:
            cns_half_deg_max = 0
        self.Relaxation = max(self.Relaxation, f_half_deg,
                              cns_half_deg_max) + self.Ord
        ###
        self.Monomials = self.MonomialsVec(2 * self.Relaxation)
        self.Info = {
            'min': 0,
            'CPU': 0,
            'Wall': 0,
            'status': 'Unknown',
            'Message': '',
            'is sos': False
        }

        from sage.interfaces.matlab import Matlab
        try:
            self.MATLAB = {
                'available': True,
                'gloptipoly': False,
                'sedumi': False,
                'sdpnal': False
            }
            a = Matlab()
            path_str = a.get('path')
            if path_str.find('gloptipoly') >= 0:
                self.MATLAB['gloptipoly'] = True
            if path_str.find('SeDuMi') >= 0:
                self.MATLAB['sedumi'] = True
            if path_str.find('SDPNAL') >= 0:
                self.MATLAB['sdpnal'] = True
            a.eval('quit;')
        except:
            self.MATLAB = {
                'available': False,
                'gloptipoly': False,
                'sedumi': False,
                'sdpnal': False
            }
コード例 #2
0
ファイル: semidefinite.py プロジェクト: hiepdang/CvxAlgGeo
    def __init__(self, Prog, Rng, Settings = {'Order':0, 'Solver':'cvxopt', 'Iterations':100, 'detail':True, 'tryKKT':0}):
        """
        Initiates a SosTools object by input data, mainly polynomials
        f and g1,..., gm
        
        Arguments:
	    Prog:
	        is the list of polynomials f, g_1,..., gm, in the following
	        format:
	                [f, [g1,..., gm]]
	
	    Rng:
	        is the polynomial ring were f, g1,..., gm are coming from.
	        A typical format is:
	               PolynomialRing(base, 'x', n)
	        where 
	        base is the base ring, such as 
	            RR: Real numbers,
	            QQ: Rational numbers
	            ZZ: Integers
	            etc,
	        'x' is a generic name for indeterminate, and
	         n is the number of indeterminates.
	
	    Settings:
	        is a dictionary of options to configure the behavior of the
	        object as well as sdp solver.
	        The options are:
	
	        Order:
	            The order of sdp relaxation is (the ceiling of
	            half degree of f, g1,..., gm) + Order. 
	        Solver:
	            'cvxopt',
	            'csdp',
	            'sdpa'.
	            by default is set to 'cvxopt', but based on available
	            sdp solvers can be chaned to 'csdp' and 'sdpa'.
	        Iterations:
	            The maximum number of iterations of the solver. The
	            default value is 100, and it currently just effects 
	            'cvxopt' solver.
	        detail:
	            Boolean value, default is True. Set to False to hide
                    the sdp solver's progress output. Only works for 'cvxopt'.
	        tryKKT:
	            A positive integer number. If the solver encounters a
	            singular KKT matrix, it continues to iterate for this 
	            given number of iterations. Only works for 'cvxopt'.
        """
                
        self.MainPolynomial = Prog[0]
        self.Field = self.MainPolynomial.base_ring()
        self.Ring = Rng
        self.vars = self.Ring.gens()
        self.NumVars = len(self.vars)
        ###
        f_tot_deg = self.MainPolynomial.total_degree()
        if (f_tot_deg % 2) == 1:
            f_half_deg = (f_tot_deg + 1)/2
        else:
            f_half_deg = f_tot_deg/2
        
        self.MainPolyHlfDeg = f_half_deg
        self.MainPolyTotDeg = f_tot_deg
        ###
        self.Constraints = []
        if len(Prog) > 1:
            self.Constraints = Prog[1]
        self.Constraints.append(self.vars[0]**0)
        self.NumberOfConstraints = len(self.Constraints)
        ###
        for g in self.Constraints:
            tmp_deg = g.total_degree()
            self.Degs.append(tmp_deg)
            if (tmp_deg % 2) == 1:
                self.HalfDegs.append((tmp_deg+1)/2)
            else:
                self.HalfDegs.append(tmp_deg/2)
        ###
        if 'Order' in Settings:
            self.Ord = Settings['Order']
        else:
            self.Ord = 0
        ###
        if 'Solver' in Settings:
            self.solver = Settings['Solver']
        else:
            self.solver = 'cvxopt'
        ###
        if 'detail' in Settings:
            self.detail = Settings['detail']
        else:
            self.detail = True
        ###
        if len(self.HalfDegs) > 0 :
            cns_half_deg_max = max(self.HalfDegs)
        else:
            cns_half_deg_max = 0
        self.Relaxation = max(self.Relaxation,f_half_deg,cns_half_deg_max)+self.Ord
        ###
        self.Monomials = self.MonomialsVec(2*self.Relaxation)
        self.Info = {'min':0, 'CPU':0, 'Wall':0, 'status':'Unknown', 'Message':'', 'is sos':False}
        
        from sage.interfaces.matlab import Matlab
        try:
            self.MATLAB = {'available':True, 'gloptipoly':False, 'sedumi':False, 'sdpnal':False}
            a = Matlab()
            path_str = a.get('path')
            if path_str.find('gloptipoly') >= 0:
                self.MATLAB['gloptipoly'] = True
            if path_str.find('SeDuMi') >= 0:
                self.MATLAB['sedumi'] = True
            if path_str.find('SDPNAL') >= 0:
                self.MATLAB['sdpnal'] = True
            a.eval('quit;')
        except:
            self.MATLAB = {'available':False, 'gloptipoly':False, 'sedumi':False, 'sdpnal':False}
コード例 #3
0
ファイル: semidefinite.py プロジェクト: lfwin/CvxAlgGeo
 def CallMatlab(self, solver='sedumi'):
     """
     """
     from sage.interfaces.matlab import Matlab
     a = Matlab()
     code_line = "mpol x " + str(self.NumVars)
     a.eval(code_line)
     code_line = "f = " + self.GloptiPolyStr(self.MainPolynomial) + ";"
     a.eval(code_line)
     code_line = "K = ["
     for idx in range(self.NumberOfConstraints - 1):
         if (idx < self.NumberOfConstraints - 1) and (self.Constraints[idx]
                                                      != 1.0):
             if code_line != "K = [":
                 code_line += ", "
             code_line += self.GloptiPolyStr(self.Constraints[idx]) + ">= 0"
     code_line += "];"
     a.eval(code_line)
     code_line = "P = msdp(min(f), K);"
     a.eval(code_line)
     if solver.lower() == 'sdpnal':
         code_line = "[A,b,c,K] = msedumi(P);"
         a.eval(code_line)
         code_line = "opts = []; opts.tol = 1e-7;"
         a.eval(code_line)
         code_line = "[blk,At,C,B] = read_sedumi(A,b,c,K);"
         a.eval(code_line)
         code_line = "[obj,X,y,Z,info,runhist] = sdpnal(blk,At,C,B,opts);"
         a.eval(code_line)
         code_line = "res = -(obj(1)+obj(2))/2;"
         a.eval(code_line)
         RES = [eval(a.get("res"))]
         code_line = "cputime = runhist.cputime(size(runhist.cputime,2));"
         a.eval(code_line)
         RES.append(eval(a.get('cputime')))
     else:
         code_line = "[status,obj] = msol(P)"
         a.eval(code_line)
         RES = [a.get("obj")]
     a.eval("quit;")
     return RES
コード例 #4
0
ファイル: semidefinite.py プロジェクト: hiepdang/CvxAlgGeo
 def CallMatlab(self, solver = 'sedumi'):
     """
     """
     from sage.interfaces.matlab import Matlab
     a = Matlab()
     code_line = "mpol x " + str(self.NumVars)
     a.eval(code_line)
     code_line = "f = " + self.GloptiPolyStr(self.MainPolynomial) + ";"
     a.eval(code_line)
     code_line = "K = ["
     for idx in range(self.NumberOfConstraints - 1):
         if (idx < self.NumberOfConstraints - 1) and (self.Constraints[idx] != 1.0):
             if code_line != "K = [":
                 code_line += ", " 
             code_line += self.GloptiPolyStr(self.Constraints[idx]) + ">= 0"
     code_line += "];"
     a.eval(code_line)
     code_line = "P = msdp(min(f), K);"
     a.eval(code_line)
     if solver.lower() == 'sdpnal':
         code_line = "[A,b,c,K] = msedumi(P);"
         a.eval(code_line)
         code_line = "opts = []; opts.tol = 1e-7;"
         a.eval(code_line)
         code_line = "[blk,At,C,B] = read_sedumi(A,b,c,K);"
         a.eval(code_line)
         code_line = "[obj,X,y,Z,info,runhist] = sdpnal(blk,At,C,B,opts);"
         a.eval(code_line)
         code_line = "res = -(obj(1)+obj(2))/2;"
         a.eval(code_line)
         RES = [eval(a.get("res"))]
         code_line = "cputime = runhist.cputime(size(runhist.cputime,2));"
         a.eval(code_line)
         RES.append(eval(a.get('cputime')))
     else:
         code_line = "[status,obj] = msol(P)"
         a.eval(code_line)
         RES = [a.get("obj")]
     a.eval("quit;")
     return RES