def sa(self): print("Creating SA curve") problem, init_state = self.get_prob(t_pct=0.15) plt.close() plt.figure() for schedule, s_str in zip( [mlrose.GeomDecay(), mlrose.ArithDecay(), mlrose.ExpDecay()], ['GeomDecay', 'ArithDecay', 'ExpDecay']): _, _, fitness_curve = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=100, max_iters=5000, init_state=init_state, curve=True) plt.plot(fitness_curve, label="schedule={}".format(s_str)) plt.title("{}: Simulated Annealing".format(self.prob_name)) plt.legend(loc="best") plt.xlabel('Number of Iterations') plt.ylabel('Fitness') plt.savefig( os.path.join(self.output_path, "{}_SA Analysis.png".format(self.prob_name)))
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 sim_anneal(prob, **kwargs): start = time.time() _, best_score, curve, fit_evals = mlrose.simulated_annealing(prob(), curve=True, **kwargs) end = time.time() return np.array([best_score, len(curve), fit_evals, end - start])
def max_iterations(n,cords,fit,temp,mint): best_fit1=[] best_fit2=[] best_fit3=[] max_iter=[] for i in range(100,n,10): best_state,best_fitness1 = mlrose.genetic_alg(problem_fit, mutation_prob = 0.2, max_attempts = 100,max_iters=i) best_state,best_fitness2 = mlrose.mimic(problem_fit,pop_size=200,keep_pct=0.3,max_attempts=10,max_iters=i) best_state,best_fitness3 = mlrose.simulated_annealing(problem_fit,schedule=mlrose.GeomDecay(init_temp=temp,decay=0.9,min_temp=mint),max_attempts=100,max_iters=i,init_state=None) max_iter.append(i) best_fit1.append(best_fitness1) best_fit2.append(best_fitness2) best_fit3.append(best_fitness3) print(best_fit1,best_fit2,best_fit3) max_iter=np.asarray(max_iter) best_fit1=np.asarray(best_fit1) best_fit2=np.asarray(best_fit2) best_fit3=np.asarray(best_fit3) line1, = plt.plot(max_iter,best_fit1,color='r',label='fitness_score') line2, = plt.plot(max_iter,best_fit2,color='g',label='fitness_score') line3, = plt.plot(max_iter,best_fit3,color='b',label='fitness_score') plt.ylabel('Fitness_score') plt.xlabel('Number of iterations') plt.show() return None
def run_sa(prob, value_range, num_runs): avgs = [] times = [] for value in value_range: problem = prob(value) print("\t\tValue: " + str(value)) run_vals = [] run_times = [] for run in range(0, num_runs): print("\t\t\tRun " + str(run)) start = timeit.default_timer() best_state, best_fitness = mlrose.simulated_annealing( problem, max_attempts=20) stop = timeit.default_timer() total_time = stop - start run_vals.append(best_fitness) run_times.append(total_time) avgs.append(np.mean(run_vals)) times.append(np.mean(run_times)) return avgs, times
def simulated_annealing_max_attempts(problem, initial_state, attempts_percentage): attempts = int(problem.length * attempts_percentage) return mlrose.simulated_annealing(problem, init_state=initial_state, curve=False, max_attempts=attempts)
def find_best_param_sa_decay(seed, problem): init_temp_String = [] init_temp = [0.1, 0.2, 0.3, 0.5, 0.66, 0.99] for int in init_temp: init_temp_String.append(str(int)) fitness_list = [] iterations = [ 1, 250, 500, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000 ] score = [] for init in init_temp: for iter in iterations: schedule2 = mlrose.GeomDecay(init_temp=500, decay=init) best_state, best_fitness = mlrose.simulated_annealing( problem, max_attempts=10, max_iters=iter, random_state=seed, schedule=schedule2) score.append(best_fitness) fitness_list.append(np.mean(score)) print("when init = ", init, " the mean score is ", np.mean(score)) sa_parameter_curve(init_temp_String, fitness_list, "sa_dacay", 0.4, "TSP")
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 runComplexityAnnealing(pType, problem): if pType == 'One Max': neighbor = 10 iterations = 400 elif pType == 'Flip Flop': neighbor = 36 iterations = 700 else: neighbor = 4 iterations = 1000 s = time() best_state, best_fitness = mlrose.simulated_annealing( problem, schedule=mlrose.ExpDecay(), max_attempts=neighbor, max_iters=iterations, random_state=1) # best_state, best_fitness, c = mlrose.simulated_annealing(problem, schedule=mlrose.ExpDecay(), # max_attempts=neighbor, # max_iters=iterations, # random_state=1) timeTaken = time() - s return best_fitness, timeTaken
def run_simulated_annealing(init_state_problem): initial_state, problem = init_state_problem return mlrose.simulated_annealing(problem, init_state=initial_state, max_attempts=int(0.5 * problem.length), curve=True)
def test_simulated_annealing_discrete_max(): """Test simulated_annealing function for a discrete maximization problem""" problem = DiscreteOpt(5, OneMax(), maximize=True) _, _, curve = simulated_annealing(problem, timing=True, max_attempts=50) assert (curve.shape[1] == 2)
def SimulatedAnnealing(self): start = time.time() KSbest_state, KSbest_fitness, KScurve = simulated_annealing( self.knapsack, curve=True) saKST = time.time() - start start = time.time() FPbest_state, FPbest_fitness, FPcurve = simulated_annealing( self.fourpeaks, curve=True) saFPT = time.time() - start start = time.time() CObest_state, CObest_fitness, COcurve = simulated_annealing( self.countones, schedule=GeomDecay(init_temp=.01, decay=.99), curve=True) saCOT = time.time() - start return KSbest_fitness, FPbest_fitness, CObest_fitness, saKST, saFPT, saCOT, KScurve, FPcurve, COcurve
def fit(length, fitness): problem = mlrose.DiscreteOpt(length = length, fitness_fn = fitness, maximize = True, max_val = 2) iterations = [10,50,100,200,400,800,1600,3200] RHC, SA, GA, MM = ([],[],[],[]) time_RHC, time_SA, time_GA, time_MM = ([],[],[],[]) for iter in iterations: print ("max iterations = " + str(iter)) start_time = time.time() best_fitness = 0 for times in range(10): best_state, best_fitness = mlrose.random_hill_climb(problem, max_attempts = 10, max_iters = iter, restarts = 0, init_state = np.random.randint(2, size=(length,))) best_fitness = max(best_fitness, best_fitness) #print(best_state) RHC.append(best_fitness) print(best_fitness) time_RHC.append((time.time() - start_time)/10) start_time = time.time() best_fitness = 0 for times in range(10): best_state, best_fitness = mlrose.simulated_annealing(problem, schedule = mlrose.GeomDecay(), max_attempts = 10, max_iters = iter, init_state = np.random.randint(2, size=(length,))) best_fitness = max(best_fitness, best_fitness) #print(best_state) SA.append(best_fitness) print(best_fitness) time_SA.append((time.time() - start_time)/10) start_time = time.time() best_fitness = 0 best_state, best_fitness = mlrose.genetic_alg(problem, pop_size = 200, mutation_prob = 0.1, max_attempts = 10, max_iters = iter) #print(best_state) GA.append(best_fitness) print(best_fitness) time_GA.append((time.time() - start_time)) start_time = time.time() 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)
def simulated_annealing_decay(problem, initial_state, decay): if decay == 'Geom': schedule = mlrose.GeomDecay() if decay == 'Exp': schedule = mlrose.ExpDecay() if decay == 'Arith': schedule = mlrose.ArithDecay() return mlrose.simulated_annealing(problem, init_state=initial_state, curve=False, schedule=schedule)
def test_simulated_annealing_continuous_min(): """Test simulated_annealing function for a continuous minimization problem""" problem = ContinuousOpt(5, OneMax(), maximize=False) best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50) x = np.array([0, 0, 0, 0, 0]) assert (np.array_equal(best_state, x) and best_fitness == 0)
def test_mimic_curve_length_max_attempts(): """Test random_hill_climb function such that when curve is True for ma_iters the length of all fitness scores should be equal to max_iters""" problem = DiscreteOpt(5, OneMax(), maximize=True) max_attempts = 10 best_state, best_fitness, all_fitness = simulated_annealing( problem, max_attempts=max_attempts, curve=True) assert len(all_fitness) != max_attempts
def simulated_annealing(problem, starting_cell): # mlrose requires indices start from zero # mlrose also requires init_state to be a 1D np array #init_cell = np.array([starting_cell-1]) optimized_cells, _ = mlrose.simulated_annealing(problem) # Add 1 to all cells since in our case, cell numbers start from one not zero! optimized_cells += 1 return list(optimized_cells)
def test_simulated_annealing_discrete_max(): """Test simulated_annealing function for a discrete maximization problem""" problem = DiscreteOpt(5, OneMax(), maximize=True) best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50) x = np.array([1, 1, 1, 1, 1]) assert (np.array_equal(best_state, x) and best_fitness == 5)
def simulatedAnnealing(problem, init_state, max_attempts, iterations): #Define decay schedule schedule = mlrose.ExpDecay() #Solve problem using Simulated Annealing sm_best_state, sm_best_fitness = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=max_attempts, max_iters=iterations, curve=False, random_state=1) return sm_best_fitness
def optimization(problem): df_optim = pd.DataFrame(columns=algos) df_iter = pd.DataFrame(columns=algos) df_time = pd.DataFrame(columns=algos) for rs in random_states: # RHC tic = time.process_time() best_state_rhc, best_fitness_rhc, curve_rhc = \ mlrose.random_hill_climb(problem, max_attempts=20, max_iters=100000, restarts=0, init_state=None, curve=True, random_state=rs) toc = time.process_time() time_rhc = toc - tic # SA tic = time.process_time() best_state_sa, best_fitness_sa, curve_sa = \ mlrose.simulated_annealing(problem, schedule=mlrose.ExpDecay(init_temp=1.0, exp_const=0.005, min_temp=0.001), max_attempts = 20, max_iters = 100000, init_state = None, curve = True, random_state = rs) toc = time.process_time() time_sa = toc - tic # GA tic = time.process_time() best_state_ga, best_fitness_ga, curve_ga = \ mlrose.genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=20, max_iters=100000, curve=True, random_state=rs) toc = time.process_time() time_ga = toc - tic # MIMIC tic = time.process_time() best_state_m, best_fitness_m, curve_m = \ mlrose.mimic(problem, pop_size=20, keep_pct=0.2, max_attempts=20, max_iters=100000, curve=True, random_state=rs, fast_mimic=False) toc = time.process_time() time_m = toc - tic # df df_optim.loc[len(df_optim)] = [ best_fitness_rhc, best_fitness_sa, best_fitness_ga, best_fitness_m ] df_iter.loc[len(df_iter)] = [ len(curve_rhc), len(curve_sa), len(curve_ga), len(curve_m) ] df_time.loc[len(df_time)] = [time_rhc, time_sa, time_ga, time_m] print(rs) return (df_optim.mean(axis=0), df_iter.mean(axis=0), df_time.mean(axis=0))
def test_simulated_annealing_curve_length_max_iters(): """Test random_hill_climb function such that when curve is True for ma_iters the length of all fitness scores should be equal to max_iters""" problem = DiscreteOpt(5, OneMax(), maximize=True) x = np.array([0, 0, 0, 0, 0]) max_iters = 300 best_state, best_fitness, all_fitness = simulated_annealing( problem, max_iters=max_iters, init_state=x, curve=True) assert len(all_fitness) == max_iters
def final_test(problem, ga, sa, rhc, mimic, trials=50): ga_samples = [] sa_samples = [] rhc_samples = [] mimic_samples = [] ga_time_samples = [] sa_time_samples = [] rhc_time_samples = [] mimic_time_samples = [] for _ in range(trials): start = time.time() _, ga_fitness, _ = mlrose.genetic_alg(problem, pop_size=ga[0], mutation_prob=ga[1]) ga_time_samples.append(time.time() - start) start = time.time() _, sa_fitness, _ = mlrose.simulated_annealing(problem, sa) sa_time_samples.append(time.time() - start) start = time.time() _, rhc_fitness, _ = mlrose.random_hill_climb(problem, rhc) rhc_time_samples.append(time.time() - start) start = time.time() _, mimic_fitness, _ = mlrose.mimic(problem, pop_size=mimic[0], keep_pct=mimic[1]) mimic_time_samples.append(time.time() - start) ga_samples.append(ga_fitness) sa_samples.append(sa_fitness) rhc_samples.append(rhc_fitness) mimic_samples.append(mimic_fitness) fitness_name = repr(problem.fitness_fn).split('.')[-1].split(' ')[0] if fitness_name == 'CustomFitness': fitness_name = 'Saw' print(f'Final results on {fitness_name}') print() print(f'GA max: {np.max(ga_samples)}') print(f'SA max: {np.max(sa_samples)}') print(f'RHC max: {np.max(rhc_samples)}') print(f'MIMIC max: {np.max(mimic_samples)}') print() print(f'GA mean: {np.mean(ga_samples)}') print(f'SA mean: {np.mean(sa_samples)}') print(f'RHC mean: {np.mean(rhc_samples)}') print(f'MIMIC mean: {np.mean(mimic_samples)}') print() print(f'GA mean execution time: {np.mean(ga_time_samples)}') print(f'SA mean execution time: {np.mean(sa_time_samples)}') print(f'RHC mean execution time: {np.mean(rhc_time_samples)}') print(f'MIMIC mean execution time: {np.mean(mimic_time_samples)}')
def test_simulated_annealing_max_iters(): """Test simulated_annealing function with max_iters less than infinite""" problem = DiscreteOpt(5, OneMax(), maximize=True) x = np.array([0, 0, 0, 0, 0]) best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=1, max_iters=1, init_state=x) assert best_fitness == 1
def simulatedAnnealing(fitness, x): # This code was originally taken and modified from https://mlrose.readthedocs.io/en/stable/source/intro.html start = time.time() # Initialize fitness function object using pre-defined class #fitness = mlrose.Queens() # Define optimization problem object if (x == 0): problem = mlrose.DiscreteOpt(length=12, fitness_fn=fitness, maximize=False, max_val=12) elif (x == 1): problem = mlrose.DiscreteOpt(length=9, fitness_fn=fitness, maximize=False, max_val=3) else: problem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=True, max_val=8) # Define decay schedule schedule = mlrose.GeomDecay() # Solve using random hill climb if (x == 0): init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) elif (x == 1): init_state = np.array([0, 1, 2, 0, 1, 2, 0, 1, 1]) else: init_state = np.array([0, 1, 2, 3, 4, 5, 6, 7]) best_state, best_fitness, fitness_curve = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=10, max_iters=1000, init_state=init_state, random_state=1, curve=True) end = time.time() print("Simulated Annealing:") print('The best state found is: ', best_state) print('The fitness at the best state is: ', best_fitness) print("Time: " + str(end - start)) return best_fitness, end - start
def _run_with_sa(self, init_weights, num_nodes, problem): fitness_curve = [] if init_weights is None: init_weights = np.random.uniform(-1, 1, num_nodes) if self.curve: fitted_weights, loss, fitness_curve = simulated_annealing( problem, schedule=self.schedule, max_attempts=self.max_attempts if self.early_stopping else self.max_iters, max_iters=self.max_iters, init_state=init_weights, curve=self.curve) else: fitted_weights, loss, _ = simulated_annealing( problem, schedule=self.schedule, max_attempts=self.max_attempts if self.early_stopping else self.max_iters, max_iters=self.max_iters, init_state=init_weights, curve=self.curve) return fitness_curve, fitted_weights, loss
def temp_SA(cords,fit,temp): best_fit=[] max_iter=[] for i in range(10,temp,1): best_state,best_fitness3 = mlrose.simulated_annealing(problem_fit,schedule=mlrose.GeomDecay(init_temp=i,decay=0.9,min_temp=0.1),max_attempts=100,max_iters=100,init_state=None) max_iter.append(i) best_fit.append(best_fitness3) print(best_fit) max_iter=np.asarray(max_iter) best_fit=np.asarray(best_fit) line1, = plt.plot(max_iter,best_fit,color='r',label='fitness_score') plt.ylabel('Fitness_score') plt.xlabel('Number of attempts') plt.show() return None
def sim_annealing(self, max_attempts=10, max_iters=np.inf, decay='geom'): decay_lookup = { 'geom': mlrose.GeomDecay(), 'exp': mlrose.ExpDecay(), 'arith': mlrose.ArithDecay() } start = time.time() best_state, best_fitness = simulated_annealing( self.problem_fit, max_attempts=max_attempts, max_iters=max_iters, schedule=decay_lookup[decay], random_state=111) end = time.time() time_elapsed = end - start return [best_fitness, time_elapsed]
def test_simulated_annealing(self, title, max_attempts_range=[100], decay_range=['geom']): decay_lookup = { 'geom': mlrose.GeomDecay(), 'exp': mlrose.ExpDecay(), 'arith': mlrose.ArithDecay() } fig, (ax1, ax2) = plt.subplots(2, figsize=(12, 8), dpi=80) fig.suptitle(title + " Simmulated Annealing") print(title + " Simulated Annealing Algo") best = [0, 0, 0] for m in max_attempts_range: fitness_arr = [] time_arr = [] for d in decay_range: start = time.time() # solve using simulated annealing best_state, best_fitness, curve = mlrose.simulated_annealing( self.problem_fit, schedule=decay_lookup[d], max_attempts=m, max_iters=np.inf, curve=True) fitness_arr.append(best_fitness) time_arr.append(round(time.time() - start, 2)) if best_fitness > best[2]: best[0] = m best[1] = d best[2] = best_fitness ax1.plot(decay_range, fitness_arr, label=m, lw=2) ax2.plot(decay_range, time_arr, lw=2) ax1.set(xlabel="Decay Range", ylabel="Fitness") ax2.set(xlabel="Decay Range", ylabel="Time(s)") print(title + " SA max_attempts={a}, # decay={b}, best_fitness={c}".format( a=best[0], b=best[1], c=best[2])) fig.legend(loc='center right', title='Attempts') plt.tight_layout() self.saveToNewDir(fig, "./", title + "_Simulated_Annealing.png") plt.clf()
def runAnnealing(problem, basePath): iterations = 500 iterations = 1000 neighborhood = [10] neighborhood = [2, 4, 12, 36, 50, 75] neighborhood = [4] schedules = [('Exp', mlrose.ExpDecay()), ('Arith', mlrose.ArithDecay()), ('Geom', mlrose.GeomDecay())] schedules = [('Exp', mlrose.ExpDecay())] times = np.zeros((len(neighborhood), iterations)) nIndex = 0 schedule = mlrose.ExpDecay() fig, ax = plt.subplots() plt.title('Annealing') for neighbor in neighborhood: s = time() x = [] y = [] for i in range(1, iterations + 1): best_state, best_fitness = mlrose.simulated_annealing( problem, schedule=schedule, max_attempts=neighbor, max_iters=i, random_state=1) x.append(i) y.append(best_fitness) e = time() timeTaken = e - s times[nIndex, i - 1] = timeTaken print('Itt: {0} - Time:{1}'.format(i, timeTaken)) nIndex += 1 plotLine(x, y, ax, 'Neighbors: {0} - {1}'.format(neighbor, 'Exponential Decay')) plotTime(x, times, ax) showLegend(fig, ax) if basePath: plt.savefig('{0}\\{1}.png'.format(basePath, 'Annealing')) else: plt.show() return
def run(self, n=1): """ n : the number of runs to do returns best_fitnesses (list), learning_curves (list) """ best_fitnesses = [] learning_curves = [] for i in np.arange(n): _, _, learning_curve = mlrose.simulated_annealing( self.problem, schedule=self.schedule, max_attempts=self.max_attempts, max_iters=self.max_iters, random_state=None, curve=True) best_fitness = np.max(learning_curve) best_fitnesses.append(best_fitness) learning_curves.append(learning_curve) return best_fitnesses, learning_curves