![](/img/trans.png)
[英]How we can use a decision variable as an index of another decision variable using ILOG CP optimizer in python?
[英]Can not solve Assignment Problem Using ILOG CPLEX Optimizer
分配問題來自Google OR-Tools
其他框架可以解決這個問題,即使使用 Excel Solver。 但是 ILOG CPLEX 不能解決這個問題。
這是我在 jupyter notebook 中的代碼:
import cplex
import docplex.mp
from docplex.mp.model import Model
import numpy as np
assignment_model = Model(name='Assignemnt_Problem', log_output=True)
costs = np.array([[90,80,75,70],
[35,85,55,65],
[125,95,90,95],
[45,110,95,115],
[50,100,90,100]])
x = assignment_model.binary_var_matrix(costs.shape[0], costs.shape[1], name="a")
assignment_model.add_constraints((sum(x[i,j] for i in range (costs.shape[0])) <=1
for j in range (costs.shape[1])), names ="workers")
assignment_model.add_constraints((sum(x[i,j] for j in range (costs.shape[1])) ==1
for i in range (costs.shape[0])), names ="tasks")
obj_fn = sum(x[i,j]*costs[i,j] for i in range (costs.shape[0]) for j in range(costs.shape[1]))
assignment_model.set_objective('min', obj_fn)
assignment_model.print_information()
assignment_model.solve()
print('Optimization is done. Objective Function Value: %.2f' % assignment_model.objective_value)
錯誤“DOcplexException: Model<Assignemnt_Problem> 沒有成功解決”
謝謝
的確。 如果您添加該行
print("status = ",assignment_model.solve_details.status)
解決后你會看到
status = integer infeasible
這就是為什么打印解決方案不起作用
現在如果你改變
costs = np.array([[90,80,75,70],
[35,85,55,65],
[125,95,90,95],
[45,110,95,115],
[50,100,90,100]])
進入
costs = np.array([[90,80,75,70],
[35,85,55,65],
[125,95,90,95],
[45,110,95,115],
[50,100,90,100]])
costs=costs.transpose()
然后你會得到一個解決方案。
(狄利克雷抽屜原理)
import cplex
import docplex.mp
from docplex.mp.model import Model
import numpy as np
assignment_model = Model(name='Assignemnt_Problem', log_output=True)
costs = np.array([[90,80,75,70],
[35,85,55,65],
[125,95,90,95],
[45,110,95,115],
[50,100,90,100]])
costs=costs.transpose()
x = assignment_model.binary_var_matrix(costs.shape[0], costs.shape[1], name="a")
assignment_model.add_constraints((sum(x[i,j] for i in range (costs.shape[0])) <=1
for j in range (costs.shape[1])), names ="workers")
assignment_model.add_constraints((sum(x[i,j] for j in range (costs.shape[1])) ==1
for i in range (costs.shape[0])), names ="tasks")
obj_fn = sum(x[i,j]*costs[i,j] for i in range (costs.shape[0]) for j in range(costs.shape[1]))
assignment_model.set_objective('min', obj_fn)
assignment_model.print_information()
assignment_model.solve()
print('Optimization is done. Objective Function Value: %.2f' % assignment_model.objective_value)
for i in range(costs.shape[0]):
for j in range(costs.shape[1]):
if (x[i,j].solution_value>=0.9):
print('Worker ',i,' assigned to task ',j)
給
Optimization is done. Objective Function Value: 265.00
Worker 0 assigned to task 3
Worker 1 assigned to task 2
Worker 2 assigned to task 1
Worker 3 assigned to task 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.