Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def main():
  4.     part_a()
  5.     #part_b()
  6.     part_c()
  7.  
  8. def part_a():
  9.     samples = np.array([0])
  10.     samples = np.delete(samples,0)
  11.     for i in range(1000):
  12.         seats_sold,profit,time_elapsed = run_simulation(100)
  13.         samples = np.append(samples,time_elapsed)
  14.     avg_min = np.average(samples)
  15.     avg_days = avg_min/1440.0
  16.     print('Average number of days to sell 150 tickets at $100 each is: ' + str(avg_days) + '!')
  17.  
  18. def part_b():
  19.  
  20.     #first round
  21.     #cur_cost = 450
  22.     #dist = 5
  23.  
  24.     #second round
  25.     #cur_cost = 410
  26.     #dist = 1
  27.     max_list = [0] * 32
  28.     max_list = np.array(max_list)
  29.     for k in range(32):
  30.         print('iteration ' + str(k))
  31.         cur = 400
  32.         dist = 5
  33.         results = [0] * 13
  34.         results = np.array(results)
  35.         for i in range(13):
  36.             print('(' + str(i) + ')')
  37.             samples = np.array([0])
  38.             samples = np.delete(samples,0)
  39.             for j in range(500):
  40.                 seats_sold,profit,time_elapsed = run_simulation(cur)
  41.                 samples = np.append(samples,profit)
  42.             avg_prof = np.average(samples)
  43.             results[i] = avg_prof
  44.             cur = cur + dist
  45.         max_val = dist*(np.argmax(results)) + 400
  46.         max_list[k] = max_val
  47.     print(max_list)
  48.  
  49.     '''
  50.     while(True):
  51.         to_test = np.array([cur_cost - dist, cur_cost, cur_cost + dist])
  52.         results = np.array([0.0,0.0,0.0])
  53.         for i in range(3):
  54.             samples = np.array([0])
  55.             samples = np.delete(samples,0)
  56.             for j in range(500):
  57.                 seats_sold,profit,time_elapsed = run_simulation(to_test[i])
  58.                 samples = np.append(samples,profit)
  59.             avg_prof = np.average(samples)
  60.             results[i] = avg_prof
  61.         if(results[0] < results[1] and results[1] > results[2]):
  62.             print('maximum of $' + str(results[1]) + ' identified at X = $' + str(to_test[1]))
  63.             break
  64.         if(results[0] > results[1] and results[1] < results[2]):
  65.             print('minimum of $' + str(results[1]) + ' identified at X = $' + str(to_test[1]))
  66.             if(results[0] > results[2]):
  67.                 cur_cost = to_test[0]
  68.             else:
  69.                 cur_cost = to_test[2]
  70.         if(results[0] < results[1] and results[1] < results[2]):
  71.             print('moving up to X = $' + str(to_test[2]))
  72.             cur_cost = to_test[2]
  73.         if(results[0] > results[1] and results[1] > results[2]):
  74.             print('moving down to X = $' + str(to_test[0]))
  75.             cur_cost = to_test[1]
  76.     '''
  77.            
  78.    
  79.  
  80.  
  81. def part_c():
  82.     pass
  83.  
  84. def run_simulation(init_cost):
  85.     total_time = 60 * 24.0 * 30
  86.     mean_time = 120.0
  87.     seats = 150
  88.     sold = 0
  89.     cur_cost = init_cost
  90.     profit = 0.0
  91.     time_elapsed = 0.0
  92.  
  93.     calls = get_calls(mean_time, total_time)
  94.  
  95.     for i in range(len(calls)):
  96.         time_elapsed = time_elapsed + calls[i]
  97.         probability = get_probability(cur_cost,sold)
  98.         drawn_sample = np.random.uniform()
  99.  
  100.         if(drawn_sample <= probability):
  101.             profit = profit + cur_cost
  102.             sold = sold + 1
  103.             if(sold == seats):
  104.                 break
  105.  
  106.     return sold,profit,time_elapsed
  107.  
  108. def get_calls(mean, time):
  109.     calls = np.array([0])
  110.     calls = np.delete(calls,0)
  111.     cur_sum = 0.0
  112.     while(True):
  113.         val = (np.random.exponential(mean,1))[0]
  114.         if(cur_sum + val > time):
  115.             break
  116.         calls = np.append(calls,val)
  117.         cur_sum = cur_sum + val
  118.     return calls
  119.  
  120. def get_probability(X,N):
  121.     f = (300 - np.e**(X/100.0))/600
  122.     g = N/350.0
  123.     p = np.min([1, np.max([f,g])])
  124.     return p
  125.  
  126. if __name__ == '__main__':
  127.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement