# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [Root]
#     language: python
#     name: Python [Root]
# ---

# + [markdown] nbpresent={"id": "aa548334-3712-4bc8-a30a-5a2a2260bd1c"} slideshow={"slide_type": "slide"}
# # BASTRA NOTEBOOK
# En este *notebook* vamos a trabajar con los datos generados por las simulaciones de BASTRA.
# Los datos se agruparán por escenarios.
#
# Author: <NAME>. sept.2016

# + [markdown] nbpresent={"id": "cc5f0cc1-0a51-4018-a01a-b9672f6f94a8"} slideshow={"slide_type": "slide"}
# ## Preliminares
# Antes de ejecutar los escenarios es preciso hacer las correspondienes importaciones de módulos de python para cálculo científico.

# + [markdown] nbpresent={"id": "3874ab56-9646-45e2-99ae-cb18c9603f38"} slideshow={"slide_type": "subslide"}
# ### Imports

# + nbpresent={"id": "69d6520c-8cb1-48f0-942b-f3c057f6d32e"} slideshow={"slide_type": "fragment"}
# %matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.lines as mlines

import sklearn as sk
import pandas as pd


# + [markdown] nbpresent={"id": "076a1b7c-343b-4bb3-a2cf-20d64498445f"} slideshow={"slide_type": "subslide"}
# ### Functions
# -

# #### Function str_vals_percent
# Given two values, print them with their percent difference

def str_vals_percent( n1, n2 ):
    return "[" + "{0:.2f}".format(n1) + "," + "{0:.2f}".format(n2) +"] "+ "{0:.2f}".format((n2-n1)*100/n1) + "%" 


# + [markdown] nbpresent={"id": "745baf99-dab3-4a9a-b114-af69d3ab1633"} slideshow={"slide_type": "fragment"}
# #### Function draw_histogram
# Plots an histogram based on the dataset.

# + nbpresent={"id": "52715953-f6ac-4bfd-bd28-e678c2e1b118"} slideshow={"slide_type": "skip"}
def draw_histogram( name, ds_raw ):
    ds = ds_raw.loc[ ds_raw['t_arrival_secs'] > 0 ]
    # ds_raw.head()
    travel_times = ds['t_traveltime_secs']

    cols = 80
    plt.figure(figsize=(18,8)) 
    y_vals, x_bins, patches = plt.hist(travel_times, cols, normed=0, facecolor='green', alpha=0.50)
    x_mean = np.mean(travel_times)
    x_median = np.median(travel_times)
    y_mean = np.mean(y_vals)
    y_median = np.median(y_vals)
    r_mean = np.mean(ds['route_path_num'])
    r_median = np.median(ds['route_path_num'])
    # l_mean = np.mean(ds['route_length'])
    # l_median = np.median(ds['route_length'])
    l_mean = 1
    l_median = 1

    # add a 'best fit' line
    y1_vals = np.append( y_vals, y_vals[cols-1] )

    x2_mean = [ x_mean for i in y1_vals ]
    x3_median = [ x_median for i in y1_vals ]
    y2_mean = [ y_mean for i in x_bins ]
    y3_median = [ y_median for i in x_bins ]

    # print( "X-VALS: ", len(x_bins), " ", x_bins.__repr__, )
    # print( "Y_VALS: ", len(y1_vals), " ", y1_vals.__repr__, )
    # print( "X-MEAN: ", len(x2_mean), " ", x2_mean.__repr__, )
    # print( "X-MEDIAN: ", len(x3_median), " ", x3_median.__repr__, )

    print(
        "Experiment:", name, "\n",
        "Trips Total:", ds_raw['id'].count(), "\n",
        "Trips Completed:", travel_times.count(), "\n",
        "   * Travel time (mean):", x_mean, "\n",
        "  * Travel time (median):", x_median, "\n",
        "  * Route lengths (mean):", l_mean, "\n",
        "  * Route lengths (median):", l_median, "\n",
        "  * Route paths (mean):", r_mean, "\n",
        "  * Route paths (median):", r_median, "\n",
        )

    line_estimation = plt.plot(x_bins, y1_vals, 'r-', label='Estimation', linewidth=2, color='darkgreen')
    line_mean = plt.plot(x2_mean, y1_vals, label='Mean', linestyle='-', linewidth=2, color='blue')
    line_median = plt.plot(x3_median, y1_vals, label='Median', linestyle='-', linewidth=2, color='red')

    plt.xlabel('Travel Time')
    plt.ylabel('Frecuency')
    # plt.title(r'$\mathrm{Histogram\ of\ Travel Times: }\ $' + name )
    plt.title("Travel Times Histogram: "+name )
    mean_line = mlines.Line2D([], [], color='blue', markersize=1, label='Mean')
    median_line = mlines.Line2D([], [], color='red', markersize=1, label='Median')
    plt.legend(handles=[median_line, mean_line], bbox_to_anchor=(1.15, 0.9))

    # plt.axis([travel_times.min(), travel_times.max()+1, 0, 0.03])
    plt.grid(True)

    # If plotting OUTSIDE the notebook, set interactive mode ON/OFF
    # plt.ioff()
    # plt.ion()

    plt.show()


