Last active
June 7, 2025 20:58
-
-
Save Logianer/c0c49ed5bc4f2ba3808e49923c2f1003 to your computer and use it in GitHub Desktop.
CarSim normal distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import random | |
random.seed() | |
def get_parking_duration(): | |
durations = [15, 20, 30, 35, 40, 45, 55, 60, 120, 180] | |
# return 60 | |
return durations[random.randint(0, len(durations)-1)] | |
def n_dist(x, mu, sigma): | |
return 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (x - mu)**2 / (2 * sigma**2) ) | |
max_visitors = 1000 | |
arrival = [] | |
mu = random.randrange(int(14.5*60), int(15.5*60)) | |
sigma = random.randrange(int(1.5*60), 3*60) | |
for x in range(9*60, 22*60): | |
arrival.append(float(n_dist(x, mu, sigma))) | |
visitors = [] | |
departs = [0] * (13*60) | |
hourly_vis = [] | |
visitor_plot = [] | |
remainder = 0 | |
for i, minute in enumerate(arrival): | |
current_visitors = minute*max_visitors | |
visitors.append(current_visitors) | |
# print(integral, current_visitors) | |
rnd = random.random() | |
vis_plus = 0 | |
if (rnd <= current_visitors): | |
vis_plus=round(current_visitors) | |
remainder += abs(vis_plus - current_visitors) | |
if (remainder >= 1): | |
vis_plus+= 1 | |
remainder = 0 | |
if(vis_plus > 0): | |
print("+{plus} ({minute}m)".format(plus=vis_plus,minute=i)) | |
for m in range(vis_plus): | |
random_time = get_parking_duration() | |
if(i+random_time < len(departs)): | |
departs[i+random_time] += 1 | |
visitor_plot.append(vis_plus) | |
""" | |
depart = visitors.copy() | |
depart[0:0] = [0] | |
depart = depart[:-1] | |
print(visitors, depart) | |
print(np.array(visitors) - np.array(depart)) | |
print(max_visitors, sum(visitors)) | |
x = range(0,13) | |
plt.plot(x, depart, label='Departures') | |
plt.plot(x, np.cumsum(visitors) - np.cumsum(depart), linestyle='dashed') | |
""" | |
plt.vlines(np.array(range(0,13))*60, ymin=0, ymax=5, color='r', alpha=0.4) | |
print(sum(visitor_plot) - sum(departs)) | |
avg_t = 15 | |
averaged_departs = [np.average(departs[i:i+avg_t]) for i in range(0, len(departs), avg_t)] | |
averaged_vis = [np.average(visitor_plot[i:i+avg_t]) for i in range(0, len(visitor_plot), avg_t)] | |
plt.bar(np.arange(0,13*60/avg_t)*avg_t, averaged_departs, width=avg_t*0.85, alpha=0.4, color="limegreen",label='Departures') | |
plt.bar(np.arange(0,13*60/avg_t)*avg_t, averaged_vis, width=avg_t*0.85, alpha=0.4, color="hotpink",label='Visitors') | |
plt.legend() | |
# plt.plot(range(0, 13*60), visitor_plot) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment