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 solve_routing_problem(filename):
    """
    Solve the routing problem on this filename
    :param filename: the name of the file
    :return: the solution of the large scale routing problem.
    """
    config_object = main_data_creation.read_config(filename)
    manager_stop = main_data_creation.read_stop(filename,date=None)

    best_tree_finder = find_best_tree.FindBestTree()
    learner, dict_constraint, dict_label, dict_proba = best_tree_finder.find_cst_best_tree()

    dict_leaf_stop,new_dict_label = find_cluster_created(dict_constraint, dict_label,manager_stop)

    route_clusters(dict_leaf_stop,manager_stop,new_dict_label,config_object)
    def __init__(self,manager_stop,config, dict_cst = None, dict_label = None, dict_proba = None):
        self.manager_stop = manager_stop
        self.config = config

        # Retrieve tree etc
        if dict_cst is None or dict_proba is None or dict_label is None:
            finder = find_best_tree.FindBestTree()
            _, self.dict_cst, self.dict_label, self.dict_proba,_ = finder.find_cst_best_tree()
        else:
            self.dict_cst = dict_cst
            self.dict_label = dict_label
            self.dict_proba = dict_proba

        # To be filled
        self.manager_cluster = manager_clusters.ManagerCluster()
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 __init__(self, list_filename, number_data_points):
        self.list_filename = list_filename
        self.tree_tuner = find_best_tree.FindBestTree()

        # To be updated at each iteration
        self.current_file = None
        self.manager_ref = None
        self.config_ref = None
        self._update_config_manager_file()

        # Params
        self.number_data_points = number_data_points
        self.iteration = 0

        # Stats
        self.list_acc_trees_train = []
        self.list_acc_trees_test = []
        self.predicted_expected_number_vehi = []
        self.predicted_nb_vehi = []
        self.list_acc_mip = []
        self.obtained_number_vehi = []
        self.list_number_instances = []
예제 #6
0
    def __init__(self,
                 manager_stops,
                 config,
                 tree=None,
                 dict_cst=None,
                 dict_dispersion=None):
        self.manager_stops = manager_stops
        self.config = config  # a config object
        if tree is None or dict_cst is None or dict_dispersion is None:
            finder = find_best_tree.FindBestTree()
            self.tree, self.dict_cst, dict_label, _, self.dict_dispersion = finder.find_cst_best_tree(
            )
        else:
            self.tree = tree  # tree object, from the interpretable ai interface.
            self.dict_cst = dict_cst  # a dict[index_leaf] = list of constraints
            self.dict_dispersion = dict_dispersion  # a dict[feature] = faro dispersion

        # To be filled
        self.manager_cluster = manager_clusters.ManagerCluster(
            manager_stops, self.tree, self.dict_cst, self.dict_dispersion)

        # Parameters hard coded
        dict_param = {
            600: (20, 25, 3, 65, 20),
            1000: (40, 30, 4, 70, 25),
            1500: (40, 35, 4, 80, 30),
            2000: (40, 35, 5, 85, 35),
            3000: (55, 40, 6, 90, 40),
            5000: (60, 45, 6, 100, 45),
            7500: (60, 45, 7, 100, 45),
            10000: (40, 45, 7, 100, 45)
        }
        self.nb_iter, self.max_clusters_improved, self.number_creation, self.number_considered_merge, self.number_treated_merge = dict_param[
            size_data.NUMBER_CUSTOMERS]
        self.threshold = 0.0

        # Heuristics
        self.cluster_operation = modify_clusters.modify_clusters(
            self.manager_cluster, self.manager_stops, self.tree, self.dict_cst,
            self.threshold)
        self.cluster_merger = merge_clusters.ClustersMerger(
            self.manager_cluster,
            self.manager_stops,
            self.tree,
            nb_considered=self.number_considered_merge,
            nb_treated=self.number_treated_merge)
        self.cluster_creation = create_clusters.CreateClusters(
            self.manager_cluster, self.manager_stops, self.config, self.tree,
            self.dict_cst)

        # tracking
        self.iteration = 0
        self.total_time_creation = 0
        self.total_time_merge = 0
        self.total_time_improv = 0

        # stats
        self.lp_value = []
        self.nb_total_clusters = []
        self.per_negative_rc = []
        self.avg_negative_rc = []
        self.avg_robustness_created = []
        self.accuracy = []
        self.real_nb_vehi = []
        self.predicted_nb_vehi = []