def tabu2(arr_edge,all_edge,nodes,tabu_list):
	solution_array = []
	free_edges = get_free_edges(arr_edge,all_edge)
	arr_edge_cost = util.blabla(nodes,arr_edge)
	#solution_array.append((arr_edge,arr_edge_cost))
	for e in arr_edge:
		temp = []
		for k in arr_edge:
			if(k.same(e)):
				pass
			else:
				temp.append(k.copy())
		nodes1 = []
		nodes2 = []
		connected_to(e.v1,temp,nodes1)
		connected_to(e.v2,temp,nodes2)
		c_edges = cut_edges(nodes1,nodes2,free_edges)
		for c_edge in c_edges:
			temp.append(c_edge)
			cost = util.blabla(nodes,temp)
			a,b = util.copy_array(temp,cost)
			solution_array.append((a,b,c_edge.copy()))
			temp.remove(c_edge)
	
	t_arr,mincost,e_min = solution_array[0]
	minpos = 0
	i=0
	for a,b,e in solution_array[1:]:
		i+=1
		if(b<mincost):
			minpos = i
			mincost = b
	res_arr,res_cost,e_min = solution_array[minpos]
	tabu_list.append(e_min)
	return((res_arr,res_cost))
def heu_cut(arr_edge,all_edge,nodes):
	solution_array = []
	free_edges = get_free_edges(arr_edge,all_edge)
	arr_edge_cost = util.blabla(nodes,arr_edge)
	#solution_array.append((arr_edge,arr_edge_cost))
	for e in arr_edge:
		temp = []
		for k in arr_edge:
			if(k.same(e)):
				pass
			else:
				temp.append(k.copy())
		nodes1 = []
		nodes2 = []
		connected_to(e.v1,temp,nodes1)
		connected_to(e.v2,temp,nodes2)
		c_edges = cut_edges(nodes1,nodes2,free_edges)
		for c_edge in c_edges:
			temp.append(c_edge)
			cost = util.blabla(nodes,temp)

			solution_array.append(util.copy_array(temp,cost))
			temp.remove(c_edge)
	
	t_arr,mincost = solution_array[0]
	minpos = 0
	i=0
	for a,b in solution_array[1:]:
		i+=1
		if(b<mincost):
			minpos = i
			mincost = b
예제 #3
0
def crossover(pop,nodes,all_edge):
	res = []
	pop.sort(key = get_solution_cost)
	res.append(pop[0])
	t1,t2 = res[0]
	for a,c in pop[1:]:
		if(util.same_array(a,t1)):
			pass
		else:
			res.append((a,c))
			break
	for i in range(POPULATION-2):	
		t1,t2 = fitness.roulette_wheel(pop)
		p1,c = t1
		p2,c = t2
		arr_temp = pr.parent_reproduction(p1,p2,nodes)
		counter =0
		for a,b in res:
			if(diffs(a,arr_temp)<2):
				#print("mutation")
				if(random.uniform(0,1)>0.5):
					arr_temp = mutate(p1,all_edge,nodes)
				else:
					arr_temp = mutate(p2,all_edge,nodes)

		cost = util.blabla(nodes,arr_temp)
		res.append((arr_temp,cost))
	return res
예제 #4
0
def generate_population(arr_edge,all_edge,nodes):
	tabu_list = []
	solution_array = []
	startcost = util.blabla(nodes,arr_edge)
	#solution_array.append((arr_edge,startcost))
	temp_list = arr_edge
	for i in range(TABU_STEP):
		temp_list,cost = tabu.tabu2(temp_list,all_edge,nodes,tabu_list)
		solution_array.append((temp_list,cost))
	return solution_array
예제 #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 first_population(nodes,temp):
	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)
		cost = util.blabla(nodes,temp_tree)
		trees.append((temp_tree,cost))
	return trees
예제 #7
0
def grasp(nodes, edges):
    start = timeit.default_timer()
    lll = spt.loop_root_SPT(nodes, edges)
    stop = timeit.default_timer()
    print("Time for Dijkstra tree generator: " + str(stop - start))
    trees = []
    grasp_list = []
    for ll in lll:
        temp_tree = []
        for lis in ll:
            for ed in lis:
                if (di.edge_in_list(ed, temp_tree)):
                    pass
                else:
                    temp_tree.append(ed)
        trees.append(temp_tree)

    trees.sort(key=lambda x: cost_tree_grasp(nodes, x))
    mincost = util.blabla(nodes, trees[0])

    res = trees[random.randint(0, 2)]
    grasp_cost_res = util.blabla(nodes, res)

    return res, grasp_cost_res, mincost
예제 #8
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 tabu(arr_edge,all_edge,nodes,tabu_list):
	free_edge = []
	solution_array = []
	for e_all in all_edge:
		find = False
		for edge in arr_edge:
			if(edge.same(e_all)):
				find = True
				break
		if(not find):
			e = e_all.copy()
			free_edge.append(e)

	for e in free_edge:
		arr_edge.append(e)
		a,b = util.cic(arr_edge)
		
		for h in b:
			if(h.same(e)):
				continue
			elif(in_list(h,tabu_list)):
				continue
			else:
				util.pop_element(arr_edge,h)
				newmin = util.blabla(nodes,arr_edge)
				x,y = util.copy_array(arr_edge,newmin)
				solution_array.append((x,y,h.copy()))
				arr_edge.append(h)

		util.pop_element(arr_edge,e)

	minarr,mincost,tabuedge = solution_array[0]
	#return((minarr,mincost))
	counter =0
	minpos =0
	for a,c,e in solution_array[1:]:
		counter +=1
		if(c < mincost):
			minpos = counter
			mincost = c
	minarr,mincost,tabuedge = solution_array[minpos]
	append_to_tabu(tabuedge,tabu_list)
	return((minarr,mincost))
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 = []
def heuristic(arr_edge,base_cost,all_edge,nodes):
	solution_array = []
	free_edge =[]
	for e_all in all_edge:
		find = False
		for edge in arr_edge:
			if(edge.same(e_all)):
				find = True
				break
		if(not find):
			e = e_all.copy()
			free_edge.append(e)

	for e in free_edge:
		arr_edge.append(e)
		a,b = util.cic(arr_edge)
		
		for h in b:
			if(h.same(e)):
				continue
			else:
				util.pop_element(arr_edge,h)
				newmin = util.blabla(nodes,arr_edge)
				solution_array.append(util.copy_array(arr_edge,newmin))
				arr_edge.append(h)

		util.pop_element(arr_edge,e)
	solution_array.append((arr_edge,base_cost))

	z,mincost = solution_array[0]
	minpos = 0
	i=0
	for a,c in solution_array:
		i+=1
		if(mincost > c):
			minpos = i
			z,mincost = a,c

	return((z,mincost))
예제 #12
0
def cost_tree_grasp(nodes, tree):
    cost = util.blabla(nodes, tree)
    return cost