# Sioux Falls network data
# http://www.bgu.ac.il/~bargera/tntp/

#Link travel time = free flow time * ( 1 + B * (flow/capacity)^Power ).
#Link generalized cost = Link travel time + toll_factor * toll + distance_factor * distance

# Traffic Assignment Data structure
type TA_Data
    network_name::String

    number_of_zones::Int64
    number_of_nodes::Int64
    first_thru_node::Int64
    number_of_links::Int64

    start_node::Array
    end_node::Array
    capacity::Array
    link_length::Array
    free_flow_time::Array
    B::Array
    power::Array
    speed_limit::Array
    toll::Array
    link_type::Array

    total_od_flow::Float64

    travel_demand::Array
    od_pairs::Array

    toll_factor::Float64
    distance_factor::Float64

    best_objective::Float64
end

function load_ta_network(network_name="Sioux Falls")

    toll_factor = 0
    distance_factor = 0

    if network_name == "Sioux Falls"
        network_data_file = "SiouxFalls_net.txt"
        trip_table_file = "SiouxFalls_trips.txt"
        best_objective = 0
    elseif network_name == "Sioux_simp"
        network_data_file = "SiouxFalls_net_simp.txt"
        trip_table_file = "SiouxFalls_trips_simp.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jan_AM"
        network_data_file = "East_Massachusetts_net_Jan_AM.txt"
        trip_table_file = "East_Massachusetts_trips_Jan_AM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jan_MD"
        network_data_file = "East_Massachusetts_net_Jan_MD.txt"
        trip_table_file = "East_Massachusetts_trips_Jan_MD.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jan_PM"
        network_data_file = "East_Massachusetts_net_Jan_PM.txt"
        trip_table_file = "East_Massachusetts_trips_Jan_PM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jan_NT"
        network_data_file = "East_Massachusetts_net_Jan_NT.txt"
        trip_table_file = "East_Massachusetts_trips_Jan_NT.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jan_weekend"
        network_data_file = "East_Massachusetts_net_Jan_weekend.txt"
        trip_table_file = "East_Massachusetts_trips_Jan_weekend.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Apr_AM"
        network_data_file = "East_Massachusetts_net_Apr_AM.txt"
        trip_table_file = "East_Massachusetts_trips_Apr_AM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Apr_MD"
        network_data_file = "East_Massachusetts_net_Apr_MD.txt"
        trip_table_file = "East_Massachusetts_trips_Apr_MD.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Apr_PM"
        network_data_file = "East_Massachusetts_net_Apr_PM.txt"
        trip_table_file = "East_Massachusetts_trips_Apr_PM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Apr_NT"
        network_data_file = "East_Massachusetts_net_Apr_NT.txt"
        trip_table_file = "East_Massachusetts_trips_Apr_NT.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Apr_weekend"
        network_data_file = "East_Massachusetts_net_Apr_weekend.txt"
        trip_table_file = "East_Massachusetts_trips_Apr_weekend.txt"
	best_objective = 0
    elseif network_name == "East_Massachusetts_Jul_AM"
        network_data_file = "East_Massachusetts_net_Jul_AM.txt"
        trip_table_file = "East_Massachusetts_trips_Jul_AM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jul_MD"
        network_data_file = "East_Massachusetts_net_Jul_MD.txt"
        trip_table_file = "East_Massachusetts_trips_Jul_MD.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jul_PM"
        network_data_file = "East_Massachusetts_net_Jul_PM.txt"
        trip_table_file = "East_Massachusetts_trips_Jul_PM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jul_NT"
        network_data_file = "East_Massachusetts_net_Jul_NT.txt"
        trip_table_file = "East_Massachusetts_trips_Jul_NT.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Jul_weekend"
        network_data_file = "East_Massachusetts_net_Jul_weekend.txt"
        trip_table_file = "East_Massachusetts_trips_Jul_weekend.txt"
	best_objective = 0
    elseif network_name == "East_Massachusetts_Oct_AM"
        network_data_file = "East_Massachusetts_net_Oct_AM.txt"
        trip_table_file = "East_Massachusetts_trips_Oct_AM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Oct_MD"
        network_data_file = "East_Massachusetts_net_Oct_MD.txt"
        trip_table_file = "East_Massachusetts_trips_Oct_MD.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Oct_PM"
        network_data_file = "East_Massachusetts_net_Oct_PM.txt"
        trip_table_file = "East_Massachusetts_trips_Oct_PM.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Oct_NT"
        network_data_file = "East_Massachusetts_net_Oct_NT.txt"
        trip_table_file = "East_Massachusetts_trips_Oct_NT.txt"
        best_objective = 0
    elseif network_name == "East_Massachusetts_Oct_weekend"
        network_data_file = "East_Massachusetts_net_Oct_weekend.txt"
        trip_table_file = "East_Massachusetts_trips_Oct_weekend.txt"
	best_objective = 0
    elseif network_name == "Sioux Falls simplified"
        network_data_file = "SiouxFalls_net_simplified.txt"
        trip_table_file = "SiouxFalls_trips_simplified.txt"
        best_objective = 0
    end


    network_data_file = joinpath("../data_traffic_assignment_uni-class", network_data_file)
    trip_table_file = joinpath("../data_traffic_assignment_uni-class", trip_table_file)




    ##################################################
    # Network Data
    ##################################################


    number_of_zones = 0
    number_of_links = 0
    number_of_nodes = 0
    first_thru_node = 0

    n = open(network_data_file, "r")

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

    @assert number_of_links > 0

    start_node = Array(Int, number_of_links)
    end_node = Array(Int, 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(Int, number_of_links)

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

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

            numbers = split(line)

            start_node[idx] = parseint(numbers[1])
            end_node[idx] = parseint(numbers[2])
            capacity[idx] = parsefloat(numbers[3])
            link_length[idx] = parsefloat(numbers[4])
            free_flow_time[idx] = parsefloat(numbers[5])
            B[idx] = parsefloat(numbers[6])
            power[idx] = parsefloat(numbers[7])
            speed_limit[idx] = parsefloat(numbers[8])
            toll[idx] = parsefloat(numbers[9])
            link_type[idx] = parsefloat(numbers[10])

            idx = idx + 1
        end
    end

    ##################################################
    # Trip Table
    ##################################################

    number_of_zones_trip = 0
    total_od_flow = 0

    f = open(trip_table_file, "r")

    while (line=readline(f)) != ""
        if contains(line, "<NUMBER OF ZONES>")
            number_of_zones_trip = parseint( line[ search(line, '>')+1 : end-1 ] )
        elseif contains(line, "<TOTAL OD FLOW>")
            total_od_flow = parsefloat( line[ search(line, '>')+1 : end-1 ] )
        elseif contains(line, "<END OF METADATA>")
            break
        end
    end

    @assert number_of_zones_trip == number_of_zones # Check if number_of_zone is same in both txt files

    travel_demand = zeros(number_of_zones, number_of_zones)
    od_pairs = Array((Int64,Int64),0)
    while (line=readline(f)) != ""
        if contains(line, "Origin")
            origin = parseint( split(line)[2] )
        elseif contains(line, ";")
            pairs = split(line, ";")
            for i=1:size(pairs)[1]
                if contains(pairs[i], ":")
                    pair = split(pairs[i], ":")
                    destination = parseint( strip(pair[1]) )
                    od_flow = parsefloat( strip(pair[2]) )
                    travel_demand[origin, destination] = od_flow
                    push!(od_pairs, (origin, destination))
                    # println("origin=$origin, destination=$destination, flow=$od_flow")
                end
            end
        end
    end

    # Preparing data to return
    ta_data = TA_Data(
        network_name,
        number_of_zones,
        number_of_nodes,
        first_thru_node,
        number_of_links,
        start_node,
        end_node,
        capacity,
        link_length,
        free_flow_time,
        B,
        power,
        speed_limit,
        toll,
        link_type,
        total_od_flow,
        travel_demand,
        od_pairs,
        toll_factor,
        distance_factor,
        best_objective)

    return ta_data

end # end of load_network function
