def test_100_3_greedy(self):
        result = simulation("./testroutes/test100-3/", GreedyPlatooning())
        self.assertAlmostEqual(result["f_relat_before_convex"],
                               0.029016718588,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_after_convex"],
                               0.0342242315794,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_spont_plat"],
                               0.00154438410258,
                               delta=10**-10)

        self.assertAlmostEqual(result["f_total_before_convex"],
                               11996.2976474,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_after_convex"],
                               11931.9599013,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_default"],
                               12354.7932051,
                               delta=10**-5)
        self.assertEqual(
            str(result["leaders"]),
            "{0: -2, 1: -2, 2: 41, 3: -2, 4: -2, 5: 17, 6: -2, 7: 22, 8: 33, 9: -1, 10: 28, 11: -2, 12: 53, 13: -2, 14: 27, 15: 64, 16: 73, 17: -1, 18: -1, 19: 88, 20: -2, 21: -2, 22: -1, 23: -2, 24: -2, 25: -2, 26: -2, 27: -1, 28: -1, 29: -2, 30: -1, 31: -2, 32: 28, 33: -1, 34: -2, 35: -2, 36: -1, 37: -2, 38: -2, 39: 81, 40: -2, 41: -1, 42: -1, 43: -1, 44: -1, 45: 43, 46: 41, 47: 56, 48: -2, 49: -2, 50: -2, 51: -2, 52: -2, 53: -1, 54: -1, 55: 22, 56: -1, 57: 22, 58: 33, 59: 43, 60: -2, 61: 80, 62: -2, 63: -1, 64: -1, 65: 44, 66: 42, 67: -2, 68: -2, 69: 81, 70: 64, 71: -1, 72: 54, 73: -1, 74: 9, 75: -2, 76: 63, 77: 71, 78: 99, 79: 81, 80: -1, 81: -1, 82: -2, 83: 18, 84: 93, 85: -2, 86: -2, 87: 53, 88: -1, 89: 18, 90: -2, 91: 36, 92: -2, 93: -1, 94: 54, 95: 30, 96: -2, 97: -2, 98: 9, 99: -1}"
        )
        self.assertEqual(
            str(result["size_stats"]),
            "{1: 33817541.353592508, 2: 13300820.579831602, 3: 5195430.8873951668, 4: 250453.40064170401}"
        )
        pass
    def test_100_1_greedy(self):
        result = simulation("./testroutes/test100-1/", GreedyPlatooning())
        self.assertAlmostEqual(result["f_relat_before_convex"],
                               0.0311919806273,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_after_convex"],
                               0.0387018044227,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_spont_plat"],
                               0.00570467709984,
                               delta=10**-10)

        self.assertAlmostEqual(result["f_total_before_convex"],
                               11537.9635517,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_after_convex"],
                               11448.5257358,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_default"],
                               11909.4426563,
                               delta=10**-5)
        self.assertEqual(
            str(result["leaders"]),
            "{0: -2, 1: -2, 2: 65, 3: 55, 4: -2, 5: -1, 6: -2, 7: 55, 8: -2, 9: -2, 10: -2, 11: 97, 12: -2, 13: 40, 14: 72, 15: -1, 16: 15, 17: -1, 18: 92, 19: 65, 20: -2, 21: -1, 22: 65, 23: 17, 24: -2, 25: 76, 26: -2, 27: 36, 28: -2, 29: -2, 30: -2, 31: -1, 32: 97, 33: -2, 34: 99, 35: 91, 36: -1, 37: -2, 38: -1, 39: -2, 40: -1, 41: 17, 42: 95, 43: 79, 44: 5, 45: 38, 46: -2, 47: -2, 48: 76, 49: 91, 50: -2, 51: -2, 52: 21, 53: 38, 54: -2, 55: -1, 56: -2, 57: 97, 58: -2, 59: 62, 60: -2, 61: -2, 62: -1, 63: 40, 64: 76, 65: -1, 66: -2, 67: 76, 68: -2, 69: 40, 70: -2, 71: -2, 72: -1, 73: -2, 74: -2, 75: 82, 76: -1, 77: -2, 78: -2, 79: -1, 80: 31, 81: -2, 82: -1, 83: -2, 84: 21, 85: -2, 86: 55, 87: 79, 88: -2, 89: -2, 90: -2, 91: -1, 92: -1, 93: -2, 94: 82, 95: -1, 96: -2, 97: -1, 98: -2, 99: -1}"
        )
        self.assertEqual(
            str(result["size_stats"]),
            "{1: 32604779.089786265, 2: 8496948.2763619889, 3: 7821469.8874862678, 4: 1746276.9944754937}"
        )
        pass
    def test_100_2_greedy(self):
        result = simulation("./testroutes/test100-2/", GreedyPlatooning())
        self.assertAlmostEqual(result["f_relat_before_convex"],
                               0.0328894925827,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_after_convex"],
                               0.0413100727447,
                               delta=10**-10)
        self.assertAlmostEqual(result["f_relat_spont_plat"],
                               0.00231776588647,
                               delta=10**-10)

        self.assertAlmostEqual(result["f_total_before_convex"],
                               12892.5716355,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_after_convex"],
                               12780.3166945,
                               delta=10**-5)
        self.assertAlmostEqual(result["f_total_default"],
                               13331.0221909,
                               delta=10**-5)
        self.assertEqual(
            str(result["leaders"]),
            "{0: 81, 1: -2, 2: -2, 3: 85, 4: 95, 5: -2, 6: 21, 7: -2, 8: -2, 9: 23, 10: -2, 11: -2, 12: -2, 13: 44, 14: -2, 15: -1, 16: 44, 17: 15, 18: -2, 19: -2, 20: 55, 21: -1, 22: 95, 23: -1, 24: -2, 25: -2, 26: -1, 27: 52, 28: 26, 29: 32, 30: 99, 31: -1, 32: -1, 33: -2, 34: -2, 35: -2, 36: -1, 37: 15, 38: -2, 39: -2, 40: 36, 41: 21, 42: -1, 43: -2, 44: -1, 45: -2, 46: -1, 47: -2, 48: -2, 49: 52, 50: -2, 51: 26, 52: -1, 53: -2, 54: 42, 55: -1, 56: -2, 57: 68, 58: -2, 59: -2, 60: -2, 61: -1, 62: 81, 63: -2, 64: -2, 65: -2, 66: 31, 67: 68, 68: -1, 69: -2, 70: 55, 71: -2, 72: 46, 73: -2, 74: -1, 75: -2, 76: -2, 77: -2, 78: 74, 79: 86, 80: 55, 81: -1, 82: 55, 83: -2, 84: 44, 85: -1, 86: -1, 87: -2, 88: 74, 89: -2, 90: 92, 91: 61, 92: -1, 93: 74, 94: -2, 95: -1, 96: -2, 97: 15, 98: 52, 99: -1}"
        )
        self.assertEqual(
            str(result["size_stats"]),
            "{1: 35559741.018573508, 2: 10601985.881495101, 3: 7742976.8253671452, 4: 2640255.3675283473, 5: 172714.86536590659}"
        )
        pass
