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
Exemple #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)
Exemple #4
0
def mutate(arr_edge,all_edge,nodes):
	res,c = util.copy_array(arr_edge,0)
	free_edges = get_free_edges(arr_edge,all_edge)
	e = res[random.randint(0,len(res)-1)]
	nodes1 = []
	nodes2 = []
	res.remove(e)
	connected_to(e.v1,res,nodes1)
	connected_to(e.v2,res,nodes2)
	c_edges = cut_edges(nodes1,nodes2,free_edges)
	best = c_edges[0]
	for c_edge in c_edges[1:]:
		if(c_edge.cost < best.cost):
			best = c_edge

	#c_edge = c_edges[random.randint(0,len(c_edges)-1)]
	res.append(best)
	
	return(res)
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 grasp(all_edge):
    temp_arr, useless = util.copy_array(all_edge, 0)
    res = []
    temp_arr.sort(key=lambda x: x.cost)
    while (True):
        if (0 == (len(temp_arr) - 2)):
            element = temp_arr[random.randint(0, 1)]
        elif (0 == (len(temp_arr) - 1)):
            element = temp_arr[0]
        else:
            element = temp_arr[random.randint(0, (RNJESUS - 1))]
        util.pop_element(temp_arr, element)
        res.append(element)
        a, b = util.cic(res)
        if (a):
            res.pop()
        if (len(temp_arr) == 0):
            break

    return res
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))