Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def main():
- part_a()
- #part_b()
- part_c()
- def part_a():
- samples = np.array([0])
- samples = np.delete(samples,0)
- for i in range(1000):
- seats_sold,profit,time_elapsed = run_simulation(100)
- samples = np.append(samples,time_elapsed)
- avg_min = np.average(samples)
- avg_days = avg_min/1440.0
- print('Average number of days to sell 150 tickets at $100 each is: ' + str(avg_days) + '!')
- def part_b():
- #first round
- #cur_cost = 450
- #dist = 5
- #second round
- #cur_cost = 410
- #dist = 1
- max_list = [0] * 32
- max_list = np.array(max_list)
- for k in range(32):
- print('iteration ' + str(k))
- cur = 400
- dist = 5
- results = [0] * 13
- results = np.array(results)
- for i in range(13):
- print('(' + str(i) + ')')
- samples = np.array([0])
- samples = np.delete(samples,0)
- for j in range(500):
- seats_sold,profit,time_elapsed = run_simulation(cur)
- samples = np.append(samples,profit)
- avg_prof = np.average(samples)
- results[i] = avg_prof
- cur = cur + dist
- max_val = dist*(np.argmax(results)) + 400
- max_list[k] = max_val
- print(max_list)
- '''
- while(True):
- to_test = np.array([cur_cost - dist, cur_cost, cur_cost + dist])
- results = np.array([0.0,0.0,0.0])
- for i in range(3):
- samples = np.array([0])
- samples = np.delete(samples,0)
- for j in range(500):
- seats_sold,profit,time_elapsed = run_simulation(to_test[i])
- samples = np.append(samples,profit)
- avg_prof = np.average(samples)
- results[i] = avg_prof
- if(results[0] < results[1] and results[1] > results[2]):
- print('maximum of $' + str(results[1]) + ' identified at X = $' + str(to_test[1]))
- break
- if(results[0] > results[1] and results[1] < results[2]):
- print('minimum of $' + str(results[1]) + ' identified at X = $' + str(to_test[1]))
- if(results[0] > results[2]):
- cur_cost = to_test[0]
- else:
- cur_cost = to_test[2]
- if(results[0] < results[1] and results[1] < results[2]):
- print('moving up to X = $' + str(to_test[2]))
- cur_cost = to_test[2]
- if(results[0] > results[1] and results[1] > results[2]):
- print('moving down to X = $' + str(to_test[0]))
- cur_cost = to_test[1]
- '''
- def part_c():
- pass
- def run_simulation(init_cost):
- total_time = 60 * 24.0 * 30
- mean_time = 120.0
- seats = 150
- sold = 0
- cur_cost = init_cost
- profit = 0.0
- time_elapsed = 0.0
- calls = get_calls(mean_time, total_time)
- for i in range(len(calls)):
- time_elapsed = time_elapsed + calls[i]
- probability = get_probability(cur_cost,sold)
- drawn_sample = np.random.uniform()
- if(drawn_sample <= probability):
- profit = profit + cur_cost
- sold = sold + 1
- if(sold == seats):
- break
- return sold,profit,time_elapsed
- def get_calls(mean, time):
- calls = np.array([0])
- calls = np.delete(calls,0)
- cur_sum = 0.0
- while(True):
- val = (np.random.exponential(mean,1))[0]
- if(cur_sum + val > time):
- break
- calls = np.append(calls,val)
- cur_sum = cur_sum + val
- return calls
- def get_probability(X,N):
- f = (300 - np.e**(X/100.0))/600
- g = N/350.0
- p = np.min([1, np.max([f,g])])
- return p
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement