runner.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. from pypdevs.simulator import Simulator
  3. from plot_template import make_plot_ships_script, make_plot_box_script
  4. # from system_solution import * # Teacher's solution
  5. from system import *
  6. ## Parameters ##
  7. gen_num = 500 # how many ships to generate
  8. # How often to generate a ship (on average)
  9. gen_rate = 1/60/4 # once every 4 minutes
  10. # Ship size will be sampled uniformly from the following list.
  11. gen_types = [1,1,2] # ship size '1' twice as likely to be generated as ship size '2'
  12. # Load balancer...
  13. priorities = {
  14. # you can outcomment one of these lines to reduce the number of experiments (useful for debugging):
  15. PRIORITIZE_BIGGER_SHIPS: "bigger",
  16. PRIORITIZE_SMALLER_SHIPS: "smaller",
  17. }
  18. strategies = {
  19. # you can outcomment one of these lines to reduce the number of experiments (useful for debugging):
  20. STRATEGY_ROUND_ROBIN: "roundrobin",
  21. STRATEGY_FILL_ER_UP: "fillerup",
  22. }
  23. # The number of locks and their capacities
  24. lock_capacities=[3,2] # two locks, of capacity 3 and 2
  25. # The different parameters to try for max_wait_duration
  26. max_wait_durations = [ 0.0+i*120.0 for i in range(5) ] # all these values will be attempted
  27. # max_wait_durations = [ 15.0 ] # <-- uncomment if you only want to run an experiment with this value (useful for debugging)
  28. # How long does it take for a ship to pass through a lock
  29. passthrough_duration = 60.0*15 # 15 minutes
  30. outdir = "assignment_output"
  31. plots_ships = []
  32. plots_box = []
  33. os.makedirs(outdir, exist_ok=True)
  34. # try all combinations of priorities and strategies (4 total)
  35. for priority in priorities:
  36. for strategy in strategies:
  37. values = []
  38. # and in each experiment, try a bunch of different values for the 'max_wait_duration' parameter:
  39. for max_wait_duration in max_wait_durations:
  40. print("Run simulation:", priorities[priority], strategies[strategy], "max_wait =",max_wait_duration)
  41. sys = LockQueueingSystem(
  42. # See system.py for explanation of these values:
  43. seed=0,
  44. gen_num=gen_num,
  45. gen_rate=gen_rate,
  46. gen_types=gen_types,
  47. load_balancer_strategy=strategy,
  48. lock_capacities=lock_capacities,
  49. priority=priority,
  50. max_wait_duration=max_wait_duration,
  51. passthrough_duration=passthrough_duration,
  52. )
  53. sim = Simulator(sys)
  54. sim.setClassicDEVS()
  55. # sim.setVerbose() # <-- uncomment to see what's going on
  56. sim.simulate()
  57. # all the ships that made it through
  58. ships = sys.sink.state.ships
  59. values.append([ship.queueing_duration for ship in ships])
  60. # Write out all the ship queueuing durations for every 'max_wait_duration' parameter
  61. # for every ship, we write a line:
  62. # <ship_num>, time_max_wait0, time_max_wait1, time_max_wait2, ... time_max_wait10
  63. filename = f'{outdir}/output_{strategies[strategy]}_{priorities[priority]}.csv'
  64. with open(filename, 'w') as f:
  65. try:
  66. for i in range(gen_num):
  67. f.write("%s" % i)
  68. for j in range(len(values)):
  69. f.write(", %5f" % (values[j][i]))
  70. f.write("\n")
  71. except IndexError as e:
  72. raise Exception("There was an IndexError, meaning that fewer ships have made it to the sink than expected.\nYour model is not (yet) correct.") from e
  73. # Generate gnuplot code:
  74. plots_ships.append(make_plot_ships_script(
  75. priority=priorities[priority],
  76. strategy=strategies[strategy],
  77. max_waits=max_wait_durations,
  78. gen_num=gen_num,
  79. ))
  80. plots_box.append(make_plot_box_script(
  81. priority=priorities[priority],
  82. strategy=strategies[strategy],
  83. max_waits=max_wait_durations,
  84. gen_num=gen_num,
  85. ))
  86. # Finally, write out a single gnuplot script that plots everything
  87. with open(f'{outdir}/plot.gnuplot', 'w') as f:
  88. # first plot the ships
  89. f.write('\n\n'.join(plots_ships))
  90. # then do the box plots
  91. f.write('\n\n'.join(plots_box))