-1

The Assignment problem is from Google OR-Tools

Others framework can solve this problem, even using Excel Solver. But ILOG CPLEX cannot solve this problem.

Here is my code in 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)

The error "DOcplexException: Model<Assignemnt_Problem> did not solve successfully"

Thanks

1 Answers1

0

Indeed. If you add the line

print("status = ",assignment_model.solve_details.status)

after the solve you will see

status =  integer infeasible

And this is why print solution does not work

Now if you change

costs = np.array([[90,80,75,70],
  [35,85,55,65],
  [125,95,90,95],
  [45,110,95,115],
  [50,100,90,100]])

into

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()

then you will get a solution.

( Dirichlet's drawer principle )

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)

gives

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
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15