def plot(K):
    # %%%%%%%%%%%% Generate Routes and start times / deadlines
    print 'retrieving the routes'
    path_data_sets = rc.generate_routes(K)

    # %%%%%% build coordination graph
    print 'building coordination graph'
    default_plans = pp.get_default_plans(path_data_sets)

    G_p = pp.build_graph(path_data_sets, default_plans)

    N_f_gr, N_l_gr, leaders_gr, counter_gr = GreedyPlatooning().clustering(G_p)
    # create adjecancy matrix

    adj_matrix = np.ones((K, K, 3))
    Klist = range(K)
    Klist.sort()
    for i in G_p:
        for j in G_p[i]:
            ii = Klist.index(i)
            jj = Klist.index(j)
            if G_p[i][j] > 0:
                if leaders_gr[i] == j:
                    adj_matrix[ii, jj, :] = np.array([1., 0., 0.])
                else:
                    adj_matrix[ii, jj, :] = np.array([0., 0., 0.])

    plt.figure()
    plt.imshow(adj_matrix, interpolation="nearest")
    plt.show(block=False)

    return
def main():
    # Ks = [100, 200, 300, 500, 1000, 1500, 2000, 3000, 5000]
    Ks = [100]

    print sys.argv
    start = int(sys.argv[1])
    stop = int(sys.argv[2])

    for i in xrange(start, stop + 1):
        results = {}
        for K in Ks:
            results[K] = simulation("./testing/testroutes/test100-3/",
                                    GreedyPlatooning())
            print_simulation_result(results[K])
