Ejemplo n.º 1
0
def test_evaluation():
    x = 1.6

    key = Config.API_KEY
    client = googlemaps.Client(key)

    # setup EleNa for evaluation purposes
    data_model = DataModel('Amherst, MA')
    graph = data_model.get_graph()
    routing = Routing(data_model)
    nodes = graph.nodes()

    # randomly sample nodes from this list of nodes
    p1 = random.sample(nodes, 1)[0]
    p2 = random.sample(nodes, 1)[0]
    from_node = (graph.nodes[p1]['y'], graph.nodes[p1]['x'])
    to_node = (graph.nodes[p2]['y'], graph.nodes[p2]['x'])
    # print(from_node, to_node)
    routing.set_start_end(from_node, to_node)
    routing.set_max_deviation(x)
    best_path_min, log_min = routing.get_shortest_path("minimize")
    best_path_max, log_max = routing.get_shortest_path("maximize")

    # get distance from Google Maps - Direction Service
    routes = client.directions(from_node, to_node, mode="walking")
    ground_truth = routes[0]["legs"][0]["distance"]["value"]

    assert ground_truth * x > log_min['optimal_path_dist']
    assert ground_truth * x > log_max['optimal_path_dist']
Ejemplo n.º 2
0
def compute_route(source, destination, per, task):
    """
     Returns the best route with the given constraints.

    Args:
        source: Coordinates of the starting position
        destination: Coordinates of the destination
        per: x percentage of the shortest path to be computed
        task: maximize or minimize

    Returns:
        JSON object containing information on the routes to be rendered

    """
    model_obj = DataModel('Amherst, MA')
    routing_obj = Routing(model_obj)

    routing_obj.set_start_end(source, destination)
    routing_obj.set_max_deviation(per)
    best_path, log = routing_obj.get_shortest_path(task)

    sampled_coords = []
    if len(best_path) > 20:
        skip = len(best_path) // 20 + 1
        i = 0
        while i < len(best_path) - 1:
            sampled_coords.append(best_path[i])
            i += skip
        sampled_coords.append(best_path[len(best_path) - 1])
    else:
        sampled_coords = best_path

    result = []
    for node in sampled_coords:
        result.append({
            "Lat": model_obj.get_graph().nodes[node]['y'],
            "Long": model_obj.get_graph().nodes[node]['x']
        })

    gmap_route = routing_obj.get_gmap_ground_truth(source, destination)
    print(gmap_route)

    return jsonify(waypoints=result,
                   elevation=round(log['optimal_path_gain'], 3),
                   distance=round(log['optimal_path_dist'], 3),
                   groundTruthDistance=round(log['shortest_path_dist'], 3),
                   groundTruthElevation=round(log['shortest_path_gain'], 3),
                   upperLimit=round(per * log['shortest_path_dist'], 3),
                   ground_truth=gmap_route)
Ejemplo n.º 3
0
    def __init__(self, x=1.5, city_name='Amherst, MA', n_tests=50):
        self.config = {}
        self.key = Config.API_KEY

        # get the API-key and set up the google maps client
        # specifically for direction service
        self.client = googlemaps.Client(self.key)
        self.x = x
        self.city_name = city_name

        # get the data model for evaluation
        self.data_model = DataModel()
        self.graph = self.data_model.get_graph()
        print(self.data_model.get_stats())
        self.num_tests = n_tests

        self.eval()
