本文整理汇总了Python中gurobipy.Model._vars方法的典型用法代码示例。如果您正苦于以下问题:Python Model._vars方法的具体用法?Python Model._vars怎么用?Python Model._vars使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy.Model
的用法示例。
在下文中一共展示了Model._vars方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tsp_gurobi
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import _vars [as 别名]
def tsp_gurobi(edges):
"""
Modeled using GUROBI python example.
"""
from gurobipy import Model, GRB, quicksum
edges = populate_edge_weights(edges)
incoming, outgoing, nodes = node_to_edge(edges)
idx = dict((n, i) for i, n in enumerate(nodes))
nedges = len(edges)
n = len(nodes)
m = Model()
step = lambda x: "u_{0}".format(x)
# Create variables
vars = {}
for i, (a, b, w) in enumerate(edges):
vars[i] = m.addVar(obj=w, vtype=GRB.BINARY, name=str(i))
for u in nodes[1:]:
u = step(u)
vars[u] = m.addVar(obj=0, vtype=GRB.INTEGER, name=u)
m.update()
# Bounds for step variables
for u in nodes[1:]:
u = step(u)
vars[u].lb = 1
vars[u].ub = n - 1
# Add degree constraint
for v in nodes:
incoming_edges = incoming[v]
outgoing_edges = outgoing[v]
m.addConstr(quicksum(vars[x] for x in incoming_edges) == 1)
m.addConstr(quicksum(vars[x] for x in outgoing_edges) == 1)
# Subtour elimination
edge_store = dict(((idx[a], idx[b]), i) for i, (a, b, w) in enumerate(edges))
# Given a list of edges, finds the shortest subtour
def subtour(s_edges):
visited = [False] * n
cycles = []
lengths = []
selected = [[] for i in range(n)]
for x, y in s_edges:
selected[x].append(y)
while True:
current = visited.index(False)
thiscycle = [current]
while True:
visited[current] = True
neighbors = [x for x in selected[current] if not visited[x]]
if len(neighbors) == 0:
break
current = neighbors[0]
thiscycle.append(current)
cycles.append(thiscycle)
lengths.append(len(thiscycle))
if sum(lengths) == n:
break
return cycles[lengths.index(min(lengths))]
def subtourelim(model, where):
if where != GRB.callback.MIPSOL:
return
selected = []
# make a list of edges selected in the solution
sol = model.cbGetSolution([model._vars[i] for i in range(nedges)])
selected = [edges[i] for i, x in enumerate(sol) if x > .5]
selected = [(idx[a], idx[b]) for a, b, w in selected]
# find the shortest cycle in the selected edge list
tour = subtour(selected)
if len(tour) == n:
return
# add a subtour elimination constraint
c = tour
incident = [edge_store[a, b] for a, b in pairwise(c + [c[0]])]
model.cbLazy(quicksum(model._vars[x] for x in incident) <= len(tour) - 1)
m.update()
m._vars = vars
m.params.LazyConstraints = 1
m.optimize(subtourelim)
selected = [v.varName for v in m.getVars() if v.x > .5]
selected = [int(x) for x in selected if x[:2] != "u_"]
results = sorted(x for i, x in enumerate(edges) if i in selected) \
if selected else None
return results