Example #1
0
def greedy_algorithm(G):
    CG = construct_cluster_graph(G)
    CG_copy = CG.copy()
    print "done constructing"
    valid_nodes = CG.nodes()
    selected_clusters = []
    pre_penalty = 0
    while len(valid_nodes) != 0:
        # node_selected = find_max_penalty(valid_nodes, CG)
        node_selected = find_min_weighted_degree(valid_nodes, CG)
        pre_penalty += CG.node[node_selected]['penalty']
        selected_clusters.append(node_selected)
        list_edges = CG.edges(node_selected, False)
        nodes_to_remove = set()
        nodes_to_remove.add(node_selected)
        for edge in list_edges:
            nodes_to_remove.add(edge[0])
            nodes_to_remove.add(edge[1])
        for node in nodes_to_remove:
            valid_nodes.remove(node)
            CG.remove_node(node)
    list_cycles = []
    for cluster in selected_clusters:
        list_cycles.append(CG_copy.node[cluster]['nodes'])
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(list_cycles)
    print(find_total_penalty(G), pre_penalty)
    return [output_string, penalty]
Example #2
0
def algorithm(graph):
    CG = construct_cluster_graph(graph)
    valid_nodes = CG.nodes()
    adults = []
    start = valid_nodes.pop()
    selected_nodes = [start]
    pre_penalty = 0
    cycles = []
    current_cycle = []
    while selected_nodes != []:
        node = selected_nodes.pop(0)
        edges = CG.edges(node, False)
        if node in current_cycle:
            cycles.append(current_cycle)
            for n in current_cycle:
                pre_penalty += n['penalty']
                valid_nodes.remove(n)
            current_cycle = []
        current_cycle.append(node)
        for inner, outer in edges:
            neighbor = CG.node[outer]
            child_cycles = []
            if neighbor['penalty'] == 2:
                child_cycles.append(neighbor)
                selected_nodes.append(neighbor)
            else:
                adults.append(neighbor)
        if child_cycles == []:
            for adult in adults:
                selected_nodes.append(adult)
        child_cycles = []
        adults = []
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(cycles)
    return [output_string, penalty]
Example #3
0
def algorithm(graph):
    CG = construct_cluster_graph(graph)
    valid_nodes = CG.nodes()
    adults = []
    start = valid_nodes.pop()
    selected_nodes = [start]
    pre_penalty = 0
    cycles = []
    current_cycle = []
    while selected_nodes != []:
        node = selected_nodes.pop(0)
        edges = CG.edges(node, False)
        if node in current_cycle:
            cycles.append(current_cycle)
            for n in current_cycle:
                pre_penalty += n['penalty']
                valid_nodes.remove(n)
            current_cycle = []
        current_cycle.append(node)
        for inner, outer in edges:
            neighbor = CG.node[outer]
            child_cycles = []
            if neighbor['penalty'] == 2:
                child_cycles.append(neighbor)
                selected_nodes.append(neighbor)
            else:
                adults.append(neighbor)
        if child_cycles == []:
            for adult in adults:
                selected_nodes.append(adult)
        child_cycles = []
        adults = []
    penalty = find_total_penalty(G) - pre_penalty
    output_string = format_output_cycles(cycles)
    return [output_string, penalty]
Example #4
0
def greedy_algorithm(G):
	CG = construct_cluster_graph(G)
	CG_copy = CG.copy()
	print "done constructing"
	valid_nodes = CG.nodes()
	selected_clusters = []
	pre_penalty = 0
	# cg_penalty = 0
	while len(valid_nodes) != 0:
		# node_selected = find_max_penalty(valid_nodes, CG)
		node_selected = find_min_weighted_degree(valid_nodes, CG)
		# node_selected = find_min_degree(valid_nodes, CG)
		# node_selected = find_random(valid_nodes, CG)
		pre_penalty += CG_copy.node[node_selected]['penalty']
		selected_clusters.append(node_selected)
		list_edges = CG.edges(node_selected, False)
		nodes_to_remove = set()
		nodes_to_remove.add(node_selected)
		# print len(list_edges)
		for edge in list_edges:
			assert (edge[0] == node_selected)
			nodes_to_remove.add(edge[1])
		for node in nodes_to_remove:
			valid_nodes.remove(node)
			# cg_penalty += CG_copy.node[node]['penalty']
			CG.remove_node(node)
	list_cycles = []
	for cluster in selected_clusters:
		list_cycles.append(CG_copy.node[cluster]['nodes'])
	penalty = find_total_penalty(G) - pre_penalty
	output_string = format_output_cycles(list_cycles)
	# print(find_total_penalty(G), pre_penalty, cg_penalty, penalty)
	print [output_string, penalty]
	return [output_string, penalty]
