def resolver(metodo_busqueda, iteraciones, haz, reinicios): visor = ConsoleViewer() if (metodo_busqueda == 'hill_climbing'): resultado = hill_climbing(problem=HnefataflProblema(INITIAL), iterations_limit=iteraciones) return resultado if (metodo_busqueda == 'hill_climbing_stochastic'): resultado = hill_climbing_stochastic( problem=HnefataflProblema(INITIAL), iterations_limit=iteraciones) return resultado if (metodo_busqueda == 'beam'): resultado = beam(problem=HnefataflProblema(None), beam_size=haz, iterations_limit=iteraciones) return resultado if (metodo_busqueda == 'hill_climbing_random_restarts'): resultado = hill_climbing_random_restarts( problem=HnefataflProblema(None), restarts_limit=reinicios, iterations_limit=iteraciones) return resultado if (metodo_busqueda == 'simulated_annealing'): resultado = simulated_annealing(problem=HnefataflProblema(INITIAL), iterations_limit=iteraciones) return resultado
def resolver(metodo_busqueda): vwr = BaseViewer() if metodo_busqueda == "hill_climbing": result = hill_climbing(DotaArmamento(INITIAL), viewer=vwr, iterations_limit=1000) elif metodo_busqueda == "beam": result = beam(DotaArmamento(), beam_size=100, viewer=vwr, iterations_limit=1000) elif metodo_busqueda == "hill_climbing_random_restarts": result = hill_climbing_random_restarts(DotaArmamento(), restarts_limit=100, viewer=vwr, iterations_limit=1000) return result
def pixelize_image(self, method, points=None): best_state = None triangle_mask = None if points is None: best_state = cacher.best_state(self.image_name, self.max_points) if best_state is not None: triangle_mask = best_state if triangle_mask is None: triangle_mask = TriangleMask(self.width, self.height) my_problem = SplitProblem( triangle_mask, split_image=self ) if method == "astar": result = astar(my_problem, graph_search = True) elif method == "beam": result = beam(my_problem) elif method == "hill_random": result = hill_climbing_random_restarts(my_problem, 1) elif method == "hill": result = hill_climbing(my_problem) else: print("Invalid method: {}".format(method)) return print("FINAL RESULT REACHED") print("RESULT: {}".format( result.state.triangles)) # TODO: Make caching work with triangle masks cacher.persist_log( self.image_name ) triangle_mask = result.state triangles = triangle_mask.triangles self.display(triangles) if self.wait: a = input("Would you like to improve on this?\n") a = a.upper().strip() if a not in {"Y","YES","YUP","YEAH"}: return method_temp = input("Which method? [{}]\n".format(method)).strip().lower() if method_temp: method = method_temp max_points = input("How many points? [{}]\n".format(self.max_points)).strip() if max_points: self.max_points = int(max_points) return self.pixelize_image(method, points) return triangles
def resolver(metodo_busqueda, iteraciones, haz, reinicios): #problema = Hnefatafl(INICIAL) if metodo_busqueda == "hill_climbing": resultado = hill_climbing(Hnefatafl(INICIAL), iteraciones) if metodo_busqueda == "hill_climbing_stochastic": resultado = hill_climbing_stochastic(Hnefatafl(INICIAL), iteraciones) if metodo_busqueda == "hill_climbing_random_restarts": resultado = hill_climbing_random_restarts(Hnefatafl(None), reinicios, iteraciones) if metodo_busqueda == "beam": resultado = beam(Hnefatafl(INICIAL), beam_size = haz, iterations_limit = iteraciones) if metodo_busqueda == "simulated_annealing": resultado = simulated_annealing(Hnefatafl(INICIAL), iterations_limit = iteraciones) return resultado
def resolver(metodo_busqueda, iteraciones, haz, reinicios): problema = HnefataflProblem(inicial()) if metodo_busqueda == "hill_climbing": resultado = hill_climbing(problema, iteraciones) return resultado if metodo_busqueda == "hill_climbing_stochastic": return hill_climbing_stochastic(problema, iteraciones) if metodo_busqueda == "beam": return beam(problema, haz, iteraciones) if metodo_busqueda == "hill_climbing_random_restarts": return hill_climbing_random_restarts(problema, reinicios, iteraciones) if metodo_busqueda == "simulated_annealing": return simulated_annealing(problema, iterations_limit=iteraciones)
def resolver(metodo_busqueda, iteraciones, haz, reinicios): problema_inicial = Hnefatafl2(INICIAL) if metodo_busqueda == "hill_climbing": return hill_climbing(problema_inicial, iteraciones) if metodo_busqueda == "hill_climbing_stochastic": return hill_climbing_stochastic(problema_inicial, iteraciones) if metodo_busqueda == "beam": return beam(problema_inicial, haz, iteraciones) if metodo_busqueda == "hill_climbing_random_restarts": return hill_climbing_random_restarts(problema_inicial, reinicios, iteraciones) if metodo_busqueda == "simulated_annealing": return simulated_annealing(problema_inicial, iterations_limit=iteraciones)
def resolver(metodo_busqueda, iteraciones, haz, reinicios): print '··· Se van a ejecutar las 10 iteraciones para la busqueda {} ···'.format( metodo_busqueda) #print 'Haz:', haz #print 'Reinicios:', reinicios prob = HnefataflProblem(INITIAL) visor = BaseViewer() if (metodo_busqueda == 'hill_climbing'): # Ascenso de colina for x in range(10): print 'Ejecutando Iteracion {} ...'.format(x) inicio = datetime.now() resultado = hill_climbing(problem=prob, iterations_limit=iteraciones) fin = datetime.now() print 'Tiempo de iteracion {} : {} segundos.'.format( x, (fin - inicio).total_seconds()) grabar('1', max(apuntos)) elif (metodo_busqueda == 'hill_climbing_stochastic' ): # Ascenso de colina, variante estocástica for x in range(10): print 'Ejecutando Iteracion {} ...'.format(x) inicio = datetime.now() resultado = hill_climbing_stochastic(problem=prob, iterations_limit=iteraciones) fin = datetime.now() print 'Tiempo de iteracion {} : {} segundos.'.format( x, (fin - inicio).total_seconds()) grabar('2', max(apuntos)) elif (metodo_busqueda == 'beam'): # Haz local for x in range(10): print 'Ejecutando Iteracion {} ...'.format(x) inicio = datetime.now() resultado = beam(problem=HnefataflProblem(None), iterations_limit=iteraciones, beam_size=haz) fin = datetime.now() print 'Tiempo de iteracion {} : {} segundos.'.format( x, (fin - inicio).total_seconds()) grabar('3', max(apuntos)) elif (metodo_busqueda == 'hill_climbing_random_restarts' ): # Ascenso de colina con reinicios aleatorios for x in range(10): print 'Ejecutando Iteracion {} ...'.format(x) inicio = datetime.now() resultado = hill_climbing_random_restarts( problem=HnefataflProblem(None), iterations_limit=iteraciones, restarts_limit=reinicios) fin = datetime.now() print 'Tiempo de iteracion {} : {} segundos.'.format( x, (fin - inicio).total_seconds()) grabar('4', max(apuntos)) elif (metodo_busqueda == 'simulated_annealing'): # Temple simulado for x in range(10): print 'Ejecutando Iteracion {} ...'.format(x) inicio = datetime.now() resultado = simulated_annealing(problem=prob, iterations_limit=iteraciones) fin = datetime.now() print 'Tiempo de iteracion {} : {} segundos.'.format( x, (fin - inicio).total_seconds()) grabar('5', max(apuntos)) return resultado
if __name__ == '__main__': ## print 'ASCENSO DE COLINA' ## for i in range(10): ## result = hill_climbing(Hnefatafl2(INICIAL), 200) ## dibujar_tablero() ## print result.value ## ## print 'ASCENSO DE COLINA ESTOCASTICA' ## for i in range(10): ## result = hill_climbing_stochastic(Hnefatafl2(INICIAL), 200) ## dibujar_tablero() ## print result.value ## print 'HAZ LOCAL' for i in range(10): result = beam(Hnefatafl2(INICIAL), 20, 200) dibujar_tablero() print result.value ## print 'ASCENSO DE COLINA CON REINICIOS ALEATORIOS' ## for i in range(10): ## result = hill_climbing_random_restarts(Hnefatafl2(INICIAL), 20, iterations_limit = 200) ## dibujar_tablero() ## print result.value ## print 'TEMPLE SIMULADO' ## for i in range(10): ## result = simulated_annealing(Hnefatafl2(INICIAL), iterations_limit = 200) ## dibujar_tablero() ## print result.value
return -ataques def generate_random_state(self): state = [] for _ in range(8): state.append(random.randint(0, 7)) return tuple(state) result = hill_climbing(QueensProblem(INITIAL)) print 'Hill climbing:' print result.state print QueensProblem().value(result.state) result = hill_climbing_stochastic(QueensProblem(INITIAL)) print 'Hill climbing stochastic:' print result.state print QueensProblem().value(result.state) result = beam(QueensProblem(), beam_size=10) print 'Beam:' print result.state print QueensProblem().value(result.state) result = hill_climbing_random_restarts(QueensProblem(), restarts_limit=100) print 'Hill climbing random restarts:' print result.state print QueensProblem().value(result.state)
return beam(problema, haz, iteraciones) if metodo_busqueda == "hill_climbing_random_restarts": return hill_climbing_random_restarts(problema, reinicios, iteraciones) if metodo_busqueda == "simulated_annealing": return simulated_annealing(problema, iterations_limit=iteraciones) if __name__ == '__main__': problema = HnefataflProblem(inicial()) for i in range(10): # Hill Climbing con 200 iteraciones # result = hill_climbing(problema, 200) # Hill Climbing Stichastic con 200 iteraciones # result = hill_climbing_stochastic(problema, 200) # Beam con 200 iteraciones y haz de 20 result = beam(problema, 20, 200) # Hill Climbing Random Restarts con 200 iteraciones y 20 reinicios # result = hill_climbing_random_restarts(problema, 20, 200) # Simulated Annealing con 200 iteraciones # result = simulated_annealing(problema, iterations_limit=200) print 'Prueba numero: ', i + 1 print 'Estado:' print result.state print 'Valor:' print result.value
#Columnas for columna in range(3): multiplo_columna = 1 for fila in range(3): multiplo_columna = multiplo_columna * int(state[fila][columna]) suma_columnas += multiplo_columna multisumageneidad = abs(suma_filas - suma_columnas) return -multisumageneidad def generate_random_state(self): numeros = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] random.shuffle(numeros) fila1 = numeros[:3] fila2 = numeros[3:6] fila3 = numeros[6:] state = [fila1, fila2, fila3] return list2str(state) my_viewer = WebViewer() #result = hill_climbing(Entrega3(INITIAL), viewer=my_viewer) #result = hill_climbing_stochastic(Entrega3(INITIAL), viewer=my_viewer) #result = hill_climbing_random_restarts(Entrega3(), restarts_limit=15, viewer=my_viewer) result = beam(Entrega3(), beam_size=5, viewer=my_viewer) #result = simulated_annealing(Entrega3(INITIAL), iterations_limit=20, viewer=my_viewer) print result