def process_stats(stats_file_name, G, matchings): """ prints the matchings and the stats generated :param stats_file_name: file to generate the stats to :param G: bipartite graph :param matchings: see documentation for collect_stats :return: None """ def avg(l): return sum(l) / len(l) def get_indices(M): return [G.E[a].index(M[a])+1 for a in G.A if a in M] # generate and print the matchings table = [['matching desc.', 'matching size', '# unstable pairs', 'min index', 'max index', 'avg index']] for matching in matchings: M = matching['algo'](graph.copy_graph(G)) # print(M) indices = get_indices(M) table.append([matching['desc'], matching_utils.matching_size(M), len(matching_utils.unstable_pairs(G, M)), min(indices), max(indices), avg(indices)]) print_matching(G, M, matching['file'](dir, iteration)) # print the stats corresponding to the matchings obtained with open(stats_file_name, encoding='utf-8', mode='w') as fout: print(tabulate(table, headers='firstrow', tablefmt='psql'), file=fout)
def print_matching_stats(G, M, filepath): size = matching_utils.matching_size(G, M) bpairs = matching_utils.unstable_pairs(G, M) bres = blocking_residents(G, bpairs) rank1 = rank_1_residents(G, M) M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) with open(filepath, mode='w', encoding='utf-8') as out: print('size: {}'.format(size), file=out) print('# blocking pair: {}'.format(len(bpairs)), file=out) print('# blocking residents: {}'.format(len(bres)), file=out) print('# residents matched to rank-1 partners: {}'.format(len(rank1)), file=out) print('total deficiency: {}'.format(sea.total_deficiency(G, M_s)), file=out)
def print_matching_stats(G, M, filepath): size = matching_utils.matching_size(G, M) bpairs = matching_utils.unstable_pairs(G, M) bres = blocking_residents(G, bpairs) rank1 = rank_1_residents(G, M) M_s = matching_algos.stable_matching_hospital_residents( graph.copy_graph(G)) with open(filepath, mode='w', encoding='utf-8') as out: print('size: {}'.format(size), file=out) print('# blocking pair: {}'.format(len(bpairs)), file=out) print('# blocking residents: {}'.format(len(bres)), file=out) print('# residents matched to rank-1 partners: {}'.format(len(rank1)), file=out) print('total deficiency: {}'.format(sea.total_deficiency(G, M_s)), file=out)
def generate_heuristic_tex(G, matchings, output_dir, stats_filename): """ print statistics for the hospital proposing heuristic as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() M_s = matching_algos.stable_matching_hospital_residents( graph.copy_graph(G)) with doc.create(Subsection('Size statistics')): with doc.create(Tabular('|c|c|c|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio', 'block-R', 'rank-1', 'deficiency')) for desc in matchings: M = matchings[desc] sig = signature(G, M) bp = matching_utils.unstable_pairs(G, M) msize = matching_utils.matching_size(G, M) table.add_hline() table.add_row(( desc, msize, len(bp), len(bp) / (m - msize), len(blocking_residents(bp)), sum_ranks(sig, (1, )), #sum_ranks(sig, (1, 2, 3)), total_deficiency(G, M_s))) table.add_hline() stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def generate_hr_tex(G, matchings, output_dir, stats_filename): """ print statistics for the resident proposing stable, max-cardinality popular, and popular amongst max-cardinality matchings as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() with doc.create(Subsection('general statistics')): with doc.create(Tabular('|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio')) for desc in matchings: M = matchings[desc] sig = signature(G, M) msize = matching_utils.matching_size(G, M) bp = matching_utils.unstable_pairs(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp)/(m - msize))) table.add_hline() # statistics w.r.t. set A stats_for_partition(G, matchings, doc) # statistics w.r.t. set B # stats_for_partition(G, matchings, doc, False) stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def generate_hr_tex(G, matchings, output_dir, stats_filename): """ print statistics for the resident proposing stable, max-cardinality popular, and popular amongst max-cardinality matchings as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() with doc.create(Subsection('general statistics')): with doc.create(Tabular('|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio')) for desc in matchings: M = matchings[desc] sig = signature(G, M) msize = matching_utils.matching_size(G, M) bp = matching_utils.unstable_pairs(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp) / (m - msize))) table.add_hline() # statistics w.r.t. set A stats_for_partition(G, matchings, doc) # statistics w.r.t. set B # stats_for_partition(G, matchings, doc, False) stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def generate_heuristic_tex(G, matchings, output_dir, stats_filename): """ print statistics for the hospital proposing heuristic as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) with doc.create(Subsection('Size statistics')): with doc.create(Tabular('|c|c|c|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio', 'block-R', 'rank-1', 'deficiency')) for desc in matchings: M = matchings[desc] sig = signature(G, M) bp = matching_utils.unstable_pairs(G, M) msize = matching_utils.matching_size(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp)/(m - msize), len(blocking_residents(bp)), sum_ranks(sig, (1,)), #sum_ranks(sig, (1, 2, 3)), total_deficiency(G, M_s))) table.add_hline() stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def is_stable(G, M): upairs = matching_utils.unstable_pairs(G, M) return len(upairs) == 0