for i in range(5): final_results_maxs = [] final_results_it = [] for p in range(2, ps + 1): print(p) fitness = mlrose.FlipFlop() istate = np.array(np.zeros(p), dtype=int) problem = mlrose.DiscreteOpt(length=p, fitness_fn=fitness, maximize=True, max_val=2) schedule = mlrose.ExpDecay(init_temp=0.5, exp_const=0.005, min_temp=0.001) best_state, best_fitness, fitness_curve = mlrose.mimic( problem, max_attempts=500, pop_size=500, keep_pct=0.3, max_iters=1000, curve=True) # Define alternative N-Queens fitness function for maximization problem # Initialize custom fitness function object # Define initial state print('The best state found is: ', best_state)
pop_size=200, mutation_prob=0.1, curve=True, max_iters=i, random_state=3) end = time.time() print('genetic algorithm @ {} iterations'.format(i)) print(best_fitness) print(end - start) print(len(c)) times_ga.append(end - start) fitns_ga.append(best_fitness) # ________________________________________________________________________________________________________________________ # Define decay schedule schedule = mlrose.ExpDecay() times_sa = [] fitns_sa = [] # Solve problem using simulated annealing for i in iterations: start = time.time() best_state, best_fitness, c = mlrose.simulated_annealing(problem_fit, schedule=schedule, max_attempts=10, max_iters=i, init_state=None, curve=True, random_state=3) end = time.time() print('sa algorithm @ {} iterations'.format(i)) print(best_fitness)