예제 #1
0
def shortest_path(graph, origin, destination, flag):
    visited, paths = dijkstra(graph, origin, flag)
    list = []
    temp = []
    path = []
    full_path = deque()
    _destination = paths[destination]
    tuple = (_destination, destination)
    list.append(tuple)
    a = visited[tuple[0]]
    b = visited[tuple[1]]
    w = int(b - a)
    ed = Edge.Edge(tuple[0], tuple[1], w)
    path.append(ed)
    temp.append(ed.toString())

    while _destination != origin:
        full_path.appendleft(_destination)
        tmp = _destination
        _destination = paths[_destination]
        tuple = (_destination, tmp)
        list.append(tuple)
        a = visited[tuple[0]]
        b = visited[tuple[1]]
        w = int(b - a)
        ed = Edge.Edge(tuple[0], tuple[1], w)
        path.append(ed)
        temp.append(ed.toString())

    temp.reverse()

    return visited[destination], path
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))
예제 #3
0
def main():
	file_name = "../graphs/graph_ppt.txt"
	nodes,temp = util.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 = []
	'''for e in all_edge:
		arr_edge.append(e)
		a,b = util.cic(arr_edge)
		if(a):
			arr_edge.pop()'''
	arr_edge = gk.kruskal(all_edge)

	
	mincost = util.blabla(nodes,arr_edge)
	minarr = arr_edge
	print("pre tabu cost "+str(mincost))
	newcost = mincost
	tabu_list = []
	counter = 0
	tot = 0
	start = timeit.default_timer()
	for i in range(30):
		newarr,newcost = tabu.tabu(arr_edge,all_edge,nodes,tabu_list)
		print("step n "+str(i+1)+" cost = "+str(newcost))
		
		stop = timeit.default_timer()
		tot += stop-start
		print("time = "+str(tot))
		start = timeit.default_timer()
		arr_edge = newarr
		if(newcost >= mincost):
			#util.print_array(tabu_list)
			counter+=1
		else:
			minarr,mincost = util.copy_array(newarr,newcost)
			counter =0
		if(counter == 5):
			break
	print("best cost = "+str(mincost))
	print("time = "+str(tot))
	stop = timeit.default_timer()
	tot += stop-start
	util.print_array(minarr)
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 = []
예제 #5
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)
예제 #6
0
def main():
	global BESTCOST
	global POPULATION
	signal.signal(signal.SIGALRM, handler)
	nodes,temp = rgt.read("../graphs/graph_ppt2.txt")
	all_edge = []
	for t in temp:
		c,a,b = t.split(" ")
		e = Edge.Edge(a,b,c)
		all_edge.append(e)
	#arr_edge = gk.kruskal(all_edge)
	print("generating population")
	start = timeit.default_timer()
	#signal.setitimer(signal.ITIMER_REAL,10,5)
	solutions = first_population(nodes,temp)
	if(len(solutions)< POPULATION):
		POPULATION = len(solutions)
	else:
		solutions = solutions[:POPULATION]
	print_population(solutions)
	best_solution = find_best(solutions)
	x,y = best_solution
	BESTCOST = y
	print("best solution pre genetic = "+str(y))
	best_in_pop = None
	counter =0
	i =0
	tot =0
	for i in range(100):
		i+=1
		print("iteration = "+str(i))
		newpopulation = crossover(solutions,nodes,all_edge)
		print_population(newpopulation)
		solutions = newpopulation
		newpopulation.sort(key = get_solution_cost)
		x,y = newpopulation[0]
		k,m = newpopulation[POPULATION-1]
		print("last cost = "+str(m))
		
		if(y< best_solution[1]):
			counter =0
			stop = timeit.default_timer()
			tot += stop -start
			start = timeit.default_timer()
			print("tot = "+str(tot))
			print("tabu")
			better_solutions_tabu = generate_population(x,all_edge,nodes)
			merge_for_new_population(better_solutions_tabu,newpopulation)
			best_solution = (x,y)
			stop = timeit.default_timer()
			tot += stop -start
			start = timeit.default_timer()
			print("tot = "+str(tot))
			print("tabu")
		else:
			counter+=1
		best_in_pop = find_best(newpopulation)
		x,y = best_in_pop
		BESTCOST = y
		print("best new pop cost = "+str(y))
		if(counter >15):
			break
	stop = timeit.default_timer()
	tot += stop-start
	print("finisced at "+str(tot))