如何解决带手术室工具的设施位置
我正在尝试使用 OR 工具编写设施位置 MIP 解决方案。我从这里翻译了一个 Scip 解决方案:
https://scipbook.readthedocs.io/en/latest/flp.html 但我得到一张只有零的表意味着没有解决方案.. 问题的框架或/和我在这里使用 OR 工具的方式应该有效吗?
def or_tools_scip_mine(facilities,customers,time_limit=None):
import numpy
import datetime
if time_limit is None:
time_limit = 1000 * 60 # 1 minute
solver = pywraplp.solver.CreateSolver('SCIP')
customer_count = range(len(customers))
facility_count = range(len(facilities))
x =[[] for _ in range(len(customers))]
y = []
facility_capacities=[facilities[i][2] for i in facility_count]
facility_setup_costs = [facilities[i][1] for i in facility_count]
demands=[customers[i][1] for i in customer_count]
c=dist_matrix(facilities,customers)
for j in facility_count:
y.append(solver.BoolVar("y(%s)" % j))
for i in customer_count:
x[i].append(solver.BoolVar("x(%s,%s)" % (i,j)))
for i in customer_count:
solver.Add(solver.Sum(x[i][j] for j in facility_count) <= demands[i])#,"Demand(%s)" % i
for j in facility_count:
solver.Add(solver.Sum(x[i][j] for i in customer_count) <= facility_capacities[j] * y[j])#,"Capacity(%s)" % j)
for j in facility_count:
for i in customer_count:
solver.Add(x[i][j] <= demands[i] * y[j])
a=solver.Sum((facility_setup_costs[j] * y[j] for j in facility_count))
b=solver.Sum((c[i,j] * x[i][j] for i in customer_count for j in facility_count))
func_=solver.Sum([a,b])
solver.Minimize(func_)
solver.set_time_limit(time_limit)
result_status = solver.solve()
print(result_status)
val = solver.Objective().Value()
x_val = [[] for _ in range(len(customers))]
solution = []
for j in range(len(facilities)):
for i in range(len(customers)):
x_val[i].append(int(x[i][j].solution_value()))
x_val = numpy.array(x_val)
for j in range(len(customers)):
solution.append(numpy.where(x_val[:,j] == 1)[0][0])
return val,solution
the Error:
solution.append(numpy.where(x_val[:,j] == 1)[0][0])
IndexError: index 0 is out of bounds for axis 0 with size 0
解决方法
在代码中的 CreateSolver 行之后添加 solver.EnableOutput()。这会让您更深入地了解正在发生的事情。
以下是帮助求解器参数和状态。如果您需要更多 详情请点击此文档 link
print('Number of variables = %d' % solver.NumVariables())
print('Number of constraints = %d' % solver.NumConstraints())
print('The Optimal Objective value =',solver.Objective().Value())
print('Total Iterations:',solver.iterations())
print('Total Nodes:',solver.nodes())
print('Total number of Variables:',solver.NumVariables())
print(pywraplp.Solver.FEASIBLE)
print(pywraplp.Solver.MODEL_INVALID)
print(pywraplp.Solver.OPTIMAL)
print(pywraplp.Solver.INFEASIBLE)
print(pywraplp.Solver.UNBOUNDED)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。