本文整理汇总了Python中pyscipopt.Model.createCons方法的典型用法代码示例。如果您正苦于以下问题:Python Model.createCons方法的具体用法?Python Model.createCons怎么用?Python Model.createCons使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyscipopt.Model
的用法示例。
在下文中一共展示了Model.createCons方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_model
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import createCons [as 别名]
def create_model():
# create solver instance
s = Model()
# add some variables
x = s.addVar("x", obj = -1.0, vtype = "I", lb=-10)
y = s.addVar("y", obj = 1.0, vtype = "I", lb=-1000)
z = s.addVar("z", obj = 1.0, vtype = "I", lb=-1000)
# add some constraint
s.addCons(314*x + 867*y + 860*z == 363)
s.addCons(87*x + 875*y - 695*z == 423)
# create conshdlr and include it to SCIP
conshdlr = MyConshdlr(shouldtrans=True, shouldcopy=False)
s.includeConshdlr(conshdlr, "PyCons", "custom constraint handler implemented in python",
sepapriority = 1, enfopriority = -1, chckpriority = 1, sepafreq = 10, propfreq = 50,
eagerfreq = 1, maxprerounds = -1, delaysepa = False, delayprop = False, needscons = True,
presoltiming = SCIP_PRESOLTIMING.FAST, proptiming = SCIP_PROPTIMING.BEFORELP)
cons1 = s.createCons(conshdlr, "cons1name")
ids.append(id(cons1))
cons2 = s.createCons(conshdlr, "cons2name")
ids.append(id(cons2))
conshdlr.createData(cons1, 10, "cons1_anothername")
conshdlr.createData(cons2, 12, "cons2_anothername")
# add these constraints
s.addPyCons(cons1)
s.addPyCons(cons2)
return s
示例2: create_sudoku
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import createCons [as 别名]
def create_sudoku():
scip = Model("Sudoku")
x = {} # values of squares
for row in range(9):
for col in range(9):
# some variables are fix
if init[row*9 + col] != 0:
x[row,col] = scip.addVar(vtype = "I", lb = init[row*9 + col], ub = init[row*9 + col], name = "x(%s,%s)" % (row,col))
else:
x[row,col] = scip.addVar(vtype = "I", lb = 1, ub = 9, name = "x(%s,%s)" % (row,col))
var = x[row,col]
#print("built var ", var.name, " with bounds: (%d,%d)"%(var.getLbLocal(), var.getUbLocal()))
conshdlr = ALLDIFFconshdlr()
# hoping to get called when all vars have integer values
scip.includeConshdlr(conshdlr, "ALLDIFF", "All different constraint", propfreq = 1, enfopriority = -10, chckpriority = -10)
# row constraints; also we specify the domain of all variables here
# TODO/QUESTION: in principle domain is of course associated to the var and not the constraint. it should be "var.data"
# But ideally that information would be handle by SCIP itself... the reason we can't is because domain holes is not implemented, right?
domains = {}
for row in range(9):
vars = []
for col in range(9):
var = x[row,col]
vars.append(var)
vals = set(range(int(round(var.getLbLocal())), int(round(var.getUbLocal())) + 1))
domains[var.ptr()] = vals
# this is kind of ugly, isn't it?
cons = scip.createCons(conshdlr, "row_%d" % row)
#print("in test: received a constraint with id ", id(cons)) ### DELETE
cons.data = SimpleNamespace() # so that data behaves like an instance of a class (ie, cons.data.whatever is allowed)
cons.data.vars = vars
cons.data.domains = domains
scip.addPyCons(cons)
# col constraints
for col in range(9):
vars = []
for row in range(9):
var = x[row,col]
vars.append(var)
cons = scip.createCons(conshdlr, "col_%d"%col)
cons.data = SimpleNamespace()
cons.data.vars = vars
cons.data.domains = domains
scip.addPyCons(cons)
# square constraints
for idx1 in range(3):
for idx2 in range(3):
vars = []
for row in range(3):
for col in range(3):
var = x[3*idx1 + row, 3*idx2 + col]
vars.append(var)
cons = scip.createCons(conshdlr, "square_%d-%d"%(idx1, idx2))
cons.data = SimpleNamespace()
cons.data.vars = vars
cons.data.domains = domains
scip.addPyCons(cons)
#scip.setObjective()
return scip, x