Ejemplo n.º 4
0
class Evaluation:
    def __init__(self, x=1.5, city_name='Amherst, MA', n_tests=50):
        self.config = {}
        self.key = Config.API_KEY

        # get the API-key and set up the google maps client
        # specifically for direction service
        self.client = googlemaps.Client(self.key)
        self.x = x
        self.city_name = city_name

        # get the data model for evaluation
        self.data_model = DataModel()
        self.graph = self.data_model.get_graph()
        print(self.data_model.get_stats())
        self.num_tests = n_tests

        self.eval()

    def load_config(self, loc):
        try:
            cfg_file = open(loc, "r")
            self.config = json.load(cfg_file)
        except OSError as err:
            print("Config File Read Error: {0}".format(err))
        else:
            cfg_file.close()

    def eval(self):
        # setup EleNa for evaluation purposes
        routing = Routing(self.data_model)
        nodes = self.graph.nodes()

        # randomly sample nodes from this list of nodes
        from_nodes = random.sample(nodes, self.num_tests)
        to_nodes = random.sample(nodes, self.num_tests)

        max_task_elev = []
        min_task_elev = []
        gt_elev = []
        max_task_dist_per = []
        min_task_dist_per = []

        for i in range(self.num_tests):
            # get results from EleNa
            # print(from_nodes[i], self.graph.nodes[from_nodes[i]]['y'])
            from_node = (self.graph.nodes[from_nodes[i]]['y'],
                         self.graph.nodes[from_nodes[i]]['x'])
            to_node = (self.graph.nodes[to_nodes[i]]['y'],
                       self.graph.nodes[to_nodes[i]]['x'])
            routing.set_start_end(from_node, to_node)
            routing.set_max_deviation(self.x)
            best_path_min, log_min = routing.get_shortest_path("minimize")
            best_path_max, log_max = routing.get_shortest_path("maximize")

            # get distance from Google MAps - Direction Service
            routes = self.client.directions(from_node, to_node, mode="walking")
            ground_truth = routes[0]["legs"][0]["distance"]["value"]
            print(json.dumps(routes, indent=4))

            if ground_truth * self.x < log_min['optimal_path_dist']:
                print(
                    "Elevation minimization task failed ground-truth verification for:"
                )
                self.dump_evalution_params()
                print("\t From: ", from_node)
                print("\t To: ", to_node)
                # sys.exit("Error in verification. Exiting!")

            if ground_truth * self.x < log_max['optimal_path_dist']:
                print(
                    "Elevation maximization task failed ground-truth verification for:"
                )
                self.dump_evalution_params()
                print("\t From: ", from_node)
                print("\t To: ", to_node)
                # sys.exit("Error in verification. Exiting!")

            max_task_elev.append(log_max['optimal_path_gain'])
            min_task_elev.append(log_min['optimal_path_gain'])
            gt_elev.append(log_min['shortest_path_gain'])

            max_task_dist_per.append(log_max['optimal_path_dist'] * 100 /
                                     ground_truth)
            min_task_dist_per.append(log_min['optimal_path_dist'] * 100 /
                                     ground_truth)

        plt.scatter([i for i in range(self.num_tests)],
                    max_task_elev,
                    color='blue',
                    label='EleNa - elevation from maximization task',
                    s=8)
        plt.scatter([i for i in range(self.num_tests)],
                    min_task_elev,
                    color='green',
                    label='EleNa - elevation from minimization task',
                    s=8)
        plt.scatter([i for i in range(self.num_tests)],
                    gt_elev,
                    color='red',
                    label='Ground truth elevation',
                    s=8)
        plt.vlines([i for i in range(self.num_tests)],
                   ymin=0,
                   ymax=200,
                   color='black',
                   linestyle="--",
                   linewidth=1)
        plt.xticks([])
        plt.ylabel("Elevation (in metres)")
        plt.legend()
        plt.title(label="EleNa - evaluating elevation w.r.t. ground truth")
        plt.tight_layout()
        # plt.show()
        plt.savefig('1.png')
        plt.close()

        plt.scatter([i for i in range(self.num_tests)],
                    max_task_dist_per,
                    color='blue',
                    label='EleNa - maximization task',
                    s=8)
        plt.scatter([i for i in range(self.num_tests)],
                    min_task_dist_per,
                    color='green',
                    label='EleNa - minimization task',
                    s=8)
        plt.vlines([i for i in range(self.num_tests)],
                   ymin=0,
                   ymax=200,
                   color='black',
                   linestyle="--",
                   linewidth=1)
        plt.xticks([])
        plt.ylabel("Percentage w.r.t ground truth")
        plt.legend()
        plt.title(label="EleNa - distance (x%) w.r.t. ground truth")
        plt.tight_layout()
        # plt.show()
        plt.savefig('2.png')
        plt.close()

    def dump_evalution_params(self):
        print(self.city_name, self.num_tests, self.x)
Ejemplo n.º 5
0
def test_model_availability():
    dm = DataModel('Amherst, MA')
    assert dm.G != {}
Ejemplo n.º 6
0
def test_get_stats_function():
    dm = DataModel('Amherst, MA')
    num_nodes, num_edges = dm.get_stats()
    assert num_nodes >= 0 and num_edges >= 0
Ejemplo n.º 7
0
def test_load_config_function():
    dm = DataModel('Amherst, MA')
    dm.load_config('../app/config.json')
    assert dm.config != {}
Ejemplo n.º 8
0
def test_load_locations_metadata_function():
    dm = DataModel('Amherst, MA')
    dm.load_locations_metadata()
    assert dm.loaded_graphs != {}
Ejemplo n.º 9
0
def test_config_value():
    d = DataModel('Amherst, MA')
    assert d.config != {}