def main():
    global BEST
    nodes, temp = rgt.read("../graphs/graph_random_5.txt")
    signal.signal(signal.SIGALRM, handler)
    all_edge = []
    plot_all_edge = []
    plot_solution = []
    counter = 0
    for t in temp:
        c, a, b = t.split(" ")
        e = Edge.Edge(a, b, c)
        all_edge.append(e)
    gr_start = timeit.default_timer()
    grasp_tree, grasp_cost, mincost = gra.grasp(nodes, temp)
    gr_stop = timeit.default_timer()
    print('Tree cost graph: ' + str(grasp_cost) + ' | time_GRASP: ' +
          str(gr_stop - gr_start) + '\n' + 'Dijkstra cost: ' + str(mincost))
    print('\n--- GRASP ---\n')
    print_array(grasp_tree)
    print('')

    time = 0
    j = 0
    start = timeit.default_timer()
    signal.setitimer(signal.ITIMER_REAL, 100, 50)
    grasp_tree, bb = heu_cut(grasp_tree, all_edge, nodes)
    stop = timeit.default_timer()
    BEST = bb
    time = stop - start
    print('Cost_grasp: ' + str(mincost) + ' -------> cost_LS: ' + str(bb) +
          ' | time_LS: ' + str(time))
    best_cost = bb

    for i in range(15):
        start = timeit.default_timer()
        grasp_tree, bb = heu_cut(grasp_tree, all_edge, nodes)
        stop = timeit.default_timer()
        time = time + (stop - start)
        print('Cost_grasp: ' + str(mincost) + ' -------> cost_LS: ' + str(bb) +
              ' | time_LS: ' + str(time))
        if (j != 4):
            if (bb > best_cost or bb == best_cost):
                j += 1
                best_cost = bb
            elif (bb < best_cost):
                best_cost = bb
                BEST = best_cost
                j = 0
        else:
            break

    print('\n--- LS ---\n')
    print_array(grasp_tree)

    for e in all_edge:
        v1, v2 = (e.v1, e.v2)
        plot_all_edge.append((v1, v2))
    for e in grasp_tree:
        v1, v2 = (e.v1, e.v2)
        plot_solution.append((v1, v2))
def main():
	nodes,temp = rgt.read("../../graph/graph_random_3.txt")
	all_edge = []
	plot_all_edge = []
	plot_solution = []
	for t in temp:
		c,a,b = t.split(" ")
		e = cost_calc.Edge(a,b,c)
		all_edge.append(e)
	list_of_list_of_list =spt.loop_root_SPT(nodes,temp)
	trees = []
	for list_of_list in list_of_list_of_list:
		temp_tree = []
		for lis in list_of_list:
			for ed in lis:
				if(edge_in_list(ed,temp_tree)):
					pass
				else:
					temp_tree.append(ed)
		trees.append(temp_tree)

	mincost = cost_calc.blabla(nodes,trees[0])
	minpos = 0
	i =0
	for t in trees[1:]:
		i+=1
		c_temp = cost_calc.blabla(nodes,t)
		if(c_temp < mincost):
			mincost = c_temp
			minpos = i
	print("pre eu cost = "+str(mincost))
	a = trees[minpos]

	for e in all_edge:
		v1,v2 = (e.v1,e.v2)
		plot_all_edge.append((v1,v2))

	tot = 0
	for i in range(10):
		start = timeit.default_timer()
		a,b = heu_cut(a,all_edge,nodes)
		stop = timeit.default_timer()
		tot = tot + (stop - start)
		print("cost = " + str(b) + ' time: ' + str(tot))

	print_array(a)
	for e in a:
		v1,v2 = (e.v1,e.v2)
		plot_solution.append((v1,v2))

	toPlot.toPlot(nodes, plot_all_edge, plot_solution)