# + [markdown] slideshow={"slide_type": "fragment"}
# #### Function draw_2histograms
# Plots the comparison between two overlapped histograms based on the datasets.

# + slideshow={"slide_type": "skip"}
def draw_2histograms( label1, experiment1, ds_raw1, label2, experiment2, ds_raw2 ):
    ds1 = ds_raw1.loc[ ds_raw1['t_arrival_secs'] > 0 ]
    ds2 = ds_raw2.loc[ ds_raw2['t_arrival_secs'] > 0 ]
    # ds_raw.head()
    travel_times1 = ds1['t_traveltime_secs']
    travel_times2 = ds2['t_traveltime_secs']
    travel_times = [ travel_times1, travel_times2 ]
    
    cols = 80
    fig, ax = plt.subplots(figsize=(18,8))

    plt.xlabel('Travel Time')
    plt.ylabel('Frecuency')
    plt.title("Travel Times Histogram: "+label1+" <--> "+label2 )
    
    # y_vals1, x_bins1, patches1 = ax.hist(travel_times1, histtype='bar', cols, normed=0, facecolor='green', alpha=0.30)
    # y_vals2, x_bins2, patches2 = ax.hist(travel_times2, histtype='bar', cols, normed=0, facecolor='blue', alpha=0.30)
    [y_vals1, y_vals2], x_bins, patches = ax.hist(travel_times, cols, normed=0, color=['green', 'blue'], alpha=0.30, histtype='bar')
    
    # first histogram ------------------------------------
    x_mean1   = np.mean(travel_times1)
    x_median1 = np.median(travel_times1)
    y_mean1   = np.mean(y_vals1)
    y_median1 = np.median(y_vals1)
    r_mean1   = np.mean(ds1['route_path_num'])
    r_median1 = np.median(ds1['route_path_num'])
    # l_mean1   = np.mean(ds1['route_length'])
    # l_median1 = np.median(ds1['route_length'])
    l_mean1   = 1
    l_median1 = 1

    # add a 'best fit' line
    y1_vals1 = np.append( y_vals1, y_vals1[cols-1] )

    x2_mean1   = [ x_mean1   for i in y1_vals1 ]
    x3_median1 = [ x_median1 for i in y1_vals1 ]
    y2_mean1   = [ y_mean1   for i in x_bins ]
    y3_median1 = [ y_median1 for i in x_bins ]

    # second histogram ------------------------------------
    x_mean2   = np.mean(travel_times2)
    x_median2 = np.median(travel_times2)
    y_mean2   = np.mean(y_vals2)
    y_median2 = np.median(y_vals2)
    r_mean2   = np.mean(ds2['route_path_num'])
    r_median2 = np.median(ds2['route_path_num'])
    # l_mean2   = np.mean(ds2['route_length'])
    # l_median2 = np.median(ds2['route_length'])
    l_mean2   = 1
    l_median2 = 1
    
    # add a 'best fit' line
    y1_vals2 = np.append( y_vals2, y_vals2[cols-1] )

    x2_mean2   = [ x_mean2   for i in y1_vals2 ]
    x3_median2 = [ x_median2 for i in y1_vals2 ]
    y2_mean2   = [ y_mean2   for i in x_bins ]
    y3_median2 = [ y_median2 for i in x_bins ]
    print(
        "Scenario: ", label1, " <--> ", label2, "\n",
        "Experiments: ", experiment1, " <--> ", experiment2, "\n",
        "Trips Total:", str_vals_percent(ds_raw1['id'].count(), ds_raw2['id'].count() ), "\n",
        "Trips Completed:", str_vals_percent(travel_times1.count(),travel_times2.count()), "\n",
        "  * Travel time (mean)    :",    str_vals_percent( x_mean1, x_mean2), "\n",
        "  * Travel time (median)  :",   str_vals_percent( x_median1, x_median2), "\n",
        # "  * Route lengths (mean)  :",   str_vals_percent( l_mean1, l_mean2), "\n",
        # "  * Route lengths (median):", str_vals_percent( l_median1, l_median2), "\n",
        "  * Route paths (mean)    :",     str_vals_percent( r_mean1, r_mean2), "\n",
        "  * Route paths (median)  :",   str_vals_percent( r_median1, r_median2), "\n",
        )

    line_estimation1 = ax.plot(x_bins, y1_vals1, 'r-', label='Estimation', linewidth=2, color='darkgreen')
    line_estimation2 = ax.plot(x_bins, y1_vals2, 'r-', label='Estimation', linewidth=2, color='darkblue')

    max_y = np.linspace(0, max( max(y_vals1), max(y_vals2)), num=len(x_bins))
    line_mean1 = ax.plot(x2_mean1, max_y, label='Mean', linestyle='--', linewidth=2, color='darkgreen')
    line_median1 = ax.plot(x3_median1, max_y, label='Median', linestyle='-', linewidth=2, color='darkgreen')

    line_mean2 = ax.plot(x2_mean2, y1_vals2, label='Mean', linestyle='--', linewidth=2, color='darkblue')
    line_median2 = ax.plot(x3_median2, max_y, label='Median', linestyle='-', linewidth=2, color='darkblue')

    mean_line1   = mlines.Line2D([], [], linestyle='--',color='darkgreen', markersize=1, label='1:Mean')
    median_line1 = mlines.Line2D([], [], linestyle='-', color='darkgreen', markersize=1, label='1:Median')
    mean_line2   = mlines.Line2D([], [], linestyle='--',color='darkblue',  markersize=1, label='2:Mean')
    median_line2 = mlines.Line2D([], [], linestyle='-', color='darkblue',  markersize=1, label='2:Median')
    ax.legend(handles=[median_line1, mean_line1, median_line2, mean_line2], bbox_to_anchor=(1.15, 0.9))

    # plt.axis([travel_times.min(), travel_times.max()+1, 0, 0.03])
    ax.grid(True)

    # If plotting OUTSIDE the notebook, set interactive mode ON/OFF
    # plt.ioff()
    # plt.ion()

    plt.show()


