def search(points, maxIterations, increase, decrease, maxCandidates): current = {} current["permutation"] = constructInitialSolution(points) current["cost"] = tourCost(current["permutation"]) best = current iteration = 0 while iteration < maxIterations: # Memory based reaction memoryBasedReaction(current, increase, decrease, iteration) # Generate Candidate list from neighborhood from current candidates = [] for index in range(0, maxCandidates): candidates.append(generateCandidateNeighborhood(current)) # From candidate list get best current, bestEdges = bestMove(candidates, iteration, len(points), best) # Update tabu list with the current candidates features for edge in bestEdges: updateTabuList(edge, iteration) # Compare cost of current with best and update best if less than current if current["cost"] < best["cost"]: best = current iteration += 1 #return best return best
def generateCandidateNeighborhood(current): result = {} permutation, edges = stochasticTwoOptWithEdges(current["permutation"]) candidate = {} candidate["permutation"] = permutation candidate["cost"] = tourCost(candidate["permutation"]) result["candidate"] = candidate result["edges"] = edges return result
def search(points, maxIterations, maxTabu, maxCandidates): # 1. конструирование начального решения # construct a random tour best ={} best["permutation"] = constructInitialSolution(points) # начальное решение print len(points),len(best["permutation"]) print best["permutation"] best["cost"] = tourCost(best["permutation"]) # ц.ф. # 2. пустой табу-лист tabuList =[] # 3. главный цикл алгоритма на убывание # StopCondition: maxIterations = 0, maxIterations = 100 в начале # таким образом имеем 100 итераций - 100 локальных оптимумов while maxIterations>0: # 4. пустой список кандидатов candidates = [] # Generate candidates using stocahstic 2-opt near current best candidate # Use Tabu list to not revisit previous rewired edges # 5. Ищем кандидатов, используя табуирование (даелаем мувы) for index in range(0,maxCandidates): # заполняем список решениями candidates.append(generateCandidates(best, tabuList, points)) # Locate the best candidate # sort the list of candidates by cost # since it is an involved sort, we write a function for getting the least cost candidate bestCandidate, bestCandidateEdges = locateBestCandidate(candidates) # compare with current best and update if necessary if bestCandidate["cost"] < best["cost"]: # set current to the best, so thatwe can continue iteration best = bestCandidate # update tabu list for edge in bestCandidateEdges: if len(tabuList) < maxTabu: tabuList.append(edge) maxIterations -=1 return best
def generateCandidates(best, tabuList, points): permutation, edges, result = None, None, {} # собственно move while permutation == None or isTabu(best["permutation"], tabuList): permutation, edges = stochasticTwoOptWithEdges(best["permutation"]) candidate ={} candidate["permutation"] = permutation candidate["cost"] = tourCost(candidate["permutation"]) result["candidate"] = candidate result["edges"] = edges return result # возвращает решение