Пример #1
0
def main():
    data_path = os.path.join("..", "data",
                             "generated_at_" + str(int(time.time())))
    outposts, vehicles, graph = utilities.generate_input(6, data_path)
    # data_path = os.path.join("..", "data", "dummy_data")
    # data_path = os.path.join("..", "data", "triangle_case")
    results_path = os.path.join(data_path, "results")
    os.makedirs(results_path, exist_ok=True)

    outposts, vehicles, graph = data_importer.import_all_data(data_path)
    starting_point = 0
    start_time = time.time()
    dwave_results = dwave_engine.calculate_routes(outposts, vehicles, graph,
                                                  starting_point)
    end_time = time.time()
    calculation_time = end_time - start_time
    print("DWave calculation time:", calculation_time)
    if dwave_results is not None:
        dwave_results.to_csv(os.path.join(results_path, "dwave_routes.csv"),
                             sep=";",
                             index_label="route_id")
        visualization.plot_solution(dwave_results,
                                    outposts,
                                    results_path,
                                    file_name="dwave_plot")

    starting_point = 0
    dummy_results = dummy_engine.calculate_routes(outposts, vehicles, graph,
                                                  starting_point)
    dummy_results.to_csv(os.path.join(results_path, "dummy_routes.csv"),
                         sep=";",
                         index_label="route_id")
    visualization.plot_solution(dummy_results,
                                outposts,
                                results_path,
                                file_name="dummy_plot")

    start_time = time.time()
    or_results = ortools_engine.calculate_routes(
        outposts,
        vehicles,
        graph,
        starting_point,
        calculation_time=calculation_time)
    end_time = time.time()
    calculation_time = end_time - start_time
    print("ORTools calculation time:", calculation_time)
    if or_results is not None:
        or_results.to_csv(os.path.join(results_path, "ortools_routes.csv"),
                          sep=";",
                          index_label="route_id")
        visualization.plot_solution(or_results,
                                    outposts,
                                    results_path,
                                    file_name="ortools_plot")
Пример #2
0
def benchmark_tsp(problem_source, num_tries, ortools_config, dwave_config):
    """Benchmark D-Wave vs ortools for given problems and configuration.

    :param problem_source: an interable with problems to solve
    :type problem source: iterable of Problem
    :param num_tries: how many times to try to solve given problem
    :type num_tries: int
    :param ortools_config: configuration for ortools. Currently only the key
     'calculation_time` is used, but can be extended should we need to add more
     parameters.
    :type ortools_config; mapping
    :param dwave_config: configuration used with D-Wave solver. Can contains any entries
     corresponding to keyword arguments used by DWaveEngine.solve method.
    :type dwave_config: mapping
    :returns: DataFrame with columns: problem_num, try_num, dwave_time, ortools_time,
     dwave_cost, ortools_cost. If any of engines failed to provide a solution fo any of the
     problem/try a np.nan is used instead of a corresponding cost.
    :rtype: :py:class:`pandas.DataFrame`
    """
    dwave_engine = DWaveEngine.default()
    results = []
    ortools_calculation_time = ortools_config.get('calculation_time', 5)
    for i, problem in enumerate(problem_source, 1):
        for j in range(1, num_tries + 1):
            print('Benchmarking problem {0}, try {1}.'.format(i, j))
            start = time()
            dwave_solution = dwave_engine.solve(problem, **dwave_config)
            dwave_time = time() - start

            start = time()
            ortools_solution = calculate_routes(
                outposts=problem.outposts,
                vehicles=problem.vehicles,
                graph=problem.graph,
                starting_point=problem.starting_point,
                calculation_time=ortools_calculation_time)
            ortools_time = time() - start
            record = Record(problem_num=i,
                            try_num=j,
                            dwave_time=dwave_time,
                            ortools_time=ortools_time,
                            dwave_cost=np.nan if dwave_solution is None else
                            dwave_solution.total_cost(),
                            ortools_cost=np.nan if ortools_solution is None
                            else ortools_solution['cost'].sum())
            results.append(record)
    return pd.DataFrame(results)
def benchmark_cvrp(problem_source, num_tries, ortools_config, dwave_config):
    """Benchmark D-Wave vs ortools for given problems and configuration.

    :param problem_source: an interable with problems to solve
    :type problem source: iterable of Problem
    :param num_tries: how many times to try to solve given problem
    :type num_tries: int
    :param ortools_config: configuration for ortools. Currently only the key
     'calculation_time` is used, but can be extended should we need to add more
     parameters.
    :type ortools_config; mapping
    :param dwave_config: configuration used with D-Wave solver. Can contains any entries
     corresponding to keyword arguments used by DWaveEngine.solve method.
    :type dwave_config: mapping
    :returns: DataFrame with columns: problem_num, try_num, dwave_time, ortools_time,
     dwave_cost, ortools_cost. If any of engines failed to provide a solution fo any of the
     problem/try a np.nan is used instead of a corresponding cost.
    :rtype: :py:class:`pandas.DataFrame`
    """
    dwave_engine = DWaveEngine.default()
    qbsolv_engine = QBSolvEngine.default()
    results = []
    ortools_calculation_time = ortools_config.get('calculation_time', 5)
    for i, problem in enumerate(problem_source, 1):
        optimal_cost = utilities.compute_all_cvrp_solutions(problem.outposts, problem.vehicles, problem.graph)[0][1]
        for j in range(1, num_tries+1):
            print('Benchmarking problem {0}, try {1}.'.format(i, j))
            number_of_partitions = len(generate_partitions(problem.outposts, problem.vehicles))
            start = time()
            dwave_solution, number_of_samples, info = dwave_engine.solve(problem, **dwave_config)
            dwave_time = time() - start

            start = time()
            qbsolv_solution = qbsolv_engine.solve(problem, **dwave_config)
            qbsolv_time = time() - start

            if number_of_samples is not None:
                sample_percentage = number_of_samples / dwave_config['num_reads'] * 100
            else:
                sample_percentage = np.nan

            start = time()
            ortools_solution = calculate_routes(outposts=problem.outposts,
                                                vehicles=problem.vehicles,
                                                graph=problem.graph,
                                                starting_point=problem.starting_point,
                                                calculation_time=ortools_calculation_time)
            ortools_time = time() - start
            
            if ortools_solution['cost'].sum() < optimal_cost:
                ortools_solution = None
            record = Record(
                problem_num=i,
                try_num=j,
                number_of_partitions=number_of_partitions,
                dwave_time=dwave_time,
                dwave_qpu_time=info['timing']['total_qpu_time'],
                qbsolv_time=qbsolv_time,
                ortools_time=ortools_time,
                sample_percentage=sample_percentage,
                dwave_cost=np.nan if dwave_solution is None else dwave_solution.total_cost(),
                qbsolv_cost=np.nan if qbsolv_solution is None else qbsolv_solution.total_cost(),
                ortools_cost=np.nan if ortools_solution is None else ortools_solution['cost'].sum(),
                optimal_cost=optimal_cost)
            results.append(record)
    return pd.DataFrame(results)