def parent_reproduction(parent1, parent2, nodes):
    res = []
    res.extend(parent1)

    for e2 in parent2:
        found = False
        for e1 in parent1:
            if (e2.same(e1)):
                found = True
                break
        if (not found):
            res.append(e2)

    a, b = util.cic(res)
    while (a):
        b.sort(key=lambda x: x.cost)
        if (len(b) > 2):
            el = b[random.randint(len(b) - 3, len(b) - 1)]
        elif (len(b) == 2):
            el = b[random.randint(len(b) - 2, len(b) - 1)]
        else:
            el = b[0]
        util.pop_element(res, el)
        a, b = util.cic(res)

    return res
def kruskal(all_edge):
    res = []
    all_edge.sort(key=lambda x: x.cost)
    for e in all_edge:
        res.append(e)
        a, b = util.cic(res)
        if (a):
            res.pop()
    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 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
Exemple #5
0
def parent_reproduction(parent1, parent2, nodes):
    res = []
    res.extend(parent1)

    for e2 in parent2:
        found = False
        for e1 in parent1:
            if (e2.same(e1)):
                found = True
                break
        if (not found):
            res.append(e2)

    a, b = cc.cic(res)

    while (a):
        res.sort(key=lambda x: x.cost)
        res.pop(random.randint(len(res) - 2, len(res) - 1))
        a, b = util.cic(res)

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