This code: liczbarowerownastacji = [10, 10, 10, 10, 10] brakroweru = [0, 0, 0, 0, 0]…
- User
This code:
liczba_rowerow_na_stacji = [10, 10, 10, 10, 10] brak_roweru = [0, 0, 0, 0, 0] overrun = [0, 0, 0, 0, 0] czas = 0 popyt_stacji = [0.3, 0.3, 0.2, 0.1, 0.1] prawdopodobienstwo_stacji = [ 0.05 0.45 0.3 0.1 0.1; 0.43 0.07 0.1 0.2 0.2; 0.34 0.2 0.06 0.2 0.2; 0.2 0.2 0.2 0.05 0.35; 0.1 0.1 0.1 0.67 0.03] ongoing = [] # Keep track of ongoing journeys wynik_sim_init = [liczba_rowerow_na_stacji, brak_roweru, overrun, czas]
function single_sim(wynik_sim = wynik_sim_init, prawdopodobienstwo_stacji = prawdopodobienstwo_stacji, popyt_stacji = popyt_stacji, czas_przejazdu = 1, ongoing_journeys = ongoing) # First update ongoing journeys for i in length(ongoing_journeys):-1:1 journey = ongoing_journeys[i] journey[3] -= czas_przejazdu if journey[3] <= 0 # The journey is completed wynik_sim[1][journey[2]] += 1 # Add the bike to the destination station if wynik_sim[1][journey[2]] >= 15 wynik_sim[3][journey[2]] += 1 wynik_sim[1][journey[2]] -= 3 wynik_sim[1][argmin(wynik_sim[1])] += 3 end # Remove the journey from the ongoing_journeys list splice!(ongoing_journeys, i) end end
codestacja_odebrania = rand(Categorical(popyt_stacji)) stacja_oddania = rand(Categorical(prawdopodobienstwo_stacji[stacja_odebrania, :])) if wynik_sim[1][stacja_odebrania] >= 1 wynik_sim[1][stacja_odebrania] -= 1 # Remove a bike from the pickup station journey = [stacja_odebrania, stacja_oddania, czas_przejazdu] push!(ongoing_journeys, journey) # Add the journey to the ongoing_journeys list else wynik_sim[2][stacja_odebrania] += 1 # Increase the number of unavailable bikes end wynik_sim[4] += czas_przejazdu return wynik_simend
function run_day(liczba_dzienna, wynik_sim = wynik_sim_init, prawdopodobienstwo_stacji = prawdopodobienstwo_stacji, czas_przejazdu = 1) wynik_sim_init = deepcopy(wynik_sim) for i in range(1, liczba_dzienna) wynik_sim = single_sim(wynik_sim_init, prawdopodobienstwo_stacji, popyt_stacji, czas_przejazdu) end return wynik_sim end
function run_sims(liczba_sim = 30, liczba_dzienna = 40, wynik_sim = wynik_sim_init, prawdopodobienstwo_stacji = prawdopodobienstwo_stacji, czas_przejazdu = 1) wynik_sim_init = deepcopy(wynik_sim) wyniki_okresu = DataFrame(dzien=Int[], czas=Float64[], nr_stacji=Int[], liczba_na_koniec_dnia = Int[], braki_na_koniec_dnia = Int[], przewoz=Int[]) for i in range(1, liczba_sim) wynik_dnia = run_day(liczba_dzienna, wynik_sim, prawdopodobienstwo_stacji, czas_przejazdu) temp_df = DataFrame(dzien=fill(i,5), czas=fill(wynik_dnia[4],5), nr_stacji=1:5, liczba_na_koniec_dnia=wynik_dnia[1], braki_na_koniec_dnia=wynik_dnia[2], przewoz=wynik_dnia[3]) wyniki_okresu = vcat(wyniki_okresu, temp_df) wynik_sim = wynik_sim_init end wynik_sim = wynik_sim_init return wyniki_okresu end
- Assistant
- User
Ok. It is very cool, however at the end of the day, all bikes must end their journey, meaning that without additional demand being added we must finish all jouneys and after the day has ended.
- Assistant
I understand, you want to ensure that all bikes have finished their journeys at the end of the simulation. In order to achieve this, you can add an additional loop after the day has ended to continue updating the ongoing journeys until they're all completed. Here's a modified version of the
single_simfunction that implements this:julia