def run(): # basePath = 'C:\\Users\\mwest\\Desktop\\ML\\source\\Machine-Learning-Local - Copy\\Graphs\\randomized\\Complexity\\One Max\\' basePath = None # lengths = range(1, 501, 25) # lengths = range(1, 101, 50) lengths = [10, 100, 200, 300, 400, 500] lengths = [10, 50, 100, 150, 200] lengths = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150 ] # lengths = [5, 10] fitness = mlrose.OneMax() runComplexity('One Max', fitness, lengths) fitness = mlrose.FourPeaks(t_pct=0.10) runComplexity('Four Peaks', fitness, lengths) fitness = mlrose.FlipFlop() runComplexity('Flip Flop', fitness, lengths) # runComplexity('TSP', None, lengths) # fitness = mlrose.Queens() # runComplexity('Queens', fitness, lengths) return
def gen_problem_fourpeaks(problem_size): fitness = mlr.FourPeaks(t_pct=0.25) maximize = True problem = mlr.DiscreteOpt(length=problem_size, fitness_fn=fitness, maximize=maximize) return problem, maximize
def FourPeaks(self, length=10, t_pct=0.1, verbose=False): self.problem = 'fourpeaks{l}'.format(l=length) self.verbose = verbose fitness_fn = mlrose.FourPeaks(t_pct=t_pct) # define optimization problem object self.problem_fit = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_fn, maximize=True)
def run(): fitness = mlrose.FourPeaks(t_pct=0.1) arrLen = 100 problem = mlrose.DiscreteOpt(length=arrLen, fitness_fn=fitness) # basePath = 'C:\\Users\\mwest\\Desktop\\ML\\source\\Machine-Learning-Local - Copy\\Graphs\\randomized\\Four Peaks\\' basePath = None runHill(problem, basePath) runAnnealing(problem, basePath) runGenetic(problem, basePath) runMimic(problem, basePath) return
def get_prob(self, t_pct=None, p_length=None): if self.prob_name == 'Four Peaks': fitness = mlrose.FourPeaks(t_pct) p_len = 100 self.schedule = mlrose.ExpDecay() self.restarts = 0 self.mutation_prob = 0.1 self.keep_pct = 0.1 self.pop_size = 500 elif self.prob_name == "Continuous Peaks": fitness = mlrose.ContinuousPeaks(t_pct) p_len = 100 self.schedule = mlrose.GeomDecay() self.restarts = 0 self.mutation_prob = 0.1 self.keep_pct = 0.2 self.pop_size = 200 elif self.prob_name == "Max K Color": fitness = mlrose.MaxKColor(self.COLOREDGE) p_len = 100 self.schedule = mlrose.ExpDecay() self.restarts = 0 self.mutation_prob = 0.2 self.keep_pct = 0.2 self.pop_size = 200 elif self.prob_name == "Flip Flop": fitness = mlrose.FlipFlop() p_len = 100 self.schedule = mlrose.ArithDecay() self.restarts = 0 self.mutation_prob = 0.2 self.keep_pct = 0.5 self.pop_size = 500 elif self.prob_name == "One Max": fitness = mlrose.OneMax() p_len = 100 self.schedule = mlrose.GeomDecay() self.restarts = 0 self.mutation_prob = 0.2 self.keep_pct = 0.1 self.pop_size = 100 else: fitness = None p_len = 0 if p_length is None: p_length = p_len problem = mlrose.DiscreteOpt(length=p_length, fitness_fn=fitness) init_state = np.random.randint(2, size=p_length) return problem, init_state
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
import numpy as np import mlrose as mlrose import pandas as pd import pickle ps = 16 results = [] for i in range(0,50): fitness = mlrose.FourPeaks() 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.random_hill_climb(problem,max_attempts =500, max_iters =500,restarts=i,init_state = istate, 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) print('The fitness at the best state is: ', best_fitness) results.append(best_fitness)
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)}') # FOUR PEAKS fourpeaks = mlrose.FourPeaks() 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
def MaxKColoring(): # edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)] state = np.array([ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ]) n = len(state) #fitness = mlrose.MaxKColor(edges) fitness = mlrose.FourPeaks(t_pct=0.15) # Define optimization problem object #problem = mlrose.DiscreteOpt(length = n, fitness_fn = fitness, maximize = False, max_val = n) problem = mlrose.DiscreteOpt(length=n, fitness_fn=fitness, maximize=True, max_val=2) best_state, best_fitness, score_curves, runtime = algorithm( problem=problem, max_attempts=500, max_iters=50) def plot_solution(algorithm_type: str, solution: list) -> None: """Given a solution, plot it and save the result to disk.""" fig = plt.figure() ax = fig.add_subplot(111, aspect='equal') ax.set_xlim((0, n)) ax.set_ylim((0, n)) count = 0 for queen in solution: ax.add_patch(patches.Rectangle((queen, count), 1, 1)) count += 1 fig.savefig(algorithm_type + '_' + ''.join([str(a) for a in solution]) + '.png', dpi=150, bbox_inches='tight') plt.close(fig) for key, value in best_state.items(): plot_solution(key, value) def PlotData(x, y, x2, y2, x3, y3, x4, y4): plt.figure() plt.plot(x, y, 'r', x2, y2, 'g', x3, y3, 'b', x4, y4, 'c', alpha=0.5) plt.xlabel('Iteration') plt.ylabel('Fitness') plt.xticks(np.arange(0, 51, 3.0)) #plt.yticks(np.arange(-5, 5, 1)) red_patch = patches.Patch(color='red', label='Genetic Algorithms') green_patch = patches.Patch(color='green', label='Simulated Annealing') blue_patch = patches.Patch(color='blue', label='MIMIC') cyan_patch = patches.Patch(color='cyan', label='Randomized Hill Climb') plt.legend(handles=[red_patch, green_patch, blue_patch, cyan_patch]) plt.show() PlotData(list(range(len(score_curves['genetic algorithm']))), score_curves['genetic algorithm'], list(range(len(score_curves['simulated annealing']))), score_curves['simulated annealing'], list(range(len(score_curves['mimic']))), score_curves['mimic'], list(range(len(score_curves['randomized hill climbing']))), score_curves['randomized hill climbing']) return best_state, best_fitness, score_curves, runtime
def OptFourPeaks(): fitness = mlrose.FourPeaks(t_pct=0.1) validate(fitness, size, 'FourPeaks')
import mlrose import numpy as np import matplotlib.pyplot as plt from time import perf_counter as pc import itertools import pandas as pd iters = 5 # number of iterations for each setting in grid search results = {} fitness_fn = mlrose.FourPeaks(t_pct=0.4) param_grid_problem = { 'fitness_fn': fitness_fn, 'max_val': 2, 'length': range(10, 101, 10), } problem = mlrose.DiscreteOpt algorithms = { 'MIMIC': { 'algorithm': mlrose.mimic, 'param_grid_algorithm': { 'return_statistics': True, 'pop_size': ['5*problem_length'], 'keep_pct': [0.2], 'max_attempts': [10], 'max_iters': 1000 }, },
def optimizationFunction(n,iMin, iMax, iStep, jMin, jMax, jStep): np.random.seed(100) optScenarios = ['Knap Sack', 'Four Peaks', 'K - Colors'] for x in range(3): if x == 0: weights = np.random.randint(1,10,size=n) #values = np.random.randint(1,50,size=n) values = [i for i in range(1,n+1)] max_weight_pct = 0.5 fitnessFunction = mlrose.Knapsack(weights, values, max_weight_pct) optModel = mlrose.DiscreteOpt(len(values), fitness_fn = fitnessFunction, maximize=True) elif x == 1: inp = [0] * int(n/2) + [1]*int(n - int(n/2)) np.random.shuffle(inp) fitnessFunction = mlrose.FourPeaks(t_pct = 0.15) optModel = mlrose.DiscreteOpt(len(inp), fitness_fn = fitnessFunction, maximize =True) elif x == 2: edges = [(np.random.randint(0,n), np.random.randint(0,n)) for ab in range(n)] fitnessFunction = mlrose.MaxKColor(edges) optModel = mlrose.DiscreteOpt(len(edges), fitness_fn = fitnessFunction, maximize =True) decay = mlrose.ExpDecay() optResults = {'iterations':[],'attempts':[],'fitness':[],'time':[], 'optimization':[]} for i in range(iMin,iMax,iStep): for j in range(jMin,jMax,jStep): start_time = timer() best_state, best_fitness = mlrose.random_hill_climb(optModel, max_attempts = j, max_iters = i, random_state=100) opt_time = timer() - start_time optResults['iterations'].append(i) optResults['attempts'].append(j) optResults['fitness'].append(best_fitness) optResults['time'].append(opt_time) optResults['optimization'].append('Random Hill') start_time = timer() best_state, best_fitness = mlrose.simulated_annealing(optModel, schedule=decay, max_attempts = j,max_iters = i,random_state=1000) opt_time = timer() - start_time optResults['iterations'].append(i) optResults['attempts'].append(j) optResults['fitness'].append(best_fitness) optResults['time'].append(opt_time) optResults['optimization'].append('Simulated Annealing') start_time = timer() best_state, best_fitness = mlrose.genetic_alg(optModel, pop_size=200, mutation_prob = 0.25, max_attempts = j, max_iters = i, random_state=5000) opt_time = timer() - start_time optResults['iterations'].append(i) optResults['attempts'].append(j) optResults['fitness'].append(best_fitness) optResults['time'].append(opt_time) optResults['optimization'].append('Genetic Algorithm') start_time = timer() best_state, best_fitness = mlrose.mimic(optModel, pop_size = 200, keep_pct = 0.3, max_attempts = j, max_iters = i, random_state=150) opt_time = timer() - start_time optResults['iterations'].append(i) optResults['attempts'].append(j) optResults['fitness'].append(best_fitness) optResults['time'].append(opt_time) optResults['optimization'].append('MIMIC') optResults = pd.DataFrame(optResults) plotGraphs(optResults,optScenarios[x])
def fourpeaks(max_iter=500, early_stop=None, mimic_early_stop=100, n_runs=10, savedir=None): print('\n\n|========= Four Peaks =========|\n') fitness = mlrose.FourPeaks(t_pct=0.10) problem_size = [30, 90, 200] 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': 2000, 'keep_pct': 0.15, 'max_attempts': mimic_early_stop, 'fast_mimic': True }, 'sa': { 'schedule': mlrose.GeomDecay(init_temp=2., decay=0.4), 'init_state': None, 'max_attempts': max_attempts }, 'ga': { 'pop_size': 1000, 'mutation_prob': 0.15, 'pop_breed_percent': 0.50, 'elite_dreg_ratio': 0.85, 'max_attempts': mimic_early_stop } } 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: t.to_csv('{}/fourpeaks_runtimes.csv'.format(savedir)) for i, df in enumerate(results): df.to_csv('{}/fourpeaks_ps{}.csv'.format(savedir, problem_size[i])) # Write the timings as a single dataframe for k, v in timings.items(): for atype, times in v.items(): tdf = pd.DataFrame(times, columns=['time', 'fitness']) tdf.to_csv('{}/fourpeaks_{}_{}_timings.csv' .format(savedir, k, atype)) return t, results, timings
#best_state, best_fitness = ml.random_hill_climb(problem_fit, random_state = 2) #best_state, best_fitness = ml.simulated_annealing(problem_fit, random_state = 2) best_state, best_fitness = ml.mimic(problem_fit, random_state = 2) print(best_state) print(best_fitness) """ sample_sizes = [] fitness = ml.FourPeaks(t_pct=0.15) main_best_fit_gene = [] main_execution_times_gene = [] main_total_iterations_gene = [] main_opt_iterations_gene = [] for y in range(10): best_fit_gene = [] execution_times_gene = [] total_iterations_gene = [] opt_iterations_gene = [] for x in range(5): start = time.time() opt = ml.DiscreteOpt((y * 6) + 6, fitness) #best_state, best_fitness, curve = ml.random_hill_climb(opt, max_iters=5, restarts = 0, curve=True, random_state = 2)
def create_problem(size): initial_state = numpy.random.randint(2, size=size) problem = mlrose.DiscreteOpt(size, mlrose.FourPeaks()) return initial_state, problem
def four_Peak(): problem = mlrose.DiscreteOpt(length=20, fitness_fn=mlrose.FourPeaks(t_pct=0.1), maximize=True, max_val=2) init_state = np.array([0] * 20) startTime = datetime.now() best_state, best_fitness, fitness_curve_rhc = mlrose.random_hill_climb( problem, max_attempts=1000, max_iters=2500, restarts=0, init_state=init_state, curve=True, random_state=1, state_fitness_callback=None, callback_user_info=None) totalTime = datetime.now() - startTime rhcTime = totalTime.total_seconds() print("RHC") print("Time: ", rhcTime) print("best_state: ", best_state) print("best_fitness: ", best_fitness) print("Iteration: %d " % len(fitness_curve_rhc)) ############################### startTime = datetime.now() best_statesa, best_fitnesssa, fitness_curve_sa = mlrose.simulated_annealing( problem, max_attempts=1000, max_iters=2500, init_state=init_state, curve=True, random_state=1, state_fitness_callback=None, callback_user_info=None) totalTime = datetime.now() - startTime saTime = totalTime.total_seconds() print("SA") print("Time: ", saTime) print("best_state: ", best_statesa) print("best_fitness: ", best_fitnesssa) print("Iteration: %d " % len(fitness_curve_sa)) ############################### startTime = datetime.now() best_statega, best_fitnessga, fitness_curve_ga = mlrose.genetic_alg( problem, max_attempts=1000, max_iters=2500, curve=True, random_state=1, state_fitness_callback=None, callback_user_info=None) totalTime = datetime.now() - startTime gaTime = totalTime.total_seconds() print("GA") print("Time: ", gaTime) print("best_state: ", best_statega) print("best_fitness: ", best_fitnessga) print("Iteration: %d " % len(fitness_curve_ga)) ############################### startTime = datetime.now() best_statemm, best_fitnessmm, fitness_curve_mm = mlrose.mimic( problem, max_attempts=1000, max_iters=2500, curve=True, random_state=1, state_fitness_callback=None, callback_user_info=None) totalTime = datetime.now() - startTime mmTime = totalTime.total_seconds() print("MIMIC") print("Time: ", mmTime) print("best_state: ", best_statemm) print("best_fitness: ", best_fitnessmm) print("Iteration: %d " % len(fitness_curve_mm))
def __discrete_bit_size_problems(problem, algorithm, length, max_iter, max_attempt, init_state, edges=None, coords=None): if problem == 'fourpeaks': __fit = mlrose.FourPeaks() __problem = mlrose.DiscreteOpt(length=length, fitness_fn=__fit, maximize=True, max_val=2) elif problem == 'kcolor': __fit = mlrose.MaxKColor(edges=edges) __problem = mlrose.DiscreteOpt(length=length, fitness_fn=__fit, maximize=True) elif problem == 'flipflop': __fit = mlrose.OneMax() __problem = mlrose.DiscreteOpt(length=length, fitness_fn=__fit, maximize=True, max_val=2) elif problem == 'continouspeaks': __fit = mlrose.ContinuousPeaks() __problem = mlrose.DiscreteOpt(length=length, fitness_fn=__fit, maximize=True, max_val=2) elif problem == 'travellingsales': __fit = mlrose.TravellingSales(coords=coords) __problem = mlrose.TSPOpt(length=length, fitness_fn=__fit, maximize=False) if algorithm == 'random_hill_climb': start_time = time.time() best_state, best_fitness, best_curve = mlrose.random_hill_climb( __problem, max_iters=max_iter, max_attempts=max_attempt, init_state=init_state, curve=True) end_time = time.time() - start_time elif algorithm == 'simulated_annealing': start_time = time.time() best_state, best_fitness, best_curve = mlrose.simulated_annealing( __problem, max_iters=max_iter, max_attempts=max_attempt, init_state=init_state, curve=True) end_time = time.time() - start_time elif algorithm == 'genetic_alg': start_time = time.time() best_state, best_fitness, best_curve = mlrose.genetic_alg( __problem, max_iters=max_iter, max_attempts=max_attempt, curve=True) end_time = time.time() - start_time elif algorithm == 'mimic': start_time = time.time() best_state, best_fitness, best_curve = mlrose.mimic( __problem, max_iters=max_iter, max_attempts=max_attempt, curve=True) end_time = time.time() - start_time return best_fitness, end_time, best_curve
#Four Peaks Genetic import mlrose import mlrose import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler, OneHotEncoder from sklearn.metrics import accuracy_score # Initialize fitness function object using pre-defined class fitness = mlrose.FourPeaks(t_pct=0.15) # Define optimization problem object problem = mlrose.DiscreteOpt(length=100, fitness_fn=fitness, maximize=True, max_val=2) # Define decay schedule schedule = mlrose.ExpDecay() # Run Genetic Algorithm best_state, best_fitness, fit_curve = mlrose.genetic_alg(problem, max_attempts=1000, max_iters=1000, curve=True, random_state=1) # Stop Timer elapsed_time = timer() - start # in seconds print('Time elapsed time in seconds is: ', elapsed_time) print('The best state found is: ', best_state) print('The fitness at the best state is: ', best_fitness) # Plot Curve
def FourPeaks(arrayLen, sizeOrIterations): fitness = mlrose.FourPeaks(t_pct=0.15) problem = mlrose.DiscreteOpt(length = arrayLen, fitness_fn = fitness, maximize = True, max_val = 2) rhcfitnessMatrix = [] safitnessMatrix = [] genalgfitnessMatrix = [] mimicfitnessMatrix = [] numIterations = 10000 dataPoints = 100 #rhc print("Begin RHC") startingTime = time() for i in range(numIterations): if i % dataPoints == 0 and not sizeOrIterations or i == 1000 and sizeOrIterations: print("RHC I: " + str(i)) t0 = time() best_state, best_fitness = mlrose.random_hill_climb(problem, max_attempts=100, max_iters=i, init_state=None) finish = time() - t0 currentTime = time() - startingTime print("CurrentTime: " + str(currentTime)) rhcfitnessMatrix.append((i, best_fitness, finish)) finishtime = time() - startingTime print("Finish Time: " + str(finishtime)) #simulated annealing schedule = mlrose.ExpDecay() startingTime = time() for i in range(numIterations): if i % dataPoints == 0 and not sizeOrIterations or i == 1000 and sizeOrIterations: print("SA I: " + str(i)) t0 = time() best_state, best_fitness = mlrose.simulated_annealing(problem, schedule = schedule, max_attempts = 100, max_iters = i, init_state = None) finish = time() - t0 currentTime = time() - startingTime print("CurrentTime: " + str(currentTime)) safitnessMatrix.append((i, best_fitness, finish)) finishtime = time() - startingTime print("Finish Time: " + str(finishtime)) #genetic alg startingTime = time() for i in range(numIterations): if i % dataPoints == 0 and not sizeOrIterations or i == 1000 and sizeOrIterations: print("GA I: " + str(i)) t0 = time() best_state, best_fitness = mlrose.genetic_alg(problem, pop_size=200, mutation_prob=0.1, max_attempts=100, max_iters=i) finish = time() - t0 currentTime = time() - startingTime print("CurrentTime: " + str(currentTime)) genalgfitnessMatrix.append((i, best_fitness, finish)) finishtime = time() - startingTime print("Finish Time: " + str(finishtime)) #mimic startingTime = time() for i in range(numIterations): if i % dataPoints == 0 and not sizeOrIterations or i == 1000 and sizeOrIterations: print("Mimic I: " + str(i)) t0 = time() best_state, best_fitness = mlrose.mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=100, max_iters=i) finish = time() - t0 currentTime = time() - startingTime print("CurrentTime: " + str(currentTime)) mimicfitnessMatrix.append((i, best_fitness, finish)) finishtime = time() - startingTime print("Finish Time: " + str(finishtime)) if not sizeOrIterations: writeToExcel.writeOptimzationProblem(rhcfitnessMatrix, safitnessMatrix, genalgfitnessMatrix, mimicfitnessMatrix, "4PeaksIterations.xlsx") return None else: return rhcfitnessMatrix, safitnessMatrix, genalgfitnessMatrix, mimicfitnessMatrix