Example #6
0
def clustering_data(testset="./testing/testroutes/test400-%d/", nr=5):
    fuel_savings = []
    for method in [
            GreedyPlatooning(),
            SubModularityPlatooning(True),
            SubModularityPlatooning(False)
    ]:
        fuel_savings.append(
            average_fuel_savings(method,
                                 [testset % (x + 1) for x in range(nr)],
                                 interval=600))
        print fuel_savings
        email_settings.mail("*****@*****.**", "Partial results",
                            str(fuel_savings))
    return fuel_savings
Example #7
0
def horizon_data(testset="./testing/testroutes/test400-%d/", nr=5):
    total = []
    for method in [
            GreedyPlatooning()
    ]:  #, RandomPlatooning(0), SubModularityPlatooning(True), SubModularityPlatooning(False)]:
        fuel_savings = []
        for horizon in range(0, 7210, 300):
            print "%s: %d/7200" % (method, horizon)
            fuel_savings.append(
                average_fuel_savings(method,
                                     [testset % (x + 1) for x in range(nr)],
                                     horizon=horizon))
            print fuel_savings
            email_settings.mail("*****@*****.**", "Partial results",
                                str(fuel_savings))
        print fuel_savings
        total.append(fuel_savings)
    print total
    return total
Example #8
0
def interval_data():
    total = []
    for method in [
            GreedyPlatooning(),
            RandomPlatooning(0),
            SubModularityPlatooning(True),
            SubModularityPlatooning(False)
    ]:
        savings = []
        for interval in range(600, 14410, 600):
            print "%d/14400" % interval
            fuel_savings = average_fuel_savings(method, [
                './testing/testroutes/test400-1/',
                './testing/testroutes/test400-2/',
                './testing/testroutes/test400-3/',
                './testing/testroutes/test400-4/',
                './testing/testroutes/test400-5/'
            ],
                                                interval=interval)
            savings.append(fuel_savings)
            print fuel_savings
        total.append(savings)
    print total
    return total
 def test_100_4_greedy(self):
     result = dynamic_simulation(GreedyPlatooning(), folder='./testroutes/test100-4/')
     fuel_saving = sum([x.current_fuel_consumption() for x in result]) / sum([x.default_plan.fuel for x in result])
     print fuel_saving
     self.assertAlmostEqual(0.94887673735, fuel_saving, delta=10 ** -10)
Example #10
0
            "start_pos": {"i":0, "x":0},
            "t_s": 1000
        },
        1: {
            "arrival_dline": 150000,
            "path": np.array([1, 3, 4, 5]),
            "path_set": {1, 3, 4, 5},
            "path_weights": np.array([80000, 1000000, 157100]),
            "start_pos": {"i": 0, "x": 0},
            "t_s": 1500
        }
    }
    routes = get_routes(TEST_FOLDER)
    # route_info = get_route_info(TEST_FOLDER)

    result = dynamic_simulation(GreedyPlatooning(), folder=TEST_FOLDER)
    # result = simulation(TEST_FOLDER, GreedyPlatooning())
    # routes = {
    #     1: {"lat": 0, "lon": 500},
    #     2: {"lat": 0, "lon": 0},
    #     3: {"lat": 500, "lon": 0},
    #     4: {"lat": 5000, "lon": 0},
    #     5: {"lat": 5500, "lon": 500},
    #     6: {"lat": 5500, "lon": -500},
    # }

    start([result, routes])

    # route_data = {}
    # route_data["node_coords_lat"] = np.array([100, 200, 300])
    # route_data["node_coords_lon"] = np.array([100, 100, 200])
