Guest User

Untitled

a guest
Apr 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. from __future__ import print_function
  2. from ortools.graph import pywrapgraph
  3. import time
  4.  
  5. def main():
  6. cost = create_data_array()
  7. rows = len(cost)
  8. cols = len(cost[0])
  9.  
  10. assignment = pywrapgraph.LinearSumAssignment()
  11. for worker in range(rows):
  12. for task in range(cols):
  13. if cost[worker][task]:
  14. assignment.AddArcWithCost(worker, task, cost[worker][task])
  15. solve_status = assignment.Solve()
  16. if solve_status == assignment.OPTIMAL:
  17. print('Total cost = ', assignment.OptimalCost())
  18. print()
  19. for i in range(0, assignment.NumNodes()):
  20. print('Worker %d assigned to task %d. Cost = %d' % (
  21. i,
  22. assignment.RightMate(i),
  23. assignment.AssignmentCost(i)))
  24. elif solve_status == assignment.INFEASIBLE:
  25. print('No assignment is possible.')
  26. elif solve_status == assignment.POSSIBLE_OVERFLOW:
  27. print('Some input costs are too large and may cause an integer overflow.')
  28. def create_data_array():
  29. cost = [[90, 76, 75, 70],
  30. [35, 85, 55, 65],
  31. [125, 95, 90, 105],
  32. [45, 110, 95, 115]]
  33. return cost
  34. if __name__ == "__main__":
  35. start_time = time.clock()
  36. main()
  37. print()
  38. print("Time =", time.clock() - start_time, "seconds")
Add Comment
Please, Sign In to add comment