Advertisement
Guest User

Untitled

a guest
Jan 21st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import itertools
  2.  
  3. machine_names = ["M1", "M2"]
  4. num_tasks = 4
  5.  
  6. time_to_complete = {
  7. "M1":[5,3,5,4],
  8. "M2":[4,2,7,5]
  9. }
  10.  
  11. price_to_do = {
  12. "M1":[5,4,2,6],
  13. "M2":[3,7,3,3]
  14. }
  15.  
  16. switching_time = {
  17. ("M1","M1"):0,
  18. ("M1","M2"):1,
  19. ("M2","M1"):2,
  20. ("M2","M2"):0
  21. }
  22.  
  23. switching_price = {
  24. ("M1","M1"):0,
  25. ("M1","M2"):2,
  26. ("M2","M1"):1,
  27. ("M2","M2"):0
  28. }
  29.  
  30. #finds the total cost for a potential sequence of machine usages.
  31. #can work using time or price - just insert whatever dicts are needed.
  32. def total_cost(machine_seq, task_cost, switching_cost):
  33. cost = 0
  34. prev = None
  35. for task_idx, cur in enumerate(machine_seq):
  36. cost += task_cost[cur][task_idx]
  37. if prev != None:
  38. cost += switching_cost[(prev, cur)]
  39. prev = cur
  40. return cost
  41.  
  42. def total_price(machine_seq):
  43. return total_cost(machine_seq, price_to_do, switching_price)
  44.  
  45. def total_time(machine_seq):
  46. return total_cost(machine_seq, time_to_complete, switching_time)
  47.  
  48. def get_cheapest_sequence(max_allowable_time):
  49. valid_seqs = []
  50. #find all sequences that are completed by the deadline
  51. for candidate_seq in itertools.product(machine_names, repeat=num_tasks):
  52. if total_time(candidate_seq) <= max_allowable_time:
  53. valid_seqs.append(candidate_seq)
  54.  
  55. if len(valid_seqs) == 0:
  56. return None
  57.  
  58. #identify cheapest valid sequence
  59. return min(valid_seqs, key=total_price)
  60.  
  61. print get_cheapest_sequence(10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement