我正在尝试使用 OR 工具编写设施位置 MIP 解决方案。我从这里翻译了一个 Scip 解决方案:
https://scipbook.readthedocs.io/en/latest/flp.html 但我得到一个只有零的表意味着没有解决方案..是问题的框架或/和我在这里使用 OR-tools 的方式应该工作?
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