def main(): if len(sys.argv) < 4: print('usage: {} <graph-file> <stable-file> <popular-file>'.format(sys.argv[0])) else: import graph_parser, matching_stats gfile, sfile, pfile = sys.argv[1], sys.argv[2], sys.argv[3] G = graph_parser.read_graph(gfile) #M_stable = stable_matching_hospital_residents(G) #print(G, M_stable, sep='\n') M_stable = stable_matching_hospital_residents(graph.copy_graph(G)) M_popular = popular_matching_hospital_residents(graph.copy_graph(G)) matching_stats.print_matching(G, M_stable, sfile) matching_stats.print_matching(G, M_popular, pfile)
def main(): if len(sys.argv) < 4: print('usage: {} <graph-file> <stable-file> <popular-file>'.format( sys.argv[0])) else: import graph_parser, matching_stats gfile, sfile, pfile = sys.argv[1], sys.argv[2], sys.argv[3] G = graph_parser.read_graph(gfile) #M_stable = stable_matching_hospital_residents(G) #print(G, M_stable, sep='\n') M_stable = stable_matching_hospital_residents(graph.copy_graph(G)) M_popular = popular_matching_hospital_residents(graph.copy_graph(G)) matching_stats.print_matching(G, M_stable, sfile) matching_stats.print_matching(G, M_popular, pfile)
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 test_g3(self): """ I = {A : Y,X,Z ; B : X,Y,Z ; C : X,Y,Z ; X : A,B,C ; Y : B,A,C ; Z : A,B,C ; } """ G = make_graph( [('A', ['Y', 'X', 'Z']), ('B', ['X', 'Y', 'Z']), ('C', ['X', 'Y', 'Z'])], [('X', ['A', 'B', 'C']), ('Y', ['B', 'A', 'C']), ('Z', ['A', 'B', 'C'])]) self.assertTrue( is_stable(G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
def test_g1(self): """ I = {a1 : b1 ; b1 : a1 ;} """ G = make_graph([('a1', ['b1'])], [('b1', ['a1'])]) self.assertTrue( is_stable( G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
def test_g1(self): """ I = {a1 : b1 ; b1 : a1 ;} """ G = make_graph( [('a1', ['b1'])], [('b1', ['a1'])]) self.assertTrue( is_stable(G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
def test_g1(self): """ I = {a1 : b1 ; b1 : a1 ;} M = {(a1, b1)} """ G = make_graph([('a1', ['b1'])], [('b1', ['a1'])]) M = matched_pairs_to_dict([('a1', 'b1')]) M_kavita = matching_algos.popular_matching_man_woman( graph.copy_graph(G)) self.assertEqual(M, M_kavita)
def test_g4(self): """ I = {m1 : w2,w4,w1,w3 ; m2 : w3,w1,w4,w2 ; m3 : w2,w3,w1,w4 ; m4 : w4,w1,w3,w2 ; w1 : m2,m1,m4,m3 ; w2 : m4,m3,m1,m2 ; w3 : m1,m4,m3,m2 ; w4 : m2,m1,m4,m3 ;} """ G = make_graph( [('m1', ['w2', 'w4', 'w1', 'w3']), ('m2', ['w3', 'w1', 'w4', 'w2']), ('m3', ['w2', 'w3', 'w1', 'w4']), ('m4', ['w4', 'w1', 'w3', 'w2'])], [('w1', ['m2', 'm1', 'm4', 'm3']), ('w2', ['m4', 'm3', 'm1', 'm2']), ('w3', ['m1', 'm4', 'm3', 'm2']), ('w4', ['m2', 'm1', 'm4', 'm3'])]) self.assertTrue( is_stable(G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
def test_g1(self): """ I = {a1 : b1 ; b1 : a1 ;} M = {(a1, b1)} """ G = make_graph( [('a1', ['b1'])], [('b1', ['a1'])]) M = matched_pairs_to_dict([('a1', 'b1')]) M_kavita = matching_algos.popular_matching_man_woman(graph.copy_graph(G)) self.assertEqual(M, M_kavita)
def feasibility_check(G): G1 = graph.copy_graph(G) #G = mahadian_k_model_generator_hospital_residents(n1, n2, k, max_capacity) for h in G.B: if graph.lower_quota(G, h) == 0: G1.B.remove(h) for r in G.E[h]: G1.E[r].remove(h) del G1.E[h] del G1.capacities[h] else: G1.capacities[h] = (graph.lower_quota(G, h), graph.lower_quota(G, h)) #print("---------- G1-----------\n") #print(G1) #print("---------- G-----------\n") #print(G) #print("-------------max_card_mat----------\n") Max_M = matching_algos.max_card_hospital_residents(graph.copy_graph(G1)) #print(graph.to_easy_format(G1, Max_M)) if check_on_max_card_matching(G1, Max_M): M = matching_algos.stable_matching_hospital_residents( graph.copy_graph(G)) #print("-------------stable_mat----------\n") #print(graph.to_easy_format(G, M)) for h in G.B: if h not in M: if graph.lower_quota(G, h) > 0: return True else: if len(M[h]) < graph.lower_quota(G, h): return True return False else: return False
def test_g3(self): """ I = {A : Y,X,Z ; B : X,Y,Z ; C : X,Y,Z ; X : A,B,C ; Y : B,A,C ; Z : A,B,C ; } """ G = make_graph([('A', ['Y', 'X', 'Z']), ('B', ['X', 'Y', 'Z']), ('C', ['X', 'Y', 'Z'])], [('X', ['A', 'B', 'C']), ('Y', ['B', 'A', 'C']), ('Z', ['A', 'B', 'C'])]) self.assertTrue( is_stable( G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
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 feasibility_check(G): G1 = graph.copy_graph(G) #G = mahadian_k_model_generator_hospital_residents(n1, n2, k, max_capacity) for h in G.B: if graph.lower_quota(G, h)==0: G1.B.remove(h) for r in G.E[h]: G1.E[r].remove(h) del G1.E[h] del G1.capacities[h] else: G1.capacities[h] = (graph.lower_quota(G, h), graph.lower_quota(G, h)) #print("---------- G1-----------\n") #print(G1) #print("---------- G-----------\n") #print(G) #print("-------------max_card_mat----------\n") Max_M = matching_algos.max_card_hospital_residents(graph.copy_graph(G1)) #print(graph.to_easy_format(G1, Max_M)) if check_on_max_card_matching(G1, Max_M): M = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) #print("-------------stable_mat----------\n") #print(graph.to_easy_format(G, M)) for h in G.B: if h not in M: if graph.lower_quota(G, h)>0: return True else: if len(M[h])<graph.lower_quota(G,h): return True return False else: return False
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 test_g4(self): """ I = {m1 : w2,w4,w1,w3 ; m2 : w3,w1,w4,w2 ; m3 : w2,w3,w1,w4 ; m4 : w4,w1,w3,w2 ; w1 : m2,m1,m4,m3 ; w2 : m4,m3,m1,m2 ; w3 : m1,m4,m3,m2 ; w4 : m2,m1,m4,m3 ;} """ G = make_graph([('m1', ['w2', 'w4', 'w1', 'w3']), ('m2', ['w3', 'w1', 'w4', 'w2']), ('m3', ['w2', 'w3', 'w1', 'w4']), ('m4', ['w4', 'w1', 'w3', 'w2'])], [('w1', ['m2', 'm1', 'm4', 'm3']), ('w2', ['m4', 'm3', 'm1', 'm2']), ('w3', ['m1', 'm4', 'm3', 'm2']), ('w4', ['m2', 'm1', 'm4', 'm3'])]) self.assertTrue( is_stable( G, matching_algos.stable_matching_man_woman(graph.copy_graph(G))))
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 main(): G = graph_parser.read_graph(sys.argv[1]) G1 = hreduction(graph.copy_graph(G)) print(graph.graph_to_UTF8_string(G1))