Пример #1
0
def benchmark_ms(algo_name, data, result_dir):
    """
    Measurement function for finding paths from set of vertices
    @param algo_name: concrete implementation of the algorithm
    @param data: dictionary in format {path to graph: list of paths to grammars}
    @param result_dir: directory for uploading results of measurement
    """
    header_index = ['graph', 'grammar', 'size_chunk', 'time', 'count_S']

    chunk_sizes = [1, 2, 4, 8, 16, 32, 50, 100, 500, 1000, 5000, 10000, None]

    for graph in data:
        result_index_file_path = result_dir.joinpath(
            f'{graph.stem}-{algo_name.__name__}-msindex')

        append_header = False
        if not exists(result_index_file_path):
            append_header = True

        result_csv = open(result_index_file_path, mode='a', newline='\n')
        csv_writer_index = csv.writer(result_csv,
                                      delimiter=',',
                                      quoting=csv.QUOTE_NONNUMERIC,
                                      escapechar=' ')

        if append_header:
            csv_writer_index.writerow(header_index)

        if not exists(result_index_file_path):
            csv_writer_index.writerow(header_index)

        g = LabelGraph.from_txt(graph)
        for grammar in data[graph]:
            algo = algo_name()
            algo.prepare(Graph.from_txt(graph), cfg_from_txt(grammar))
            for chunk_size in chunk_sizes:
                chunks = []
                if chunk_size is None:
                    chunks = g.chunkify(g.matrices_size)
                else:
                    chunks = g.chunkify(chunk_size)

                for chunk in tqdm(chunks, desc=f'{graph.stem}-{grammar.stem}'):
                    algo.clear_src(
                    )  # Attention (TODO): remove this line if you want to cache the result !
                    start = time()
                    res = algo.solve(chunk)
                    finish = time()

                    csv_writer_index.writerow([
                        graph.stem, grammar.stem, chunk_size, finish - start,
                        res.matrix_S.nvals
                    ])
Пример #2
0
def test_case_1_graph_1(algo):
    # (0)-[a]->(1)-[a]->(2)-[b]->(3)-[b]->(4)
    # S -> a S b | a b

    test_case_1_path = LOCAL_CFPQ_DATA.joinpath('test_case_1')

    graph = LabelGraph.from_txt(
        test_case_1_path.joinpath('Matrices/graph_1.txt'))
    grammar = CnfGrammar.from_cnf(
        test_case_1_path.joinpath('Grammars/grammar.cnf'))
    single_source: SingleSourceSolver = algo(graph, grammar)

    m, _ = single_source.solve([1])
    assert vecbool_to_list(m[1]) == [3]

    m, _ = single_source.solve([0])
    assert vecbool_to_list(m[0]) == [4]
Пример #3
0
def test_single_source_benchmark_total(graph, grammar, algo, chunk_size,
                                       benchmark):
    g = LabelGraph.from_txt(graph)
    gr = CnfGrammar.from_cnf(grammar)
    chunks = g.chunkify(g.matrices_size if chunk_size is None else chunk_size)

    if chunk_size is not None and chunk_size > g.matrices_size:
        return

    def run_suite():
        a = algo(g, gr)
        for chunk in tqdm(
                chunks,
                desc=
                f'{get_file_name(graph)}-{get_file_name(grammar)}-{algo.__name__}-{chunk_size}'
        ):
            a.solve(chunk)

    benchmark.pedantic(run_suite, rounds=5, iterations=1, warmup_rounds=0)
Пример #4
0
def check_single_source_per_chunk(graph,
                                  grammar,
                                  algo,
                                  chunk_count=None,
                                  verbose=True):
    g = LabelGraph.from_txt(graph)
    gr = CnfGrammar.from_cnf(grammar)
    a = algo(g, gr)

    base_algo = MatrixBaseAlgo(graph[:-4], grammar[:-4])
    m = base_algo.solve()

    if chunk_count is None:
        chunk_count = g.matrices_size
    chunk_size = max(g.matrices_size // chunk_count, 1)
    chunks = g.chunkify(chunk_size)
    for chunk in tqdm(chunks,
                      desc=get_file_name(graph)) if verbose else chunks:
        m1, _ = a.solve(chunk)
        assert m1.extract_matrix(chunk).iseq(m.extract_matrix(chunk))
Пример #5
0
def test_case_1_graph_2(algo):
    #       (6)
    #      /   \
    #    (2)   (5)
    #   /  \   /  \
    # (0) (1) (3) (4)
    # Upstream edges labeled by "a", downstream by "b"
    # S -> a S b | a b

    test_case_1_path = LOCAL_CFPQ_DATA.joinpath('test_case_1')

    graph = LabelGraph.from_txt(
        test_case_1_path.joinpath('Matrices/graph_2.txt'))
    grammar = CnfGrammar.from_cnf(
        test_case_1_path.joinpath('Grammars/grammar.cnf'))
    single_source: SingleSourceSolver = algo(graph, grammar)

    m, _ = single_source.solve([0])
    assert vecbool_to_list(m[0]) == [0, 1, 3, 4]

    m, _ = single_source.solve([2])
    assert vecbool_to_list(m[2]) == [2, 5]
Пример #6
0
def test_single_source_benchmark_granularity(graph, grammar, algo, chunk_size,
                                             result_folder):
    g = LabelGraph.from_txt(graph)
    gr = CnfGrammar.from_cnf(grammar)
    a = algo(g, gr)

    gr_name = get_file_name(grammar)
    a_name = type(a).__name__

    if chunk_size is None:
        chunk_size = g.matrices_size
    chunks = g.chunkify(chunk_size)

    result_file = f'{get_file_name(graph)}.csv'
    result_file_path = os.path.join(result_folder, result_file)
    append_headers = False
    if not os.path.exists(result_file_path):
        append_headers = True

    with open(result_file_path, mode='a', newline='\n') as csv_file:
        csv_writer = csv.writer(csv_file,
                                delimiter=',',
                                quoting=csv.QUOTE_NONNUMERIC,
                                escapechar=' ')
        headers = ['grammar', 'algo', 'chunk_size', 'times']

        timer = SimpleTimer()
        times_of_chunks = []
        for chunk in chunks:
            timer.tic()
            a.solve(chunk)
            chunk_time = timer.toc()
            times_of_chunks.append(chunk_time)
        if append_headers:
            csv_writer.writerow(headers)
        csv_writer.writerow([gr_name, a_name, chunk_size, times_of_chunks])
Пример #7
0
from src.grammar.cnf_grammar import CnfGrammar
from src.graph.label_graph import LabelGraph
from src.utils.time_profiler import SimpleTimer
from src.algo.matrix_base import matrix_base_algo

g = LabelGraph.from_txt('deps/CFPQ_Data/data/WorstCase/Matrices/worstcase_128.txt')
gr = CnfGrammar.from_cnf('deps/CFPQ_Data/data/WorstCase/Grammars/Brackets.cnf')

with SimpleTimer():
    m = matrix_base_algo(g, gr)
Пример #8
0
 def __init__(self, path_to_graph: Path, path_to_grammar: Path):
     super().__init__(path_to_graph, path_to_grammar)
     self.graph = LabelGraph.from_txt(str(path_to_graph) + ".txt")
     self.grammar = CnfGrammar.from_cnf(str(path_to_grammar) + ".cnf")