本文整理汇总了Python中pyscipopt.Model.getStatus方法的典型用法代码示例。如果您正苦于以下问题:Python Model.getStatus方法的具体用法?Python Model.getStatus怎么用?Python Model.getStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyscipopt.Model
的用法示例。
在下文中一共展示了Model.getStatus方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_model
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
def test_model():
# create solver instance
s = Model()
# add some variables
x = s.addVar("x", vtype = 'C', obj = 1.0)
y = s.addVar("y", vtype = 'C', obj = 2.0)
assert x.getObj() == 1.0
assert y.getObj() == 2.0
s.setObjective(4.0 * y + 10.5, clear = False)
assert x.getObj() == 1.0
assert y.getObj() == 4.0
assert s.getObjoffset() == 10.5
# add some constraint
c = s.addCons(x + 2 * y >= 1.0)
assert c.isLinear()
s.chgLhs(c, 5.0)
s.chgRhs(c, 6.0)
assert s.getLhs(c) == 5.0
assert s.getRhs(c) == 6.0
# solve problem
s.optimize()
solution = s.getBestSol()
# print solution
assert (s.getVal(x) == s.getSolVal(solution, x))
assert (s.getVal(y) == s.getSolVal(solution, y))
assert round(s.getVal(x)) == 5.0
assert round(s.getVal(y)) == 0.0
assert s.getSlack(c, solution) == 0.0
assert s.getSlack(c, solution, 'lhs') == 0.0
assert s.getSlack(c, solution, 'rhs') == 1.0
assert s.getActivity(c, solution) == 5.0
s.writeProblem('model')
s.writeProblem('model.lp')
s.freeProb()
s = Model()
x = s.addVar("x", vtype = 'C', obj = 1.0)
y = s.addVar("y", vtype = 'C', obj = 2.0)
c = s.addCons(x + 2 * y <= 1.0)
s.setMaximize()
s.delCons(c)
s.optimize()
assert s.getStatus() == 'unbounded'
示例2: parity
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
def parity(number):
try:
assert number == int(round(number))
m = Model()
m.hideOutput()
### variables are non-negative by default since 0 is the default lb.
### To allow for negative values, give None as lower bound
### (None means -infinity as lower bound and +infinity as upper bound)
x = m.addVar("x", vtype="I", lb=None, ub=None) #ub=None is default
n = m.addVar("n", vtype="I", lb=None)
s = m.addVar("s", vtype="B")
### CAVEAT: if number is negative, x's lb must be None
### if x is set by default as non-negative and number is negative:
### there is no feasible solution (trivial) but the program
### does not highlight which constraints conflict.
m.addCons(x==number)
m.addCons(s == x-2*n)
m.setObjective(s)
m.optimize()
assert m.getStatus() == "optimal"
if verbose:
for v in m.getVars():
print("%s %d" % (v,m.getVal(v)))
print("%d%%2 == %d?" % (m.getVal(x), m.getVal(s)))
print(m.getVal(s) == m.getVal(x)%2)
xval = m.getVal(x)
sval = m.getVal(s)
sstr = sdic[sval]
print("%d is %s" % (xval, sstr))
except (AssertionError, TypeError):
print("%s is neither even nor odd!" % number.__repr__())
示例3: parity
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
def parity(number):
"""
Prints if a value is even/odd/neither per each value in a example list
This example is made for newcomers and motivated by:
- modulus is unsupported for pyscipopt.scip.Variable and int
- variables are non-integer by default
Based on this: #172#issuecomment-394644046
Args:
number: value which parity is checked
Returns:
sval: 1 if number is odd, 0 if number is even, -1 if neither
"""
sval = -1
if verbose:
print(80*"*")
try:
assert number == int(round(number))
m = Model()
m.hideOutput()
# x and n are integer, s is binary
# Irrespective to their type, variables are non-negative by default
# since 0 is the default lb. To allow for negative values, give None
# as lower bound.
# (None means -infinity as lower bound and +infinity as upper bound)
x = m.addVar("x", vtype="I", lb=None, ub=None) #ub=None is default
n = m.addVar("n", vtype="I", lb=None)
s = m.addVar("s", vtype="B")
# CAVEAT: if number is negative, x's lower bound must be None
# if x is set by default as non-negative and number is negative:
# there is no feasible solution (trivial) but the program
# does not highlight which constraints conflict.
m.addCons(x==number)
# minimize the difference between the number and twice a natural number
m.addCons(s == x-2*n)
m.setObjective(s)
m.optimize()
assert m.getStatus() == "optimal"
boolmod = m.getVal(s) == m.getVal(x)%2
if verbose:
for v in m.getVars():
print("%*s: %d" % (fmtlen, v,m.getVal(v)))
print("%*d%%2 == %d?" % (fmtlen, m.getVal(x), m.getVal(s)))
print("%*s" % (fmtlen, boolmod))
xval = m.getVal(x)
sval = m.getVal(s)
sstr = sdic[sval]
print("%*d is %s" % (fmtlen, xval, sstr))
except (AssertionError, TypeError):
print("%*s is neither even nor odd!" % (fmtlen, number.__repr__()))
finally:
if verbose:
print(80*"*")
print("")
return sval
示例4: test_gastrans
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
#.........这里部分代码省略.........
("Petange", None, -1.919, 25.0, 66.2, 0.0 ), # 12
("Peronnes", 0.0, 0.96, 0.0, 66.2, 1.68 ), # 13
("Sinsin", 0.0, 0.0, 0.0, 63.0, 0.0 ), # 14
("Voeren", 20.344, 22.012, 50.0, 66.2, 1.68 ), # 15
("Wanze", 0.0, 0.0, 0.0, 66.2, 0.0 ), # 16
("Warnand", 0.0, 0.0, 0.0, 66.2, 0.0 ), # 17
("Zeebrugge", 8.87, 11.594, 0.0, 77.0, 2.28 ), # 18
("Zomergem", 0.0, 0.0, 0.0, 80.0, 0.0 ) # 19
]
arcs = [
# node1 node2 diameter length active */
( 18, 6, 890.0, 4.0, False ),
( 18, 6, 890.0, 4.0, False ),
( 6, 5, 890.0, 6.0, False ),
( 6, 5, 890.0, 6.0, False ),
( 5, 19, 890.0, 26.0, False ),
( 9, 1, 590.1, 43.0, False ),
( 1, 7, 590.1, 29.0, False ),
( 7, 19, 590.1, 19.0, False ),
( 19, 13, 890.0, 55.0, False ),
( 15, 3, 890.0, 5.0, True ),
( 15, 3, 395.0, 5.0, True ),
( 3, 8, 890.0, 20.0, False ),
( 3, 8, 395.0, 20.0, False ),
( 8, 17, 890.0, 25.0, False ),
( 8, 17, 395.0, 25.0, False ),
( 17, 11, 890.0, 42.0, False ),
( 11, 0, 890.0, 40.0, False ),
( 0, 13, 890.0, 5.0, False ),
( 13, 10, 890.0, 10.0, False ),
( 10, 4, 890.0, 25.0, False ),
( 17, 16, 395.5, 10.5, False ),
( 16, 14, 315.5, 26.0, True ),
( 14, 2, 315.5, 98.0, False ),
( 2, 12, 315.5, 6.0, False )
]
scip = Model()
# create flow variables
flow = {}
for arc in arcs:
flow[arc] = scip.addVar("flow_%s_%s"%(nodes[arc[0]][0],nodes[arc[1]][0]), # names of nodes in arc
lb = 0.0 if arc[4] else None) # no lower bound if not active
# pressure difference variables
pressurediff = {}
for arc in arcs:
pressurediff[arc] = scip.addVar("pressurediff_%s_%s"%(nodes[arc[0]][0],nodes[arc[1]][0]), # names of nodes in arc
lb = None)
# supply variables
supply = {}
for node in nodes:
supply[node] = scip.addVar("supply_%s"%(node[0]), lb = node[1], ub = node[2], obj = node[5])
# square pressure variables
pressure = {}
for node in nodes:
pressure[node] = scip.addVar("pressure_%s"%(node[0]), lb = node[3]**2, ub = node[4]**2)
# node balance constrains, for each node i: outflows - inflows = supply
for nid, node in enumerate(nodes):
# find arcs that go or end at this node
flowbalance = 0
for arc in arcs:
if arc[0] == nid: # arc is outgoing
flowbalance += flow[arc]
elif arc[1] == nid: # arc is incoming
flowbalance -= flow[arc]
else:
continue
scip.addCons(flowbalance == supply[node], name="flowbalance%s"%node[0])
# pressure difference constraints: pressurediff[node1 to node2] = pressure[node1] - pressure[node2]
for arc in arcs:
scip.addCons(pressurediff[arc] == pressure[nodes[arc[0]]] - pressure[nodes[arc[1]]], "pressurediffcons_%s_%s"%(nodes[arc[0]][0],nodes[arc[1]][0]))
# pressure loss constraints:
# active arc: flow[arc]^2 + coef * pressurediff[arc] <= 0.0
# regular pipes: flow[arc] * abs(flow[arc]) - coef * pressurediff[arc] == 0.0
# coef = 96.074830e-15*diameter(i)^5/(lambda*compressibility*temperatur*length(i)*density)
# lambda = (2*log10(3.7*diameter(i)/rugosity))^(-2)
from math import log10
for arc in arcs:
coef = 96.074830e-15 * arc[2]**5 * (2.0*log10(3.7*arc[2]/RUGOSITY))**2 / COMPRESSIBILITY / GASTEMP / arc[3] / DENSITY
if arc[4]: # active
scip.addCons(flow[arc]**2 + coef * pressurediff[arc] <= 0.0, "pressureloss_%s_%s"%(nodes[arc[0]][0],nodes[arc[1]][0]))
else:
scip.addCons(flow[arc]*abs(flow[arc]) - coef * pressurediff[arc] == 0.0, "pressureloss_%s_%s"%(nodes[arc[0]][0],nodes[arc[1]][0]))
scip.setRealParam('limits/time', 5)
scip.optimize()
if scip.getStatus() == 'timelimit':
pytest.skip()
assert abs(scip.getPrimalbound() - 89.08584) < 1.0e-9
示例5: range
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
m.addCons(quicksum(x[i,j,k] for k in range(9)) == 1)
# set up row and column constraints
for ind in range(9):
for k in range(9):
m.addCons(quicksum(x[ind,j,k] for j in range(9)) == 1)
m.addCons(quicksum(x[i,ind,k] for i in range(9)) == 1)
# set up square constraints
for row in range(3):
for col in range(3):
for k in range(9):
m.addCons(quicksum(x[i+3*row, j+3*col, k] for i in range(3) for j in range(3)) == 1)
m.hideOutput()
m.optimize()
if m.getStatus() != 'optimal':
print('Sudoku is not feasible!')
else:
print('\nSudoku solution:\n')
sol = {}
for i in range(9):
out = ''
for j in range(9):
for k in range(9):
if m.getVal(x[i,j,k]) == 1:
sol[i,j] = k+1
out += str(sol[i,j]) + ' '
print(out)
示例6: print
# 需要导入模块: from pyscipopt import Model [as 别名]
# 或者: from pyscipopt.Model import getStatus [as 别名]
("Baga","Sweet"):1,
("Castelao","Dry"):0,
("Castelao","Medium"):0,
("Castelao","Sweet"):1
}
# Create variables
x = {}
for j in Blends:
x[j] = model.addVar(vtype="C", name="x(%s)"%j)
# Create constraints
c = {}
for i in Grapes:
c[i] = model.addCons(quicksum(Use[i,j]*x[j] for j in Blends) <= Inventory[i], name="Use(%s)"%i)
# Objective
model.setObjective(quicksum(Profit[j]*x[j] for j in Blends), "maximize")
model.optimize()
if model.getStatus() == "optimal":
print("Optimal value:", model.getObjVal())
for j in x:
print(x[j].name, "=", model.getVal(x[j]))
for i in c:
print("dual of", c[i].name, ":", model.getDualsolLinear(c[i]))
else:
print("Problem could not be solved to optimality")