def maximize_pairwise_exchange(cycles, vertices, dirname, edges, ilp): """[summary] Args: cycles ([type]): [description] vertices ([type]): [description] dirname ([type]): [description] edges ([type]): [description] ilp ([type]): [description] Returns: [type]: [description] """ coef = [] precomputation = CyclePrecomputation() for cycle in cycles: x = precomputation.calculate_backarc(cycle, edges) # + len(cycle) x = x + len(cycle) print(cycle, " ", x, " ", len(cycle), " ", x - len(cycle)) coef.append(x) solution_values = optimize_length(cycles, vertices, dirname, coef, ilp) print(solution_values) return solution_values
def run_results(solution_values, cycles, altruistic, edges, cycleswt): transplants = 0 altruistic_involved = 0 paired_transplants = 0 two_ways = 0 three_ways = 0 three_ways_embedded = 0 effective_pairwise = 0 weight = 0 precomputation = CyclePrecomputation() for i in range(0, len(solution_values)): if solution_values[i] != 0: transplants = transplants + len(cycles[i]) if cycles[i][0] == cycles[i][-1]: transplants = transplants - 1 paired_transplants = paired_transplants + len(cycles[i]) - 1 if len(cycles[i]) == 3: two_ways = two_ways + 1 effective_pairwise = effective_pairwise + 1 elif len(cycles[i]) == 4: three_ways = three_ways + 1 effective_pairwise = effective_pairwise + len( cycles[i]) - 1 three_ways_embedded = ( three_ways_embedded + precomputation.calculate_backarc(cycles[i], edges)) else: effective_pairwise = effective_pairwise + len( cycles[i]) - 1 else: altruistic_involved = altruistic_involved + 1 weight = weight + cycleswt[tuple(cycles[i])] return ( transplants + len(altruistic) - altruistic_involved, paired_transplants, len(altruistic) - altruistic_involved, two_ways, three_ways, three_ways_embedded, effective_pairwise, weight, )
def maximize_pairwise_exchange(cycles, vertices, dirname, edges, ilp): coef = [] precomputation = CyclePrecomputation() for cycle in cycles: x = precomputation.calculate_backarc(cycle, edges) #+ len(cycle) x = x + len(cycle) print(cycle, " ", x, " ", len(cycle), " ", x - len(cycle)) coef.append(x) solution_values = optimize_length(cycles, vertices, dirname, coef, ilp) print(solution_values) return solution_values
def pipeline(file_name, option, max_cycle_length, max_chain_length, ilp): # x = datetime.datetime.now() x = "result" path = os.getcwd() + "/" dirName = path + str(x) # if not os.path.exists(dirName): # os.mkdir(dirName) # else: # print("Directory " , dirName , " already exists") # sys.exit(-1) # delete the directory if exists if os.path.exists(dirName): shutil.rmtree(dirName) # create an empty directory os.mkdir(dirName) destination = dirName shutil.copy(file_name, destination) graph = dirName + "/" + "graph" f = open(graph, "w") json_convert = DataConvert(json_file) precomputation = CyclePrecomputation() names, edges, weight, altruistic_donors = json_convert.convert_altruistic() f.write(str(len(names)) + " " + str(len(altruistic_donors)) + " " + str(len(edges))) for name in names: f.write(str(name) + "\n") for altruistic_donor in altruistic_donors: f.write(str(altruistic_donor) + "\n") for edge in edges: f.write("[" + edge[0] + " " + edge[1] + "] " + str(weight[tuple(edge)]) + "\n") f.close() # option = int(input("Choose option \n1.Maximize the number of effective pairwise exchange \n2.Maximize the total number of transplants\n3.Maximize the total number of backarcs\n4.Maximize the total weight\n5.Minimize the number of 3-way exchange\n6.Maximize the number of pairwise exchange ")) # Basically maximizing the total number of cycles which does not involve altruistic donor start = time.time() if option == 1: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges ) end = time.time() print(cyclesAndChains) cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_pairwise_exchange( cyclesAndChains, names, dirName, edges, ilp ) transplants = print_solution( 1, "Maximize the number of effective pairwise exchange", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName, ) fillexcel( len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the number of effective pairwise exchange", ) # Maximize the number of patients that get a kidney. This will involve altruistic donors as well elif option == 2: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges ) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_transplants( cyclesAndChains, names + altruistic_donors, dirName, ilp ) transplants = print_solution( 1, "Maximize the total number of transplants", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName, ) fillexcel( len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the total number of transplants", ) # No of backarcs in 3-way exchange should be maximized(a 3-way exchange can contain more than one backarc.) elif option == 3: print("Yet to be done") sys.exit() # Maximize the total weight calculated by summing over all cycles and chains elif option == 4: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges ) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_weight( cyclesAndChains, names + altruistic_donors, cycleandchain_wt, dirName, ilp ) transplants = print_solution( 1, "Maximize the total weight", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName, ) fillexcel( len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the total weight", ) elif option == 5: print("Yet to be done") sys.exit() # Maximize the number of pairwise exchange plus unused altruists:- elif option == 6: cyclesAndChains = precomputation.findCyclesAndChains( names, 2, 1, altruistic_donors, edges ) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_transplants( cyclesAndChains, names + altruistic_donors, dirName, ilp ) transplants = print_solution( 1, "Maximize pairwise exchange", 2, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName, ) fillexcel( len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the number of pairwise exchange", ) print("Runtime of the program is ", end - start)
def run_results(solution_values, cycles, altruistic, edges, cycleswt): """[summary] Args: solution_values ([type]): [description] cycles ([type]): [description] altruistic ([type]): [description] edges ([type]): [description] cycleswt ([type]): [description] Returns: [type]: [description] """ transplants = 0 altruistic_involved = 0 paired_transplants = 0 two_ways = 0 three_ways = 0 three_ways_embedded = 0 effective_pairwise = 0 weight = 0 precomputation = CyclePrecomputation() for i in range(len(solution_values)): if solution_values[i] != 0: transplants += len(cycles[i]) if cycles[i][0] == cycles[i][-1]: transplants -= 1 paired_transplants = paired_transplants + len(cycles[i]) - 1 if len(cycles[i]) == 3: two_ways += 1 effective_pairwise += 1 elif len(cycles[i]) == 4: three_ways += 1 effective_pairwise = effective_pairwise + len( cycles[i]) - 1 three_ways_embedded += precomputation.calculate_backarc( cycles[i], edges) else: effective_pairwise = effective_pairwise + len( cycles[i]) - 1 else: altruistic_involved += 1 weight += cycleswt[tuple(cycles[i])] return ( transplants + len(altruistic) - altruistic_involved, paired_transplants, len(altruistic) - altruistic_involved, two_ways, three_ways, three_ways_embedded, effective_pairwise, weight, )
def pipeline(n): # with open('kidney.csv', 'a', newline='') as file: # writer = csv.DictWriter(file, fieldnames=fnames) # writer.writeheader() if len(sys.argv) != 2: print("Error :Please specify number of nodes") sys.exit(-1) file_name = sys.argv[1] x = datetime.datetime.now() dirName = str(x) if not os.path.exists(dirName): os.mkdir(dirName) else: print("Directory ", dirName, " already exists") sys.exit(-1) destination = dirName shutil.copy(file_name, destination) graph = dirName + '/' + "graph" f = open(graph, "w") json_convert = DataConvert() precomputation = CyclePrecomputation() names, edges, weight, altruistic_donors = json_convert.convert_altruistic( file_name) f.write( str(len(names)) + " " + str(len(altruistic_donors)) + " " + str(len(edges))) for name in names: f.write(str(name) + "\n") for altruistic_donor in altruistic_donors: f.write(str(altruistic_donor) + "\n") for edge in edges: f.write("[" + edge[0] + " " + edge[1] + "] " + str(weight[tuple(edge)]) + "\n") f.close() max_cycle_length = int(input("Maximum Cycle size")) max_chain_length = int(input("Altruistic Chain Length")) option = int( input( "Choose option \n1.Maximize the number of effective pairwise exchange \n2.Maximize the total number of transplants\n3.Maximize the total number of backarcs\n4.Maximize the total weight\n5.Minimize the number of 3-way exchange\n6.Maximize the number of pairwise exchange" )) #Basically maximizing the total number of cycles which does not involve altruistic donor start = time.time() if option == 1: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges) end = time.time() print(cyclesAndChains) cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_pairwise_exchange(cyclesAndChains, names, dirName, edges) transplants = print_solution( 1, "Maximize the number of effective pairwise exchange", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName) fillexcel(len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the number of effective pairwise exchange") #Maximize the number of patients that get a kidney. This will involve altruistic donors as well if option == 2: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_transplants(cyclesAndChains, names + altruistic_donors, dirName) transplants = print_solution( 1, "Maximize the total number of transplants", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName) fillexcel(len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the total number of transplants") #No of backarcs in 3-way exchange should be maximized(a 3-way exchange can contain more than one backarc.) if option == 3: print("Yet to be done") #Maximize the total weight calculated by summing over all cycles and chains if option == 4: cyclesAndChains = precomputation.findCyclesAndChains( names, max_cycle_length, max_chain_length, altruistic_donors, edges) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_weight(cyclesAndChains, names + altruistic_donors, cycleandchain_wt, dirName) transplants = print_solution(1, "Maximize the total weight", max_cycle_length, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName) fillexcel(len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the total weight") if option == 5: print("Yet to be done") #Maximize the number of pairwise exchange plus unused altruists:- if option == 6: cyclesAndChains = precomputation.findCyclesAndChains( names, 2, 1, altruistic_donors, edges) end = time.time() cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight) solution_values = maximize_total_transplants(cyclesAndChains, names + altruistic_donors, dirName) transplants = print_solution(1, "Maximize pairwise exchange", 2, solution_values, cyclesAndChains, altruistic_donors, edges, cycleandchain_wt, names, dirName) fillexcel(len(names), len(altruistic_donors), max_cycle_length, max_chain_length, transplants, "Maximize the number of pairwise exchange") print("Runtime of the program is ", end - start)