def find_all_teeth(F, G, handle): eligible_teeth = nx.Graph() for domino in G.nodes(): A = G.node[domino]['A'] B = G.node[domino]['B'] E_A_B = edges_cross(F, A, B) xE_A_B = sum(F[u][v]['weight'] for (u, v) in E_A_B) #print('find all teeth %.5f' % xE_A_B) # we only want that teeth if 1/2teethsurplus < x(E(A,B)), not even equality if 0.5 * G.node[domino]['surplus'] <= xE_A_B - epsilon: if (G.node[domino]['A'] <= handle and len(G.node[domino]['B'] & handle) == 0 or G.node[domino]['B'] <= handle and len(G.node[domino]['A'] & handle) == 0): eligible_teeth.add_node(domino, surplus=G.node[domino]['surplus'], vertices=G.node[domino]['vertices']) for u in eligible_teeth.nodes(): for v in eligible_teeth.nodes(): uteeth = eligible_teeth.node[u]['vertices'] vteeth = eligible_teeth.node[v]['vertices'] if (v != u) and not uteeth.isdisjoint(vteeth): eligible_teeth.add_edge(u, v) print('total number of dominoes that can be teeth of the comb is: %d' % len(list(eligible_teeth.nodes()))) return eligible_teeth
def find_all_teeth(F, G, handle): global newfile eligible_teeth=nx.Graph() for domino in G.nodes(): A=G.node[domino]['A'] B=G.node[domino]['B'] E_A_B=edges_cross(F,A,B) xE_A_B=sum(F[u][v]['weight'] for (u,v) in E_A_B) #print('find all teeth %.5f' % xE_A_B) # we only want that teeth if 1/2teethsurplus < x(E(A,B)), not even equality if 0.5*G.node[domino]['surplus'] <= xE_A_B -epsilon: if (A<= handle and len(B & handle) ==0) or (B<= handle and len(A& handle) ==0): eligible_teeth.add_node(domino) #, surplus = G.node[domino]['surplus'],vertices = G.node[domino]['A'].union(G.node[domino]['B'])) for u in eligible_teeth.nodes(): for v in eligible_teeth.nodes(): uteeth = G.node[u]['vertices'] vteeth= G.node[v]['vertices'] if (v!=u) and not uteeth.isdisjoint(vteeth): eligible_teeth.add_edge(u,v) newfile.write(' All eligible teeth for this handle are: \n') newfile.write(repr(set(eligible_teeth.nodes())) +' \n') newfile.write(' Total number of eligible teeth is: %d \n' % len(list(eligible_teeth.nodes()))) print('total number of dominoes that can be teeth of the comb is: %d' % len(list(eligible_teeth.nodes()))) return eligible_teeth
def find_handle(F, G, candidate_dom, total_surplus, vio_upper_bd): start = timer() LHS_list = [] for node in candidate_dom: A = G.node[node]['A'] B = G.node[node]['B'] from ABcut import edges_cross E_A_B = edges_cross(F, A, B) xE_A_B = sum(F[u][v]['weight'] for (u, v) in E_A_B) # print('x*(E(A,B))=%.5f' % xE_A_B) LHS = 0.5 * G.node[node]['surplus'] - xE_A_B LHS_list.append(LHS) #print('LHS= %.5f' % LHS) sumLHS = sum(x for x in LHS_list) all_patterns = list(product([0, 1], repeat=len(candidate_dom))) for pattern in all_patterns: shrink = shrink_dom_graph(F, G, candidate_dom, pattern) Fshrink = shrink[0] s = shrink[1] t = shrink[2] inHandle = shrink[3] notinHandle = shrink[4] xdeltaH, partitions = nx.minimum_cut(Fshrink, s, t, capacity='weight') print('xdeltaH: %.5f' % xdeltaH) comb_surplus = xdeltaH + sumLHS if comb_surplus < vio_upper_bd: end = timer() print('success!!!!!!!!!!!!!!') print(partitions[0]) print('comb surplus: %.5f' % comb_surplus) return None ''' edge_cut_list=[] for p1_node in partitions[0]: for p2_node in partitions[1]: if Fshrink.has_edge(p1_node,p2_node): edge_cut_list.append((p1_node,p2_node)) ''' ''' print('cut weight = %.5f' % cutweight) print('total surplus = %.5f' % total_surplus) print('cut weight + total surplus = %.5f' % cutweight+total_surplus) print('running time = %.5f seconds \n'% (end-start)) ''' print('Fails :(') return None #[cutweight, cutweight+total_surplus, edge_cut_list]
def comb_surplus_interval(F, G, candidate_dom, total_surplus, pattern_upper_bound): start = timer() global zero_to_one, one_to_two, one_to_two_1, one_to_two_2, one_to_two_3, one_to_two_4, one_to_two_5, two_to_three_1, two_to_three_2, two_to_three_3, two_to_three_4, two_to_three_5, two_to_three, three_to_four, three_to_four_1, three_to_four_2, three_to_four_3, three_to_four_4, three_to_four_5, four_to_five, four_to_five_1, four_to_five_2, four_to_five_3, four_to_five_4, four_to_five_5, five_to_six, five_to_six_1, five_to_six_2, five_to_six_3, five_to_six_4, five_to_six_5, six_to_seven, seven_to_eight, more_than_eight, viol_comb_surplus_lst # for each node in candidate_dom, LHS =1/2surplus(Ti)-x*(E(A,B)) # sumLHS = \sum 1/2 surplus(Ti)-x*(E(Ai,Bi)). Independ on H, the handle chosen LHS_list = [] print('Number of teeth: %d ' % len(candidate_dom)) for node in candidate_dom: A = G.node[node]['A'] B = G.node[node]['B'] from ABcut import edges_cross E_A_B = edges_cross(F, A, B) xE_A_B = sum(F[u][v]['weight'] for (u, v) in E_A_B) #print('x*(E(A,B))=%.5f' % xE_A_B) LHS = 0.5 * G.node[node]['surplus'] - xE_A_B LHS_list.append(LHS) sumLHS = sum(x for x in LHS_list) #print(sumLHS) all_patterns = list(product(['0', '1'], repeat=len(candidate_dom))) if len(all_patterns) < pattern_upper_bound: step = 1 else: step = math.floor(len(all_patterns) / pattern_upper_bound) for i in range(len(all_patterns)): if i % step == 0: lst_pattern = all_patterns[i] pattern = ''.join(lst_pattern) Fbar, inHandle, notinHandle = add_s_t(F, G, candidate_dom, pattern) ''' print('inHandle:') print(inHandle) print('notinHandle:') print(notinHandle) ''' xdeltaH, partitions = nx.minimum_cut(Fbar, 's', 't', capacity='weight') ''' print('H:') print(partitions[0]) print((inHandle< partitions[0]) or (notinHandle < partitions[0])) ''' #print('x(delta(H))= %.5f' % xdeltaH) comb_surplus = xdeltaH + sumLHS ## Intervals: if 0 <= comb_surplus < 1: zero_to_one += 1 viol_comb_surplus_lst.append(comb_surplus) elif 1 <= comb_surplus < 1.2: one_to_two_1 += 1 one_to_two += 1 elif 1.2 <= comb_surplus < 1.4: one_to_two_2 += 1 one_to_two += 1 elif 1.4 <= comb_surplus < 1.6: one_to_two_3 += 1 one_to_two += 1 elif 1.6 <= comb_surplus < 1.8: one_to_two_4 += 1 one_to_two += 1 elif 1.8 <= comb_surplus < 2.0: one_to_two_5 += 1 one_to_two += 1 #elif 1<= comb_surplus <2: # one_to_two+=1 elif 2 <= comb_surplus < 2.2: two_to_three_1 += 1 two_to_three += 1 elif 2.2 <= comb_surplus < 2.4: two_to_three_2 += 1 two_to_three += 1 elif 2.4 <= comb_surplus < 2.6: two_to_three_3 += 1 two_to_three += 1 elif 2.6 <= comb_surplus < 2.8: two_to_three_4 += 1 two_to_three += 1 elif 2.8 <= comb_surplus < 3.0: two_to_three_5 += 1 two_to_three += 1 elif 3 <= comb_surplus < 3.2: three_to_four_1 += 1 three_to_four += 1 elif 3.2 <= comb_surplus < 3.4: three_to_four_2 += 1 three_to_four += 1 elif 3.4 <= comb_surplus < 3.6: three_to_four_3 += 1 three_to_four += 1 elif 3.6 <= comb_surplus < 3.8: three_to_four_4 += 1 three_to_four += 1 elif 3.8 <= comb_surplus < 4.0: three_to_four_5 += 1 three_to_four += 1 elif 4 <= comb_surplus < 4.2: four_to_five_1 += 1 four_to_five += 1 elif 4.2 <= comb_surplus < 4.4: four_to_five_2 += 1 four_to_five += 1 elif 4.4 <= comb_surplus < 4.6: four_to_five_3 += 1 four_to_five += 1 elif 4.6 <= comb_surplus < 4.8: four_to_five_4 += 1 four_to_five += 1 elif 4.8 <= comb_surplus < 5.0: four_to_five_5 += 1 four_to_five += 1 elif 5 <= comb_surplus < 5.2: five_to_six_1 += 1 five_to_six += 1 elif 5.2 <= comb_surplus < 5.4: five_to_six_2 += 1 five_to_six += 1 elif 5.4 <= comb_surplus < 5.6: five_to_six_3 += 1 five_to_six += 1 elif 5.6 <= comb_surplus < 5.8: five_to_six_4 += 1 five_to_six += 1 elif 5.8 <= comb_surplus < 6.0: five_to_six_5 += 1 five_to_six += 1 elif 6 <= comb_surplus < 7: six_to_seven += 1 elif 7 <= comb_surplus < 8: seven_to_eight += 1 else: more_than_eight += 1 print('comb_surplus: %.5f' % comb_surplus) #print('\n') ''' if comb_surplus < comb_upper_bd: print('success!!!!!!!!!!!!!!') print(partitions[0]) print('comb surplus: %.5f' % comb_surplus) return partitions[0] ''' del_s_t(F) return None end = timer()
def find_handle(F, G, candidate_dom, total_surplus, comb_upper_bd, pattern_upper_bound): start = timer() # for each node in candidate_dom, LHS =1/2surplus(Ti)-x*(E(A,B)) # sumLHS = \sum 1/2 surplus(Ti)-x*(E(Ai,Bi)). Independ on H, the handle chosen LHS_list = [] print('Number of teeth: %d ' % len(candidate_dom)) for node in candidate_dom: A = G.node[node]['A'] B = G.node[node]['B'] from ABcut import edges_cross E_A_B = edges_cross(F, A, B) xE_A_B = sum(F[u][v]['weight'] for (u, v) in E_A_B) #print('x*(E(A,B))=%.5f' % xE_A_B) LHS = 0.5 * G.node[node]['surplus'] - xE_A_B LHS_list.append(LHS) sumLHS = sum(x for x in LHS_list) #print(sumLHS) all_patterns = list(product(['0', '1'], repeat=len(candidate_dom))) if len(all_patterns) < pattern_upper_bound: step = 1 else: step = math.floor(len(all_patterns) / pattern_upper_bound) for i in range(len(all_patterns)): if i % step == 0: lst_pattern = all_patterns[i] pattern = ''.join(lst_pattern) Fbar, inHandle, notinHandle = add_s_t(F, G, candidate_dom, pattern) ''' print('inHandle:') print(inHandle) print('notinHandle:') print(notinHandle) ''' xdeltaH, partitions = nx.minimum_cut(Fbar, 's', 't', capacity='weight') ''' print('H:') print(partitions[0]) print((inHandle< partitions[0]) or (notinHandle < partitions[0])) ''' #print('x(delta(H))= %.5f' % xdeltaH) comb_surplus = xdeltaH + sumLHS print('comb_surplus: %.5f' % comb_surplus) #print('\n') if comb_surplus < comb_upper_bd: print('success!!!!!!!!!!!!!!') print(partitions[0]) print('comb surplus: %.5f' % comb_surplus) return partitions[0] del_s_t(F) return None end = timer() print('running time: %.5f seconds' % (end - start))