# + [markdown] nbpresent={"id": "66306679-3be0-4087-bed0-760c7d275993"} slideshow={"slide_type": "fragment"}
# #### Function process_scenario
# Uploads results from scenario execution and plots an histogram based on the dataset.
# * Select only those vehicles that finish the trip, i.e. have arrive time > 0.

# + nbpresent={"id": "e69b902d-06a7-43de-8d38-6545b079eeb8"} slideshow={"slide_type": "skip"}
def process_scenario( path, experiment ):
    datafile = path + "/experiments/" + experiment + ".csv"
    # print( "DATAFILE: ", datafile )
    # ds means "dataset"
    ds_raw = pd.read_csv(datafile)
    draw_histogram( experiment, ds_raw )


# + [markdown] slideshow={"slide_type": "fragment"}
# #### Function compare_scenarios
# Uploads results from 2 scenario executions and plotting the histogram curves based on the dataset.
# * Select only those vehicles that finish the trip, i.e. have arrive time > 0.

# + slideshow={"slide_type": "skip"}
def compare_scenarios( path, label1, experiment1, label2, experiment2 ):
    datafile1 = path + "/experiments/" + experiment1 + ".csv"
    datafile2 = path + "/experiments/" + experiment2 + ".csv"
    # print( "DATAFILE: ", datafile )
    # ds means "dataset"
    ds_raw1 = pd.read_csv(datafile1)
    ds_raw2 = pd.read_csv(datafile2)
    draw_2histograms( label1, experiment1, ds_raw1, label2, experiment2, ds_raw2 )


# -

compare_scenarios( "/Users/alvaro/Desktop/workspace/mutraff/uah-gist-mutraff-bastra",
                "GRID/BASTRA deactivated", "data_Grid_noBastra_160928_134826",
                "GRID/BASTRA activated", "data_Grid_160928_121606" )

