abbarnes

Greedy Boss Simulation [codeskulptor]

Dec 12th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. """
  2. Simulator for greedy boss scenario
  3. """
  4. #http://www.codeskulptor.org/#user43_0qlLMcnaGRs6Bb7_1.py
  5.  
  6. import simpleplot
  7. import math
  8. import codeskulptor
  9. codeskulptor.set_timeout(50)
  10.  
  11. STANDARD = True
  12. LOGLOG = False
  13.  
  14. # constants for simulation
  15. INITIAL_SALARY = 100
  16. SALARY_INCREMENT = 100
  17. INITIAL_BRIBE_COST = 1000
  18.  
  19.  
  20. def greedy_boss(days_in_simulation, bribe_cost_increment, plot_type = STANDARD):
  21.     """
  22.    Simulation of greedy boss
  23.    """
  24.    
  25.     # initialize necessary local variables
  26.     current_day = 0
  27.     salary = 100
  28.     bribe = 100
  29.     bribe_cost = 1000
  30.     total_salary = 0
  31.     net_worth = 0
  32.    
  33.     # define  list consisting of days vs. total salary earned for analysis
  34.     days_vs_earnings = []
  35.  
  36.     # Each iteration of this while loop simulates one bribe
  37.     while current_day <= days_in_simulation:
  38.         # update list with days vs total salary earned
  39.         days_vs_earnings.append((current_day, total_salary/(math.e**(0.095*current_day))))
  40.        
  41.         # use plot_type to control whether regular or log/log plot
  42.        
  43.         # check whether we have enough money to bribe without waiting
  44.         while net_worth >= bribe_cost:
  45.             #print "Bribing boss with ", bribe_cost, " on day ", current_day
  46.            
  47.             net_worth -= bribe_cost
  48.             #print "net worth: ", net_worth
  49.             #print
  50.             salary += 100
  51.             bribe_cost += bribe_cost_increment
  52.        
  53.         # advance current_day to day of next bribe (DO NOT INCREMENT BY ONE DAY)
  54.         days_until_bribe = math.ceil(float(bribe_cost-net_worth)/salary)
  55.         current_day += int(days_until_bribe)
  56.        
  57.         # update state of simulation to reflect bribe
  58.         check = int(days_until_bribe) * salary
  59.         net_worth += check
  60.         total_salary += check
  61.         #print "Jumping ", days_until_bribe, "days at salary ", salary, "Made ", check,  " Net worth: ", net_worth, " Total Earned ", total_salary
  62.        
  63.     return days_vs_earnings
  64.  
  65. def exponential(x1, x2, increment, a):
  66.     """
  67.    returns a list of values for y = e^ax from x= x1 to x=x2
  68.    """
  69.     ans = []
  70.     x = x1
  71.     while x <= x2:
  72.         ans.append((x, math.e**(a*x)))
  73.         x+= increment
  74.     return ans
  75.  
  76. def polynomial(x1, x2, increment, a, b):
  77.     ans = []
  78.     x = x1
  79.     while x <= x2:
  80.         ans.append((x, a*x**b))
  81.         x+= increment
  82.     return ans
  83.      
  84.  
  85. def run_simulations():
  86.     """
  87.    Run simulations for several possible bribe increments
  88.    """
  89.     plot_type = STANDARD
  90.     days = 50
  91.     inc_0 = greedy_boss(days, 0, plot_type)
  92.     inc_500 = greedy_boss(days, 500, plot_type)
  93.     inc_1000 = greedy_boss(days, 1000, plot_type)
  94.     inc_2000 = greedy_boss(days, 2000, plot_type)
  95.     exp_9_5 = exponential(0,50,1,9.5)
  96.     exp__095 = exponential(0,50,1,0.095)
  97.     poly_95_2 = polynomial(0,50,1,95,2)
  98.     poly_9_5_4 = polynomial(0,50,1,9.5,4)
  99.     simpleplot.plot_lines("Greedy boss", 600, 600, "days", "total earnings",
  100.                           [inc_0], False,
  101.                          ["Bribe increment = 0", "Bribe increment = 500",
  102.                           "Bribe increment = 1000", "Bribe increment = 2000"])
  103. #    simpleplot.plot_lines("Greedy boss vs exponentials", 600, 600, "x", "y", [inc_0, exp_9_5, exp__095], False,["Bribe increment = 0",
  104. #                          "Bribe increment = 500", "Bribe increment = 1000", "Bribe increment = 2000"])
  105.  
  106. run_simulations()
  107.  
  108. #print greedy_boss(35, 100)
  109. # should print [(0, 0), (10, 1000), (16, 2200), (20, 3400), (23, 4600), (26, 6100), (29, 7900), (31, 9300), (33, 10900), (35, 12700)]
  110.  
  111. #print greedy_boss(35, 0)
  112. # should print [(0, 0), (10, 1000), (15, 2000), (19, 3200), (21, 4000), (23, 5000), (25, 6200), (27, 7600), (28, 8400), (29, 9300), (30, 10300), (31, 11400), (32, 12600), (33, 13900), (34, 15300), (34, 15300), (35, 16900)]
  113.  
  114. #print exponential(0,50,1,0.095)
Advertisement
Add Comment
Please, Sign In to add comment