Skip to content

Commit

Permalink
Merge pull request #17 from chkwon/v0.7
Browse files Browse the repository at this point in the history
V0.7
  • Loading branch information
chkwon committed Aug 10, 2018
2 parents 032d952 + c342e0c commit 68d17d1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 52 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Expand Up @@ -3,16 +3,18 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
- julia: 1.0
- julia: nightly
notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -p 2 -e 'Pkg.init(); Pkg.clone(pwd()); Pkg.add("LightGraphs"); Pkg.add("Optim"); Pkg.test("TrafficAssignment", coverage=true)'
- julia -p 2 -e 'using Pkg; Pkg.clone(pwd()); Pkg.test("TrafficAssignment", coverage=true)'
after_success:
- echo $TRAVIS_JULIA_VERSION
- julia -e 'cd(Pkg.dir("TrafficAssignment")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'using Pkg; cd(Pkg.dir("TrafficAssignment")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
4 changes: 2 additions & 2 deletions REQUIRE
@@ -1,4 +1,4 @@
julia 0.6.0-pre
julia 0.7
LightGraphs 0.8
Optim 0.7
BinDeps
BinDeps 0.8
4 changes: 2 additions & 2 deletions appveyor.yml
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
2 changes: 1 addition & 1 deletion src/TrafficAssignment.jl
Expand Up @@ -2,7 +2,7 @@ module TrafficAssignment

# package code goes here
using LightGraphs, Optim, BinDeps

using Distributed, Printf, LinearAlgebra, SparseArrays

TNTP_SHA = "ca933f74c03af688dfffa33f4b00826cba672479"

Expand Down
6 changes: 3 additions & 3 deletions src/frank_wolfe.jl
Expand Up @@ -113,7 +113,7 @@ function ta_frank_wolfe(ta_data; method=:bfw, max_iter_no=2000, step=:exact, log
end

function hessian_diag(x)
h_diag = Array{Float64}(size(x))
h_diag = Array{Float64}(undef, size(x))
for i=1:length(x)
if power[i] >= 1.0
h_diag[i] = free_flow_time[i] * B[i] * power[i] * (x[i]^(power[i]-1)) / (capacity[i]^power[i])
Expand Down Expand Up @@ -153,7 +153,7 @@ function ta_frank_wolfe(ta_data; method=:bfw, max_iter_no=2000, step=:exact, log
vv = zeros(size(start_node))
x = zeros(size(start_node))

x = x + @parallel (+) for r=1:size(travel_demand)[1]
x = x + @distributed (+) for r=1:size(travel_demand)[1]
# for each origin node r, find shortest paths to all destination nodes
# if there is any travel demand starting from node r.
vv = zeros(size(start_node))
Expand Down Expand Up @@ -186,7 +186,7 @@ function ta_frank_wolfe(ta_data; method=:bfw, max_iter_no=2000, step=:exact, log
all_or_nothing_parallel(travel_time)
else
all_or_nothing_single(travel_time)
# when nprocs()==1, using @parallel just adds unnecessary setup time. I guess.
# when nprocs()==1, using @distributed just adds unnecessary setup time. I guess.
end
end

Expand Down
66 changes: 37 additions & 29 deletions src/load_network.jl
Expand Up @@ -5,7 +5,7 @@
#Link generalized cost = Link travel time + toll_factor * toll + distance_factor * distance

# Traffic Assignment Data structure
type TA_Data
mutable struct TA_Data
network_name::String

number_of_zones::Int
Expand Down Expand Up @@ -36,6 +36,9 @@ type TA_Data
end


search_sc(s,c) = something(findfirst(isequal(c), s), 0)


function download_tntp(force_download=false)
ta_root_dir = joinpath(dirname(dirname(@__FILE__)))
dest = ta_root_dir
Expand Down Expand Up @@ -66,17 +69,17 @@ function read_ta_network(network_name)
trip_table_file = ""

for f in readdir(network_dir)
if contains(lowercase(f), ".zip")
if occursin(".zip", lowercase(f))
zipfile = joinpath(network_dir, f)
run(unpack_cmd(zipfile, network_dir, ".zip", ""))
rm(zipfile)
end
end

for f in readdir(network_dir)
if contains(lowercase(f), "_net") && contains(lowercase(f), ".tntp")
if occursin("_net", lowercase(f)) && occursin(".tntp", lowercase(f))
network_data_file = joinpath(network_dir, f)
elseif contains(lowercase(f), "_trips") && contains(lowercase(f), ".tntp")
elseif occursin("_trips", lowercase(f)) && occursin(".tntp", lowercase(f))
trip_table_file = joinpath(network_dir, f)
end
end
Expand All @@ -92,7 +95,7 @@ end
function load_ta_network(network_name; best_objective=-1.0, toll_factor=0.0, distance_factor=0.0)
network_data_file, trip_table_file = read_ta_network(network_name)

return load_ta_network(network_name, network_data_file, trip_table_file, best_objective=best_objective, toll_factor=toll_factor, distance_factor=distance_factor)
load_ta_network(network_name, network_data_file, trip_table_file, best_objective=best_objective, toll_factor=toll_factor, distance_factor=distance_factor)
end


Expand All @@ -114,40 +117,40 @@ function load_ta_network(network_name, network_data_file, trip_table_file; best_
n = open(network_data_file, "r")

while (line=readline(n)) != ""
if contains(line, "<NUMBER OF ZONES>")
number_of_zones = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<NUMBER OF NODES>")
number_of_nodes = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<FIRST THRU NODE>")
first_thru_node = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<NUMBER OF LINKS>")
number_of_links = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<END OF METADATA>")
if occursin("<NUMBER OF ZONES>", line)
number_of_zones = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<NUMBER OF NODES>", line)
number_of_nodes = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<FIRST THRU NODE>", line)
first_thru_node = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<NUMBER OF LINKS>", line)
number_of_links = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<END OF METADATA>", line)
break
end
end

@assert number_of_links > 0

start_node = Array{Int64}(number_of_links)
end_node = Array{Int64}(number_of_links)
start_node = Array{Int64}(undef, number_of_links)
end_node = Array{Int64}(undef, number_of_links)
capacity = zeros(number_of_links)
link_length = zeros(number_of_links)
free_flow_time = zeros(number_of_links)
B = zeros(number_of_links)
power = zeros(number_of_links)
speed_limit = zeros(number_of_links)
toll = zeros(number_of_links)
link_type = Array{Int64}(number_of_links)
link_type = Array{Int64}(undef, number_of_links)

idx = 1
while !eof(n)
line = readline(n)
if contains(line, "~") || line == ""
if occursin("~", line) || line == ""
continue
end

if contains(line, ";")
if occursin(";", line)
line = strip(line, [' ', '\n', ';'])

numbers = split(line)
Expand Down Expand Up @@ -176,11 +179,11 @@ function load_ta_network(network_name, network_data_file, trip_table_file; best_
f = open(trip_table_file, "r")

while (line=readline(f)) != ""
if contains(line, "<NUMBER OF ZONES>")
number_of_zones_trip = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<TOTAL OD FLOW>")
total_od_flow = parse(Float64, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<END OF METADATA>")
if occursin("<NUMBER OF ZONES>", line)
number_of_zones_trip = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<TOTAL OD FLOW>", line)
total_od_flow = parse(Float64, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<END OF METADATA>", line)
break
end
end
Expand All @@ -189,25 +192,30 @@ function load_ta_network(network_name, network_data_file, trip_table_file; best_
@assert total_od_flow > 0

travel_demand = zeros(number_of_zones, number_of_zones)
od_pairs = Array{Tuple{Int64, Int64}}(0)
od_pairs = Array{Tuple{Int64, Int64}}(undef, 0)

origin = -1

while !eof(f)
line = readline(f)

if line == ""
origin = -1
continue
elseif contains(line, "Origin")
elseif occursin("Origin", line)
origin = parse(Int, split(line)[2] )
elseif contains(line, ";")
elseif occursin(";", line)
pairs = split(line, ";")
for i=1:size(pairs)[1]
if contains(pairs[i], ":")
if occursin(":", pairs[i])
pair = split(pairs[i], ":")
destination = parse(Int64, strip(pair[1]) )
od_flow = parse(Float64, strip(pair[2]) )

# println("origin=$origin, destination=$destination, flow=$od_flow")

travel_demand[origin, destination] = od_flow
push!(od_pairs, (origin, destination))
# println("origin=$origin, destination=$destination, flow=$od_flow")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
@@ -1,5 +1,5 @@
using TrafficAssignment
using Base.Test
using Test

include("test_functions.jl")

Expand Down
25 changes: 14 additions & 11 deletions test/test_functions.jl
Expand Up @@ -8,9 +8,10 @@ function test_tntp()
try
read_ta_network(d)
load_ta_network(d)
info("Network '$d' is OK.")
@info "Network '$d' is OK."
catch e
warn("Network '$d' is not usable.")
@show e
@warn "Network '$d' is not usable."
end
end
end
Expand Down Expand Up @@ -76,16 +77,18 @@ function read_ta_summary(network_data_file)

n = open(network_data_file, "r")

search_sc(s,c) = something(findfirst(isequal(c), s), 0)

while (line=readline(n)) != ""
if contains(line, "<NUMBER OF ZONES>")
number_of_zones = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<NUMBER OF NODES>")
number_of_nodes = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<FIRST THRU NODE>")
first_thru_node = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<NUMBER OF LINKS>")
number_of_links = parse(Int, line[ search(line, '>')+1 : end ] )
elseif contains(line, "<END OF METADATA>")
if occursin("<NUMBER OF ZONES>", line)
number_of_zones = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<NUMBER OF NODES>", line)
number_of_nodes = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<FIRST THRU NODE>", line)
first_thru_node = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<NUMBER OF LINKS>", line)
number_of_links = parse(Int, line[ search_sc(line, '>')+1 : end ] )
elseif occursin("<END OF METADATA>", line)
break
end
end
Expand Down

0 comments on commit 68d17d1

Please sign in to comment.