Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
  3.  
  4. """
  5. Python source code - replace this with a description of the code and write the code below this text.
  6. """
  7. import sys
  8. import random
  9. import os
  10. import matplotlib.pyplot as pyplot
  11. import math
  12.  
  13. class Person:
  14.     def __init__(self):
  15.         # Rat's remaining lifespan in weeks
  16.         self.rat = 0
  17.         # Number of weeks until out of fertilizer
  18.         self.fertilizer = 0
  19.  
  20.     def update(self, weeks):
  21.         # Reduce the remaining rat/fertilizer
  22.         self.rat = max(self.rat - weeks, 0)
  23.         self.fertilizer = max(self.fertilizer - weeks, 0)
  24.  
  25.     def choose_button(self):
  26.         if self.rat == 0 and self.fertilizer == 0:
  27.             return "c"
  28.         elif self.rat == 0:
  29.             return "b"
  30.         elif self.fertilizer == 0:
  31.             return "a"
  32.         else:
  33.             return None
  34.  
  35.     def add_weeks(self, item, weeks):
  36.         # Add weeks to the item received from machine
  37.         if item == "rat":
  38.             self.rat += weeks
  39.         elif item == "fertilizer":
  40.             self.fertilizer += weeks
  41.  
  42. if __name__ == "__main__":
  43.     # Assume a rat lifespan of 130 weeks
  44.     rat = 30
  45.     fertilizer = int(sys.argv[1])
  46.     machine = int(sys.argv[2])
  47.     visits = int(sys.argv[3]) // machine
  48.     if os.path.exists(sys.argv[4]):
  49.         print("Output file {} exists. Exiting.".format(sys.argv[4]))
  50.         sys.exit(1)
  51.     else:
  52.         fig_file = sys.argv[4]
  53.  
  54.     people = [Person() for i in range(100)]
  55.     buttons = []
  56.     items = {'rat': rat, 'fertilizer': fertilizer}
  57.        
  58.     for i in range(visits):
  59.         presses = {"a": 0, "b": 0, "c": 0}
  60.         for p in people:
  61.             p.update(machine)
  62.             press = p.choose_button()
  63.             if press == "c":
  64.                 item = random.choice(list(items.keys()))
  65.             elif press == "b":
  66.                 item = 'rat'
  67.             elif press == "a":
  68.                 item = 'fertilizer'
  69.             else:
  70.                 continue
  71.             weeks = items[item]
  72.             p.add_weeks(item, weeks)
  73.             presses[press] += 1
  74.         buttons.append(presses)
  75.  
  76.     # Make the lists to plot, leading zeroes to get cumulative total
  77.     a, b, c, = [0], [0], [0]
  78.     for button in buttons:
  79.         a.append(a[-1] + button["a"])
  80.         b.append(b[-1] + button["b"])
  81.         c.append(c[-1] + button["c"])
  82.     # drop the leading zero
  83.     a.pop(0)
  84.     b.pop(0)
  85.     c.pop(0)
  86.  
  87.     pyplot.plot(a)
  88.     pyplot.plot(b)
  89.     pyplot.plot(c)
  90.     pyplot.legend(["fertilizer", "rat", "random"])
  91.     pyplot.xlabel("Visits to machine")
  92.     pyplot.ylabel("Cumulative button presses")
  93.     pyplot.title("Visits every {} weeks, fertilizer lasts {} weeks".format(
  94.         machine, fertilizer))
  95.  
  96.     pyplot.savefig(fig_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement