当前位置: 首页>>代码示例>>Python>>正文


Python Model.includePricer方法代码示例

本文整理汇总了Python中pyscipopt.Model.includePricer方法的典型用法代码示例。如果您正苦于以下问题:Python Model.includePricer方法的具体用法?Python Model.includePricer怎么用?Python Model.includePricer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyscipopt.Model的用法示例。


在下文中一共展示了Model.includePricer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: solve

# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import includePricer [as 别名]
    def solve(self, integer=False):
        '''
        By default we solve a linear version of the column generation.
        If integer is True than we solve the problem of finding the best
        routes to be used without column generation.
        '''
        self.integer = integer
        if self.patterns is None:
            self.genInitialPatterns()

        # Creating master Model:
        master = Model("Master problem")

        # Creating pricer:
        if not integer:
            master.setPresolve(SCIP_PARAMSETTING.OFF)

        # Populating master model.
        # Binary variables z_r indicating whether
        # pattern r is used in the solution:
        z = {}

        for i, _ in enumerate(self.patterns):
            z[i] = master.addVar(vtype="B" if integer else "C",
                                 lb=0.0, ub=1.0, name="z_%d" % i)

        # Set objective:
        master.setObjective(quicksum(self.patCost(p)*z[i] for i, p in enumerate(self.patterns)),
                            "minimize")

        clientCons = [None]*len(self.clientNodes)
        
        for i, c in enumerate(self.clientNodes):
            cons = master.addCons(
            quicksum(self.isClientVisited(c, p)*z[i] for i, p in enumerate(self.patterns)) == 1,
                "Consumer_%d" % c,
                separate=False, modifiable=True)

            clientCons[i] = cons

        if not integer:
            pricer = VRPpricer(z, clientCons, self.data, self.patterns,
                               self.data.costs, self.isClientVisited,
                               self.patCost, self.maxPatterns)

            master.includePricer(pricer, "VRP pricer", "Identifying new routes")

            self.pricer = pricer

        if integer:
            print("Finding the best patterns among:")
            for p in self.patterns:
                print(p)

        self.master = master  # Save master model.
        
        master.optimize()
开发者ID:pedrocastellucci,项目名称:playground,代码行数:59,代码来源:vrp_scip_cg.py

示例2: test_cuttingstock

# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import includePricer [as 别名]
def test_cuttingstock():
    # create solver instance
    s = Model("CuttingStock")

    s.setPresolve(0)

    # creating a pricer
    pricer = CutPricer()
    s.includePricer(pricer, "CuttingStockPricer", "Pricer to identify new cutting stock patterns")

    # item widths
    widths = [14, 31, 36, 45]
    # width demand
    demand = [211, 395, 610, 97]
    # roll length
    rollLength = 100
    assert len(widths) == len(demand)

    # adding the initial variables
    cutPatternVars = []
    varNames = []
    varBaseName = "Pattern"
    patterns = []

    initialCoeffs = []
    for i in range(len(widths)):
        varNames.append(varBaseName + "_" + str(i))
        cutPatternVars.append(s.addVar(varNames[i], obj = 1.0))

    # adding a linear constraint for the knapsack constraint
    demandCons = []
    for i in range(len(widths)):
        numWidthsPerRoll = float(int(rollLength/widths[i]))
        demandCons.append(s.addCons(numWidthsPerRoll*cutPatternVars[i] >= demand[i],
                                    separate = False, modifiable = True))
        newPattern = [0]*len(widths)
        newPattern[i] = numWidthsPerRoll
        patterns.append(newPattern)

    # Setting the pricer_data for use in the init and redcost functions
    pricer.data = {}
    pricer.data['var'] = cutPatternVars
    pricer.data['cons'] = demandCons
    pricer.data['widths'] = widths
    pricer.data['demand'] = demand
    pricer.data['rollLength'] = rollLength
    pricer.data['patterns'] = patterns

    # solve problem
    s.optimize()

    # print original data
    printWidths = '\t'.join(str(e) for e in widths)
    print('\nInput Data')
    print('==========')
    print('Roll Length:', rollLength)
    print('Widths:\t', printWidths)
    print('Demand:\t', '\t'.join(str(e) for e in demand))

    # print solution
    widthOutput = [0]*len(widths)
    print('\nResult')
    print('======')
    print('\t\tSol Value', '\tWidths\t', printWidths)
    for i in range(len(pricer.data['var'])):
        rollUsage = 0
        solValue = round(s.getVal(pricer.data['var'][i]))
        if solValue > 0:
            outline = 'Pattern_' + str(i) + ':\t' + str(solValue) + '\t\tCuts:\t'
            for j in range(len(widths)):
                rollUsage += pricer.data['patterns'][i][j]*widths[j]
                widthOutput[j] += pricer.data['patterns'][i][j]*solValue
                outline += str(pricer.data['patterns'][i][j]) + '\t'
            outline += 'Usage:' + str(rollUsage)
            print(outline)

    print('\t\t\tTotal Output:','\t'.join(str(e) for e in widthOutput))
开发者ID:fserra,项目名称:PySCIPOpt,代码行数:79,代码来源:test_pricer.py


注:本文中的pyscipopt.Model.includePricer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。