def main():
	file_name = "../graphs/graph_ppt.txt"
	nodes,temp = rgt.read(file_name)
	all_edge = []
	for t in temp:
		c,a,b = t.split(" ")
		all_edge.append(Edge.Edge(a,b,c))
	#all_edge.sort(key = lambda x: x.cost)
	arr_edge = []
	arr_edge = gk.kruskal(all_edge)
	
	#arr_edge = gk.grasp(all_edge)
	mincost = util.blabla(nodes,arr_edge)
	print("mincost first solution = "+str(mincost))
	print("first solution")
	util.print_array(arr_edge)
	print("heuristic")
	newcost= mincost
	tot = 0
	final_res,t = util.copy_array(arr_edge,mincost)
	start = timeit.default_timer()
	for i in range(50):
		print(str(i+1)+"/50",end="\r")
		arr_edge,newcost= heuristic(arr_edge,newcost,all_edge,nodes)
		print("step "+str(i+1)+" solution to optimize cost = "+str(newcost))
		stop = timeit.default_timer()
		tot += stop-start
		print("time = "+str(tot))
		start = timeit.default_timer()
		if(mincost> newcost):
			mincost = newcost
			final_res = arr_edge
		elif(mincost == newcost):
			break
	print("result = "+str(mincost))
	util.print_array(arr_edge)
	
	



	'''plot_all_edge = []
Beispiel #4
0
def main():
	nodes,temp = rgt.read("../graphs/graph_2.txt")
	all_edge = []
	for t in temp:
		c,a,b = t.split(" ")
		e = Edge.Edge(a,b,c)
		all_edge.append(e)
	list_of_list_of_list =spt.loop_root_SPT(nodes,temp)
	trees = []
	for list_of_list in list_of_list_of_list:
		temp_tree = []
		for lis in list_of_list:
			for ed in lis:
				if(util.edge_in_list(ed,temp_tree)):
					pass
				else:
					temp_tree.append(ed)
		trees.append(temp_tree)



	mincost = util.blabla(nodes,trees[0])
	minpos = 0
	i =0
	for t in trees[1:]:
		i+=1
		c_temp = util.blabla(nodes,t)
		if(c_temp < mincost):
			mincost = c_temp
			minpos = i
	print("pre eu cost = "+str(mincost))
	a = trees[minpos]
	mincost = util.blabla(nodes,a)
	for i in range(10):
		a,b = heu_cut(a,all_edge,nodes)
		print("cost = "+str(b))
		if(b<mincost):
			mincost = b
		elif(b == mincost):
			break
	util.print_array(a)
    for i in nodes:
        if (i != root):
            cost, path = dk.shortest_path(graph, root, i, flag)
            list.append(path)
    return list


def loop_root_SPT(nodes, edges):
    list = []
    for i in nodes:
        list.append(tree_dijk(i, nodes, edges))

    return list


if __name__ == '__main__':
    list = []
    list2 = []
    nodes = []
    edges = []

    nodes, edges = rd.read('graph_1.txt')

    list = tree_dijk('B', nodes, edges)
    list2 = loop_root_SPT(nodes, edges)

    print(list)
    print('\n')
    print(list2)
def main():
    nodes, temp = rgt.read("../graph/graph_1.txt")
    all_edge = []
    for t in temp:
        c, a, b = t.split(" ")
        all_edge.append(cost_calc.Edge(a, b, c))
    all_edge.sort(key=lambda x: x.cost)
    arr_edge = []
    free_edge = []
    for e in all_edge:
        arr_edge.append(e)
        a, b = cost_calc.cic(arr_edge)
        if (a):
            arr_edge.pop()
            free_edge.append(e.copy())
    mincost = cost_calc.blabla(nodes, arr_edge)

    solution_array = []
    solution_array.append(copy_array(arr_edge, mincost))
    heuristic(arr_edge, all_edge, nodes, solution_array)
    i = 0
    welcome_to_matrix_neo = []
    res = []
    merge(res, solution_array)
    for arr, c in solution_array:
        if (i == 0):
            i += 1
            continue

        sol = []
        sol.append(copy_array(arr, c))
        heuristic(arr, all_edge, nodes, sol)
        merge(res, sol)
        welcome_to_matrix_neo.append(sol)

    welcome_to_matrix_neo.insert(0, solution_array)
    i = 0
    #print_all_solution(solution_array)
    '''for arr,c in res:
		i+=1
		print("\niterazione n = "+str(i)+" costo = "+str(c)+"\n")
		print_array(arr)'''
    mincost = 9000
    minpos = -1
    i = 0
    for a, c in res:
        if (mincost > c):
            mincost = c
            minpos = i
        i += 1
    print("best solution cost = " + str(mincost))
    a, b = res[minpos]
    print_array(a)
    plot_all_edge = []
    for e in all_edge:
        v1, v2 = (e.v1, e.v2)
        plot_all_edge.append((v1, v2))
    plot_solution = []
    for e in a:
        v1, v2 = (e.v1, e.v2)
        plot_solution.append((v1, v2))

    toPlot.toPlot(nodes, plot_all_edge, plot_solution)
    '''f.write(str(res))