def test_two_nonterm(algo): test_data_path = LOCAL_CFPQ_DATA.joinpath('two_nonterm') base_algo: BaseProblem = algo() graph = Graph.from_txt(test_data_path.joinpath('Graphs/graph_1.txt')) grammar = cfg_from_txt(test_data_path.joinpath('Grammars/g.cfg')) base_algo.prepare(graph, grammar) result: ResultAlgo = base_algo.solve() assert result.matrix_S.nvals == 156
def test_two_nonterm(algo): test_data_path = LOCAL_CFPQ_DATA.joinpath('two_nonterm') ms_algo: MultipleSourceProblem = algo() graph = Graph.from_txt(test_data_path.joinpath('Graphs/graph_1.txt')) grammar = cfg_from_txt(test_data_path.joinpath('Grammars/g.cfg')) ms_algo.prepare(graph, grammar) result: ResultAlgo result, with_cache = ms_algo.solve([1]) assert result.matrix_S.nvals == 1 and with_cache.nvals == 1
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 ])
def test_two_nonterm(algo): test_data_path = LOCAL_CFPQ_DATA.joinpath('two_nonterm') singlepath_algo: SinglePathProblem = algo() graph = Graph.from_txt(test_data_path.joinpath('Graphs/graph_1.txt')) grammar = cfg_from_txt(test_data_path.joinpath('Grammars/g.cfg')) singlepath_algo.prepare(graph, grammar) result: ResultAlgo = singlepath_algo.solve() assert result.matrix_S.nvals == 156 paths = singlepath_algo.getPath(1, 1, "S") assert paths == 2
def test_two_nonterm(algo): test_data_path = LOCAL_CFPQ_DATA.joinpath('two_nonterm') allpath_algo: AllPathsProblem = algo() graph = Graph.from_txt(test_data_path.joinpath('Graphs/graph_1.txt')) grammar = cfg_from_txt(test_data_path.joinpath('Grammars/g.cfg')) allpath_algo.prepare(graph, grammar) result: ResultAlgo = allpath_algo.solve() assert result.matrix_S.nvals == 156 allpath_algo.prepare_for_exctract_paths() paths = allpath_algo.getPaths(1, 1, "S", 3) assert len(paths) == 1
def benchmark_index(algo_name, data, result_dir, rounds): """ Measurement function for finding paths between all pairs 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 @param rounds: number of measurement rounds @return: variance value for each round of measurements """ header_index = ['graph', 'grammar', 'time', 'count_S', 'variance'] variances = [] for graph in data: result_index_file_path = result_dir.joinpath( f'{graph.stem}-{algo_name.__name__}-index') 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) for grammar in data[graph]: algo = algo_name() algo.prepare(Graph.from_txt(graph), cfg_from_txt(grammar)) count_S = 0 times = [] for _ in tqdm(range(rounds), desc=f'{graph.stem}-{grammar.stem}'): algo.prepare_for_solve() start = time() res = algo.solve() finish = time() times.append(finish - start) count_S = res.matrix_S.nvals sample_mean = get_sample_mean(times) variances.append(get_variance(times, sample_mean)) csv_writer_index.writerow([ graph.stem, grammar.stem, sample_mean, count_S, get_variance(times, sample_mean) ]) return variances
def benchmark_single_path(algo_name, data, result_dir): """ Measurement function for extract single path @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_paths = ['graph', 'grammar', 'len_path', 'time'] for graph in data: result_paths_file_path = result_dir.joinpath( f'{graph.stem}-{algo_name.__name__}-singlepaths') append_header = False if not exists(result_paths_file_path): append_header = True result_csv = open(result_paths_file_path, mode='a', newline='\n') csv_writer_paths = csv.writer(result_csv, delimiter=',', quoting=csv.QUOTE_NONNUMERIC, escapechar=' ') if append_header: csv_writer_paths.writerow(header_paths) if not exists(result_paths_file_path): csv_writer_paths.writerow(header_paths) for grammar in data[graph]: algo = algo_name() algo.prepare(Graph.from_txt(graph), cfg_from_txt(grammar)) res = algo.solve() for elem in tqdm(res.matrix_S, desc=f'{graph.stem}-{grammar}-paths'): start = time() paths = algo.getPath(elem[0], elem[1], "S") finish = time() csv_writer_paths.writerow( [graph.stem, grammar.stem, paths, finish - start])
from src.problems.Base.algo.matrix_base.matrix_base import MatrixBaseAlgo from src.problems.MultipleSource.algo.matrix_ms.matrix_ms import MatrixMSBruteAlgo, MatrixMSOptAlgo from src.problems.MultipleSource.algo.tensor_ms.tensor_ms import TensorMSAlgo from src.problems.AllPaths.algo.tensor.tensor import TensorSimpleAlgo, TensorDynamicAlgo from src.problems.SinglePath.algo.matrix_single_path.matrix_single_path_index import MatrixSingleAlgo from src.graph.graph import Graph from cfpq_data import cfg_from_txt from src.problems.utils import ResultAlgo from pathlib import Path CASE = Path("test/data/binary_tree/") graph = Graph.from_txt(CASE.joinpath("Graphs/graph_1.txt")) grammar = cfg_from_txt(CASE.joinpath("Grammars/g.cfg")) algo = MatrixBaseAlgo() algo.prepare(graph, grammar) res: ResultAlgo = algo.solve() print(f'MatrixBaseAlgo: {res.matrix_S.nvals}') graph = Graph.from_txt(CASE.joinpath("Graphs/graph_1.txt")) grammar = cfg_from_txt(CASE.joinpath("Grammars/g.cfg")) algo = TensorSimpleAlgo() algo.prepare(graph, grammar) res: ResultAlgo = algo.solve() print(f'TensorSimpleAlgo: {res.matrix_S.nvals}') graph = Graph.from_txt(CASE.joinpath("Graphs/graph_1.txt")) grammar = cfg_from_txt(CASE.joinpath("Grammars/g.cfg"))