本文整理汇总了Python中pyscipopt.Model.addConsSOS1方法的典型用法代码示例。如果您正苦于以下问题:Python Model.addConsSOS1方法的具体用法?Python Model.addConsSOS1怎么用?Python Model.addConsSOS1使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyscipopt.Model
的用法示例。
在下文中一共展示了Model.addConsSOS1方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gcp_sos
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import addConsSOS1 [as 别名]
def gcp_sos(V,E,K):
"""gcp_sos -- model for minimizing the number of colors in a graph
(use sos type 1 constraints)
Parameters:
- V: set/list of nodes in the graph
- E: set/list of edges in the graph
- K: upper bound to the number of colors
Returns a model, ready to be solved.
"""
model = Model("gcp - sos constraints")
x,y = {},{}
for k in range(K):
y[k] = model.addVar(vtype="B", name="y(%s)"%k)
for i in V:
x[i,k] = model.addVar(vtype="B", name="x(%s,%s)"%(i,k))
for i in V:
model.addCons(quicksum(x[i,k] for k in range(K)) == 1, "AssignColor(%s)" % i)
model.addConsSOS1([x[i,k] for k in range(K)])
for (i,j) in E:
for k in range(K):
model.addCons(x[i,k] + x[j,k] <= y[k], "NotSameColor(%s,%s,%s)"%(i,j,k))
for k in range(K-1):
model.addCons(y[k] >= y[k+1], "LowColor(%s)"%k)
model.setObjective(quicksum(y[k] for k in range(K)), "minimize")
model.data = x
return model
示例2: bpp
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import addConsSOS1 [as 别名]
def bpp(s,B):
"""bpp: Martello and Toth's model to solve the bin packing problem.
Parameters:
- s: list with item widths
- B: bin capacity
Returns a model, ready to be solved.
"""
n = len(s)
U = len(FFD(s,B)) # upper bound of the number of bins
model = Model("bpp")
# setParam("MIPFocus",1)
x,y = {},{}
for i in range(n):
for j in range(U):
x[i,j] = model.addVar(vtype="B", name="x(%s,%s)"%(i,j))
for j in range(U):
y[j] = model.addVar(vtype="B", name="y(%s)"%j)
# assignment constraints
for i in range(n):
model.addCons(quicksum(x[i,j] for j in range(U)) == 1, "Assign(%s)"%i)
# bin capacity constraints
for j in range(U):
model.addCons(quicksum(s[i]*x[i,j] for i in range(n)) <= B*y[j], "Capac(%s)"%j)
# tighten assignment constraints
for j in range(U):
for i in range(n):
model.addCons(x[i,j] <= y[j], "Strong(%s,%s)"%(i,j))
# tie breaking constraints
for j in range(U-1):
model.addCons(y[j] >= y[j+1],"TieBrk(%s)"%j)
# SOS constraints
for i in range(n):
model.addConsSOS1([x[i,j] for j in range(U)])
model.setObjective(quicksum(y[j] for j in range(U)), "minimize")
model.data = x,y
return model