# + [markdown] nbpresent={"id": "a622efcf-0563-4216-9d5a-f4b1f55d42a0"} slideshow={"slide_type": "slide"}
# ## Simulation: GRID basic
# Apply 2 weighted maps with penalty to routes, inside a network with GRID topology. Maps will be applied during all the simulation. Only 50% of the vehicles will use the system.
#
# **Scenario**
# * Name = "Grid"
# * Basic reference GRID scenario.
# * Execution date: 160926_124432
# * Process time: 781 sec
# * Platform: MAC-PRO OS-X Darwin
#
# **Simulation Time**
# * Begin = 0
# * End = 10000
#
# **SUMO Traffic**
# * Network:
#     * Grid style: 16x16
#     * File = "../scenes/Grid/grid.net.xml"
# * Vehicle Types:
#     * car (weight=4 -> 80%)
#     * motorcycle (weight=1 -> 20%)
# * Demand:
#     * Traffic background:
#         * Time: full simulation
#         * Volume: 3000 vehicles
#         * Distribution: O/D random (normal)
#     * Traffic flows:
#         * Time: full simulation
#         * 4 traffic flows end2end of grid.
#         * Volume: 100 vehicles
#
# **Bastra parameters**
# * Logit = 0,5
# * Use_blance = true
# * foresight_steps = 1 <-- Number of edges in the planned route to be scanned to consider jam.
# * foresight_tries = 3 <-- Number of trials to achieve a new route that avoids jam.
# * foresight_halting = 3 <-- Size of vehicles queued to understand that there is a jam. 
# * foresight_penalty = 50.0 <-- Weight-factor to apply to an edge in the rerouting algorithm, to force duarouter to over-weight this edge selection and thus avoid selection.
# * Commands:
#     * **Weighted maps**
#         * Apply during all the simulation
#         * 2 weighted maps, applying 50% to each vehicle type (2) with weights_factor=3 (x3) in the selected routes. So "edge weight = travel_time * weight_factor"
#             * file="../scenes/Grid/maps/map1.pen.map.xml" prob="0.50" tag="motorcycle"
#             * file="../scenes/Grid/maps/map2.pen.map.xml" prob="0.50" tag="motorcycle"
#             * file="../scenes/Grid/maps/map1.pen.map.xml" prob="0.50" tag="car"
#             * file="../scenes/Grid/maps/map2.pen.map.xml" prob="0.50" tag="car"

# + nbpresent={"id": "db3fb542-6dc1-44d6-8c08-adc17d7d044d"} slideshow={"slide_type": "subslide"}
process_scenario( "/Users/alvaro/Desktop/workspace/mutraff/uah-gist-mutraff-bastra", "data_Grid_160928_121606" )

# + [markdown] nbpresent={"id": "c7f66994-f059-4fa7-b87c-fcf87fcd4692"} slideshow={"slide_type": "slide"}
# ## Simulation: GRID basic (II)
# Same as before. Repetition to contrast results.
#
# Apply 2 weighted maps with penalty to routes, inside a network with GRID topology. Maps will be applied during all the simulation. Only 50% of the vehicles will use the system.
#
# **Scenario**
# * Name = "Grid"
# * Basic reference GRID scenario.
# * Execution date: 160928_130326
# * Process time: 535 sec
# * Platform: MAC-PRO OS-X Darwin
#
# **Simulation Time**
# * Begin = 0
# * End = 10000
#
# **SUMO Traffic**
# * Network:
#     * Grid style: 16x16
#     * File = "../scenes/Grid/grid.net.xml"
# * Vehicle Types:
#     * car (weight=4 -> 80%)
#     * motorcycle (weight=1 -> 20%)
# * Demand:
#     * Traffic background:
#         * Time: full simulation
#         * Volume: 3000 vehicles
#         * Distribution: O/D random (normal)
#     * Traffic flows:
#         * Time: full simulation
#         * 4 traffic flows end2end of grid.
#         * Volume: 100 vehicles
#
# **Bastra parameters**
# * Logit = 0,5
# * Use_blance = true
# * foresight_steps = 1 <-- Number of edges in the planned route to be scanned to consider jam.
# * foresight_tries = 3 <-- Number of trials to achieve a new route that avoids jam.
# * foresight_halting = 3 <-- Size of vehicles queued to understand that there is a jam. 
# * foresight_penalty = 50.0 <-- Weight-factor to apply to an edge in the rerouting algorithm, to force duarouter to over-weight this edge selection and thus avoid selection.
# * Commands:
#     * **Weighted maps**
#         * Apply during all the simulation
#         * 2 weighted maps, applying 50% to each vehicle type (2) with weights_factor=3 (x3) in the selected routes. So "edge weight = travel_time * weight_factor"
#             * file="../scenes/Grid/maps/map1.pen.map.xml" prob="0.50" tag="motorcycle"
#             * file="../scenes/Grid/maps/map2.pen.map.xml" prob="0.50" tag="motorcycle"
#             * file="../scenes/Grid/maps/map1.pen.map.xml" prob="0.50" tag="car"
#             * file="../scenes/Grid/maps/map2.pen.map.xml" prob="0.50" tag="car"

