def solve_clustering_algo(manager_stop,config):
    """
    Solve the problem using the clustering algo designed
    :param manager_stop: manager stops considered
    :param config: the config associated
    :return:
    """
    object_clustering = main_clustering_algo.main_clustering_algo(manager_stop,config)
    object_clustering.create_relevant_clusters(read_cluster=False)
    cluster_selected = object_clustering.solve()
    print("Number of clusters ", len(cluster_selected))
    tot_vehi = 0
    predicted= 0
    acc = 0
    for clus_id in tqdm(cluster_selected, desc='Routing in clustering benchmark'):
        clus = object_clustering.manager_cluster[clus_id]
        predicted += clus.prediction

        router = cvrptw_routing_solver.RoutingSolverCVRPTW(clus,config)
        num_vehicle,tot_distance,list_routes = router.solve_parse_routing()
        tot_vehi += num_vehicle

        if clus.prediction == num_vehicle:
            acc +=1
    print("Benchmark clustering algo", tot_vehi, " vehicles for predicted ", predicted, " accuracy ", acc/len(cluster_selected))

    return predicted,len(cluster_selected),acc/len(cluster_selected), tot_vehi,"Clustering_algo"
def route_clusters(dict_cluster_stop, manager_stop,dict_leaf_label,config_object):
    """
    Rout the obtained cluster to obtain the final solution
    :param dict_cluster_stop: a dict[leaf_id] = list_stops
    :param manager_stop: a manager stop
    :return: a dict of list of routes for each cluster
    """
    accuracy = 0
    tot_num_vehicles = 0
    total_distance = 0
    dict_routes = {}
    for cluster_id in dict_cluster_stop.keys():
        cluster_manager_stop = stops_manager_cvrptw.StopsManagerCVRPTW.from_sublist(dict_cluster_stop[cluster_id],manager_stop)
        routing_solver = cvrptw_routing_solver.RoutingSolverCVRPTW(cluster_manager_stop,config_object)
        num_vehicle,distance,list_routes = routing_solver.solve_parse_routing()
        dict_routes[cluster_id] = list_routes
        tot_num_vehicles += num_vehicle
        total_distance += distance

        # check accuracy
        if num_vehicle == dict_leaf_label[cluster_id]:
            accuracy += 1

    accuracy = accuracy/len(dict_cluster_stop)
    print(" We have an accuracy of ", accuracy, " a total number of vehicles ", tot_num_vehicles, " and a total distance ",total_distance)
def solve_whole_local_solver(manager_stop,config):
    """
    Solve the problem as a whole, for the allocated time
    :param manager_stop: the manager stop as a whole
    :param config: the associated config
    :return: tot_vehi
    """
    tree_finder = find_best_tree.FindBestTree()
    dict_feature = derivingFeaturesCreatedDataset.DerivingFeaturesCreatedDataset(man_ref)
    tree, dict_cst_computed, dict_leaf_label_computed, dict_proba_computed, dict_dispersion = tree_finder.find_cst_best_tree()
    list_cst = [cst for l in dict_cst_computed.values() for cst in l]
    list_features = [cst.get_feature_name(dict_feature) for cst in list_cst]
    list_features=list(set(list_features))
    router = cvrptw_routing_solver.RoutingSolverCVRPTW(manager_stop,config,time_routing=3600)
    num_vehicle,tot_distance,list_routes = router.solve_parse_routing()

    number_vehicle_predicted = 0
    acc = 0
    for route in list_routes:
        new_manager = stops_manager_cvrptw.StopsManagerCVRPTW.from_sublist(route,manager_stop)
        new_cluster = cluster.Cluster.from_manager_stops(manager_stop=new_manager,
                                                         guid='clu_0',
                                                        tree=tree,
                                                         dict_cst= dict_cst_computed,
                                                         list_useful_features=list_features,
                                                         dict_disp=dict_dispersion)
        print('For the route we have a prediction of ', new_cluster.prediction)
        number_vehicle_predicted += new_cluster.prediction
        if new_cluster.prediction == 1:
            acc += 1


    print('Benchmark local solver ', num_vehicle, ' for prediction according to tree ', number_vehicle_predicted, ' and accuracy ',acc/len(list_routes))
    return number_vehicle_predicted,len(list_routes),acc/len(list_routes), num_vehicle,"LocalSolver"
    def check_accuracy(self,list_clu):
        """
        Check the clusters accuracy...
        :return:
        """
        accuracy = 0
        num_total_vehicle = 0
        total_dist = 0

        for clus_id in list_clu:
            clus = self.manager_cluster[clus_id]
            routing_solver = cvrptw_routing_solver.RoutingSolverCVRPTW(clus,self.config)
            num_vehicle,distance,list_routes = routing_solver.solve_parse_routing()

            total_dist += distance
            num_total_vehicle += num_vehicle

            # check accuracy
            if num_vehicle == clus.prediction:
                print("accurate ",clus.prediction,clus.expected_prediction, " vs ", num_vehicle, " for ",clus.guid)
                accuracy += 1
            else:
                print("error ",clus.prediction,clus.expected_prediction, " vs ", num_vehicle, " for ",clus.guid)

        accuracy = accuracy/len(list_clu)
        print(" We have an accuracy of ", accuracy, " for total number vehu ", num_total_vehicle, " nad distance ",total_dist)