def one_simulation(K):
    # %%%%%%%%%%%% Generate Routes and start times / deadlines
    print 'retrieving the routes'
    path_data_sets = rc.generate_routes(K)

    default_plans = pp.get_default_plans(path_data_sets)

    G_p = pp.build_graph(path_data_sets, default_plans)

    # %%%%%% clustering
    print 'clustering'
    N_f, N_l, leaders, counter = GreedyPlatooning().clustering(G_p)

    # %%%%%% joint optimization for all clusters
    print 'starting convex optimization'
    assignments = [Truck(i, path_data_sets[i]) for i in path_data_sets]
    plans = pp.retrieve_adapted_plans(assignments, leaders, G_p)
    followers_dict = cv.get_followers(N_l, leaders)

    def plot_one_group(n_l):
        #  n_l = 0
        n_f = followers_dict[n_l]
        n_all = [n_l] + n_f

        xref = 400
        yref = 300

        XYLL = {n: transform_path(path_data_sets[n]) for n in n_all}

        all_x = np.array([])
        all_y = np.array([])
        all_lat = np.array([])
        all_lon = np.array([])
        for n in n_all:
            all_x = np.concatenate((all_x, XYLL[n][0]))
            all_y = np.concatenate((all_y, XYLL[n][1]))
            all_lat = np.concatenate((all_lat, XYLL[n][2]))
            all_lon = np.concatenate((all_lon, XYLL[n][3]))

            #  print '{},{}   {},{}'.format(all_lat[0]*180/np.pi,all_lon[0]*180/np.pi,all_lat[-1]*180/np.pi,all_lon[-1]*180/np.pi)

        # compute zoom factor
        x_w = all_x.max() - all_x.min()
        zoom_x = np.log2(xref / x_w)
        y_w = all_y.max() - all_y.min()
        zoom_y = np.log2(yref / y_w)
        max_zoom = min([zoom_x, zoom_y])
        zoom = np.floor(max_zoom)

        # zoom XYLL

        all_x_zoom = all_x * 2**zoom
        all_y_zoom = all_y * 2**zoom

        for n in n_all:
            XYLL[n][0] = (XYLL[n][0] - all_x.min()) * 2**zoom
            XYLL[n][0] += (xref -
                           (all_x_zoom.max() - all_x_zoom.min())) / 2  # center
            XYLL[n][1] = (XYLL[n][1] - all_y.min()) * 2**zoom
            XYLL[n][1] += (
                yref - (all_y_zoom.max() - all_y_zoom.min())) / 2 - 3  # center

        c_lat = (all_lat.max() + all_lat.min()) / 2. * 180 / np.pi
        c_lon = (all_lon.max() + all_lon.min()) / 2. * 180 / np.pi

        mapquest_url = 'open.mapquestapi.com'
        request = '/staticmap/v4/getmap?key=98P6M5uV2XIEeJO1KGAx0WXpOER1HA1X&size={},{}&zoom={}&center={:.6f},{:.6f}'.format(
            int(xref), int(yref), int(zoom), c_lat, c_lon)

        conn = httplib.HTTPConnection(mapquest_url)
        conn.request("GET", request)
        map_resp = conn.getresponse()
        data = map_resp.read()
        image = StringIO(data)
        from PIL import Image
        image.seek(0)
        map_image = Image.open(image)
        conn.close()
        map_im_np = np.array(map_image)

        # plotting
        plt.figure()
        plt.imshow(map_im_np)
        colors = [
            'b', 'g', 'r', 'c', 'm', 'b', 'g', 'r', 'c', 'm', 'b', 'g', 'r',
            'c', 'm', 'b', 'g', 'r', 'c', 'm', 'b', 'g', 'r', 'c', 'm'
        ]

        plt.plot(XYLL[n_l][0], XYLL[n_l][1], '-k', linewidth=2.)
        plt.plot(XYLL[n_l][0][0], XYLL[n_l][1][0], '*k', markersize=10.)
        for i, n in enumerate(n_f):
            plt.plot(XYLL[n][0], XYLL[n][1], '--' + colors[i], linewidth=2.)
            plt.plot(XYLL[n][0][0],
                     XYLL[n][1][0],
                     '*' + colors[i],
                     markersize=10.)

            start, stop = get_platoon_segment_inds(path_data_sets[n], plans[n])
            plt.plot(XYLL[n][0][start],
                     XYLL[n][1][start],
                     '^' + colors[i],
                     markersize=10.)
            plt.plot(XYLL[n][0][stop],
                     XYLL[n][1][stop],
                     'v' + colors[i],
                     markersize=10.)

        plt.xlim((0, xref))
        plt.ylim((0, yref))
        ax = plt.gca()
        ax.invert_yaxis()
        plt.show(block=False)

        plt.savefig(new_dir + '{}.png'.format(n_l))

    max_plot_num = 10
    new_dir = '.{}/'.format(time.time())
    os.mkdir(new_dir)
    for i, n_l in enumerate(list(N_l)):
        if i == max_plot_num:
            break
        try:
            plot_one_group(n_l)
        except Exception as e:
            print 'Could not plot for leader {}'.format(n_l)
    return