abbarnes icon

Greedy Boss Simulation [codeskulptor]

abbarnes | PRO | 12/12/17 05:48:11 PM UTC | 0 ⭐ | 237 👁️ | Never ⏰ | []
Python |

3.89 KB

|

None

|

0 👍

/

0 👎

"""
Simulator for greedy boss scenario
"""
#http://www.codeskulptor.org/#user43_0qlLMcnaGRs6Bb7_1.py
 
import simpleplot
import math
import codeskulptor
codeskulptor.set_timeout(50)
 
STANDARD = True
LOGLOG = False
 
# constants for simulation
INITIAL_SALARY = 100
SALARY_INCREMENT = 100
INITIAL_BRIBE_COST = 1000
 
 
def greedy_boss(days_in_simulation, bribe_cost_increment, plot_type = STANDARD):
    """
    Simulation of greedy boss
    """
    
    # initialize necessary local variables
    current_day = 0
    salary = 100
    bribe = 100
    bribe_cost = 1000
    total_salary = 0
    net_worth = 0
    
    # define  list consisting of days vs. total salary earned for analysis
    days_vs_earnings = []
 
    # Each iteration of this while loop simulates one bribe
    while current_day <= days_in_simulation:
        # update list with days vs total salary earned
        days_vs_earnings.append((current_day, total_salary/(math.e**(0.095*current_day))))
        
        # use plot_type to control whether regular or log/log plot
        
        # check whether we have enough money to bribe without waiting
        while net_worth >= bribe_cost:
            #print "Bribing boss with ", bribe_cost, " on day ", current_day
            
            net_worth -= bribe_cost
            #print "net worth: ", net_worth
            #print
            salary += 100
            bribe_cost += bribe_cost_increment
        
        # advance current_day to day of next bribe (DO NOT INCREMENT BY ONE DAY)
        days_until_bribe = math.ceil(float(bribe_cost-net_worth)/salary)
        current_day += int(days_until_bribe)
        
        # update state of simulation to reflect bribe
        check = int(days_until_bribe) * salary
        net_worth += check
        total_salary += check
        #print "Jumping ", days_until_bribe, "days at salary ", salary, "Made ", check,  " Net worth: ", net_worth, " Total Earned ", total_salary
        
    return days_vs_earnings
 
def exponential(x1, x2, increment, a):
    """
    returns a list of values for y = e^ax from x= x1 to x=x2
    """
    ans = []
    x = x1
    while x <= x2:
        ans.append((x, math.e**(a*x)))
        x+= increment
    return ans
 
def polynomial(x1, x2, increment, a, b):
    ans = []
    x = x1
    while x <= x2:
        ans.append((x, a*x**b))
        x+= increment
    return ans
     
 
def run_simulations():
    """
    Run simulations for several possible bribe increments
    """
    plot_type = STANDARD
    days = 50
    inc_0 = greedy_boss(days, 0, plot_type)
    inc_500 = greedy_boss(days, 500, plot_type)
    inc_1000 = greedy_boss(days, 1000, plot_type)
    inc_2000 = greedy_boss(days, 2000, plot_type)
    exp_9_5 = exponential(0,50,1,9.5)
    exp__095 = exponential(0,50,1,0.095)
    poly_95_2 = polynomial(0,50,1,95,2)
    poly_9_5_4 = polynomial(0,50,1,9.5,4)
    simpleplot.plot_lines("Greedy boss", 600, 600, "days", "total earnings", 
                          [inc_0], False,
                         ["Bribe increment = 0", "Bribe increment = 500",
                          "Bribe increment = 1000", "Bribe increment = 2000"])
#    simpleplot.plot_lines("Greedy boss vs exponentials", 600, 600, "x", "y", [inc_0, exp_9_5, exp__095], False,["Bribe increment = 0", 
#                          "Bribe increment = 500", "Bribe increment = 1000", "Bribe increment = 2000"])
 
run_simulations()
 
#print greedy_boss(35, 100)
# should print [(0, 0), (10, 1000), (16, 2200), (20, 3400), (23, 4600), (26, 6100), (29, 7900), (31, 9300), (33, 10900), (35, 12700)]
 
#print greedy_boss(35, 0)
# 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)]
 
#print exponential(0,50,1,0.095)

Comments