I have this Julia simulation: @withkw mutable struct BikeTrip origin::Int…
- User
I have this Julia simulation:
@with_kw mutable struct BikeTrip origin::Int destination::Int trip_time::Int end
@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame() end
function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end
function run_simulation!(sim::BikeSimulation, num_bikes::Int, max_time::Int) for station in 1:length(sim.bike_stations) for _ in 1:min(num_bikes, sim.station_capacity) start_trip!(sim, (0, station)) end end
while !isempty(sim.event_queue) && sim.event_queue[1][1] <= max_time event = popfirst!(sim.event_queue) time, event_name, data = event if event_name == "end_trip!" end_trip!(sim, data) end append!(sim.log, DataFrame(time=time, event=event_name, bike_status=sim.bike_stations)) endend
event_id=1 function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end end
trip_distributions = [ [1 2 10; 2 3 1; 3 4 2; 4 5 4; 5 1 3], [1 3 5; 2 4 6; 3 5 9; 4 1 2; 5 2 3], [1 4 4; 2 5 7; 3 1 8; 4 2 12; 5 3 9], [1 5 9; 2 1 3; 3 2 4; 4 3 8; 5 4 6], [1 1 6; 2 2 4; 3 3 6; 4 4 7; 5 5 7] ]
bike_simulation = BikeSimulation(bike_stations=[20, 20, 20, 20, 20], station_capacity=40, trip_distributions=trip_distributions)
run_simulation!(bike_simulation, 10, 100) # Runs the simulation for a maximum of 100 time units
println(bike_simulation.log)
The problem is that it was supposed to measure movements between bike stations. I need to know : how many bikes are at each moment of time at the station, whether there was reported a demand for bike but no bike at station, whether bikes at station exceeeded station capacity.
- Assistant
To keep track of the bikes at each station and to capture when there is no bike available at the station or the number of bikes exceeds the station capacity, you can modify the simulation as follows:
First, add
demand_lossandexcess_bikesfields toBikeSimulationstruct to keep track of lost demands and excess bikes.@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame() demand_loss::Array{Int, 1} = zeros(Int, length(bike_stations)) excess_bikes::Array{Int, 1} = zeros(Int, length(bike_stations)) endModify the
start_trip!function to check for available bikes at the station before starting a trip:function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 endModify the
end_trip!function to check for excess bikes at the station when ending a trip:function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end endAfter running the simulation, you can display the bike status at each station, lost demand, and excess bikes:
println("Bike status at each station:", bike_simulation.bike_stations) println("Lost demand at each station:", bike_simulation.demand_loss) println("Excess bikes at each station:", bike_simulation.excess_bikes)Now the simulation will capture the number of bikes at each station, the demand for bikes when there are no bikes at the station, and the cases when bikes at the station exceed the station capacity.
- User
Ok. I think there is a misunderstanding. I want to have multiple bikes travelling at the same time. I want to see the number of bikes at each station at every time moment and I want to see lost demand both lost demand and bike excess at each station at every time moment. Store it in dataframe. Rember, multiple bikes travel at the same time and multiple customers can report demand when bikes are still travelling.
- Assistant
Thank you for the clarification. I suggest modifying the simulation to have the DataFrame record the bike status, demand_loss, and excess_bikes for each station at every time in the simulation.
Change the creation of the log field in the
BikeSimulationstruct to create columns for bike_status, demand_loss, and excess_bikes:@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame(time=Int[], event=String[], bike_status=Any[], demand_loss=Any[], excess_bikes=Any[]) endUpdate the
start_trip!andend_trip!functions to record bike status, demand_loss, and excess_bikes at each event:function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 append!(sim.log, DataFrame( time=time, event="demand_loss", bike_status=[sim.bike_stations...], demand_loss=[sim.demand_loss...], excess_bikes=[sim.excess_bikes...]) ) return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 append!(sim.log, DataFrame( time=time, event="excess_bikes", bike_status=[sim.bike_stations...], demand_loss=[sim.demand_loss...], excess_bikes=[sim.excess_bikes...]) ) return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end endTo see the bike status, demand_loss, and excess_bikes at each time moment in the DataFrame, after running the simulation, print the log:
println(bike_simulation.log)This will provide a DataFrame containing bike status, demand_loss, and excess_bikes for each station at each time moment in the simulation.
- User
In this simulation I get the error:
@with_kw mutable struct BikeTrip origin::Int destination::Int trip_time::Int end
@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame(time=Int[], event=String[], bike_status=Any[], demand_loss=Any[], excess_bikes=Any[]) end
function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 append!(sim.log, DataFrame( time=time, event="demand_loss", bike_status=[sim.bike_stations…], demand_loss=[sim.demand_loss…], excess_bikes=[sim.excess_bikes…]) ) return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end
function run_simulation!(sim::BikeSimulation, num_bikes::Int, max_time::Int) for station in 1:length(sim.bike_stations) for _ in 1:min(num_bikes, sim.station_capacity) start_trip!(sim, (0, station)) end end
while !isempty(sim.event_queue) && sim.event_queue[1][1] <= max_time event = popfirst!(sim.event_queue) time, event_name, data = event if event_name == "end_trip!" end_trip!(sim, data) end append!(sim.log, DataFrame(time=time, event=event_name, bike_status=sim.bike_stations)) endend
event_id = 1 function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 append!(sim.log, DataFrame( time=time, event="excess_bikes", bike_status=[sim.bike_stations…], demand_loss=[sim.demand_loss…], excess_bikes=[sim.excess_bikes…]) ) return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end end
trip_distributions = [ [1 2 10; 2 3 1; 3 4 2; 4 5 4; 5 1 3], [1 3 5; 2 4 6; 3 5 9; 4 1 2; 5 2 3], [1 4 4; 2 5 7; 3 1 8; 4 2 12; 5 3 9], [1 5 9; 2 1 3; 3 2 4; 4 3 8; 5 4 6], [1 1 6; 2 2 4; 3 3 6; 4 4 7; 5 5 7] ]
bike_simulation = BikeSimulation(bike_stations=[20, 20, 20, 20, 20], station_capacity=20, trip_distributions=trip_distributions)
run_simulation!(bike_simulation, 10, 100) # Runs the simulation for a maximum of 100 time units
println(bike_simulation.log) syntax: unexpected "]"
Stacktrace: [1] top-level scope @ In[3]:25
- Assistant
Apologies for the errors in the code. It looks like the ellipses
...inside the brackets[]for bike_status, demand_loss, and excess_bikes are causing the issue.Please find the corrected code below:
function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 append!(sim.log, DataFrame( time=[time], event=["demand_loss"], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 append!(sim.log, DataFrame( time=[time], event=["excess_bikes"], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end end # ... (the rest of the code is unchanged)Now, when you run the simulation, the log DataFrame should be correctly populated with bike status, demand_loss, and excess_bikes for each event.
- User
ArgumentError: Column names :demand_loss and :excess_bikes were found in only one of the passed data frames and
cols == :setequalStacktrace: [1] run_simulation!(sim::BikeSimulation, num_bikes::Int64, max_time::Int64) @ Main .\In[2]:54 [2] top-level scope @ In[2]:88
- Assistant
Apologies for the confusion. I realized the
append!function in therun_simulation!function is missing thedemand_lossandexcess_bikescolumns. Please find the correctedrun_simulation!function below:function run_simulation!(sim::BikeSimulation, num_bikes::Int, max_time::Int) for station in 1:length(sim.bike_stations) for _ in 1:min(num_bikes, sim.station_capacity) start_trip!(sim, (0, station)) end end while !isempty(sim.event_queue) && sim.event_queue[1][1] <= max_time event = popfirst!(sim.event_queue) time, event_name, data = event if event_name == "end_trip!" end_trip!(sim, data) end append!(sim.log, DataFrame( time=[time], event=[event_name], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) end endNow, when you run the simulation, the log DataFrame should be correctly populated with bike status, demand_loss, and excess_bikes for each event without any error.
- User
type BikeSimulation has no field demand_loss
Stacktrace: [1] getproperty @ .\Base.jl:37 [inlined] [2] run_simulation!(sim::BikeSimulation, num_bikes::Int64, max_time::Int64) @ Main .\In[2]:54 [3] top-level scope @ In[2]:95
- Assistant
Apologies for the oversight. It seems I missed adding the
demand_lossandexcess_bikesfields to theBikeSimulationstruct in the provided code. Please find the corrected struct definition below:@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame(time=Int[], event=String[], bike_status=Any[], demand_loss=Any[], excess_bikes=Any[]) demand_loss::Array{Int, 1} = zeros(Int, length(bike_stations)) excess_bikes::Array{Int, 1} = zeros(Int, length(bike_stations)) endNow, after adding the fields to the struct and initializing them to zero arrays with the same length as
bike_stations, the provided code should work without any issues. - User
Ok so from this:
@with_kw mutable struct BikeTrip origin::Int destination::Int trip_time::Int end
@with_kw mutable struct BikeSimulation bike_stations::Array{Int, 1} station_capacity::Int trip_distributions::Array{Array{Int, 2}, 1} event_queue::Vector{Tuple{Int, String, Tuple{Int, Int}}} = [] log::DataFrame = DataFrame(time=Int[], event=String[], bike_status=Any[], demand_loss=Any[], excess_bikes=Any[]) demand_loss::Array{Int, 1} = zeros(Int, length(bike_stations)) excess_bikes::Array{Int, 1} = zeros(Int, length(bike_stations)) end function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 append!(sim.log, DataFrame( time=[time], event=["demand_loss"], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end
function run_simulation!(sim::BikeSimulation, num_bikes::Int, max_time::Int) for station in 1:length(sim.bike_stations) for _ in 1:min(num_bikes, sim.station_capacity) start_trip!(sim, (0, station)) end end
while !isempty(sim.event_queue) && sim.event_queue[1][1] <= max_time event = popfirst!(sim.event_queue) time, event_name, data = event if event_name == "end_trip!" end_trip!(sim, data) end append!(sim.log, DataFrame( time=[time], event=[event_name], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) endend
event_id = 1 function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 append!(sim.log, DataFrame( time=[time], event=["excess_bikes"], bike_status=[sim.bike_stations], demand_loss=[sim.demand_loss], excess_bikes=[sim.excess_bikes]) ) return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end end
trip_distributions = [ [1 2 10; 2 3 1; 3 4 2; 4 5 4; 5 1 3], [1 3 5; 2 4 6; 3 5 9; 4 1 2; 5 2 3], [1 4 4; 2 5 7; 3 1 8; 4 2 12; 5 3 9], [1 5 9; 2 1 3; 3 2 4; 4 3 8; 5 4 6], [1 1 6; 2 2 4; 3 3 6; 4 4 7; 5 5 7] ]
bike_simulation = BikeSimulation(bike_stations=[20, 20, 20, 20, 20], station_capacity=20, trip_distributions=trip_distributions)
run_simulation!(bike_simulation, 10, 100) # Runs the simulation for a maximum of 100 time units
println(bike_simulation.log)
I get such resluts: 852×5 DataFrame Row │ time event bike_status demand_loss excess_bikes
│ Int64 String Any Any Any
─────┼────────────────────────────────────────────────────────────────────────── 1 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 2 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 3 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 4 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 5 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 6 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 7 │ 2 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 8 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 9 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 10 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 11 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 12 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 13 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 14 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 15 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 16 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 17 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 18 │ 3 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 19 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 20 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 21 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 22 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 23 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 24 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 25 │ 4 end_trip! [10, 10, 10, 10, 10] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0]It appears that number of bikes at stations is not changing and the bikes are not moving between stations and They should move (more than one at the same time).
- Assistant
Apologies for the confusion. The issue arises because the bike_status, demand_loss, and excess_bikes arrays are passed as references in the DataFrame. To fix this, we need to create copies of these arrays when appending them to the log.
Please find the corrected
start_trip!andend_trip!functions below:function start_trip!(sim::BikeSimulation, time_origin::Tuple{Int, Int}) global event_id time, origin = time_origin if sim.bike_stations[origin] == 0 sim.demand_loss[origin] += 1 append!(sim.log, DataFrame( time=[time], event=["demand_loss"], bike_status=[copy(sim.bike_stations)], demand_loss=[copy(sim.demand_loss)], excess_bikes=[copy(sim.excess_bikes)]) ) return end trip_distribution = sim.trip_distributions[origin] destination = sample(trip_distribution[:, 2], Weights(trip_distribution[:, 1])) trip_time = sample(trip_distribution[:, 3], Weights(trip_distribution[:, 1])) sim.bike_stations[origin] -= 1 push!(sim.event_queue, (time + trip_time, "end_trip!", (time + trip_time, destination))) sort!(sim.event_queue) event_id += 1 end function end_trip!(sim::BikeSimulation, time_destination::Tuple{Int, Int}) time, destination = time_destination if sim.bike_stations[destination] >= sim.station_capacity sim.excess_bikes[destination] += 1 append!(sim.log, DataFrame( time=[time], event=["excess_bikes"], bike_status=[copy(sim.bike_stations)], demand_loss=[copy(sim.demand_loss)], excess_bikes=[copy(sim.excess_bikes)]) ) return end sim.bike_stations[destination] += 1 if sim.bike_stations[destination] < sim.station_capacity start_trip!(sim, (time, destination)) end endAfter making these changes, you should now see the bike_status array changing and reflecting the movement of bikes between stations. Multiple bikes should also move concurrently between stations.