Example #5
0
def random_algorithm(input_graph):
    G = input_graph.copy()
    cycle_list = []
    pre_penalty = 0
    while G.nodes():
        try:
            edge_cycle = nx.find_cycle(G)
            vertex_cycle = []
            for edge in edge_cycle:
                vertex_cycle.append(edge[0])
                pre_penalty += G.node[edge[0]]['penalty']
                G.remove_node(edge[0])
            cycle_list.append(vertex_cycle)
        except:
            print("No cycle found")
            break
    penalty = find_total_penalty(input_graph) - pre_penalty
    formatted_cycle_list = format_output_cycles(cycle_list)
    return [formatted_cycle_list, penalty]
Example #6
0
def random_algorithm(input_graph):
	G = input_graph.copy()
	cycle_list = []
	pre_penalty = 0
	while G.nodes():
		try:
			edge_cycle = nx.find_cycle(G)
			vertex_cycle = []
			for edge in edge_cycle:
				vertex_cycle.append(edge[0])
				pre_penalty += G.node[edge[0]]['penalty']
				G.remove_node(edge[0])
			cycle_list.append(vertex_cycle)
		except:
			print("No cycle found")
			break	
	penalty = find_total_penalty(input_graph) - pre_penalty
	formatted_cycle_list = format_output_cycles(cycle_list)
	return [formatted_cycle_list, penalty]
Example #7
0
def random_algorithm_3(input_graph):
    G = input_graph.copy()
    cycle_list = []
    pre_penalty = 0
    random_result = build_randomized_graph(input_graph)
    randG = random_result[0]
    nodes_dict = random_result[1]
    while randG.nodes():
        source_node = randG.nodes()[0]
        cycle = find_cycle(randG, source_node)
        if cycle == []:
            randG.remove_node(source_node)
        else:
            real_cycle = []
            for node in cycle:
                real_cycle.append(nodes_dict[node])
            cycle_list.append(real_cycle)
            for node in cycle:
                real_node = nodes_dict[node]
                pre_penalty += G.node[real_node]['penalty']
                randG.remove_node(node)
    penalty = find_total_penalty(input_graph) - pre_penalty
    formatted_cycle_list = format_output_cycles(cycle_list)
    return [formatted_cycle_list, penalty]
Example #8
0
def random_algorithm_4(input_graph):
	G = input_graph.copy()
	cycle_list = []
	pre_penalty = 0
	random_result = build_randomized_graph(input_graph)
	randG = random_result[0]
	nodes_dict = random_result[1]
	while randG.nodes():
		source_node = randG.nodes()[0]
		cycle = find_random_length_cycle(randG, source_node)
		if cycle == []:
			randG.remove_node(source_node)
		else:
			real_cycle = []
			for node in cycle:
				real_cycle.append(nodes_dict[node])
			cycle_list.append(real_cycle)
			for node in cycle:
				real_node = nodes_dict[node]
				pre_penalty += G.node[real_node]['penalty']
				randG.remove_node(node)
	penalty = find_total_penalty(input_graph) - pre_penalty
	formatted_cycle_list = format_output_cycles(cycle_list)
	return [formatted_cycle_list, penalty]
Example #9
0
from utils import find_total_penalty

#file_a = "CURRENT BEST SOLUTIONS"
#file_b = "SOLUTION_RECORDS_TONY.txt"
#combine(file_a, file_b)
#write_condensed_solutions("COMBINED SOLUTIONS")
"""G = create_graph("instances/12.in")
contains = False
edge_to_check = (119, 57)
for edge in G.edges():
	if edge_to_check == edge:
		contains = True
print(contains) 
optimal_sol_list = do_not_test_set("COMBINED SOLUTIONS")
out_file = open("Sub-Optimal Instances", "w")
read_file = open("Condensed Solution Records", "r")
read_data = read_file.readlines()
for element in range(1, 493):
	if element not in optimal_sol_list:
		line_data = read_data[element - 1]
		pre_penalty = line_data.split(":")
		penalty = pre_penalty[1].strip()
		out_file.write(str(element) + ": " + penalty + "\n") """

G = create_graph("instances/8.in")	
penalty = find_total_penalty(G)
print(penalty)
#CG = construct_cluster_graph(G)
#print_cluster_graph_cycles(CG)