Esempio n. 5
0
def solve_clustering_algo(manager_stop, config, list_running_time):
    """
    Solve the problem using the clustering algo designed
    :param manager_stop: manager stops considered
    :param config: the config associated
    :param list_running_time: the list of test time to run local solver
    :return:
    """
    object_clustering = main_clustering_algo.main_clustering_algo(
        manager_stop, config)
    object_clustering.create_relevant_clusters(read_cluster=False)
    cluster_selected = object_clustering.solve()
    print("Number of clusters ", len(cluster_selected))

    data = []
    for ti in list_running_time:
        tot_vehi = 0
        predicted = 0
        acc = 0
        row = dict()
        for clus_id in tqdm(cluster_selected,
                            desc='Routing in clustering benchmark'):
            clus = object_clustering.manager_cluster[clus_id]
            predicted += clus.prediction

            router = cvrptw_routing_solver.RoutingSolverCVRPTW(clus,
                                                               config,
                                                               time_routing=ti)
            num_vehicle, tot_distance, list_routes = router.solve_parse_routing(
            )
            tot_vehi += num_vehicle

            if clus.prediction == num_vehicle:
                acc += 1

        row['nb_cluster'] = len(cluster_selected)
        row['indiv_time'] = ti
        row['accuracy'] = acc / len(cluster_selected)
        row['total_nb_veji'] = tot_vehi
        row['predicted_number'] = predicted
        for a in [2, 4, 6, 8]:
            q = math.ceil(len(cluster_selected) / a)
            row['parall_' + str(a)] = q * ti

        data.append(row)

    data = pd.DataFrame(data)
    data.to_csv(
        '/Users/jpoullet/Documents/MIT/Thesis/database/cvrptw/benchmark_running_time.csv',
        index=False)
Esempio n. 6
0
def solve_whole_local_solver(manager_stop, config):
    """
    Solve the problem as a whole, for the allocated time
    :param manager_stop: the manager stop as a whole
    :param config: the associated config
    :return: tot_vehi
    """
    router = cvrptw_routing_solver.RoutingSolverCVRPTW(manager_stop,
                                                       config,
                                                       time_routing=1000)
    num_vehicle, tot_distance, list_routes = router.solve_parse_routing()

    print('Benchmark local solver ', num_vehicle)
    return num_vehicle, 'LocalSolver'
Esempio n. 7
0
    def check_accuracy(self, list_clu):
        """
        Check the clusters accuracy...
        :return:
        """
        accuracy = 0
        num_total_vehicle = 0
        total_dist = 0
        prediction = 0
        for clus_id in tqdm(list_clu,
                            desc='checking accuracy in main clustering algo'):
            clus = self.manager_cluster[clus_id]
            routing_solver = cvrptw_routing_solver.RoutingSolverCVRPTW(
                clus, self.config)
            num_vehicle, distance, list_routes = routing_solver.solve_parse_routing(
            )

            total_dist += distance
            num_total_vehicle += num_vehicle

            # check accuracy
            prediction += clus.prediction
            if num_vehicle == clus.prediction:
                clustering_logger.info("accurate " + str(clus.prediction) +
                                       ' ' + str(clus.expected_prediction) +
                                       " vs " + str(num_vehicle) + " for " +
                                       clus.guid +
                                       str(self.manager_cluster[clus_id].
                                           is_robust(self.threshold)))
                accuracy += 1
            else:
                clustering_logger.info("error " + str(clus.prediction) + ' ' +
                                       str(clus.expected_prediction) + " vs " +
                                       str(num_vehicle) + " for " + clus.guid +
                                       str(self.manager_cluster[clus_id].
                                           is_robust(self.threshold)))

        accuracy = accuracy / len(list_clu)
        clustering_logger.info("Number of clusters selected " +
                               str(len(list_clu)))
        clustering_logger.info(" We have an accuracy of " + str(accuracy) +
                               "for prediction" + str(prediction) +
                               " for total number vehu " +
                               str(num_total_vehicle) + " and distance " +
                               str(total_dist))

        return accuracy, num_total_vehicle
