max_edges=params_dict['poligonProblem.max_edges'], vns_vnd=params_dict['poligonProblem.vns_vnd']) while initial_solution is None or len(initial_solution) < params_dict['initialSolution.lenght']: problem.num_shapes = i # una posible mejora podria ser optimizar los polígonos que están introduciendo más error # i-1 optimiza el último poligono introducido problem.polygon_list = [i-1] if not initial_solution is None: initial_solution.append([problem.get_random_polygon(), problem.get_random_color()]) #Ejecuta busqueda tabú searcher = TabuSearch(problem, max_iterations=params_dict['initialTabuSearch.max_iterations'], list_length=params_dict['initialTabuSearch.list_length'], improved_event=improve, tolerance=params_dict['initialTabuSearch.tolerance']) searcher.search(initial_solution=initial_solution) # Comprueba si la solucion actual es mejor que la encontrada if current_fitness > searcher.best_fitness or initial_solution is None: initial_solution = searcher.best current_fitness = searcher.best_fitness # aqui ya tenemos 2 vecinos if i > 1: # Indices de los poligonos que quiero optimizar # Con None le indico que lo aplique a todos los poligonos problem.polygon_list=None
from optimization.tabu import TabuSearch from problems.tsp.tsp_problem import TSPProblem from problems.tsp.tsplib import get_cities_tup if __name__ == '__main__': def improve(caller): current = [cities[i] for i in caller.best] + [cities[caller.best[0]]] problem.plot_cities(current) cities = get_cities_tup(file='berlin52.tsp') problem = TSPProblem(cities, candidates_by_iteration=200, neighborhood=2) searcher = TabuSearch(problem, max_iterations=1000, list_length=10, improved_event=improve) searcher.search() best = [cities[i] for i in searcher.best] + [cities[searcher.best[0]]] problem.plot_cities(best)
current_fitness = 100 problem = TrianglesProblem(img, num_shapes=i, candidates_by_iteration=1000, triangle_list=[i - 1], sol_file='data/images/little_mondrian_sol.png') while len(initial_solucion) < 50: problem.num_shapes = i problem.triangle_list = [i - 1] initial_solucion.append( [problem.get_random_triangle(), problem.get_random_color()]) searcher = TabuSearch(problem, max_iterations=1000, list_length=10, improved_event=improve, tolerance=100) searcher.search(initial_solution=initial_solucion) if current_fitness > searcher.best_fitness: initial_solucion = searcher.best current_fitness = searcher.best_fitness i += 1 print("Solution length: %d" % i) else: initial_solucion = initial_solucion[:-1] print("num fitness per second: %f" % (problem.fitness_count / problem.fitness_time))
return sol def perturb(self, sol): sol = copy(sol) sol = self.local_search.problem.swap(sol, self.perturbation_level) return sol if __name__ == '__main__': def improve(caller): current = [cities[i] for i in caller.best] + [cities[caller.best[0]]] problem.plot_cities(current) cities = get_cities_tup(file='berlin52.tsp') problem = TSPProblem(cities, candidates_by_iteration=200, neighborhood=2) searcher = TabuSearch(problem, max_iterations=100, list_length=10, verbose=True, improved_event=improve) ils = ILSTSP(searcher, max_iterations=10, perturbation_level=2) ils.search() best = [cities[i] for i in searcher.best] + [cities[searcher.best[0]]] problem.plot_cities(best) print("Best score: %f" % problem.best_fitness) print("Finish")