We are in a closed network, where no one can enter or exit. There are 91 bikes in total, with 83 bikes in the stations and 8 bikes on the way to their destination. There are 5 stations where bikes can be dropped off. We have a matrix with travel times and another matrix with transition probabilities. The goal of the code is to generate a departure of a bike for each station at every minute t. Therefore, we remove a bike from the station and add it to the matrix of bikes on their way. At each iteration, we check if the travel time has been exceeded. If so, we add a bike to the station and remove it from the matrix of bikes on their way. However, there seems to be a tendency for bikes to accumulate in the 4th trajectory and I don't understand why.
def probabilite_deplacement(station_i):
p = [[0,0.22,0.32,0.2,0.26],
[0.17,0,0.34,0.21,0.28],
[0.19,0.26,0,0.24,0.31],
[0.17,0.22,0.33,0,0.28],
[0.18,0.24,0.35,0.23,0]]
return p[station_i]
def temps_trajet(station_i,station_j):
t = [[0,np.random.poisson(3),np.random.poisson(5),np.random.poisson(7),np.random.poisson(7)],
[np.random.poisson(2),0,np.random.poisson(2),np.random.poisson(5),np.random.poisson(5)],
[np.random.poisson(4),np.random.poisson(2),0,np.random.poisson(3),np.random.poisson(3)],
[np.random.poisson(8),np.random.poisson(6),np.random.poisson(4),0,np.random.poisson(2)],
[np.random.poisson(7),np.random.poisson(7),np.random.poisson(5),np.random.poisson(2),0]]
return t[station_i][station_j]
def simulation3(condition_initiale,nb_velo_trajet):
'''
t : temps en minutes
'''
z = np.array([[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]])
t = 0
while t < 9000:
if t%60 == 0:
taux_depart = np.array([2.8/60,3.7/60,5.5/60,3.5/60,4.6/60]) # le taux de départ de base est divisé par 60
if t%60 != 0:
taux_depart += taux_depart
i = 0
while i < 5:
nouvelle_station = np.random.choice(len(probabilite_deplacement(i)), size=1, p=probabilite_deplacement(i))
print("\nIl y aura un trajet de la station",i,"vers la station ",nouvelle_station[0])
if z[i][nouvelle_station[0]] >= temps_trajet(i,nouvelle_station[0]) and nb_velo_trajet[i][nouvelle_station[0]] >= 1:
nb_velo_trajet[i][nouvelle_station[0]] -= 1 # enlève le vélo du trajet
condition_initiale[nouvelle_station[0]] += 1 # rajoute à la nouvelle station le vélo
z[i][nouvelle_station[0]] = 0 # Réanitialise le temps à 0
print('\nOn a enlevé un vélo du trajet ', nb_velo_trajet[i][nouvelle_station[0]])
print('\nOn a rajouté à la station ',nouvelle_station[0], 'un vélo')
if taux_depart[i] >= 1 and condition_initiale[i] >= 1: # dès que le taux de départ dépasse 1, on envoi un vélo
condition_initiale[i] -= 1 # enlève le vélo à la station
nb_velo_trajet[i][nouvelle_station[0]] += 1 # Rajoute le vélo sur le trajet
taux_depart[i] -= 1
z[i][nouvelle_station[0]] = 0
print('\nOn a enlevé un vélo de la station ', i)
print('\nOn a rajouté ce vélo en trajet ')
i += 1
print("\ntemps en seconde : ",t)
print("\nNombre de vélo par station",condition_initiale)
print('\nNombre de vélo en trajet',nb_velo_trajet)
t = t + 1
z += np.array([[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]])
return condition_initiale,nb_velo_trajet,np.sum(np.array(nb_velo_trajet)) + np.sum(np.array(condition_initiale)),z
I've tried several different methods, and the code I provide seems to be the closest version to what I want, but I can't seem to get it to work.