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


Python Model.relax方法代码示例

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


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

示例1: solveCuttingStock

# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import relax [as 别名]
def solveCuttingStock(w,q,B):
    """solveCuttingStock: use column generation (Gilmore-Gomory approach).
    Parameters:
        - w: list of item's widths
        - q: number of items of a width
        - B: bin/roll capacity
    Returns a solution: list of lists, each of which with the cuts of a roll.
    """
    t = []      # patterns
    m = len(w)

    # Generate initial patterns with one size for each item width
    for (i,width) in enumerate(w):
        pat = [0]*m  # vector of number of orders to be packed into one roll (bin)
        pat[i] = int(B/width)
        t.append(pat)

    # if LOG:
    #     print "sizes of orders=",w
    #     print "quantities of orders=",q
    #     print "roll size=",B
    #     print "initial patterns",t

    K = len(t)
    master = Model("master LP") # master LP problem
    x = {}

    for k in range(K):
        x[k] = master.addVar(vtype="I", name="x(%s)"%k)

    orders = {}

    for i in range(m):
        orders[i] = master.addCons(
            quicksum(t[k][i]*x[k] for k in range(K) if t[k][i] > 0) >= q[i], "Order(%s)"%i)

    master.setObjective(quicksum(x[k] for k in range(K)), "minimize")

    # master.Params.OutputFlag = 0 # silent mode

    # iter = 0
    while True:
        # print "current patterns:"
        # for ti in t:
        #     print ti
        # print

        # iter += 1
        relax = master.relax()
        relax.optimize()
        pi = [relax.getDualsolLinear(c) for c in relax.getConss()] # keep dual variables

        knapsack = Model("KP")     # knapsack sub-problem
        knapsack.setMaximize       # maximize
        y = {}

        for i in range(m):
            y[i] = knapsack.addVar(lb=0, ub=q[i], vtype="I", name="y(%s)"%i)

        knapsack.addCons(quicksum(w[i]*y[i] for i in range(m)) <= B, "Width")

        knapsack.setObjective(quicksum(pi[i]*y[i] for i in range(m)), "maximize")

        knapsack.hideOutput() # silent mode
        knapsack.optimize()
        # if LOG:
        #     print "objective of knapsack problem:", knapsack.ObjVal
        if knapsack.getObjVal() < 1+EPS: # break if no more columns
            break

        pat = [int(y[i].X+0.5) for i in y]      # new pattern
        t.append(pat)
        # if LOG:
        #     print "shadow prices and new pattern:"
        #     for (i,d) in enumerate(pi):
        #         print "\t%5s%12s%7s" % (i,d,pat[i])
        #     print

        # add new column to the master problem
        col = Column()
        for i in range(m):
            if t[K][i] > 0:
                col.addTerms(t[K][i], orders[i])
        x[K] = master.addVar(obj=1, vtype="I", name="x(%s)"%K, column=col)

        # master.write("MP" + str(iter) + ".lp")
        K += 1


    # Finally, solve the IP
    # if LOG:
    #     master.Params.OutputFlag = 1 # verbose mode
    master.optimize()

    # if LOG:
    #     print
    #     print "final solution (integer master problem):  objective =", master.ObjVal
    #     print "patterns:"
    #     for k in x:
    #         if x[k].X > EPS:
#.........这里部分代码省略.........
开发者ID:SCIP-Interfaces,项目名称:PySCIPOpt,代码行数:103,代码来源:cutstock.py


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