def get_solver_object(filename, manager_stop, config_cst):
    """
    Choose the right solver for the type of problem
    :param filename: the filename
    :param manager_stop: corresponding manager_stop
    :param config_cst: corresponding congig_cst
    :return:
    """
    if 'cvrptw' in filename:
        return cvrptw_routing_solver.RoutingSolverCVRPTW(
            manager_stop, config_cst)
    elif 'cvrp' in filename:
        return cvrp_routing_solver.RoutingSolverCVRP(manager_stop, config_cst)
    elif 'ups' in filename:
        return ups_routing_solver.RoutingSolverUPS(manager_stop, config_cst)
    else:
        raise customs_exception.FunctionalityNotImplemented
def solve_via_cluster(manager_stop,config,perce):
    """
    Route the whole manager sotp with respect to the config via naive Kmean
    :param manager_stop:
    :param config:
    :param perce: percentage of the vehicle's capacity.
    :return:
    """
    tree_finder = find_best_tree.FindBestTree()
    dict_feature = derivingFeaturesCreatedDataset.DerivingFeaturesCreatedDataset(man_ref)
    tree, dict_cst_computed, dict_leaf_label_computed, dict_proba_computed, dict_dispersion = tree_finder.find_cst_best_tree()
    list_cst = [cst for l in dict_cst_computed.values() for cst in l]
    list_features = [cst.get_feature_name(dict_feature) for cst in list_cst]
    list_features=list(set(list_features))

    config.cluster_method = "NAIVE"
    clustering_object = perform_clustering.StopClustering()
    list_cluster = clustering_object.perform_clustering(manager_stop,config,perce)

    print("Number of clusters ", len(list_cluster))
    tot_vehi = 0
    predicted= 0
    acc = 0

    for clu in tqdm(list_cluster,desc='Routing in naive benchmark'):
        man_stop_clus = stops_manager_cvrptw.StopsManagerCVRPTW.from_sublist(list_stops=[stop.guid for stop in clu.list_stops],reference_manager_stop=manager_stop)
        cluster_object = cluster.Cluster.from_manager_stops(man_stop_clus,'clu_0',tree,dict_cst_computed,list_features,dict_dispersion)
        predicted += cluster_object.prediction
        current_manager_stops = stops_manager_cvrptw.StopsManagerCVRPTW.init_from_cluster(clu)
        current_manager_stops.set_depot(manager_stop.depot)

        router = cvrptw_routing_solver.RoutingSolverCVRPTW(current_manager_stops,config)
        num_vehicle,tot_distance,list_routes = router.solve_parse_routing()
        tot_vehi += num_vehicle

        if num_vehicle ==  cluster_object.prediction:
            acc +=1

    print("Benchmark naive Kmean", tot_vehi,"for predicted ", predicted, " accuracy ", acc/len(list_cluster))
    return predicted,len(list_cluster),acc/len(list_cluster), tot_vehi,"Kmean"
    def _add_new_data_points(self, list_manager_stop, stats=False):
        """
        Solve the manager stops in the given list and ouput the results in a file
        :param list_manager_stop: a list of manager stop object
        :param stats: indicates if we stock the stats or not
        :return: the list of vehicles, in the same order as the manager stop
        """
        list_vehi = []
        tot_vehi = 0
        acc = 0
        for manager_stop in tqdm(list_manager_stop,
                                 desc='solving the true routing'):
            solver_obj = cvrptw_routing_solver.RoutingSolverCVRPTW(
                manager_stop, self.config_ref)
            num_vehicle, tot_distance, list_routes = solver_obj.solve_routing()
            tot_vehi += num_vehicle
            list_vehi.append(num_vehicle)

            if isinstance(manager_stop, cluster.Cluster):
                if manager_stop.prediction == num_vehicle:
                    coordinator_logger.info(
                        'Accurate ' + str(manager_stop.prediction) + ' ' +
                        str(manager_stop.expected_prediction) + ' vs ' +
                        str(num_vehicle) + ' for cluster ' + manager_stop.guid)
                    acc += 1
                else:
                    coordinator_logger.info(
                        'Error ' + str(manager_stop.prediction) + ' ' +
                        str(manager_stop.expected_prediction) + ' vs ' +
                        str(num_vehicle) + ' for cluster ' + manager_stop.guid)

        if stats:
            self.obtained_number_vehi.append(tot_vehi)
            self.list_acc_mip.append(acc / len(list_manager_stop))

        coordinator_logger.info('Overall accuracy ' +
                                str(acc / len(list_manager_stop)))

        return list_vehi