def main(): name_of_exp = "Eight Queens" fitness = mlrose.CustomFitness(queens_max) problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=True, max_val=8) # Define initial state mimic = [] init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7]) for i in [mlrose.ExpDecay(), mlrose.GeomDecay(), mlrose.ArithDecay()]: best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing( problem, init_state=init_state, schedule=i, max_attempts=1000, max_iters=1000, curve=True, random_state=1) mimic.append(learning_curve) print(i) print(best_fitness) for x, z in zip(['Exp', 'Geom', 'Arith'], mimic): plt.plot(z, label=str(x)) plt.legend() plt.title( 'SA Randomized Optimization DecaySchedule vs Fitness Curve (8-Queens)') plt.xlabel('Function iteration count') plt.ylabel('Fitness function value') plt.show()
def tsp_factory(length=10, min_nodeval=1, max_nodeval=20): coords = [] for i in range(length): node = ( np.random.randint(min_nodeval, max_nodeval), np.random.randint(min_nodeval, max_nodeval), ) while node in coords: node = ( np.random.randint(min_nodeval, max_nodeval), np.random.randint(min_nodeval, max_nodeval), ) coords.append(node) fitness = count_evaluations(mlrose.TravellingSales, coords) fitness_final = mlrose.CustomFitness(fitness) fitness_final.get_prob_type = ( lambda: "tsp" ) # Just a hack to make it work with a custom function. problem = mlrose.TSPOpt(length=length, fitness_fn=fitness_final, maximize=False) dists = generate_distances(coords) optimum, _ = held_karp(dists) return problem, optimum
def optimize(state_length, fitness_fn, algorithm, algorithm_kwargs, **cust_fitness_fn_kwargs): """Uses optimization techniques to identify binary state vector that minimizes fitness (MOER) over forecast period. Args: state_length (int): length of state_vector to be optimized (minimized). (represents length of forecast in periods) ex: 1 hr forecast series given in 5 minute periods would have a length of 12. fitness_fn: callable Function for calculating fitness of a state with the signature fitness_fn(state, **kwargs) algorithm (mlrose optimization object): One of: mlrose.simulated_anneling, mlrose.random_hill_climb mlrose.hill_climb, mlrose.genetic_alg, or mlrose.mimic. See mlrose documentation for details. algorithm_kwargs (dict): kwargs for mlrose optimization algorithims. Returns: best_state: (array) Numpy array containing state that optimizes the fitness function. best_fitness: (float) Value of fitness (MOER) at best state. curve: (array) Numpy array containing the fitness at every iteration. Must include in kwargs curve=True. """ #create custom fitness class using mlrose constructor cust_fitness_fn = mlrose.CustomFitness(fitness_fn, **cust_fitness_fn_kwargs) #define problem using mlrose constructor prob = mlrose.DiscreteOpt(length=state_length, fitness_fn=cust_fitness_fn, maximize=False, max_val=2) #set initial state using heuristic to accelerate optimization init_state = initial_state(cust_fitness_fn_kwargs.get('fridge_temp'),state_length=state_length) #use mlrose optimization algo to find state vector with minimum MOER best_state, best_fitness, curve = algorithm(prob,init_state=init_state, **algorithm_kwargs) return best_state, best_fitness, curve
def base_test(algorithm, kwargs=None): x_train, x_test, y_train, y_test = load_data() model = get_model(len(x_train.keys()), len(y_train[0])) fitness_kwargs = {'model': model, 'x_train': x_train, 'y_train': y_train} fitness = mlrose.CustomFitness(propagate, **fitness_kwargs) problem = mlrose.ContinuousOpt(model.count_params(), fitness, maximize=False, min_val=-0.5, max_val=0.5, step=0.01) if kwargs is None: best_state, best_fitness, history = algorithm(problem, curve=True) else: best_state, best_fitness, history = algorithm(problem, curve=True, **kwargs) algorithm_name = repr(algorithm).split(' ')[1] np.save(f'nn_{algorithm_name}_history', history) update_model(model, best_state) np.save(f'nn_{algorithm_name}_weights', np.array(model.get_weights())) print(f'{algorithm_name}: {model.evaluate(x_test, y_test, verbose=False)[1] * 100}% of accuracy') plot.style.use('seaborn-darkgrid') plot.title(f'Training history for {algorithm_name}') plot.ylabel('Categorical crossentropy') plot.xlabel('Epoch') plot.plot(history) plot.show()
def queens_problem(max_attempts, max_iters): fitness_cust = mlrose.CustomFitness(queens_max) problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=False, max_val=8) #problem = mlrose.DiscreteOpt(length=8, fitness_fn=queens_max, maximize=True, max_val=8) # Define decay schedule schedule = mlrose.ExpDecay() # Define initial state init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7]) # Solve problem using simulated annealing best_state, best_fitness = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=max_attempts, max_iters=max_iters, init_state=init_state, random_state=1) print(best_state) print(best_fitness)
def flipflop_factory(length=30): fitness = count_evaluations(mlrose.FlipFlop) fitness_final = mlrose.CustomFitness(fitness) flipflop = mlrose.DiscreteOpt(length, fitness_final) global_optimum = length - 1 return flipflop, global_optimum
def gen_problem_pairiodic(problem_size): fitness = mlr.CustomFitness(fitness_fn=pairiodic6, problem_type='discrete') maximize = True problem = mlr.DiscreteOpt(length=problem_size, fitness_fn=fitness, maximize=maximize, max_val=2) return problem, maximize
def queens(max_iter=500, early_stop=None, mimic_early_stop=100, n_runs=10, savedir=None): print('\n\n|========= N Queens =========|\n') fitness = mlrose.CustomFitness(queens_max) problem_size = [8, 64, 128] max_attempts = max_iter * 2 if early_stop is None else early_stop mimic_early_stop = max_attempts if mimic_early_stop is None else mimic_early_stop hyperparams = { 'rhc': { 'restarts': 0, 'max_attempts': max_attempts }, 'mimic': { 'pop_size': 500, 'keep_pct': 0.1, 'max_attempts': mimic_early_stop, 'fast_mimic': True }, 'sa': { 'schedule': mlrose.GeomDecay(), 'init_state': None, 'max_attempts': max_attempts }, 'ga': { 'pop_size': 500, 'mutation_prob': 0.2, 'pop_breed_percent': 0.6, 'elite_dreg_ratio': 0.95, 'max_attempts': mimic_early_stop } } print('Hyperparameters: ', hyperparams) results = [] runtimes = [] timings = {} for ps in problem_size: problem = mlrose.DiscreteOpt(ps, fitness, max_val=2, maximize=True) print('Running with input size', ps) print('-----------------------------') r, t, wt = util.optimize_iters(problem, max_iter, hyperparams, n_runs) results.append(r) runtimes.append(t) timings['ps{}'.format(ps)] = wt print('final runtimes') t = pd.DataFrame(runtimes, index=problem_size) print(t) if savedir: util.save_output('flipflop', savedir, t, results, timings, problem_size) return t, results, timings
def fourpeaks_factory(length=50, t_pct=0.1): fourpeaks_fitness = count_evaluations(mlrose.FourPeaks, t_pct=t_pct) fourpeaks_fitness_final = mlrose.CustomFitness(fourpeaks_fitness) fourpeaks = mlrose.DiscreteOpt(length=length, fitness_fn=fourpeaks_fitness_final) T = int(t_pct * length) global_optimum = 2 * length - T - 1 return fourpeaks, global_optimum
def queens_factory(length=8): fitness = count_evaluations(QueensCustom) fitness_final = mlrose.CustomFitness(fitness) problem = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_final, max_val=length) global_optimum = int(comb(length, 2)) # I think? return problem, global_optimum
def run_optimization_loop(self, sentence, init_state): fitness_function = mlrose.CustomFitness(partial(self.scoringNN.score, sentence)) problem = mlrose.DiscreteOpt(length=len(sentence), fitness_fn=fitness_function, maximize=True, max_val=len(ALL_TAGS)) try: best_state, best_fitness = self.search_class(problem, init_state=init_state, **self.search_params) except TypeError: best_state, best_fitness = self.search_class(problem, **self.search_params) return best_state
def shortest_path(coords, start, end): tsp_fitness = mlrose.TravellingSales(coords=coords) def filter_endpoints(state): if state[0] == start and state[-1] == end: return tsp_fitness.evaluate(state) else: return math.inf fitness = mlrose.CustomFitness(filter_endpoints, 'tsp') problem = mlrose.TSPOpt(length=len(coords), fitness_fn=fitness, maximize=False) path, length = mlrose.genetic_alg(problem, random_state=2) return [coords[i] for i in path]
def queens_max(state): # Initialize counter fitness_cnt = 0 # For all pairs of queens for i in range(len(state) - 1): for j in range(i + 1, len(state)): # Check for horizontal, diagonal-up and diagonal-down attacks if (state[j] != state[i]) and (state[j] != state[i] + (j - i)) and (state[j] != state[i] - (j - i)): # If no attacks, then increment counter fitness_cnt += 1 return fitness_cnt # Initialize custom fitness function object fitness_cust = mlrose.CustomFitness(queens_max)
def knapsack_factory(N_items=20, max_weight=20, max_value=20, max_weight_pct=0.6): weights = np.random.randint(1, high=max_weight, size=N_items) values = np.random.randint(1, high=max_value, size=N_items) capacity = int(np.ceil(max_weight_pct * weights.sum())) global_optimum = calculate_knapsack_global_optimum( values, weights, N_items, capacity) knapsack_fitness = count_evaluations(mlrose.Knapsack, weights, values, max_weight_pct) knapsack_fitness_final = mlrose.CustomFitness(knapsack_fitness) knapsack = mlrose.DiscreteOpt(length=N_items, fitness_fn=knapsack_fitness_final) return knapsack, global_optimum
def main(): # want to maximize this fitness = mlrose.CustomFitness(detected_max) problem = mlrose.DiscreteOpt(length=24, fitness_fn=fitness, maximize=True, max_val=scale_factor) schedule = mlrose.ExpDecay() best_state, max_faces = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=10, max_iters=1000, init_state=initial_state, random_state=1) print('Optimal state found: ', best_state) print('Max fitness found: ', max_faces) # save the optimal found get_img_from_state(best_state) print("Number of faces in output: ", len(detect_faces(cv2.imread(OUTPUT))))
def main(): # mlrose fitness_cust = mlrose.CustomFitness(simulate) problem = mlrose.DiscreteOpt(fitness_fn=fitness_cust, maximize=False, length=2, max_val = 30) schedule = mlrose.ExpDecay() init_state = np.array([28,7]) best_time_values, best_avg_time = mlrose.simulated_annealing(problem, schedule = schedule, max_attempts = 10, max_iters = 100, init_state = init_state, random_state = 1) print("Best time values:") print(best_time_values) print("Best avg time:") print(best_avg_time)
def fitness_function(f, bits, rs, verbose): if verbose: print('\n\n----------', f, ':', bits, 'bits ----------') if f == 'Four Peaks': fitness_fn = mlrose.FourPeaks( t_pct=0.15 ) # Note: T= np.ceil(t_pct * n), per source code for FourPeaks.evaluate elif f == 'MaxKColor': # fitness_fn = mlrose.MaxKColor(edges) # default mlrose fitness function # edges = [(0, 1), (0, 2), (1, 3), (2, 3)] # 4 nodes, 2 by 2 grid, no diagonals # edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)] edges = generate_graph(bits) kwargs = {'edges': edges} fitness_fn = mlrose.CustomFitness( kcolors_max, **kwargs) # custom fitness function for maximization problem elif f == 'Knapsack': # weights = [10, 5, 2, 8, 15] # values = [1, 2, 3, 4, 5] weights, values = generate_knapsack(bits, rs) if verbose: print('\nKnapsack\n', weights, values) max_weight_pct = 0.6 fitness_fn = mlrose.Knapsack(weights, values, max_weight_pct) elif f == 'FlipFlop': fitness_fn = mlrose.FlipFlop() # Check fitness for ad-hoc states # test_state = np.array([1, 0, 1, 1, 0]) # print("Fitness for test_state", test_state, ":", fitness_fn.evaluate(test_state)) return fitness_fn
def main(): name_of_exp = "Eight Queens" fitness = mlrose.CustomFitness(queens_max) problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=True, max_val=8) # Define initial state init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7]) x_s = [] y_s = [] z_s = ['RHC', 'SA', 'GA', 'MIMIC'] w_s = [] max_val = 28.0 found_flag = False for restarts in np.arange(0, 5): if found_flag: break for max_iter_atts in np.arange(10, 1000, 10): if found_flag: break # Solve problem using simulated annealing best_state, best_fitness, learning_curve, timing_curve = mlrose.random_hill_climb( problem, max_attempts=int(max_iter_atts), max_iters=int(max_iter_atts), restarts=int(restarts), init_state=init_state, curve=True, random_state=1) if best_fitness == max_val: x_s.append(np.arange(0, len(learning_curve))) y_s.append(learning_curve) w_s.append(timing_curve) print(best_state) print(best_fitness) print(max_iter_atts) print(restarts) found_flag = True found_flag = False for sched in [mlrose.ExpDecay(), mlrose.GeomDecay(), mlrose.ArithDecay()]: if found_flag: break for max_iter_atts in np.arange(10, 1000, 10): if found_flag: break best_state, best_fitness, learning_curve, timing_curve = mlrose.simulated_annealing( problem, max_attempts=int(max_iter_atts), max_iters=int(max_iter_atts), schedule=sched, init_state=init_state, curve=True, random_state=1) if best_fitness == max_val: x_s.append(np.arange(0, len(learning_curve))) y_s.append(learning_curve) w_s.append(timing_curve) print(best_state) print(best_fitness) print(max_iter_atts) print(sched) found_flag = True found_flag = False for prob in np.arange(0.1, 1.1, 0.1): if found_flag: break for pop_size in np.arange(100, 1000, 100): if found_flag: break for max_iter_atts in np.arange(100, 1000, 100): if found_flag: break best_state, best_fitness, learning_curve, timing_curve = mlrose.genetic_alg( problem, pop_size=int(pop_size), mutation_prob=prob, max_attempts=int(max_iter_atts), max_iters=int(max_iter_atts), curve=True, random_state=1) if best_fitness == max_val: x_s.append(np.arange(0, len(learning_curve))) y_s.append(learning_curve) w_s.append(timing_curve) print(best_state) print(best_fitness) print(max_iter_atts) print(prob) print(pop_size) found_flag = True found_flag = False for prob in np.arange(0.1, 0.5, 0.1): if found_flag: break for pop_size in np.arange(100, 1000, 100): if found_flag: break for max_iter_atts in np.arange(100, 1000, 100): if found_flag: break best_state, best_fitness, learning_curve, timing_curve = mlrose.mimic( problem, pop_size=int(pop_size), keep_pct=prob, max_attempts=int(max_iter_atts), max_iters=int(max_iter_atts), curve=True, random_state=1, fast_mimic=True) if best_fitness == max_val: x_s.append(np.arange(0, len(learning_curve))) y_s.append(learning_curve) w_s.append(timing_curve) print(best_state) print(best_fitness) print(max_iter_atts) print(prob) print(pop_size) found_flag = True for x, y, z in zip(x_s, y_s, z_s): plt.plot(x, y, label=z) plt.legend() plt.title( 'Randomized Optimization Iterations vs Fitness Function Value for {}'. format(name_of_exp)) plt.xlabel('Function iteration count') plt.ylabel('Fitness function value') plt.show() plt.clf() for x, w, z in zip(x_s, w_s, z_s): plt.plot(x, w, label=z) plt.legend() plt.title( 'Randomized Optimization Time vs Fitness Function Value for {}'.format( name_of_exp)) plt.xlabel('Function iteration count') plt.ylabel('Time in Seconds') plt.show()
def my_best_fitness(arr): print("Erger") return np.maximum.accumulate(arr) def my_reach_max(arr, value): args_max = np.argwhere(arr == value) if len(args_max) == 0: return None else: return args_max[0][0] / 1000.0 fitness = mlrose.CustomFitness(my_fitness) lengths = [10, 20, 30, 40, 50, 60, 70, 80] max_fitness = [11, 21, 31, 41, 51, 61, 71, 81] # Should be manually written index_plot = 5 # What type curves_annealing = [] curves_ga = [] curves_rhl = [] curves_mimic = [] times_annealing = [] times_ga = [] times_rhl = [] times_mimic = []
# Multi Peak def multipeak_eq(state): x = state[0] + 2 * state[1] + 4 * state[2] + 8 * state[3] + 16 * state[ 4] + 32 * state[5] + 64 * state[6] + 128 * state[7] + 256 * state[ 8] + 512 * state[9] y = state[10] + 2 * state[11] + 4 * state[12] + 8 * state[13] + 16 * state[ 14] + 32 * state[15] + 64 * state[16] + 128 * state[17] + 256 * state[ 18] + 512 * state[19] z = max( 0, 10 * np.sin(x / 20 + 2.3) + 4 * (x % 10) + 10 * np.sin(y / 25 + 1) + 4 * (y % 15) + 20) return z multipeak_fn = mlrose.CustomFitness(multipeak_eq) # Knapsack knapsack_weights = [5, 10, 15, 20, 25, 30, 35, 40, 45] knapsack_values = np.arange(1, len(knapsack_weights) + 1) knapsack_max_weight = 0.7 knapsack_fn = mlrose.Knapsack(knapsack_weights, knapsack_values, knapsack_max_weight) # K-colors kcolor_edges = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 4), (1, 5), (1, 6), (1, 7), (2, 3), (2, 5), (2, 7), (3, 5), (3, 6), (3, 7), (3, 9), (4, 5), (5, 6), (5, 7), (5, 8), (6, 7), (7, 8), (8, 9)] kcolor_edges = list({tuple(sorted(edge)) for edge in kcolor_edges}) kcolor_fn = mlrose.MaxKColor(kcolor_edges)
fourpeaks_problem = mlrose.DiscreteOpt(length=60, maximize=True, max_val=2, fitness_fn=fourpeaks) base_test(fourpeaks, theoretical_best_fitness=lambda x: 2 * x - 0.1 * x - 1) # 73s optimize_ga(fourpeaks_problem) # 768s optimize_sa(fourpeaks_problem) # 532s optimize_rhc(fourpeaks_problem) # 822s optimize_mimic(fourpeaks_problem) # 2464s final_test(fourpeaks_problem, [700, 0.4], mlrose.ExpDecay(8, 0.00001), 5000, [500, 0.022]) # 1191s # SAW saw_fitness = mlrose.CustomFitness(saw, problem_type='discrete') saw_problem = mlrose.DiscreteOpt(length=700, maximize=True, max_val=2, fitness_fn=saw_fitness) base_test(saw_fitness, theoretical_best_fitness=lambda x: x, lengths=range(200, 701, 100)) # 2476s optimize_ga(saw_problem) # 7255s optimize_sa(saw_problem) # 8730s optimize_rhc(saw_problem) # 103s optimize_mimic(saw_problem) # 2192s final_test(saw_problem, [600, 0.001], mlrose.ExpDecay(5, 0.0007), 1500, [500, 0.075]) # 14808
) ap.add_argument( "-a", "--algo", type=int, default=0, help= "Use which of the algorithms: 0) randomized hill climbing 1) simulated annealing \ 2) genetic algorithm 3) MIMIC, default: 0") params = vars(ap.parse_args()) num_queen = params["number"] max_iter = params["iteration"] max_atp = max_iter / 10 #fitness funtion queens_fitness = mlrose.CustomFitness(queens_max) #optimization problems: DiscreteOpt / ContinuousOpt / TSPOpt # discrete problem = mlrose.DiscreteOpt(length=num_queen, fitness_fn=queens_fitness, maximize=True, max_val=num_queen) if params["problem"] == 1: # continuous # ContinuousOpt(length, fitness_fn, maximize=True, min_val=0, max_val=1, step=0.1) problem = mlrose.ContinuousOpt(length=num_queen, fitness_fn=queens_fitness, maximize=True, max_val=num_queen) elif params["problem"] == 2: # TSP #TSPOpt(length, fitness_fn=None, maximize=False, coords=None, distances=None)
id_voo += 1 volta = voos[(destino, origem)][agenda[id_voo]] total_preco += volta[2] return total_preco #------------------------------------------------------------------------------------------------------------ #pip install mlrose --> iNSTALANDO MLROSE import six import sys sys.modules['sklearn.externals.six'] = six import mlrose fitness = mlrose.CustomFitness( fitness_function ) #retorna o preco dos voos (temos uma funcao personalizada de fitness) problema = mlrose.DiscreteOpt(length=12, fitness_fn=fitness, maximize=False, max_val=10) #(length =tamanho da solucao (12 voos),objeto que criamos acima #maximize = True -> maximiza o valor retornado, maior preco #maximize = False -> minimiza o valor retornado, menor preco #max_val = o algoritmo precisa gerar uma lista com 12 posicoes e o valor maximo é 10 #max_val é a quantidade de voos que temos #Algoritmo gera uma lista com 12 posicoes e em cada posicao pode variar entre 0 e 9 (10 numeros) # HILL CLIMB ------------------------------------------------------------------------------------------ """## Hill climb
# FITNESS FUNCTION def fitness_function(solucao): custo = 0 soma_espaco = 0 for i in range(len(solucao)): if solucao[i] == 1: custo += produtos[i][2] soma_espaco += produtos[i][1] if soma_espaco > espaco_disponivel: custo = 1 return custo fitness_function([0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1]) fitness = mlrose.CustomFitness( fitness_function) # CUSTOM FITNESS FUNCTION DO MLROSE problema = mlrose.DiscreteOpt( length=14, fitness_fn= fitness, #14 PRODUTOS DIFERENTES, PARA CADA UM PODE ASSUMIR 2 VALORES (0 E 1) maximize=True, max_val=2) """## Hill climb""" melhor_solucao, melhor_custo = mlrose.hill_climb(problema) melhor_solucao, melhor_custo imprimir_solucao(melhor_solucao) """## Simulated annealing""" melhor_solucao, melhor_custo = mlrose.simulated_annealing(problema)
import numpy as np import matplotlib.pyplot as plt import plotly.express as px import six sys.modules['sklearn.externals.six'] = six import mlrose import sys sys.path.insert(1, 'C:/Users/André Viniciu/OneDrive/Pasta/Documentos/Python_ML_Financas/Codigos') from Alocacao_Otimizacao import * # Variaveis p/ Fitness # fitness_function(dtset, acoes_pesos, sem_risco, dinheiro_total) # fitness_function(dtset, acoes_pesos, 0.000378, 5000) fitness = mlrose.CustomFitness(fitness_function) # Maximizacao problema_maximizacao = mlrose.ContinuousOpt(length=6 ,fitness_fn=fitness ,maximize=True ,min_val=0 ,max_val=1) # Minimizacao problema_minimizacao = mlrose.ContinuousOpt(length=6 ,fitness_fn=fitness ,maximize=False ,min_val=0 ,max_val=1)
def perform_mimic_analysis(problems, graph_map): for fitness_func in problems: function_name = fitness_func.__name__ if function_name not in graph_map: graph_map[function_name] = {} fitness = mlrose.CustomFitness(fitness_func) problem = mlrose.DiscreteOpt(length=16, fitness_fn=fitness, maximize=True) # pop_size parameter = 'default_pop_size' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} intervals = 20 interval_size = 20 section_scores = [] for i in range(1, intervals + 1): pop_size = i * interval_size best_state, best_fitness = mlrose.mimic(problem, pop_size=pop_size, random_state=7) print(best_state, best_fitness) section_scores.append([pop_size, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') parameter = 'best_pop_size' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} section_scores = [] for i in range(1, intervals + 1): pop_size = i * interval_size best_state, best_fitness = mlrose.mimic( problem, pop_size=pop_size, keep_pct=best_keep_pct, max_attempts=best_max_attempts, max_iters=best_max_iters, random_state=7) print(best_state, best_fitness) section_scores.append([pop_size, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') # keep_pct parameter = 'default_keep_pct' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} intervals = 20 interval_size = 0.02 section_scores = [] for i in range(1, intervals + 1): keep_pct = i * interval_size best_state, best_fitness = mlrose.mimic(problem, keep_pct=keep_pct, random_state=7) print(best_state, best_fitness) section_scores.append([keep_pct, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') parameter = 'best_keep_pct' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} section_scores = [] for i in range(1, intervals + 1): keep_pct = i * interval_size best_state, best_fitness = mlrose.mimic( problem, pop_size=best_pop_size, keep_pct=keep_pct, max_attempts=best_max_attempts, max_iters=best_max_iters, random_state=7) print(best_state, best_fitness) section_scores.append([keep_pct, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') # max_attempt parameter = 'default_max_attempt' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} intervals = 20 interval_size = 1 section_scores = [] for i in range(1, intervals + 1): max_attempts = i * interval_size best_state, best_fitness = mlrose.mimic(problem, max_attempts=max_attempts, random_state=7) print(best_state, best_fitness) section_scores.append([max_attempts, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') parameter = 'best_max_attempt' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} section_scores = [] for i in range(1, intervals + 1): max_attempts = i * interval_size best_state, best_fitness = mlrose.mimic(problem, pop_size=best_pop_size, keep_pct=best_keep_pct, max_attempts=max_attempts, max_iters=best_max_iters, random_state=7) print(best_state, best_fitness) section_scores.append([max_attempts, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') # max_iters parameter = 'default_max_iters' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} intervals = 20 interval_size = 1 section_scores = [] for i in range(1, intervals + 1): max_iters = i * interval_size best_state, best_fitness = mlrose.mimic(problem, max_iters=max_iters, random_state=7) print(best_state, best_fitness) section_scores.append([max_iters, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') parameter = 'best_max_iters' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} section_scores = [] for i in range(1, intervals + 1): max_iters = i * interval_size best_state, best_fitness = mlrose.mimic( problem, pop_size=best_pop_size, keep_pct=best_keep_pct, max_iters=max_iters, max_attempts=best_max_attempts, random_state=7) print(best_state, best_fitness) section_scores.append([max_iters, best_fitness]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png') # time parameter = 'time' if parameter not in graph_map[function_name]: graph_map[function_name][parameter] = {} intervals = 20 section_scores = [] for i in range(0, intervals): start = timer() best_state, best_fitness = mlrose.mimic( problem, pop_size=best_pop_size, keep_pct=best_keep_pct, max_iters=best_max_iters, max_attempts=best_max_attempts, random_state=7) end = timer() time = end - start section_scores.append([i, time]) graph_map[function_name][parameter][algorithm] = section_scores plot_frame = pd.DataFrame(section_scores, columns=[parameter, "Fitness"]) title = function_name + '-' + parameter graph = plot_frame.plot(x=parameter, y='Fitness', title=title) graph.set_xlabel(parameter) graph.set_ylabel("Fitness") plt.savefig(graph_directory + '/' + title + '.png')
def queens_max(state): # Initialize counter fitness_cnt = 0 # For all pairs of queens for i in range(len(state) - 1): for j in range(i + 1, len(state)): # Check for horizontal, diagonal-up and diagonal-down attacks if (state[j] != state[i]) \ and (state[j] != state[i] + (j - i)) and (state[j] != state[i] - (j - i)): # If no attacks, then increment counter fitness_cnt += 1 return fitness_cnt # Initialize custom fitness function object fitness_cust = mlrose.CustomFitness(queens_max) # Initialize fitness function object using pre-defined class fitness = mlrose.Queens() # Define optimization problem object problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness_cust, maximize=True, max_val=8) # Define decay schedule schedule = mlrose.ExpDecay() # Start Timer from timeit import default_timer as timer start = timer() # Run Randomized Hill Climbing
best_fitness = 0 best_state, best_fitness = mlrose.mimic(problem, pop_size = 200, keep_pct = 0.2, max_attempts = 10, max_iters = iter) #print(best_state) MM.append(best_fitness) print(best_fitness) time_MM.append((time.time() - start_time)) plot(RHC, SA, GA, MM, time_RHC, time_SA, time_GA, time_MM, iterations) filewrite_array("iterations:", iterations) filewrite_array("Fitness(RHC):", RHC) filewrite_array("Fitness(SA):", SA) filewrite_array("Fitness(GA):", GA) filewrite_array("Fitness(MM):", MM) filewrite_array("Fitness(time_RHC):", time_RHC) filewrite_array("Fitness(time_SA):", time_SA) filewrite_array("Fitness(time_GA):", time_GA) filewrite_array("Fitness(time_MM):", time_MM) state = np.array([0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0]) def cust_fn(state): arr = np.reshape(state, (-1, 8)) ans = 1 for row in arr: integer = row.dot(1 << np.arange(row.size)[::-1]) ans *= (math.pow((integer % 13),2) % 7) + abs(math.sin(integer)) return ans fitness = mlrose.CustomFitness(cust_fn) print(fitness.evaluate(state)) fit(48, fitness) file.close()
def run_experiment(in_fitness_function, max_val): lengths = [10, 20, 30, 50, 100] ga_fitnesses = [] sa_fitnesses = [] mi_fitnesses = [] rhc_fitnesses = [] ga_func_evals, sa_func_evals, mi_func_evals, rhc_func_evals = [], [], [], [] rhc_iters, ga_iters, sa_iters, mi_iters = [], [], [], [] rhc_curves, sa_curves, ga_curves, mi_curves = [], [], [], [] rhc_times, sa_times, ga_times, mi_times = [], [], [], [] fitness = mlrose.CustomFitness(in_fitness_function) for length in lengths: #init_state = np.array([0]*length) init_state = np.random.randint(0, max_val, length) rhc_fitness, rhc_func_eval, rhc_iter, rhc_curve, rhc_time, \ sa_fitness, sa_func_eval, sa_iter, sa_curve, sa_time, \ ga_fitness, ga_func_eval, ga_iter, ga_curve, ga_time, \ mi_fitness, mi_func_eval, mi_iter, mi_curve, mi_time = generate_results(fitness, init_state, max_val) rhc_fitnesses.append(rhc_fitness) sa_fitnesses.append(sa_fitness) ga_fitnesses.append(ga_fitness) mi_fitnesses.append(mi_fitness) rhc_iters.append(rhc_iter) sa_iters.append(sa_iter) ga_iters.append(ga_iter) mi_iters.append(mi_iter) rhc_times.append(rhc_time) sa_times.append(sa_time) ga_times.append(ga_time) mi_times.append(mi_time) rhc_func_evals.append(rhc_func_eval) sa_func_evals.append(sa_func_eval) ga_func_evals.append(ga_func_eval) mi_func_evals.append(mi_func_eval) # best_fitness = [sa_fitnesses.max(), ga_fitnesses.max(), mi_fitnesses.max()] plt.figure(1) plt.plot(lengths, rhc_fitnesses) plt.plot(lengths, sa_fitnesses, '--+') plt.plot(lengths, ga_fitnesses, '--o') plt.plot(lengths, mi_fitnesses, '--') plt.legend([ 'Randomized Hill Climbing', 'Simulated Annealing', 'Genetic Algorithm', 'MIMIC' ], fontsize=11) plt.title('Average fitness of each algorithm', fontsize=14) plt.xlabel('Problem length (bits)', fontsize=14) plt.ylabel('Average fitness', fontsize=14) plt.xticks(lengths) plt.figure(2) plt.plot(lengths, rhc_iters) plt.plot(lengths, sa_iters, '--+') plt.plot(lengths, ga_iters, '--o') plt.plot(lengths, mi_iters, '--') plt.legend([ 'Randomized Hill Climbing', 'Simulated Annealing', 'Genetic Algorithm', 'MIMIC' ], fontsize=11) plt.title('Average iterations required', fontsize=14) plt.xlabel('Problem length (bits)', fontsize=14) plt.ylabel('Required iterations', fontsize=14) plt.xticks(lengths) plt.figure(3) plt.plot(rhc_curve) plt.plot(sa_curve, '--+') plt.plot(ga_curve, '--o') plt.plot(mi_curve, '--') plt.legend([ 'Randomized Hill Climbing', 'Simulated Annealing', 'Genetic Algorithm', 'MIMIC' ], fontsize=11) plt.title('Fitness at each iternation', fontsize=14) plt.xlabel('Iteration', fontsize=14) plt.ylabel('Fitness', fontsize=14) plt.figure(4) plt.plot(lengths, rhc_times) plt.plot(lengths, sa_times, '--+') plt.plot(lengths, ga_times, '--o') plt.plot(lengths, mi_times, '--') plt.legend([ 'Randomized Hill Climbing', 'Simulated Annealing', 'Genetic Algorithm', 'MIMIC' ], fontsize=11) plt.title('Average time required', fontsize=14) plt.xlabel('Problem length (bits)', fontsize=14) plt.ylabel('Time (s)', fontsize=14) plt.xticks(lengths) plt.figure(5) plt.plot(lengths, rhc_func_evals) plt.plot(lengths, sa_func_evals, '--+') plt.plot(lengths, ga_func_evals, '--o') plt.plot(lengths, mi_func_evals, '--') plt.legend([ 'Randomized Hill Climbing', 'Simulated Annealing', 'Genetic Algorithm', 'MIMIC' ], fontsize=11) plt.title('Average function evaluations required', fontsize=14) plt.xlabel('Problem length (bits)', fontsize=14) plt.ylabel('Required function evaluations', fontsize=14) plt.xticks(lengths) plt.show()
if (best_fitness.max().max() > 1000000): axs[1].set_ylabel(r"$\frac{fitness}{10^{N}}$") best_fitness.apply(lambda x: x / np.power(10, x.name / 10), axis=1).plot(ax=axs[1]) else: best_fitness.plot(ax=axs[1]) axs[1].set_ylabel("fitness") axs[1].legend() return fig continuous_peaks = mlrose.ContinuousPeaks(t_pct=0.1) six_peaks = mlrose.SixPeaks(t_pct=0.1) flip_flop = mlrose.FlipFlop() product_consec_ones = mlrose.CustomFitness(fitness_fn=prod_consec_one, problem_type="discrete") count_ones = mlrose.CustomFitness(fitness_fn=lambda state: sum(state), problem_type="discrete") convert_bin_swap = mlrose.CustomFitness(fitness_fn=func_convert_bin_swap, problem_type="discrete") if __name__ == "__main__": records_by_prob = {six_peaks: [], flip_flop: [], convert_bin_swap: []} nbits = range(10, 101, 10) for fitness_fn, records in records_by_prob.items(): print(fitness_fn) for nbit in nbits: print(f"nbit={nbit}") records.extend( run_one(fitness_fn=fitness_fn, nbit=nbit, algos=algos))