# + nbpresent={"id": "478def4f-6428-4d47-a089-295db8ebbb0f"} slideshow={"slide_type": "subslide"}
process_scenario( "/Users/alvaro/Desktop/workspace/mutraff/uah-gist-mutraff-bastra",
                 "data_Grid_160928_130326" )

# + [markdown] slideshow={"slide_type": "slide"}
# ## Simulation: GRID no Bastra (I)
# Execute the simulation without Bastra rerouting mechanism.
#
# **Scenario**
# * Name = "Grid"
# * Basic reference GRID scenario.
# * Execution date: 160928_235551
# * Process time: 621 sec
# * Platform: MAC-PRO OS-X Darwin
#
# **Simulation Time**
# * Begin = 0
# * End = 10000
#
# **SUMO Traffic**
# * Network:
#     * Grid style: 16x16
#     * File = "../scenes/Grid/grid.net.xml"
# * Vehicle Types:
#     * car (weight=4 -> 80%)
#     * motorcycle (weight=1 -> 20%)
# * Demand:
#     * Traffic background:
#         * Time: full simulation
#         * Volume: 3000 vehicles
#         * Distribution: O/D random (normal)
#     * Traffic flows:
#         * Time: full simulation
#         * 4 traffic flows end2end of grid.
#         * Volume: 100 vehicles
#
# **Bastra parameters**
# * Logit = 0
# * Use_balance = false
# * foresight_steps = 0 <-- Number of edges in the planned route to be scanned to consider jam.
# * foresight_tries = 0 <-- Number of trials to achieve a new route that avoids jam.
# * foresight_halting = 0 <-- Size of vehicles queued to understand that there is a jam. 
# * foresight_penalty = 1 <-- Weight-factor to apply to an edge in the rerouting algorithm, to force duarouter to over-weight this edge selection and thus avoid selection.
# * Commands:
#     * **Weighted maps**
#         * NONE

# + slideshow={"slide_type": "subslide"}
process_scenario( "/Users/alvaro/Desktop/workspace/mutraff/uah-gist-mutraff-bastra",
                 "data_Grid_noBastra_160928_235551" )

# + [markdown] slideshow={"slide_type": "slide"}
# ## Simulation: GRID no Bastra (II)
# Same as before. Repetition to contrast results.
#
# Execute the simulation without Bastra rerouting mechanism.
#
# **Scenario**
# * Name = "Grid"
# * Basic reference GRID scenario.
# * Execution date: 160928_134826
# * Process time: 4048 sec (second plane &)
# * Platform: MAC-PRO OS-X Darwin
#
# **Simulation Time**
# * Begin = 0
# * End = 10000
#
# **SUMO Traffic**
# * Network:
#     * Grid style: 16x16
#     * File = "../scenes/Grid/grid.net.xml"
# * Vehicle Types:
#     * car (weight=4 -> 80%)
#     * motorcycle (weight=1 -> 20%)
# * Demand:
#     * Traffic background:
#         * Time: full simulation
#         * Volume: 3000 vehicles
#         * Distribution: O/D random (normal)
#     * Traffic flows:
#         * Time: full simulation
#         * 4 traffic flows end2end of grid.
#         * Volume: 100 vehicles
#
# **Bastra parameters**
# * Logit = 0
# * Use_balance = false
# * foresight_steps = 0 <-- Number of edges in the planned route to be scanned to consider jam.
# * foresight_tries = 0 <-- Number of trials to achieve a new route that avoids jam.
# * foresight_halting = 0 <-- Size of vehicles queued to understand that there is a jam. 
# * foresight_penalty = 1 <-- Weight-factor to apply to an edge in the rerouting algorithm, to force duarouter to over-weight this edge selection and thus avoid selection.
# * Commands:
#     * **Weighted maps**
#         * NONE

# + slideshow={"slide_type": "subslide"}
process_scenario( "/Users/alvaro/Desktop/workspace/mutraff/uah-gist-mutraff-bastra",
                 "data_Grid_noBastra_160928_134826" )

# + nbpresent={"id": "08badeff-9fcf-4a72-882f-406306564576"}


StarcoderdataJupyterScriptsDedupFiltered