当前位置: 首页>>代码示例>>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;未经允许,请勿转载。