import numpy as np import mlrose.mlrose as mlrose import pandas as pd import pickle ps = 32 mimic_maxs = [] mimic_its = [] for i in range(5): final_results_maxs = [] final_results_it = [] for p in range(2, ps + 1): print(p) fitness = mlrose.FourPeaks() 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,
import numpy as np import mlrose.mlrose as mlrose import pandas as pd import pickle ps = 32 results = [] for i in range(10, 1000, 25): fitness = mlrose.FourPeaks(t_pct=0.15) istate = np.array(np.zeros(ps), dtype=int) problem = mlrose.DiscreteOpt(length=ps, 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=200, keep_pct=0.3, max_iters=i, random_state=1, 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)
# import mlrose from mlrose import mlrose import numpy as np import matplotlib.pyplot as plt import time from util import plot_fitness_curve np.random.seed(seed=0) # Initialize fitness function object fitness_fn = mlrose.FourPeaks(t_pct=0.1) state = np.random.randint(low=0, high=2, size=1000) # Define optimization problem object problem_fit = mlrose.DiscreteOpt(length=len(state), fitness_fn=fitness_fn, maximize=True, max_val=2) iterations = [1, 10, 100, 1000, 10000] # ________________________________________________________________________________________________________________________ times_ga = [] fitns_ga = [] # Solve problem using the genetic algorithm for i in iterations: start = time.time() best_state, best_fitness, c = mlrose.genetic_alg(problem_fit, pop_size=200, mutation_prob=0.1, curve=True, max_iters=i,