Skip to content
Snippets Groups Projects
Commit 8004e87e authored by rhaag's avatar rhaag
Browse files

current Version

parent 9d869d30
No related branches found
No related tags found
No related merge requests found
Merge branch 'main' of gitlab.gwdg.de:rhaag/ridepooling_simulations into main
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
#Julia 1.6.2
#RidePooling 0.8.2
using RidePooling
using DelimitedFiles
#error function (to be minimized)
function f(x,N,target,t0,specs)
if x < 0
return 1.0
end
model=get_model(;N_bus=N,ν=x/t0,specs...);
#now comes the initial phase that will be skipped for the statistics
run!(model,dropped=10*max(N,100),time=2*t0) #on average, every bus has dropped off >10 users + simulation time 4 times bigger than avrg. direct distance.
startindex=length(model.requests)
run!(model,requested=startindex+20*max(N,100))
res=served_percentage(model,startindex=startindex)-target
if abs(res)>0.5
return res
else
run!(model,requested=startindex+50*max(N,100))
res=served_percentage(model,startindex=startindex)-target
if abs(res)>0.15
return res
else
run!(model,requested=startindex+100*max(N,100))
res=served_percentage(model,startindex=startindex)-target
if abs(res)>0.05
return res
else
run!(model,requested=startindex+200*max(N,100))
return served_percentage(model,startindex=startindex)-target
end
end
end
end
function prnt(x)
println(x)
flush(stdout)
end
percentage=eval(Meta.parse(ARGS[1]))
index=eval(Meta.parse(ARGS[2]))
Ns=unique(Integer.(round.(exp.(range(log(1),log(1000);length=61)))))
N=Ns[index]
target=percentage/100
filepath=pwd()*"/../evaluation/f$(percentage)_$(N).txt"
guesspath=pwd()*"/../evaluation/guess_f$(percentage)_$(N).txt"
map_folder=pwd()*"/../../map/"
include(map_folder*"map.jl")
include(pwd()*"/../dispatcher.jl")
#initiate model
specs=(;
map=map,
route_matrix=RM,
subspaces=:all_edges,
routing=:lookup,
speed_dict=speed_dict,
seed=1
)
specs=merge(specs,dispatcher)
TEST=false
init=1.6*N^1.14
occursin("32",pwd()) ? prnt("map with a 32. init prefactor = 1.6") : nothing
occursin("64",pwd()) ? (init*=2.6/1.6;prnt("map with a 64. init prefactor = 2.6")) : nothing
occursin("128",pwd()) ? (init*=5.5/1.6;prnt("map with a 128. init prefactor = 5.5")) : nothing
while TEST==false
A=init*(0.95+0.1*rand())
prnt("################")
prnt("N = $(N)")
prnt("initial guess A = $(round(A;digits=4))")
prnt("################\n")
fA=f(A,N,target,t0,specs)
prnt("fA=$(round(fA;digits=4))")
if fA>0
prnt("A=$(round(A;digits=4)) too small, B must be larger.")
best_positive=A
best_negative=false
B=A
while best_negative==false
B*=1.1
prnt("Trying B=$(B)")
fB=f(B,N,target,t0,specs)
prnt("fB=$(fB)")
best_negative= fB<0 ? B : false
fB>0 ? ((A,fA)=(B,fB)) : nothing
end
else
prnt("A too large, B must be smaller.")
B,fB=(A,fA)
prnt("Setting B=$(round(A;digits=4)) to maintain the convention that A<B")
best_negative=B
best_positive=false
while best_positive==false
A/=1.1
prnt("Trying A=$(round(A;digits=4))")
fA=f(A,N,target,t0,specs)
prnt("fA=$(round(fA;digits=4))")
best_positive= fA>0 ? A : false
fA<0 ? ((B,fB)=(A,fA)) : nothing
if A<1e-3
prnt("This many buses don't seem to be able to serve $(percentage) percent of any demand. Exiting.")
exit()
end
end
end
iteration=0
while true
if (fA<0.02 && fB>-0.02)
prnt("")
prnt("breaking free after $(iteration) iterations with")
prnt("A=$(round(A;digits=4)), fA=$(round(fA;digits=4))")
prnt("B=$(round(B;digits=4)), fB=$(round(fB;digits=4))")
break
end
iteration+=1
C_mean=(A+B)/2
C_secante=B-fB*(B-A)/(fB-fA)
writedlm(guesspath,C_secante);prnt("\n ..writing $(round(C_secante;digits=4)) (current secante root) to guess_path.")
if fA<0.01
C=(2*C_secante+B)/3
elseif fB>0.01
C=(2*C_secante+A)/3
else
C=(2*C_secante+C_mean)/3
end
fC=f(C,N,target,t0,specs)
prnt("iteration $(iteration)")
prnt("A=$(round(A;digits=4)), fA=$(round(fA;digits=4))")
prnt("B=$(round(B;digits=4)), fB=$(round(fB;digits=4))")
prnt("C=$(round(C;digits=4)), fC=$(round(fC;digits=4))")
if fC>0
A,fA=(C,fC)
else
B,fB=(C,fC)
end
end
C=B-fB*(B-A)/(fB-fA)
writedlm(guesspath,C);prnt(" ..writing $(round(C;digits=4)) (final secante root) to guess_path.")
#find root of parabola
fC=f(C,N,target,t0,specs)
mat=[A^2 A 1;B^2 B 1;C^2 C 1]
a,b,c=inv(mat)*[fA,fB,fC]
p,q=(b/a,c/a)
x1=-p/2-sqrt((p/2)^2-q)
x2=-p/2+sqrt((p/2)^2-q)
C= abs(x1-C)<abs(x2-C) ? x1 : x2
writedlm(guesspath,C);prnt(" ..writing $(round(C;digits=4)) (root of parabola) to guess_path.")
prnt("\nFINAL: $(round(C;digits=4))")
#test served percentage
fFINAL=f(C,N,target,t0,specs)
global TEST=abs(fFINAL)<0.02
prnt("TEST: served percentage = $(round(100*(fFINAL+target);digits=2))%\n\n\n")
if TEST
prnt("test worked.")
writedlm(filepath,C)
else
global init=C
prnt("test failed.")
end
end
#!/bin/bash
#$ -S /bin/bash
#$ -cwd
#$ -q titan.q
#$ -j yes
#$ -N earliest_pickup
#$ -t 1-100
N=20
SAVE_PATH="./example/ruben/"
julia sim.jl $N $SAVE_PATH $SGE_TASK_ID
export data_functions, save_path, getValues
import Pkg
Pkg.activate(".")
using DataFrames
using DelimitedFiles
# Dict of the functions used for evaluation
# QUESTION Calculate Variance for all of these?
data_type_functions = Dict(
:served_percentage => RP.served_percentage,
:driven_distance => RP.driven_distance,
:requested_distance => RP.requested_distance,
:mean_relative_delay => RP.mean_relative_delay,
# :mean_occupancy => RP.mean_occupancy, #BUG In Reading TUples from a CSV File
)
save_path = "" #TODO What savepath?
"""
Converts a given index to a 2D Matrix Index with the given Matrix size
"""
function getValue(index, xmin, xmax, xlen, ymin, ymax, ylen)
xstep = (xmax-xmin)/xlen
ystep = (ymax-ymin)/ylen
x = (index%xlen) * xstep + xmin
y = trunc(Int64, index/ylen) * ystep + ymin
return x,y
end
"""
Function for Running a RidePooling simulation with the normalized Frequency x
"""
function simulate_rp(paths::Dict, N::Int64, x, y, t0::Float64, specs; served = 10*N, requested=10*N)
#Make Model
model=RP.get_model(;N_bus=N,ν=x/t0,specs...);
RP.run!(model;requested=requested, served=served)
data = Dict()
data[:frequency] = x
data[:dt_latest_dropoff] = y
for (name, func) in data_type_functions
data[name] = func(model)
end
# Save the calculated Data
data = DataFrame(data)
#CSV.write(paths[:data]*"$index.csv", data)
open(paths[:data]*"$index.csv", "w") do io
writedlm(io, Iterators.flatten(([names(data)], eachrow(data))), ';')
end
#Save the model for possible later reference
RP.savemodel(paths[:model]*"$index.model",model;route_matrix=false)
end
#!/bin/bash #!/bin/bash
./settings.sh
N=1 N=1
SAVE_PATH="/scratch01.local/rhaag/latest_dropoff_try_2/results/"
INDEX=1 INDEX=1
DATA_PATH="${SAVE_PATH}data/"
MODEL_PATH="${SAVE_PATH}model/"
mkdir -p $DATA_PATH mkdir -p $DATA_PATH
mkdir $MODEL_PATH mkdir $MODEL_PATH
......
...@@ -29,7 +29,7 @@ CSV.write(archive_path*"results.csv", df) ...@@ -29,7 +29,7 @@ CSV.write(archive_path*"results.csv", df)
println("Moving Models to $archive_path") println("Moving $(length(model_filenames)) Models to $archive_path")
try try
mkdir("$archive_path/models/") mkdir("$archive_path/models/")
catch e catch e
......
This diff is collapsed.
max_waiting_time=t0
max_relative_detour=1.0
dispatcher=(;
cost=:delays,
rejection_criterion=((:any_relative_detour,max_relative_detour),)
)
map_specs=(:star_grid_map,(64,16))
#t0=readdlm(map_folder*"timescale.txt")[1]
#map
speed_dict=Dict(1=>3.6)
mymap=RP.get_map(map_specs)
RM=RP.get_route_matrix(map_folder)
using DelimitedFiles
t0=readdlm(map_folder*"timescale.txt")[1]
File deleted
45.339190347724944
File deleted
#!/bin/bash
#$ -S /bin/bash
#$ -cwd
#$ -q titan.q
#$ -j yes
#$ -N myBunchOfSimulations
#$ -t 1-2082
/usr/dcf/julia-1.5.2/bin/julia scripts/myJuliaScript.jl $SGE_TASK_ID
N=20
REQUEST_TYPE="earliest_pickup"
NAME="newSystem_try1"
SAVE_PATH="/scratch01.local/rhaag/${REQUEST_TYPE}/${NAME}/results/"
DATA_PATH="${SAVE_PATH}data/"
MODEL_PATH="${SAVE_PATH}model/"
using Pkg using Pkg
Pkg.activate(".") Pkg.activate(".")
#Pkg.add(url="https://gitlab.gwdg.de/smuehle1/RidePooling/", rev="planned_pickup") Pkg.add(url="https://gitlab.gwdg.de/smuehle1/RidePooling/", rev="planned_pickup")
#Pkg.instantiate() #there may be an error here concerning PyCall. The dependencies assume Python to be installed on your system, with package 'matplotlib' installed. Pkg.instantiate() #there may be an error here concerning PyCall. The dependencies assume Python to be installed on your system, with package 'matplotlib' installed.
#Either just run 'conda install -c conda-forge matplotlib' in your system shell, or follow instructions below (after running this cell and getting an error) to solve everything from within Julia. #Either just run 'conda install -c conda-forge matplotlib' in your system shell, or follow instructions below (after running this cell and getting an error) to solve everything from within Julia.
import RidePooling import RidePooling
...@@ -18,34 +18,44 @@ include("./initialize.jl") ...@@ -18,34 +18,44 @@ include("./initialize.jl")
index=1 index=1
save_path = pwd()*"/example/ruben/" save_path = pwd()*"/example/ruben/"
N=10 N=10
request_type = :now
# Get Parameters from commandline # Get Parameters from commandline
println(ARGS) println(ARGS)
try
global N = eval(Meta.parse(ARGS[1]))
global save_path = ARGS[2]
global index = eval(Meta.parse(ARGS[3]))
global request_type = Symbol(Meta.parse(ARGS[3]))
println("Using Command line parameters \n\tN = $N\n\tsave_path = $save_path\n\tindex = $index\n\trequest_type=$request_type")
catch e
println(e)
try try
global N = eval(Meta.parse(ARGS[1])) global N = eval(Meta.parse(ARGS[1]))
global save_path = ARGS[2] global save_path = ARGS[2]
global index = eval(Meta.parse(ARGS[3])) global index = eval(Meta.parse(ARGS[3]))
println("Using Command line parameters \n\tN = $N\n\tsave_path = $save_path\n\tindex = $index") println("Using Command line parameters \n\tN = $N\n\tsave_path = $save_path\n\tindex = $index\n\trequest_type=$request_type")
catch e catch e
println(e) println(e)
try try
global N=10 global N=10
global save_path = Meta.parse(ARGS[1]) global save_path = Meta.parse(ARGS[1])
global index = eval(Meta.parse(ARGS[2])) global index = eval(Meta.parse(ARGS[2]))
println("No N found. Falling back to standart value:\n\tN=$N") println("No N found. Falling back to standart value:\n\tN=$N\n\trequest_type=$request_type")
catch e catch e
println(e) println(e)
try try
global save_path = pwd()*"/results/" global save_path = pwd()*"/results/"
global N=10 global N=10
global index=eval(Meta.parse(ARGS[1])) global index=eval(Meta.parse(ARGS[1]))
println("No Savepath or N found. Falling back to standart values:\n\tN=$N\n\tfilepath=$save_path") println("No Savepath or N found. Falling back to standart values:\n\tN=$N\n\tfilepath=$save_path\n\trequest_type=$request_type")
catch e catch e
println(e) println(e)
global index=1 global index=1
global save_path = pwd()*"/results/" global save_path = pwd()*"/results/"
global N=10 global N=10
println("No Filepath and no Index found. Falling back to standart values:\n\tindex = $index\n\tSave Path = $save_path") println("No Filepath and no Index found. Falling back to standart values:\n\tindex = $index\n\tSave Path = $save_path\n\trequest_type=$request_type")
end end
end end
end end
...@@ -83,7 +93,7 @@ include(paths[:dispatcher]*"dispatcher.jl") ...@@ -83,7 +93,7 @@ include(paths[:dispatcher]*"dispatcher.jl")
x, y = getValue(index-1, 4, 60, 40, 0.2, 2.5, 60) x, y = getValue(index-1, 4, 60, 40, 0.2, 2.5, 60)
println("$x\t$y") println("$x\t$y")
random_gens = Dict(:dropoff => [:notRandom, y*t0]) random_gens = Dict(request_type => [:notRandom, y*t0])
specs=(; specs=(;
map=mymap, map=mymap,
route_matrix=RM, route_matrix=RM,
...@@ -91,7 +101,7 @@ specs=(; ...@@ -91,7 +101,7 @@ specs=(;
routing=:lookup, routing=:lookup,
speed_dict = speed_dict, speed_dict = speed_dict,
seed = 1, seed = 1,
request_type= :latest_dropoff, request_type = request_type,
random_gens = random_gens) random_gens = random_gens)
specs = merge(specs, dispatcher) specs = merge(specs, dispatcher)
......
...@@ -7,12 +7,8 @@ ...@@ -7,12 +7,8 @@
#$ -N latest_dropoff #$ -N latest_dropoff
#$ -t 1-2400 #$ -t 1-2400
N=20 ./settings.sh
SAVE_PATH="/scratch01.local/rhaag/latest_dropoff/results/"
INDEX=1
DATA_PATH="${SAVE_PATH}data/"
MODEL_PATH="${SAVE_PATH}model/"
/usr/ds/bin/julia sim.jl $N $SAVE_PATH $SGE_TASK_ID /usr/ds/bin/julia sim.jl $N $SAVE_PATH $SGE_TASK_ID $REQUEST_TYPE
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment