當前位置: 首頁>>代碼示例>>Python>>正文


Python LpProblem.writeMPS方法代碼示例

本文整理匯總了Python中pulp.LpProblem.writeMPS方法的典型用法代碼示例。如果您正苦於以下問題:Python LpProblem.writeMPS方法的具體用法?Python LpProblem.writeMPS怎麽用?Python LpProblem.writeMPS使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pulp.LpProblem的用法示例。


在下文中一共展示了LpProblem.writeMPS方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from pulp import LpProblem [as 別名]
# 或者: from pulp.LpProblem import writeMPS [as 別名]
class LPSolver:
    '''
    LP solver class
    Used for solving LP and MILP problems
    '''
    
    def __init__(self,name='analysis',verbose=False):
        self.lpProblem = LpProblem()
        self.lpVariables = {}
        self.lpObjective = {}
        self.predictionMap = {}
        self.objectiveValue = None
        
        self.rowNames= []
        self.columnNames= []
        self.configFile = ''
        self.statusCode = None
        
        self.verbose = verbose
        self.useLimitTag = False
        self.mpsLogFile = False
        self.ignoreBadReferences = False
        self.Mip = True
        self.scip_path = ":" + os.environ["HOME"] + "/bin"
            
    def _parseSCIPLog(self, fileName):
        '''
        Parse result of SCIP analysis.
        This section needs a much more solid testing as it key interface with the solver 
        but often solver errors are dropped when they occur with no / minimal warnings.
        
        @var fileName: name and directory information of file being parsed, usually a temp file 
        @type fileName: string
        @return: (values of fluxes, value of objective function, status of optimization)
        @rtype: (dict,float,string) 
        '''
        fileHandle = open(fileName, 'r')
        location = 0
        line = fileHandle.readline()
        
        while not line.startswith('SCIP Status        :'):
            if line.startswith('Syntax error'):
                print "SCIP parsing syntax error: %s" % line
                line = fileHandle.readline()
                print line
            ilocation = fileHandle.tell() 
            if  location == ilocation:
                print "failed to read file %s" %(fileName)
                return ({},0.0,"failed to read file")
            location = ilocation

            line = fileHandle.readline()
        
        m = re.match('[\w\s:]*\[([\w\s]*)\]', line)
        statusString = m.group(1)

        objectiveValue = None
        result = dict()
        
        if statusString == 'optimal solution found':
            line = fileHandle.readline()
            while not line.startswith('objective value:'):
                #Good place to check all the features of the solution.
                if line.startswith('solution violates'):
                    print "Error solving optimization [%s]" % line
                ilocation = fileHandle.tell() 
                if  location == ilocation:
                    print "failed to read file %s" %(fileName)
                    return ({},0.0,"failed to read file")
                line = fileHandle.readline()
                
            m = re.match('objective value:\s+([\S]+)', line)
            objectiveValue = float(m.group(1))
            
            line = fileHandle.readline()
            while not line.strip() == '':
                m = re.match('([\S]+)\s+([\S]+)', line)
                name = m.group(1)
                result[name] = float(m.group(2))
                line = fileHandle.readline()
        
        fileHandle.close()
        
        return (result, objectiveValue, statusString)
        
        
    def writeMPS(self,fileName):
        """
        @param fileName: name of file
        @type fileName: String
        """
        self.lpProblem.writeMPS(fileName)
        return True
        
    def writeLP(self,fileName):
        """
        @param fileName: name of file
        @type fileName: String
        """
        self.lpProblem.writeLP(fileName,writeSOS=0)
#.........這裏部分代碼省略.........
開發者ID:bionomicron,項目名稱:Redirector,代碼行數:103,代碼來源:LPSolver.py


注:本文中的pulp.LpProblem.writeMPS方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。