[英]Why won't my MIP solver take 'NA' values?
I have the below solver and it works perfectly if I replace the 'NA' values with integers in the cost array and take out the if statement checking if the value is 'NA' but throws a key error otherwise. 我有下面的求解器,如果我用cost数组中的整数替换'NA'值并取出if语句检查该值是否为'NA',但它抛出一个关键错误,它可以完美地工作。
I have been stuck on this for hours and I know it is very simple but just can't get it! 我已经坚持了好几个小时,而且我知道这很简单,但却无法理解!
I am also new to Google OR-Tools and Optimization so would love to know if I am using the right tool/solver or is there a better/ more efficient tool. 我也是Google OR-Tools and Optimization的新手,所以很想知道我使用的是正确的工具/求解器还是有更好/更有效的工具。
def main():
solver = pywraplp.Solver('SolveAssignmentProblemMIP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
cost = [[15.25, 18.50, 'NA', 17.25],
[18.50, 19.50, 26.50, 21.00],
[12.50, 'NA', 'NA', 'NA'],
['NA', 'NA', 23.50, 'NA'],
['NA', 'NA', 'NA', 16.50]
]
num_workers = len(cost)
num_tasks = len(cost[1])
x = {}
for i in range(0, num_workers):
for j in range(0, num_tasks):
if cost[i][j] != 'NA':
x[i, j] = solver.BoolVar('x[%i,%i]' % (i, j))
# Objective
solver.Minimize(solver.Sum([cost[i][j] * x[i,j] for i in range(num_workers)
for j in range(num_tasks)]))
# Constraints
# Each worker is assigned to at most 1 task.
for i in range(num_workers):
solver.Add(solver.Sum([x[i, j] for j in range(num_tasks)]) <= 1)
# Each task is assigned to exactly one worker.
for j in range(num_tasks):
solver.Add(solver.Sum([x[i, j] for i in range(num_workers)]) == 1)
sol = solver.Solve()
print('Total cost = ', solver.Objective().Value())
print()
for i in range(num_workers):
for j in range(num_tasks):
if x[i, j].solution_value() > 0:
print('Vessel %d assigned to Voyage %d. Cost = %d' % (
i,
j,
cost[i][j]))
print()
print("Time = ", solver.WallTime(), " milliseconds")
if __name__ == '__main__':
main()
KeyError Traceback (most recent call last)
<ipython-input-52-2f567281d1e6> in <module>()
56 print("Time = ", solver.WallTime(), " milliseconds")
57 if __name__ == '__main__':
---> 58 main()
1 frames
<ipython-input-52-2f567281d1e6> in main()
26
27 # Objective
---> 28 solver.Minimize(solver.Sum([cost[i][j] * x[i,j] for i in range(num_workers)
29 for j in range(num_tasks)]))
30
<ipython-input-52-2f567281d1e6> in <listcomp>(.0)
27 # Objective
28 solver.Minimize(solver.Sum([cost[i][j] * x[i,j] for i in range(num_workers)
---> 29 for j in range(num_tasks)]))
30
31 # Constraints
KeyError: (0, 2)
You only generate variables x[i,j]
where cost[i][j]
is not NA. 您只生成cost[i][j]
不是NA的变量x[i,j]
。 So you need to add protection everywhere. 因此,您需要在所有地方添加保护。 In your objective: 在您的目标中:
solver.Minimize(solver.Sum([cost[i][j] * x[i,j] for i in range(num_workers)
for j in range(num_tasks)]))
you run over all i,j. 你跑遍了我 Instead you should run only over i,j where cost[i][j]
is not NA. 相反,您应该仅在cost[i][j]
不是NA的i,j上运行。 Eg 例如
solver.Minimize(solver.Sum([cost[i][j] * x[i,j] for i in range(num_workers)
for j in range(num_tasks)
if cost[i][j] != 'NA']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.