def runAntSystem(pop_multiplier, alpha, beta, evaporation_rate, execution): dist = readDistFile(instance) ant_system = AntSystem(dist, len(dist) * pop_multiplier, ITERATIONS, alpha, beta, evaporation_rate, Q) ant_system.initPheromone() ant_system.initAnts() for t in range(ant_system.iterations): best_evaluation_iter = float('inf') for ant in ant_system.population: while (len(ant.solution) < ant_system.node_count): ant_system.goToNextNode(ant) ant.solution.append(ant.solution[0]) ant.evaluation = ant_system.calculateFO(ant.solution) if (ant.evaluation < best_evaluation_iter): best_evaluation_iter = ant.evaluation if (ant.evaluation < ant_system.best_evaluation): ant_system.best_solution = ant.solution ant_system.best_evaluation = ant.evaluation fileOperations(best_evaluation_iter, pop_multiplier, alpha, beta, evaporation_rate, execution, 0) ant_system.pheromoneUpdate() ant_system.restartAnts() fileOperations(ant_system.best_evaluation, pop_multiplier, alpha, beta, evaporation_rate, execution, 1)
def solve_it(input_data, method="", oporation="", solo=False, default=True): # Modify this code to run your optimization algorithm # parse the input lines = input_data.split('\n') nodeCount = int(lines[0]) points = [] for i in range(1, nodeCount + 1): line = lines[i] parts = line.split() points.append(Point(float(parts[0]), float(parts[1]))) if len(points) > 30000 and default: print("greedy") return greedy(points) NPproblem_tsp = tsp.tsp(points) if default: if len(points) > 200: approx = NPproblem_tsp.christofides() return print_solution(NPproblem_tsp, approx, "simulatedAnneling") else: approx = NPproblem_tsp.gurobi_method() return approx if method == 'approximation2': approx = NPproblem_tsp.approximation2() elif method == 'christofides': approx = NPproblem_tsp.christofides() elif method == 'antSystem': Hormigas = AntSystem.AntSystem(points, 1, 2, 0.02, 10, nodeCount, nodeCount) algo = Hormigas.calcule_route() return verbose(algo[1], algo[0]) elif method == 'gurobi': approx = NPproblem_tsp.gurobi_method() return approx else: approx = NPproblem_tsp.christofides() if solo: return verbose(approx[1], approx[0]) else: return print_solution(NPproblem_tsp, approx, oporation)
from sys import argv from utils import * from constants import * from AntSystem import * instanceFile = 'LAU15.txt' if len(argv) < 2 else argv[1] dist = readDistFile(instanceFile) ant_system = AntSystem(dist, len(dist) * POP_MULTIPLIER, ITERATIONS, ALPHA, BETA, EVAPORATION_RATE, Q) ant_system.initPheromone() ant_system.initAnts() for t in range(ant_system.iterations): best_evaluation_iter = float('inf') for ant in ant_system.population: while (len(ant.solution) < ant_system.node_count): ant_system.goToNextNode(ant) ant.solution.append(ant.solution[0]) ant.evaluation = ant_system.calculateFO(ant.solution) if (ant.evaluation < best_evaluation_iter): best_evaluation_iter = ant.evaluation if (ant.evaluation < ant_system.best_evaluation): ant_system.best_solution = ant.solution ant_system.best_evaluation = ant.evaluation print(f'Best evaluation of iteration {t}: {best_evaluation_iter}')