def get_fitness_functions(): df = pd.read_csv("./houston2008_order.csv") coord_list = list(df[['lat', 'long']].apply(tuple, axis=1)) coord_list = coord_list[0:30] fitness_tsp = mlrose.TravellingSales(coords=coord_list) problem_tsp = mlrose.TSPOpt(length=len(coord_list), fitness_fn=fitness_tsp, maximize=False) fitness_fourpeak = mlrose.FourPeaks(t_pct=.3) problem_fourpeak = mlrose.DiscreteOpt(length=20, fitness_fn=fitness_fourpeak) fitness_flipflop = mlrose.FlipFlop() problem_flipflop = mlrose.DiscreteOpt(length=30, fitness_fn=fitness_flipflop) fitness_one_max = mlrose.OneMax() problem_one_max = mlrose.DiscreteOpt( length=35, fitness_fn=fitness_one_max, ) weights = [10, 5, 2, 8, 15] values = [1, 2, 3, 4, 5] max_weight_pct = 0.6 fitness_knapsack = mlrose.Knapsack(weights, values, max_weight_pct) problem_knapsack = mlrose.DiscreteOpt(length=5, fitness_fn=fitness_knapsack) return { "tsp": problem_tsp, "four_peaks": problem_fourpeak, "one_max": problem_one_max, }
def get_flip_flop(size): flip_flop = mlrose.FlipFlop() state = np.array([0,1,0,1,1,1,1]) flip_flop.evaluate(state) problem = mlrose.DiscreteOpt( length=size, fitness_fn=flip_flop, maximize=True, max_val=2 # makes it bit string ) return problem
def main(): verbose = True num_runs = 20 max_iters = 1000 # define flip flop fitness function to maximize fitness_fn = mlrose.FlipFlop() # define optimization problem length = 500 ff_problem = mlrose.FlipFlopOpt( length=length, fitness_fn=fitness_fn, maximize=True, ) ff_problem.set_mimic_fast_mode(True) # set initial state initial_state = np.random.randint(0, 2, size=length) # randomized hill climbing rhc_fitness_dfs = flip_flop_rhc(ff_problem, initial_state, max_iters, num_runs, verbose) print('---') # simulated annealing sa_fitness_dfs = flip_flop_sa(ff_problem, initial_state, max_iters, num_runs, verbose) print('---') # genetic algorithm ga_fitness_dfs = flip_flop_ga(ff_problem, max_iters, num_runs, verbose) print('---') # MIMIC algorithm mimic_fitness_dfs = flip_flop_mimic(ff_problem, max_iters, num_runs, verbose) print('---') # compare algorithm performance plotting.compare_algos( problem_name='flip_flop', rhc_dfs=rhc_fitness_dfs, sa_dfs=sa_fitness_dfs, ga_dfs=ga_fitness_dfs, mimic_dfs=mimic_fitness_dfs, )
def main(): ## SET SOME PARAMS TO USE GLOBALLY max_iters_list = [50, 100, 1000] #,32,64,128,256,512,1024] max_iters_list_full = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] rand_list = [1, 11, 22] #,44,55,66,77,88,99] rand_list_full = [0, 11, 22, 33, 44, 55, 66, 77, 88, 99] input_location = 'data/' output_location = 'outputs/' chart_output_location = 'charts/' prefix = '5th_' ## DEFINE PROBLEMS TO SOLVE # Traveling Salesman Problem (TSP) space_length = 1000 cities_cnt = 200 coords_list, x, y = create_TSP(space_length, cities_cnt, return_lists_too=True) plt.plot(x, y, 'o') plt.savefig(chart_output_location + 'TPS_visual' + '.png') fitness_coords = mlrose.TravellingSales(coords=coords_list) problem_TSP = mlrose.TSPOpt(length=len(coords_list), fitness_fn=fitness_coords, maximize=False) # 4 Peaks t_pct = 0.1 length = 200 fitness_4_peaks = mlrose.FourPeaks(t_pct=t_pct) problem_4P = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_4_peaks, maximize=True, max_val=2) problem_4P_small = mlrose.DiscreteOpt(length=50, fitness_fn=fitness_4_peaks, maximize=True, max_val=2) problem_4P_big = mlrose.DiscreteOpt(length=1000, fitness_fn=fitness_4_peaks, maximize=True, max_val=2) # Continuous Peaks t_pct = 0.1 length = 200 fitness_cont_peaks = mlrose.ContinuousPeaks(t_pct=t_pct) problem_cont_peaks = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_cont_peaks, maximize=True, max_val=2) # Flip Flop length = 200 fitness_FF = mlrose.FlipFlop() problem_FF = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_FF, maximize=True, max_val=2) problem_FF_small = mlrose.DiscreteOpt(length=50, fitness_fn=fitness_FF, maximize=True, max_val=2) problem_FF_big = mlrose.DiscreteOpt(length=1000, fitness_fn=fitness_FF, maximize=True, max_val=2) # Knapsack length = 200 weights, values = create_Knapsack(length) weights_big, values_big = create_Knapsack(1000) weights_small, values_small = create_Knapsack(50) fitness_KS = mlrose.Knapsack(weights, values, max_weight_pct=0.65) fitness_KS_big = mlrose.Knapsack(weights_big, values_big, max_weight_pct=0.65) fitness_KS_small = mlrose.Knapsack(weights_small, values_small, max_weight_pct=0.65) problem_KS = mlrose.DiscreteOpt(length=length, fitness_fn=fitness_KS, maximize=True, max_val=2) problem_KS_big = mlrose.DiscreteOpt(length=1000, fitness_fn=fitness_KS_big, maximize=True, max_val=2) problem_KS_small = mlrose.DiscreteOpt(length=50, fitness_fn=fitness_KS_small, maximize=True, max_val=2) dict_of_param_dict = {} dict_of_param_dict['GA'] = { 'pop_size': [100, 200], #,1000], 'mutation_prob': [0.5, 0.1, 0.2], 'max_attempts': [5, 10, 30], 'max_iters': max_iters_list, 'random_state': rand_list } dict_of_param_dict['RHC'] = { 'max_attempts': [30, 50, 100], #[5,10,20,50] 'restarts': [5, 10, 20], #[0,1,2,5] 'max_iters': max_iters_list, 'random_state': rand_list } dict_of_param_dict['SA'] = { 'max_attempts': [10, 50, 100], 'init_temp': [1.0, 10.0, 0.5, 20, 100, 1000], 'decay': [0.99, 0.8, 0.5], 'max_iters': max_iters_list, 'random_state': rand_list } dict_of_param_dict['MIMIC'] = { 'pop_size': [100, 150], 'keep_pct': [0.5, 0.2], 'max_attempts': [10], 'max_iters': [100], 'random_state': rand_list } MIMIC_FF = { 'pop_size': 100, 'keep_pct': 0.5, 'max_attempts': 30, 'max_iters': [2, 4, 8, 16, 32, 64, 128], ## put full list here before uploading 'random_state': [0, 11, 22, 33, 44] } MIMIC_4P = { 'pop_size': 150, 'keep_pct': 0.2, 'max_attempts': 30, 'max_iters': [2, 4, 8, 16, 32, 64, 128], ## put full list here before uploading 'random_state': [0, 11, 22, 33, 44] } MIMIC_KS = { 'pop_size': 150, 'keep_pct': 0.5, 'max_attempts': 30, 'max_iters': [2, 4, 8, 16, 32, 64, 128], ## put full list here before uploading 'random_state': [0, 11, 22, 33, 44] } MIMIC_CP = { 'pop_size': 200, 'keep_pct': 0.2, 'max_attempts': 30, 'max_iters': [2, 4, 8, 16, 32, 64, 128], ## put full list here before uploading 'random_state': [0, 11, 22, 33, 44] } GA_FF = { 'pop_size': 200, #,1000], 'mutation_prob': 0.5, 'max_attempts': 30, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } MIMIC_FF2 = { 'pop_size': [100], 'keep_pct': [0.5], 'max_attempts': [30, 50], 'max_iters': [64], 'random_state': [55] #,66,77,88,99] } print("starting MIMIC FF") # GETTING MIMIC FF RESULTS print("starting MIMIC FF...") ''' ## Started running at 3am results_df, curve_output_list = fitness_by_iter('MIMIC', problem_FF, MIMIC_FF['max_iters'], MIMIC_FF['random_state']\ , pop_size=MIMIC_FF['pop_size'], max_attempts=MIMIC_FF['max_attempts'], curve=True, keep_pct=MIMIC_FF['keep_pct']) results_df.to_csv(output_location + 'final_MIMIC_FF_attempt_3am.csv') results_df, curve_output_list = fitness_by_iter('MIMIC', problem_4P, MIMIC_4P['max_iters'], MIMIC_4P['random_state']\ , pop_size=MIMIC_4P['pop_size'], max_attempts=MIMIC_4P['max_attempts'], curve=True, keep_pct=MIMIC_4P['keep_pct']) results_df.to_csv(output_location + 'final_MIMIC_4P_attempt_3am.csv') results_df, curve_output_list = fitness_by_iter('MIMIC', problem_KS, MIMIC_KS['max_iters'], MIMIC_KS['random_state']\ , pop_size=MIMIC_KS['pop_size'], max_attempts=MIMIC_KS['max_attempts'], curve=True, keep_pct=MIMIC_KS['keep_pct']) results_df.to_csv(output_location + 'final_MIMIC_KS_attempt_3am.csv') results_df, curve_output_list = fitness_by_iter('MIMIC', problem_cont_peaks, MIMIC_CP['max_iters'], MIMIC_CP['random_state']\ , pop_size=MIMIC_CP['pop_size'], max_attempts=MIMIC_CP['max_attempts'], curve=True, keep_pct=MIMIC_CP['keep_pct']) results_df.to_csv(output_location + 'final_MIMIC_CP_attempt_3am.csv') ''' ## USED FOR GRID SEARCHING PARAMETERS FOR RO ON 3 PROBLEMS GA_params_dict = get_params_for_grid_search('GA', max_iters_list=[200]) print("Here are my GA params for grid search: ", GA_params_dict) SA_params_dict = get_params_for_grid_search('SA', max_iters_list=max_iters_list) print("Here are my SA params for grid search: ", SA_params_dict) RHC_params_dict = get_params_for_grid_search('RHC', max_iters_list=max_iters_list) print("Here are my RHC params for grid search: ", RHC_params_dict) MIMIC_params_dict = get_params_for_grid_search( 'MIMIC', max_iters_list=max_iters_list) print("Here are my MIMIC params for grid search: ", MIMIC_params_dict) #grid_search_MIMIC = MIMIC_best_params(problem_TPS, MIMIC_params_dict, inverse_fitness=False) #grid_search_MIMIC.to_csv(output_location + 'grid_search_MIMIC.csv') ''' grid_search_GA = GA_best_params(problem_FF, GA_params_dict, inverse_fitness=False) grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_FF_really.csv') print("finished GA") grid_search_MIMIC = MIMIC_best_params(problem_FF, MIMIC_params_dict, inverse_fitness=False) grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_FF_really.csv') ''' print("finished MIMIC FF") print("Doing GA rn") #results_df, curve_output_list = fitness_by_iter('GA', problem_FF, GA_FF['max_iters'], GA_FF['random_state']\ #, pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], mutation_prob=GA_FF['mutation_prob'],curve=True) #results_df.to_csv(output_location + 'final_MIMIC_FF_attempt_1am.csv') print("finished GA") ''' GRID SEARCHING print("Starting grid search for RHC") grid_search_RHC = RHC_best_params(problem_TSP, RHC_params_dict, inverse_fitness=False) grid_search_RHC.to_csv(output_location + prefix +'grid_search_RHC_TSP.csv') grid_search_RHC = RHC_best_params(problem_FF, RHC_params_dict, inverse_fitness=False) grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_FF.csv') grid_search_RHC = RHC_best_params(problem_cont_peaks, RHC_params_dict, inverse_fitness=False) grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_cont_peaks.csv') grid_search_RHC = RHC_best_params(problem_4P, RHC_params_dict, inverse_fitness=False) grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_4P.csv') print("Starting grid search for SA") grid_search_SA = SA_best_params(problem_TSP, SA_params_dict, inverse_fitness=False) grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_TSP.csv') grid_search_SA = SA_best_params(problem_FF, SA_params_dict, inverse_fitness=False) grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_FF.csv') grid_search_SA = SA_best_params(problem_cont_peaks, SA_params_dict, inverse_fitness=False) grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_cont_peaks.csv') grid_search_SA = SA_best_params(problem_4P, SA_params_dict, inverse_fitness=False) grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_4P.csv') print("Starting grid search for GA") grid_search_GA = GA_best_params(problem_TSP, GA_params_dict, inverse_fitness=False) grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_TSP.csv') grid_search_GA = GA_best_params(problem_FF, GA_params_dict, inverse_fitness=False) grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_FF.csv') grid_search_GA = GA_best_params(problem_cont_peaks, GA_params_dict, inverse_fitness=False) grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_cont_peaks.csv') grid_search_GA = GA_best_params(problem_4P, GA_params_dict, inverse_fitness=False) grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_4P.csv') ''' ''' print("Starting grid search for MIMIC") grid_search_MIMIC = MIMIC_best_params(problem_FF, MIMIC_params_dict, inverse_fitness=False) grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_FF.csv') #grid_search_MIMIC = MIMIC_best_params(problem_cont_peaks, MIMIC_params_dict, inverse_fitness=False) #grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_cont_peaks.csv') grid_search_MIMIC = MIMIC_best_params(problem_4P, MIMIC_params_dict, inverse_fitness=False) grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_4P.csv') #grid_search_MIMIC = MIMIC_best_params(problem_TSP, MIMIC_params_dict, inverse_fitness=False) #grid_search_MIMIC.to_csv(output_location + 'grid_search_MIMIC_TSP.csv') print("Finished MIMIC grid searches") print("Starting grid search for Knapsack") #grid_search_MIMIC = MIMIC_best_params(problem_KS, MIMIC_params_dict, inverse_fitness=False) #grid_search_MIMIC.to_csv(output_location + prefix + 'grid_search_MIMIC_KS.csv') #grid_search_GA = GA_best_params(problem_KS, GA_params_dict, inverse_fitness=False) #grid_search_GA.to_csv(output_location + prefix + 'grid_search_GA_KS.csv') grid_search_SA = SA_best_params(problem_KS, SA_params_dict, inverse_fitness=False) grid_search_SA.to_csv(output_location + prefix + 'grid_search_SA_KS.csv') grid_search_RHC = RHC_best_params(problem_KS, RHC_params_dict, inverse_fitness=False) grid_search_RHC.to_csv(output_location + prefix + 'grid_search_RHC_KS.csv') ''' ## Fitting MIMIC separately and with fewer iterations for all except the FF as run time is so long for MIMIC max = 128 ''' MIMIC CURVE FOR CHARTS ##### Started (again) at 8am ###### print("Fitting for MIMIC using the 'curve=True' functionality") print("First for KS") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_KS, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_KS_full_curve.csv') df2.to_csv(output_location+'MIMIC_KS_short_curve.csv') print("Finished KS") print("Next for 4 Peaks") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_short_curve.csv') print("Finished 4 Peaks") print("Next for 4 Peaks with 100 and 0.5") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_pop100_keep50_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_pop100_keep50_short_curve.csv') print("Finished 4 Peaks") print("Next for 4 Peaks with 100 and 0.2") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_pop100_keep20_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_pop100_keep20_short_curve.csv') print("Finished 4 Peaks") print("Next for 4 Peaks with 150 and 0.5") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P, pop_size=100, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_pop150_keep50_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_pop150_keep50_short_curve.csv') print("Finished 4 Peaks") print("Next for 4 Peaks Big") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P_big, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_big_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_big_short_curve.csv') print("Finished 4 Peaks Big") print("Next for KS Small") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_KS_small, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_KS_small_full_curve.csv') df2.to_csv(output_location+'MIMIC_KS_small_short_curve.csv') print("Finished KS small") print("Next FF small") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_FF_small, pop_size=100, keep_pct=0.5, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_FF_small_full_curve.csv') df2.to_csv(output_location+'MIMIC_KS_small_short_curve.csv') print("Finished FF Small") print("Next for 4 Peaks Small") start_time_fit = time.perf_counter() a,b,curve_output = mlrose.mimic(problem_4P_small, pop_size=150, keep_pct=0.2, max_attempts=10, max_iters=128, curve=True\ , random_state=0) end_time_fit = time.perf_counter() time_used = end_time_fit - start_time_fit df1, df2 = curve_to_df(curve_output, max) df2['time_to_128'] = time_used df1.to_csv(output_location+'MIMIC_4P_small_full_curve.csv') df2.to_csv(output_location+'MIMIC_4P_small_short_curve.csv') print("Finished 4 Peaks Small") ''' ### Now GA GA_FF = { 'pop_size': 100, #,1000], 'mutation_prob': 0.1, 'max_attempts': 30, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } GA_KS = { 'pop_size': 200, #,1000], 'mutation_prob': 0.2, 'max_attempts': 30, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } GA_4P = { 'pop_size': 200, #,1000], 'mutation_prob': 0.5, 'max_attempts': 30, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' More fitness by iteration calculations #results_df, curve_output_list = fitness_by_iter('GA', problem_FF, GA_FF['max_iters'], GA_FF['random_state']\ #, pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob']) #results_df.to_csv(output_location + 'final_GA_FF_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_FF_small, GA_FF['max_iters'], GA_FF['random_state']\ , pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob']) results_df.to_csv(output_location + 'final_GA_FF_small_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_FF_big, GA_FF['max_iters'], GA_FF['random_state']\ , pop_size=GA_FF['pop_size'], max_attempts=GA_FF['max_attempts'], curve=True, mutation_prob=GA_FF['mutation_prob']) results_df.to_csv(output_location + 'final_GA_FF_big_attempt_8am.csv') #results_df, curve_output_list = fitness_by_iter('GA', problem_4P, GA_4P['max_iters'], GA_4P['random_state']\ #, pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob']) #results_df.to_csv(output_location + 'final_GA_4P_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_4P_big, GA_4P['max_iters'], GA_4P['random_state']\ , pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob']) results_df.to_csv(output_location + 'final_GA_4P_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_4P_small, GA_4P['max_iters'], GA_4P['random_state']\ , pop_size=GA_4P['pop_size'], max_attempts=GA_4P['max_attempts'], curve=True, mutation_prob=GA_4P['mutation_prob']) results_df.to_csv(output_location + 'final_GA_4P_small_attempt_8am.csv') #results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\ #, pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob']) #results_df.to_csv(output_location + 'final_GA_KS_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_KS_big, GA_KS['max_iters'], GA_KS['random_state']\ , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob']) results_df.to_csv(output_location + 'final_GA_KS_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_KS_small, GA_KS['max_iters'], GA_KS['random_state']\ , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=GA_KS['mutation_prob']) results_df.to_csv(output_location + 'final_GA_KS_small_attempt_8am.csv') ''' ########### SA print("now doing SA") SA_4P = { 'max_attempts': 10, 'schedule': mlrose.GeomDecay(init_temp=100, decay=0.8), 'max_iters': max_iters_list_full, 'random_state': rand_list_full } SA_FF = { 'max_attempts': 10, 'schedule': mlrose.GeomDecay(init_temp=100, decay=0.8), 'max_iters': max_iters_list_full, 'random_state': rand_list_full } results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_4P_big, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_small_attempt_8am.csv') ''' more fitness by iteration calculations #results_df, curve_output_list = fitness_by_iter('SA', problem_FF, SA_FF['max_iters'], SA_FF['random_state']\ #, schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True) #results_df.to_csv(output_location + 'final_SA_FF_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_FF_big, SA_FF['max_iters'], SA_FF['random_state']\ , schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_FF_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_FF_small, SA_FF['max_iters'], SA_FF['random_state']\ , schedule=SA_FF['schedule'], max_attempts=SA_FF['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_FF_small_attempt_8am.csv') SA_4P = { 'max_attempts':10, 'schedule':mlrose.GeomDecay(init_temp=100, decay=0.8), 'max_iters':max_iters_list_full, 'random_state':rand_list_full } results_df, curve_output_list = fitness_by_iter('KS', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_4P_big, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_small_attempt_8am.csv') ''' print("picking up where I left off on making the final curves..") SA_KS = { 'max_attempts': 10, 'schedule': mlrose.GeomDecay(init_temp=1000, decay=0.99), 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' more fitness by iteration calculations results_df, curve_output_list = fitness_by_iter('SA', problem_KS, SA_KS['max_iters'], SA_KS['random_state']\ , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_KS_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_KS_big, SA_KS['max_iters'], SA_KS['random_state']\ , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_KS_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_KS_small, SA_KS['max_iters'], SA_KS['random_state']\ , schedule=SA_KS['schedule'], max_attempts=SA_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_KS_small_attempt_8am.csv') ''' RHC_KS = { 'max_attempts': 50, 'restarts': 20, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' results_df, curve_output_list = fitness_by_iter('RHC', problem_KS, RHC_KS['max_iters'], RHC_KS['random_state']\ , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_KS_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_KS_big, RHC_KS['max_iters'], RHC_KS['random_state']\ , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_KS_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_KS_small, RHC_KS['max_iters'], RHC_KS['random_state']\ , restarts=RHC_KS['restarts'], max_attempts=RHC_KS['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_KS_small_attempt_8am.csv') ''' RHC_FF = { 'max_attempts': 50, 'restarts': 20, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' results_df, curve_output_list = fitness_by_iter('RHC', problem_FF, RHC_FF['max_iters'], RHC_FF['random_state']\ , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_FF_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_FF_big, RHC_FF['max_iters'], RHC_FF['random_state']\ , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_FF_big_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_FF_small, RHC_FF['max_iters'], RHC_FF['random_state']\ , restarts=RHC_FF['restarts'], max_attempts=RHC_FF['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_FF_small_attempt_8am.csv') ''' RHC_4P = { 'max_attempts': 50, 'restarts': 20, 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' results_df, curve_output_list = fitness_by_iter('RHC', problem_4P, RHC_4P['max_iters'], RHC_4P['random_state']\ , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_4P_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_4P_small, RHC_4P['max_iters'], RHC_4P['random_state']\ , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_4P_small_attempt_8am.csv') results_df, curve_output_list = fitness_by_iter('RHC', problem_4P_big, RHC_4P['max_iters'], RHC_4P['random_state']\ , restarts=RHC_4P['restarts'], max_attempts=RHC_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_RHS_4P_big_attempt_8am.csv') ''' ## where it stopped print("I will now make the complexity curves for other algos") SA_4P_hacked = { 'max_attempts': 10, 'schedule': mlrose.GeomDecay(init_temp=100, decay=0.99), 'max_iters': max_iters_list_full, 'random_state': rand_list_full } ''' results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=SA_4P_hacked['schedule'], max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_decay_99.csv') results_df, curve_output_list = fitness_by_iter('SA', problem_4P, SA_4P['max_iters'], SA_4P['random_state']\ , schedule=mlrose.GeomDecay(init_temp=1, decay=0.8), max_attempts=SA_4P['max_attempts'], curve=True) results_df.to_csv(output_location + 'final_SA_4P_T_1_decay_80.csv') results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\ , pop_size=GA_KS['pop_size'], max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=0.1) results_df.to_csv(output_location + 'final_GA_KS_mutation_01.csv') ''' results_df, curve_output_list = fitness_by_iter('GA', problem_KS, GA_KS['max_iters'], GA_KS['random_state']\ , pop_size=100, max_attempts=GA_KS['max_attempts'], curve=True, mutation_prob=0.2) results_df.to_csv(output_location + 'final_GA_KS_mutation_02_pop_100.csv') ## Need a few more MIMIC chart inputs #print("Need a few more MIMIC chart inputs, so I will now make those") #print("Next FF p=100 keep=0.2") ''' MIMIC inputs for charts
def runPart1(savePath): fitness = mlrose.FourPeaks(t_pct=0.15) init_state = None fourPeaksProblem = mlrose.DiscreteOpt(length=12, fitness_fn=fitness, maximize=True, max_val=2) part1_1 = Part1(name='Four Peaks', fitness=fitness, problem=fourPeaksProblem, init_state=init_state) part1_1.runAll(savePath) fitness = mlrose.Queens() init_state = None eightQueensProblem = mlrose.DiscreteOpt(length=8, fitness_fn=fitness, maximize=False, max_val=8) part1_2 = Part1(name='Eight Queens', fitness=fitness, problem=eightQueensProblem, init_state=init_state) part1_2.runAll(savePath) fitness = mlrose.SixPeaks(t_pct=0.15) init_state = None sixPeaksProblem = mlrose.DiscreteOpt(length=11, fitness_fn=fitness, maximize=True, max_val=2) part1_4 = Part1(name='Six Peaks', fitness=fitness, problem=sixPeaksProblem, init_state=init_state) part1_4.runAll(savePath) fitness = mlrose.FlipFlop() init_state = None flipFlopProblem = mlrose.DiscreteOpt(length=7, fitness_fn=fitness, maximize=True, max_val=2) part1_5 = Part1(name='Flip Flop - 7', fitness=fitness, problem=flipFlopProblem, init_state=init_state) part1_5.runAll(savePath) fitness = mlrose.FlipFlop() init_state = None flipFlopProblem = mlrose.DiscreteOpt(length=100, fitness_fn=fitness, maximize=True, max_val=2) part1_5 = Part1(name='Flip Flop - 100', fitness=fitness, problem=flipFlopProblem, init_state=init_state) part1_5.runAll(savePath) fitness = mlrose.Queens() init_state = None eightQueensProblem = mlrose.DiscreteOpt(length=80, fitness_fn=fitness, maximize=False, max_val=8) part1_2 = Part1(name='Eighty Queens', fitness=fitness, problem=eightQueensProblem, init_state=init_state) part1_2.runAll(savePath) fitness = mlrose.FlipFlop() init_state = None flipFlopProblem = mlrose.DiscreteOpt(length=15, fitness_fn=fitness, maximize=True, max_val=2) part1_5 = Part1(name='Flip Flop - 15', fitness=fitness, problem=flipFlopProblem, init_state=init_state) part1_5.runAll(savePath) edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)] fitness = mlrose.MaxKColor(edges) init_state = None maxKColorsProblem = mlrose.DiscreteOpt(length=7, fitness_fn=fitness, maximize=False, max_val=2) part1_3 = Part1(name='Max-K Color', fitness=fitness, problem=maxKColorsProblem, init_state=init_state) part1_3.runAll(savePath) # ============================================================= # Source - Tutorial from MLRose Docs # https://mlrose.readthedocs.io/en/stable/source/tutorial2.html # # ============================================================= # Create list of city coordinates coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)] # Initialize fitness function object using coords_list fitness_coords = mlrose.TravellingSales(coords = coords_list) # Create list of distances between pairs of cities dist_list = [(0, 1, 3.1623), (0, 2, 4.1231), (0, 3, 5.8310), (0, 4, 4.2426), \ (0, 5, 5.3852), (0, 6, 4.0000), (0, 7, 2.2361), (1, 2, 1.0000), \ (1, 3, 2.8284), (1, 4, 2.0000), (1, 5, 4.1231), (1, 6, 4.2426), \ (1, 7, 2.2361), (2, 3, 2.2361), (2, 4, 2.2361), (2, 5, 4.4721), \ (2, 6, 5.0000), (2, 7, 3.1623), (3, 4, 2.0000), (3, 5, 3.6056), \ (3, 6, 5.0990), (3, 7, 4.1231), (4, 5, 2.2361), (4, 6, 3.1623), \ (4, 7, 2.2361), (5, 6, 2.2361), (5, 7, 3.1623), (6, 7, 2.2361)] # Initialize fitness function object using dist_list fitness_dists = mlrose.TravellingSales(distances = dist_list) # Define optimization problem object problem_fit = mlrose.TSPOpt(length = 8, fitness_fn = fitness_coords, maximize=False) coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)] # Define optimization problem object problem_no_fit = mlrose.TSPOpt(length = 8, coords = coords_list, maximize=False) part1_6 = Part1(name='TSP', fitness=coords_list, problem=problem_no_fit, init_state=None) part1_6.runAll(savePath) # Knapsack weights = np.random.randint(2, high=20, size=50) values = np.random.randint(2, high=100, size=50) max_weight_pct = 0.8 fitness = mlrose.Knapsack(weights, values, max_weight_pct) knapsackProblem = mlrose.DiscreteOpt(length=50, fitness_fn=fitness, maximize=False, max_val=2) part1_7 = Part1(name='Knapsack', fitness=fitness, problem=knapsackProblem, init_state=None) part1_7.runAll(savePath)
gen_evals=[] mimic_times=[] mimic_evals=[] for i in range(10, 51, 10): if i==0: continue # ex = {"weights": [random.randint(1, 20) for i in range(i)], "values": [random.randint(1, 10) for i in range(i)], "state": np.array([random.randint(0, 2) for i in range(i)])} input_sizes.append(i) # weights = ex['weights'] # values = ex['values'] # state = ex['state'] # edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)] # state = np.array([random.randint(1, 20) for i in range(i)]) state = np.array([random.randint(0, 2) for i in range(i)]) # fitness = mlrose_hiive.FourPeaks(t_pct=0.15) fitness = mlrose_hiive.FlipFlop() problem = mlrose_hiive.DiscreteOpt(length = len(state), fitness_fn = fitness, maximize = True, max_val = int(max(state))+1) # problem = mlrose_hiive.DiscreteOpt(length = 5, fitness_fn = fitness, maximize = False, max_val = 2) # problem = mlrose_hiive.DiscreteOpt(length = len(state), fitness_fn = fitness, maximize = True, max_val = int(max(state))+1) times = [] best_scores = [] start_time = time.time() best_state, best_fitness, fitness_curve = sa(problem,state, 10, 1000) elapsed_time = time.time() - start_time print(elapsed_time) times.append(elapsed_time*1000) best_scores.append(best_fitness) sa_times.append(elapsed_time*1000) sa_evals.append(len(fitness_curve)) plt.close()
def flip_flop(): print('Flip Flop') sa = [] rhc = [] ga = [] mim = [] input_sizes = [100] for i in input_sizes: state = np.array([np.random.randint(0, 2) for i in range(i)]) # state = np.zeros(100) # fitness = mlr.FourPeaks(0.1) fitness = mlr.FlipFlop() problem = mlr.DiscreteOpt(length=i, fitness_fn=fitness, maximize=True, max_val=2) best_state, best_fitness, fitness_curve, time = randomized_hill_climb( problem, state, 10, 1000) rhc.append((best_fitness, fitness_curve, time)) best_state, best_fitness, fitness_curve, time = simulated_annealing( problem, state, 10, 1000) sa.append((best_fitness, fitness_curve, time)) best_state, best_fitness, fitness_curve, time = genetic_algorithm( problem, state, 10, 1000) ga.append((best_fitness, fitness_curve, time)) best_state, best_fitness, fitness_curve, time = mimic( problem, state, 10, 1000) mim.append((best_fitness, fitness_curve, time)) plot_data([i + 1 for i in range(len(rhc[0][1]))], rhc[0][1], title="Flip Flop (Input Size = " + str(len(state)) + ")", x_label="Iterations", y_label="Fitness Score", color="blue", label='RHC') plot_data([i + 1 for i in range(len(sa[0][1]))], sa[0][1], title="Flip Flop (Input Size = " + str(len(state)) + ")", x_label="Iterations", y_label="Fitness Score", color="orange", label='SA') plot_data([i + 1 for i in range(len(ga[0][1]))], ga[0][1], title="Flip Flop (Input Size = " + str(len(state)) + ")", x_label="Iterations", y_label="Fitness Score", color="green", label='GA') plot_data([i + 1 for i in range(len(mim[0][1]))], mim[0][1], title="Flip Flop (Input Size = " + str(len(state)) + ")", x_label="Iterations", y_label="Fitness Score", color="red", label='MIMIC') title = 'Flip Flop' plt.savefig('output/' + title + '.png') plt.close()
return 100 return 0 def cf2(state): score = 0 for i in range(len(state)): score += (state[i] * (i + 1) % 7) return score FITNESS_FUNCS = { 'fourpeaks': mlrose.FourPeaks(), 'onemax': mlrose.OneMax(), # 'path': mlrose.CustomFitness(path, problem_type='discrete'), 'flipflop': mlrose.FlipFlop(), # 'cliffs': mlrose.CustomFitness(cf1, problem_type='discrete'), # 'cliffs': mlrose.CustomFitness(is_larger, problem_type='discrete'), # 'max2color': mlrose.MaxKColorGenerator.generate(seed=42, number_of_nodes=PROBLEM_LENGTH, max_colors=2), # 'mod': mlrose.CustomFitness(cf2, problem_type='discrete') } RANDOM_STATE = 42 DEFAULTS = {'random_state': RANDOM_STATE, 'curve': True, 'max_attempts': 10} ALGORITHMS = { 'rhc': lambda p: mlrose.random_hill_climb(p, **DEFAULTS), 'sa': lambda p: mlrose.simulated_annealing(p, **DEFAULTS), 'ga': lambda p: mlrose.genetic_alg(p, **DEFAULTS), 'mimic': lambda p: mlrose.mimic(p, **DEFAULTS) }
def run_FlipFlop(self, mode=None): fitness_fn = mlrose.FlipFlop() self.run_complexity(fitness_fn, mode)
import numpy as np import pandas as pd import param_search import mlrose_hiive as mlrose import matplotlib.pyplot as plt import os this_dir = os.path.dirname(__file__) #=================================================================================================== # STEP1: DEFINE A FITNESS FUNCTION, PROBLEM, AND OUTPUT DIRECTORY #=================================================================================================== fitness = mlrose.FlipFlop() problem = mlrose.DiscreteOpt(length=100, fitness_fn=fitness, maximize=True, max_val=2) problem_name = 'flipflop' output_dir = os.path.join( this_dir, problem_name ) #r"C:\Users\AREHAN2\Documents\omscs\CS7641\randomized_optimization\4peaks" #=================================================================================================== # STEP2: RUN OPTIMIZATIONS FOR A RANGE OF HYPERPARAMETERS FOR RHC, SA, GA, AND MIMIC #=================================================================================================== param_search.run_param_search(problem, output_dir) #=================================================================================================== # STEP3: READ IN THE DATA REGARDING THE RAN OPTIMIZATION #=================================================================================================== rhc_run_curves = pd.read_csv( os.path.join(output_dir, 'RHC', 'rhc__RHC__curves_df.csv')
def fitness_counter(state): global eval_count fitness = mlrose.FlipFlop() eval_count += 1 return fitness.evaluate(state)