def run(self): runner = MIMICRunner(problem=self.problem, experiment_name=self.name, seed=self.seed, iteration_list=self.iterations, population_sizes=self.population, keep_percent_list=self.keep_pct, max_attempts=self.attempts, generate_curves=True, output_directory=self.output) runner.run()
def run(experiment_name, input_size=25): SEED = 1 OUTPUT_DIRECTORY = './output' # pop_size_list = list(range(input_size, input_size*6, input_size)) pop_size_list = [50, 200, 250, 500] if (experiment_name == '4peaks'): ## Four Peaks problem = generate_problem(experiment_name, seed=SEED, input_size=50) mmc = MIMICRunner(problem=problem, experiment_name=experiment_name, output_directory=OUTPUT_DIRECTORY, seed=SEED, iteration_list=2**np.arange(10), population_sizes=[50, 200, 250, 500], max_attempts=100, keep_percent_list=[0.1, 0.25, 0.5, 0.75], use_fast_mimic=True) df_run_stats, df_run_curves = mmc.run() elif (experiment_name == 'flipflop'): ## Flip Flop problem = generate_problem(experiment_name, seed=SEED, input_size=25) mmc = MIMICRunner(problem=problem, experiment_name=experiment_name, output_directory=OUTPUT_DIRECTORY, seed=SEED, iteration_list=2**np.arange(10), population_sizes=pop_size_list, max_attempts=50, keep_percent_list=[0.1, 0.25, 0.5, 0.75], use_fast_mimic=True) df_run_stats, df_run_curves = mmc.run() elif (experiment_name == 'knapsack'): ## Knapsack problem = generate_problem(experiment_name, seed=SEED, input_size=150) mmc = MIMICRunner(problem=problem, experiment_name=experiment_name, output_directory=OUTPUT_DIRECTORY, seed=SEED, iteration_list=2**np.arange(10), population_sizes=[200, 250, 750], max_attempts=50, keep_percent_list=[0.1, 0.25, 0.5, 0.75], use_fast_mimic=True) df_run_stats, df_run_curves = mmc.run() else: print('input error') print('input size:', input_size)
def main(): experiment_name_ga = 'Max_K_GA' OUTPUT_DIRECTORY = './output' problem = MaxKColorGenerator.generate(seed=seed, number_of_nodes=40, max_connections_per_node=4) ga = GARunner(problem=problem, experiment_name=experiment_name_ga, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[150, 200, 250, 300], mutation_rates=[0.03, 0.1, 0.2, 0.3, 0.4]) ga.run() experiment_name_mimic = 'Max_K_MIMIC' mmc = MIMICRunner(problem=problem, experiment_name=experiment_name_mimic, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[150, 200, 250, 300], keep_percent_list=[0.03, 0.1, 0.2, 0.3, 0.4]) mmc.run() experiment_name_rhc = 'Max_K_RHC' rhc = RHCRunner(problem=problem, experiment_name=experiment_name_rhc, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=5000, restart_list=[25, 75, 100]) rhc.run() experiment_name_sa = 'Max_K_SA' sa = SARunner( problem=problem, experiment_name=experiment_name_sa, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(14), max_attempts=200, temperature_list=[1, 10, 50, 100, 250, 500, 1000, 2500, 5000, 10000]) sa.run()
output_directory=constants.OUTPUT_DIRECTORY, seed=0, iteration_list=constants.iteration_list, max_attempts=100, population_sizes=population_sizes, mutation_rates=mutation_rates) ga_run_stats, ga_run_curves = ga.run() if algorithm_name == "MIMIC" and False: population_sizes = [100] keep_percent_list = [0.05, 0.1, 0.2, 0.3, 0.4] mimic = MIMICRunner(problem=problem, experiment_name=experiment_name, output_directory=constants.OUTPUT_DIRECTORY, seed=0, iteration_list=[1, 2, 3, 4, 5, 10, 15, 20, 25, 30], population_sizes=population_sizes, max_attempts=100, keep_percent_list=keep_percent_list, use_fast_mimic=True) mimic_run_stats, mimic_run_curves = mimic.run() type = "curves" dir = 'output/{0}-{1}-{2}-{3}-experiment'.format(problem_name, algorithm_name, problem_size, t) filename = '{3}__{0}-{3}-{1}-{4}-experiment__{2}_df.csv'.format( problem_name, problem_size, type, algorithm_name, t) path = '{0}/{1}'.format(dir, filename) df = pd.read_csv(path) max_row = df.iloc[df["Fitness"].idxmax()]
def run_traveling_salesman(): OUTPUT_DIRECTORY = './output/TSP_results/3' SEED = 903387974 # Traveling Salesman problem = generateTSP(seed=SEED, number_of_cities=22) population = [350, 500] prob_rates = [0.01, 0.1, 0.3, 0.5] # Genetic Algorithm ga = GARunner(problem=problem, experiment_name="TSP_Maximize_GA_3", seed=SEED, iteration_list=2 ** np.arange(8), max_attempts=1000, population_sizes=population, mutation_rates=prob_rates, output_directory=OUTPUT_DIRECTORY) df_ga_run_stats, df_ga_run_curves = ga.run() ga_data_strings = { 'title': 'Genetic_Algorithms_22_Cities_3', 'Parameters': ['Mutation Rate', 'Population Size'] } best_fitness = max(df_ga_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_ga_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_ga_run_stats['Mutation Rate'][i]) print(df_ga_run_stats['Population Size'][i]) print(df_ga_run_stats['Time'][i]) print(df_ga_run_stats['Time'][i].sum()/len(df_ga_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_ga_run_curves).set_title('GA 22 Cities ' + str(best_fitness) + ' ' + str(avg_time)) fig.savefig('GA_22_Cities_Seaborn_3.png') generate_graphs(df_ga_run_stats, df_ga_run_curves, ga_data_strings) plt.show() OUTPUT_DIRECTORY = './output/TSP_results/2' # MIMIC Algorithm mmc = MIMICRunner(problem=problem, experiment_name="TSP_Maximize_MIMIC_2", seed=69, iteration_list=2 ** np.arange(8), max_attempts=500, population_sizes=population, keep_percent_list=prob_rates, use_fast_mimic=False, output_directory=OUTPUT_DIRECTORY) # Proportion of samples to keep at each iteration df_mmc_run_stats, df_mmc_run_curves = mmc.run() mmc_data_strings = { 'title': 'MIMIC_22_Cities_2', 'Parameters': ['Population Size', 'Keep Percent'] } print("=============") best_fitness = max(df_mmc_run_stats['Fitness']) print(best_fitness) index_array = np.where(df_mmc_run_stats['Fitness'] == best_fitness) print("Index:") print(index_array) time = 0 for i in index_array: print(df_mmc_run_stats['Keep Percent'][i]) print(df_mmc_run_stats['Population Size'][i]) print(df_mmc_run_stats['Time'][i]) print(df_mmc_run_stats['Time'][i].sum()/len(df_mmc_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_mmc_run_curves).set_title('MIMIC 22 Cities ' + str(best_fitness) + ' ' + str(avg_time)) fig.savefig('MIMIC_22_Cities_Seaborn_2.png') generate_graphs(df_mmc_run_stats, df_mmc_run_curves, mmc_data_strings) plt.show() # Randomized Hill Climbing rhc = RHCRunner(problem=problem, experiment_name="TSP_Maximize_RHC_2", seed=SEED, iteration_list=2 ** np.arange(8), max_attempts=500, restart_list=[5, 15, 25, 50, 75, 95], output_directory=OUTPUT_DIRECTORY) # Number of random restarts df_rhc_run_stats, df_rhc_run_curves = rhc.run() rhc_data_string = { 'title': 'RHC_22_Cities_2', 'Parameters': ['Restarts'] } best_fitness = max(df_rhc_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_rhc_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_rhc_run_stats['Restarts'][i]) print(df_rhc_run_stats['Time'][i]) print(df_rhc_run_stats['Time'][i].sum()/len(df_rhc_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_rhc_run_curves).set_title('RHC 22 Cities ' + str(best_fitness) + ' ' + str(avg_time)) fig.savefig('RHC_22_Cities_Seaborn_2.png') generate_graphs(df_rhc_run_stats, df_rhc_run_curves, rhc_data_string) plt.show() # Simulated Annealing sa = SARunner(problem=problem, experiment_name="TSP_Maximize_SA_2", seed=SEED, iteration_list=2 ** np.arange(10), max_attempts=1000, temperature_list=[0.001, 0.05, 0.1, 0.25, 0.5, 0.8], output_directory=OUTPUT_DIRECTORY) # Temperature is just the decay df_sa_run_stats, df_sa_run_curves = sa.run() sa_data_string = { 'title': 'SA_22_Cities_2', 'Parameters': ['Temperature'] } best_fitness = max(df_sa_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_sa_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_sa_run_stats['Temperature'][i]) print(df_sa_run_stats['Time'][i]) print(df_sa_run_stats['Time'][i].sum() / len(df_sa_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_sa_run_curves).set_title('SA 22 Cities ' + str(best_fitness) + ' ' + str(avg_time)) fig.savefig('SA_22_Cities_Seaborn_2.png') generate_graphs(df_sa_run_stats, df_sa_run_curves, sa_data_string) plt.show()
def run_knapsack(): # Knapsack OUTPUT_DIRECTORY = './output/Knapsack_results/4' SEED = 903387974 problem = KnapsackGenerator.generate(seed=SEED, number_of_items_types=12, max_item_count=6, max_weight_per_item=26, max_value_per_item=12, max_weight_pct=0.8) population = [50, 130, 210, 500] prob_rates = [0.05, 0.3, 0.5, 0.8] # Genetic Algorithm ga = GARunner(problem=problem, experiment_name="Knapsack_Maximize_GA_4", seed=SEED, iteration_list=2 ** np.arange(6), max_attempts=500, population_sizes=population, mutation_rates=prob_rates, output_directory=OUTPUT_DIRECTORY) df_ga_run_stats, df_ga_run_curves = ga.run() ga_data_strings = { 'title': 'Genetic_Algorithms_Knapsack_4', 'Parameters': ['Mutation Rate', 'Population Size'] } best_fitness = max(df_ga_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_ga_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_ga_run_stats['Mutation Rate'][i]) print(df_ga_run_stats['Population Size'][i]) print(df_ga_run_stats['Time'][i]) print(df_ga_run_stats['Time'][i].sum()/len(df_ga_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_ga_run_curves).set_title('GA_Knapsack_' + str(best_fitness) + '_' + str(avg_time)) fig.savefig('GA_Knapsack_Seaborn_4.png') generate_graphs(df_ga_run_stats, df_ga_run_curves, ga_data_strings) plt.show() # MIMIC Algorithm mmc = MIMICRunner(problem=problem, experiment_name="Knapsack_Maximize_MIMIC_4", seed=SEED, iteration_list=2 ** np.arange(6), max_attempts=500, population_sizes=population, keep_percent_list=prob_rates, use_fast_mimic=False, output_directory=OUTPUT_DIRECTORY) # Proportion of samples to keep at each iteration df_mmc_run_stats, df_mmc_run_curves = mmc.run() mmc_data_strings = { 'title': 'MIMIC_Knapsack_4', 'Parameters': ['Population Size', 'Keep Percent'] } best_fitness = max(df_mmc_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_mmc_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_mmc_run_stats['Keep Percent'][i]) print(df_mmc_run_stats['Population Size'][i]) print(df_mmc_run_stats['Time'][i]) print(df_mmc_run_stats['Time'][i].sum()/len(df_mmc_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_mmc_run_curves).set_title('MIMIC_Knapsack_' + str(best_fitness) + '_' + str(avg_time)) fig.savefig('MIMIC_Knapsack_Seaborn_4.png') generate_graphs(df_mmc_run_stats, df_mmc_run_curves, mmc_data_strings) plt.show() # Randomized Hill Climbing rhc = RHCRunner(problem=problem, experiment_name="Knapsack_Maximize_RHC_4", seed=SEED, iteration_list=2 ** np.arange(2), max_attempts=250, restart_list=[5, 15, 50], output_directory=OUTPUT_DIRECTORY) # Number of random restarts df_rhc_run_stats, df_rhc_run_curves = rhc.run() rhc_data_string = { 'title': 'RHC_Knapsack_4', 'Parameters': ['Restarts'] } best_fitness = max(df_rhc_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_rhc_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_rhc_run_stats['Restarts'][i]) print(df_rhc_run_stats['Time'][i]) print(df_rhc_run_stats['Time'][i].sum()/len(df_rhc_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_rhc_run_curves).set_title('RHC_Knapsack_' + str(best_fitness) + '_' + str(avg_time)) fig.savefig('RHC_Knapsack_Seaborn_4.png') generate_graphs(df_rhc_run_stats, df_rhc_run_curves, rhc_data_string) plt.show() # Simulated Annealing sa = SARunner(problem=problem, experiment_name="Knapsack_Maximize_SA_4", seed=SEED, iteration_list=2 ** np.arange(8), max_attempts=1000, temperature_list=[0.001, 0.05, 0.1, 0.25, 0.5, 0.8], output_directory=OUTPUT_DIRECTORY) # Temperature is just the Geometric decay df_sa_run_stats, df_sa_run_curves = sa.run() sa_data_string = { 'title': 'SA_Knapsack_4', 'Parameters': ['Temperature'] } best_fitness = max(df_sa_run_stats['Fitness']) print("=============") print(best_fitness) index_array = np.where(df_sa_run_stats['Fitness'] == best_fitness) time = 0 for i in index_array: print(df_sa_run_stats['Temperature'][i]) print(df_sa_run_stats['Time'][i]) print(df_sa_run_stats['Time'][i].sum()/len(df_sa_run_stats['Time'][i])) avg_time = time / len(index_array) print(avg_time) print("============") fig = plt.figure(dpi=100, figsize=(11, 8)) sns.lineplot(x='Iteration', y='Fitness', data=df_sa_run_curves).set_title('SA_Knapsack_' + str(best_fitness) + '_' + str(avg_time)) fig.savefig('SA_Knapsack_Seaborn_4.png') generate_graphs(df_sa_run_stats, df_sa_run_curves, sa_data_string) plt.show()
def run_continuous_peaks(run_rhc, run_sa, run_ga, run_mimic, find_params): np.random.seed(0) continuous_peaks_problem = CustomContinuousPeaksGenerator.generate(1, 80) rhc_run_iterations = None rhc_run_average_restarts = None rhc_run_average_time = None sa_run_iterations = None sa_run_average = None sa_run_average_time = None ga_run_iterations = None ga_run_average = None ga_run_average_time = None mimic_run_iterations = None mimic_run_average = None mimic_run_average_time = None if run_rhc: out_dir = TRAVELING_SALES_PERSON_PATH + RHC_PATH if find_params: title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (ax1), = plt.subplots(1, 1, figsize=(3, 2)) ax1.set_title( "Randomized Hill Climbing with Various Restarts \n Continous Peaks Problem", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=6) ax1.tick_params(axis="y", labelsize=6) ax1.set_xlabel("Iteration", title_dic) num_restarts = [3, 5, 15, 25, 50] restart_params = {3: [], 5: [], 15: [], 25: [], 50: []} for param in num_restarts: restarts = [] iterations = [] for i in range(param): seed = np.random.randint(100) # run RHC runner with param restarts. rhc = RHCRunner(problem=continuous_peaks_problem, experiment_name="Grid Search Restart " + str(i) + " of max restarts " + str(param), output_directory=out_dir, seed=seed, iteration_list=np.arange(1, 2200, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() iterations = rhc_run_stats['Iteration'].tolist() restarts.append(rhc_run_stats['Fitness'].tolist()) average_restarts = np.mean(np.asarray(restarts), axis=0) ax1.plot(iterations, average_restarts, label=str(param) + " Restarts") # Put a legend to the right of the current axis box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.99, box.height]) ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "various restarts CPP problem.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) # Reproduce results from 3 restart run. restarts = [] iterations = [] times = [] for i in range(15): # run RHC runner with param restarts. rhc = RHCRunner(problem=continuous_peaks_problem, experiment_name="RHC CPP", output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 3500, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() rhc_run_iterations = rhc_run_stats['Iteration'].tolist() restarts.append(rhc_run_stats['Fitness'].tolist()) times.append(rhc_run_stats["Time"].iloc[-1] * 1000) rhc_run_average_restarts = np.mean(np.asarray(restarts), axis=0) rhc_run_average_time = np.mean(times) if run_sa: out_dir = TRAVELING_SALES_PERSON_PATH + SA_PATH if find_params: decay_list = [mlrose_hiive.GeomDecay, mlrose_hiive.ExpDecay] decay_names = { mlrose_hiive.GeomDecay: ('Geometric', 'solid'), mlrose_hiive.ExpDecay: ('Exponential', 'dotted'), mlrose_hiive.ArithDecay: ('Arithmetic', 'dashed') } # Arithmetic decay was way too slow. No progress was getting made. decay_names = { mlrose_hiive.GeomDecay: ('Geometric', 'solid'), mlrose_hiive.ExpDecay: ('Exponential', 'dotted') } #mlrose_hiive.ArithDecay: ('Arithmetic', 'dashed')} temperature_list = [1, 10, 50, 100, 250, 500, 1000] parameters = [] for i in decay_list: for j in temperature_list: parameters.append((i, j)) title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (ax1), = plt.subplots(1, 1, figsize=(5, 2.5)) ax1.set_title( "Simulated Annealing with Various Decay Schedules \n and Initial Temperature", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=6) ax1.tick_params(axis="y", labelsize=6) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): sa = SARunner( problem=continuous_peaks_problem, experiment_name= "TSP Simulated Annealing with Decay Schedule" + str(entry[0]) + "and Initial Temperature " + str(entry[1]) + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 5000, 100), max_attempts=200, decay_list=[entry[0]], temperature_list=[entry[1]]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() runs.append(sa_run_stats['Fitness'].tolist()) sa_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(sa_run_iterations, sa_run_average, linestyle=decay_names[entry[0]][1], label=decay_names[entry[0]][0] + " Init Temp:" + str(entry[1])) # Put a legend to the right of the current axis box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.99, box.height]) ax1.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=6) ax1.grid() plt.tight_layout() filename = "decay_and_initial_temp.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) runs = [] times = [] for i in range(5): sa = SARunner( problem=continuous_peaks_problem, experiment_name= "TSP Simulated Annealing after Param Tuning Decay Schedule", output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 5000, 100), max_attempts=1000, decay_list=[mlrose_hiive.GeomDecay], temperature_list=[50]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() times.append(sa_run_stats["Time"].iloc[-1] * 1000) runs.append(sa_run_stats['Fitness'].tolist()) sa_run_average = np.mean(np.asarray(runs), axis=0) sa_run_average_time = np.mean(times) if run_ga: out_dir = TRAVELING_SALES_PERSON_PATH + GA_PATH if find_params: population_sizes = [150, 200, 250] pop_keys = {250: ('solid')} # first iteration revealed a pop size of 150 was sufficient. population_sizes = [250] mutation_rates = [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6] #pop_keys = { # 150: ('solid') #} parameters = [] for i in population_sizes: for j in mutation_rates: parameters.append((i, j)) title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (ax1), = plt.subplots(1, 1, figsize=(3, 2)) ax1.set_title("Genetic Algorithms with Various Mutation Rates", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=6) ax1.tick_params(axis="y", labelsize=6) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): ga = GARunner(problem=continuous_peaks_problem, experiment_name="TSP GA with Population" + str(entry[0]) + "and Mutation Split " + str(entry[1]), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 500, 25), max_attempts=150, population_sizes=[entry[0]], mutation_rates=[entry[1]]) ga_run_stats, _ = ga.run() ga_run_iterations = ga_run_stats['Iteration'].tolist() runs.append(ga_run_stats['Fitness'].tolist()) ga_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(ga_run_iterations, ga_run_average, linestyle=pop_keys[entry[0]], label="MutRate:" + str(entry[1])) # Put a legend to the right of the current axis box = ax1.get_position() ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "population_and_mutation_rates.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) runs = [] times = [] for i in range(5): ga = GARunner(problem=continuous_peaks_problem, experiment_name="CPP GA After Parameter Tuning" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 600, 25), max_attempts=150, population_sizes=[250], mutation_rates=[0.3]) ga_run_stats, _ = ga.run() ga_run_iterations = ga_run_stats['Iteration'].tolist() times.append(ga_run_stats["Time"].iloc[-1] * 1000) runs.append(ga_run_stats['Fitness'].tolist()) ga_run_average = np.mean(np.asarray(runs), axis=0) ga_run_average_time = np.mean(times) if run_mimic: out_dir = TRAVELING_SALES_PERSON_PATH + MIMIC_PATH if find_params: population_sizes = [200, 250, 300] # Second search, 200 provided the overall best. population_sizes = [250] keep_percent = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] pop_keys = { 250: ('solid'), } parameters = [] for i in population_sizes: for j in keep_percent: parameters.append((i, j)) title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (ax1), = plt.subplots(1, 1, figsize=(3, 2)) ax1.set_title( "Mimic with 250 Population Size and Various \n Keep Percentages", title_dic) ax1.set_ylabel("Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=6) ax1.tick_params(axis="y", labelsize=6) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): mimic = MIMICRunner( problem=continuous_peaks_problem, experiment_name="TSP Mimic with Population" + str(entry[0]) + "and Keep Percent " + str(entry[1]), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 250, 25), population_sizes=[entry[0]], keep_percent_list=[entry[1]], max_attempts=5, generate_curves=True, use_fast_mimic=True) df_run_stats, _ = mimic.run() mimic_run_iterations = df_run_stats['Iteration'].tolist() runs.append(df_run_stats['Fitness'].tolist()) mimic_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(mimic_run_iterations, mimic_run_average, linestyle=pop_keys[entry[0]], label="Keep %:" + str(entry[1])) # Put a legend to the right of the current axis box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.99, box.height]) ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "population_and_keep_percent_rates.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) runs = [] times = [] for i in range(5): mimic = MIMICRunner(problem=continuous_peaks_problem, experiment_name="CPP Mimic" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 150, 5), population_sizes=[250], keep_percent_list=[0.5], max_attempts=50, generate_curves=True, use_fast_mimic=True) mimic_run_stats, _ = mimic.run() mimic_run_iterations = mimic_run_stats['Iteration'].tolist() times.append(mimic_run_stats["Time"].iloc[-1] * 1000) runs.append(mimic_run_stats['Fitness'].tolist()) mimic_run_average = np.mean(np.asarray(runs), axis=0) mimic_run_average_time = np.mean(times) title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (fitness_cp, time_cp) = plt.subplots(1, 2, figsize=(6, 2)) fitness_cp.set_title( "Fitness vs Iterations\n Continuous Peak Problem N = 80", title_dic) fitness_cp.set_ylabel("Average Fitness Score", title_dic) fitness_cp.tick_params(axis="x", labelsize=6) fitness_cp.tick_params(axis="y", labelsize=6) fitness_cp.set_xlabel("Iteration", title_dic) fitness_cp.plot(sa_run_iterations, sa_run_average, label="SA") fitness_cp.plot(ga_run_iterations, ga_run_average, label="GA") fitness_cp.plot(rhc_run_iterations, rhc_run_average_restarts, label="RHC") fitness_cp.plot(mimic_run_iterations, mimic_run_average, label="MIMIC") fitness_cp.plot(sa_run_iterations, np.ones((len(sa_run_iterations), 1)) * 151, label="Global\nOptimum", linestyle='--', color='k') fitness_cp.legend(loc='best', fontsize=6) time_cp.set_xlabel("Time (ms)", title_dic) time_cp.set_title("Runtime \n Continuous Peaks Problem N = 80", title_dic) time_cp.set_ylabel("Algorithm", title_dic) time_cp.tick_params(axis="x", labelsize=6) time_cp.tick_params(axis="y", labelsize=6) x = ['RHC', 'SA', 'GA', 'MIMIC'] x_pos = [i for i, _ in enumerate(x)] time_cp.set_yticks(x_pos) time_cp.set_yticklabels(x) time_cp.barh(x_pos, [ rhc_run_average_time, sa_run_average_time, ga_run_average_time, mimic_run_average_time ]) time_cp.grid() fitness_cp.grid() plt.tight_layout() path = TRAVELING_SALES_PERSON_PATH filename = "fitness_vs_iteration.png" filename = os.path.join(path, filename) plt.savefig(filename) title_dic = {'fontsize': 6, 'fontweight': 'bold'} fig, (fitness_cp, time_cp) = plt.subplots(1, 2, figsize=(6, 2)) fitness_cp.set_title("Fitness vs Problem Size\n Continuous Peak Problem", title_dic) fitness_cp.set_ylabel("Average Fitness Score", title_dic) fitness_cp.tick_params(axis="x", labelsize=6) fitness_cp.tick_params(axis="y", labelsize=6) fitness_cp.set_xlabel("Problem Size(N)", title_dic) # Running on different problem set sizes sizes = [50, 75, 100, 125, 150] rhc_average_size = [] sa_average_size = [] ga_average_size = [] mimic_average_size = [] rhc_average_time = [] sa_average_time = [] ga_average_time = [] mimic_average_time = [] for size in sizes: continuous_peaks_problem = CustomContinuousPeaksGenerator.generate( 1, size) restarts = [] iterations = [] seeds = [44, 43, 29] times = [] for i in range(3): seed = seeds[i] # run RHC runner with param restarts. rhc = RHCRunner(problem=continuous_peaks_problem, experiment_name="RHC CPP", output_directory=out_dir, seed=seed, iteration_list=np.arange(1, 2200, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() rhc_run_iterations = rhc_run_stats['Iteration'].tolist() restarts.append(rhc_run_stats['Fitness'].tolist()) times.append(rhc_run_stats["Time"].iloc[-1] * 1000) rhc_run_average_restarts = np.mean(np.asarray(restarts), axis=0) rhc_run_average_time = np.mean(times) rhc_average_size.append(rhc_run_average_restarts[-1]) rhc_average_time.append(rhc_run_average_time) runs = [] times = [] for i in range(5): sa = SARunner( problem=continuous_peaks_problem, experiment_name= "TSP Simulated Annealing after Param Tuning Decay Schedule", output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 5000, 100), max_attempts=1000, decay_list=[mlrose_hiive.GeomDecay], temperature_list=[1000]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() times.append(sa_run_stats["Time"].iloc[-1] * 1000) runs.append(sa_run_stats['Fitness'].tolist()) sa_run_average = np.mean(np.asarray(runs), axis=0) sa_run_average_time = np.mean(times) sa_average_size.append(sa_run_average[-1]) sa_average_time.append(sa_run_average_time) runs = [] times = [] for i in range(5): ga = GARunner(problem=continuous_peaks_problem, experiment_name="CPP GA After Parameter Tuning", output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 500, 25), max_attempts=150, population_sizes=[150], mutation_rates=[0.3]) ga_run_stats, _ = ga.run() ga_run_iterations = ga_run_stats['Iteration'].tolist() times.append(ga_run_stats["Time"].iloc[-1] * 1000) runs.append(ga_run_stats['Fitness'].tolist()) ga_run_average = np.mean(np.asarray(runs), axis=0) ga_run_average_time = np.mean(times) ga_average_size.append(ga_run_average[-1]) ga_average_time.append(ga_run_average_time) runs = [] times = [] for i in range(5): mimic = MIMICRunner(problem=continuous_peaks_problem, experiment_name="CPP Mimic", output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 150, 5), population_sizes=[250], keep_percent_list=[0.5], max_attempts=50, generate_curves=True, use_fast_mimic=True) mimic_run_stats, _ = mimic.run() mimic_run_iterations = mimic_run_stats['Iteration'].tolist() times.append(mimic_run_stats["Time"].iloc[-1] * 1000) runs.append(mimic_run_stats['Fitness'].tolist()) mimic_run_average = np.mean(np.asarray(runs), axis=0) mimic_run_average_time = np.mean(times) mimic_average_size.append(mimic_run_average[-1]) mimic_average_time.append(mimic_run_average_time) fitness_cp.plot(sizes, sa_average_size, label="SA") fitness_cp.plot(sizes, ga_average_size, label="GA") fitness_cp.plot(sizes, rhc_average_size, label="RHC") fitness_cp.plot(sizes, mimic_average_size, label="MIMIC") fitness_cp.legend(loc='best', fontsize=6) time_cp.set_ylabel("Time (ms)", title_dic) time_cp.set_title("Runtime vs Problem Size \n Continuous Peaks Problem", title_dic) time_cp.set_xlabel("Problem Size(N)", title_dic) time_cp.tick_params(axis="x", labelsize=6) time_cp.tick_params(axis="y", labelsize=6) time_cp.plot(sizes, sa_average_time, label="SA") time_cp.plot(sizes, ga_average_time, label="GA") time_cp.plot(sizes, rhc_average_time, label="RHC") time_cp.plot(sizes, mimic_average_time, label="MIMIC") time_cp.legend(loc='best', fontsize=6) time_cp.grid() fitness_cp.grid() plt.tight_layout() path = TRAVELING_SALES_PERSON_PATH filename = "fitness_vs_iteration_various_problem_sizes.png" filename = os.path.join(path, filename) plt.savefig(filename)
print("fitness evaluations: {}".format(eval_count)) rhc_evals_list.append(eval_count) eval_count = 0 mimic_evals_list = [] df_mimic_stats_list = [] df_mimic_curves_list = [] for r in range(mimic_repeats): problem.set_mimic_fast_mode(True) mimic = MIMICRunner(problem=problem, experiment_name="mimic_test", output_directory="./results/", seed=r, iteration_list=2 ** np.arange(18), max_attempts=200, use_fast_mimic=True, keep_percent_list=[0.05], population_sizes=[200]) df_mimic_stats, df_mimic_curves = mimic.run() df_mimic_stats_list.append(df_mimic_stats) df_mimic_curves_list.append(df_mimic_curves) print("fitness evaluations: {}".format(eval_count)) mimic_evals_list.append(eval_count) eval_count = 0 # # fitness vs iterations
def main(): experiment_name_ga = 'TSP_Maximize_GA' OUTPUT_DIRECTORY = './output' problem = TSPGenerator.generate(seed=seed, number_of_cities=50, maximize=True) ga = GARunner(problem=problem, experiment_name=experiment_name_ga, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[150, 200, 250, 300], mutation_rates=[0.01, 0.02, 0.03, 0.1, 0.2, 0.3, 0.4]) ga.run() experiment_name_mimic = 'TSP_Maximize_MIMIC' mmc = MIMICRunner(problem=problem, experiment_name=experiment_name_mimic, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[150, 200, 250, 300], keep_percent_list=[0.01, 0.02, 0.03, 0.1, 0.2, 0.3, 0.4]) mmc.run() experiment_name_rhc = 'TSP_Maximize_RHC' rhc = RHCRunner(problem=problem, experiment_name=experiment_name_rhc, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, restart_list=[25, 75, 100]) rhc.run() experiment_name_sa = 'TSP_Maximize_SA' sa = SARunner( problem=problem, experiment_name=experiment_name_sa, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(14), max_attempts=200, temperature_list=[1, 10, 50, 100, 250, 500, 1000, 2500, 5000, 10000]) sa.run() OUTPUT_DIRECTORY = './output/N_CITIES' for i in range(10, 100): problem = TSPGenerator.generate(seed=seed, number_of_cities=i, maximize=True) experiment_name_cities_ga = str(i) + '_' + 'City_Fitness_ga' ga = GARunner(problem=problem, experiment_name=experiment_name_cities_ga, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[300], mutation_rates=[0.03]) ga.run() experiment_name_cities_MIMIC = str(i) + '_' + 'City_Fitness_MIMIC' mmc = MIMICRunner(problem=problem, experiment_name=experiment_name_cities_MIMIC, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, population_sizes=[300], keep_percent_list=[0.2]) mmc.run() experiment_name_cities_RHC = str(i) + '_' + 'City_Fitness_RHC' rhc = RHCRunner(problem=problem, experiment_name=experiment_name_cities_RHC, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(12), max_attempts=200, restart_list=[25]) rhc.run() experiment_name_cities_sa = str(i) + '_' + 'City_Fitness_SA' sa = SARunner(problem=problem, experiment_name=experiment_name_cities_sa, output_directory=OUTPUT_DIRECTORY, seed=seed, iteration_list=2**np.arange(14), max_attempts=200, temperature_list=[10]) sa.run()
def run_knapsack_experiment(run_rhc, run_sa, run_ga, run_mimic, find_params): title_dic = {'fontsize': 7, 'fontweight': 'bold'} axis_label_size = 7 tuning_figure_size = (3.5, 2) np.random.seed(0) problem = CustomKnapSackGenerator.generate( 1, number_of_items_types=100, max_item_count=50, max_weight_per_item=100, max_value_per_item=50, max_weight_pct=0.6, multiply_by_max_item_count=True, crossover=one_point_crossover.OnePointCrossOver, mutator=change_one_mutator.ChangeOneMutator) rhc_run_iterations = None rhc_run_average_restarts = None rhc_run_average_time = None sa_run_iterations = None sa_run_average = None sa_run_average_time = None ga_run_iterations = None ga_run_average = None ga_run_average_time = None mimic_run_iterations = None mimic_run_average = None mimic_run_average_time = None if run_rhc: out_dir = KNAPSACK_PATH + RHC_PATH if find_params: fig, (ax1), = plt.subplots(1, 1, figsize=tuning_figure_size) ax1.set_title( "Randomized Hill Climbing with Various Restarts \n Continous Peaks Problem", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=axis_label_size) ax1.tick_params(axis="y", labelsize=axis_label_size) ax1.set_xlabel("Iteration", title_dic) num_restarts = [3, 5, 15, 25, 50] restart_params = {3: [], 5: [], 15: [], 25: [], 50: []} for param in num_restarts: restarts = [] iterations = [] for i in range(param): seed = np.random.randint(100) # run RHC runner with param restarts. rhc = RHCRunner(problem=problem, experiment_name="Grid Search Restart " + str(i) + " of max restarts " + str(param), output_directory=out_dir, seed=seed, iteration_list=np.arange(1, 1000, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() iterations = rhc_run_stats['Iteration'].tolist() fitness = np.asarray(rhc_run_stats['Fitness'].tolist()) restarts.append(fitness) average_restarts = np.mean(np.asarray(restarts), axis=0) ax1.plot(iterations, average_restarts, label=str(param) + " Restarts") # Put a legend to the right of the current axis box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.99, box.height]) ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "various restarts CPP problem.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) # Reproduce results from 3 restart run. restarts = [] iterations = [] times = [] for i in range(15): # run RHC runner with param restarts. rhc = RHCRunner(problem=problem, experiment_name="RHC QUEENS" + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1000, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() rhc_run_iterations = rhc_run_stats['Iteration'].tolist() fitness = np.asarray(rhc_run_stats['Fitness'].tolist()) restarts.append(fitness) times.append(rhc_run_stats["Time"].iloc[-1] * 1000) rhc_run_average_restarts = np.mean(np.asarray(restarts), axis=0) rhc_run_average_time = np.mean(times) if run_sa: out_dir = KNAPSACK_PATH + SA_PATH if find_params: #decay_list = [mlrose_hiive.GeomDecay, mlrose_hiive.ExpDecay] # Round two: Exp Decay was better decay_list = [mlrose_hiive.ExpDecay] decay_names = { mlrose_hiive.GeomDecay: ('Geometric', 'dotted'), mlrose_hiive.ExpDecay: ('Exponential', 'solid'), mlrose_hiive.ArithDecay: ('Arithmetic', 'dashed') } temperature_list = [500, 1000, 2500, 3500, 4500, 5000] parameters = [] for i in decay_list: for j in temperature_list: parameters.append((i, j)) fig, (ax1), = plt.subplots(1, 1, figsize=tuning_figure_size) ax1.set_title( "Simulated Annealing with Exponential Decay \n Schedule and Initial Temperature - Knapsack", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=axis_label_size) ax1.tick_params(axis="y", labelsize=axis_label_size) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): sa = SARunner( problem=problem, experiment_name= "KnapSack Simulated Annealing with Decay Schedule" + str(entry[0]) + "and Initial Temperature " + str(entry[1]) + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 2000, 100), max_attempts=1000, decay_list=[entry[0]], temperature_list=[entry[1]]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() fitness = np.asarray(sa_run_stats['Fitness'].tolist()) runs.append(fitness) sa_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(sa_run_iterations, sa_run_average, linestyle=decay_names[entry[0]][1], label="Init Temp:" + str(entry[1])) ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "decay_and_initial_temp.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) # Run 5 times and average runs = [] times = [] for i in range(5): sa = SARunner( problem=problem, experiment_name= "Knapsack Simulated Annealing after Param Tuning Decay Schedule " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1500, 100), max_attempts=1000, decay_list=[mlrose_hiive.ExpDecay], temperature_list=[3500]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() times.append(sa_run_stats["Time"].iloc[-1] * 1000) fitness = np.asarray(sa_run_stats['Fitness'].tolist()) runs.append(fitness) sa_run_average = np.mean(np.asarray(runs), axis=0) sa_run_average_time = np.mean(times) if run_ga: out_dir = KNAPSACK_PATH + GA_PATH if find_params: population_sizes = [25, 50, 75, 100] # first iteration revealed a pop size of 75 was sufficient. population_sizes = [75] pop_keys = {100: ('dashed'), 50: ('dashed'), 75: ('solid')} mutation_rates = [ 0.1, 0.3, 0.6, 0.9, ] parameters = [] for i in population_sizes: for j in mutation_rates: parameters.append((i, j)) fig, (ax1), = plt.subplots(1, 1, figsize=tuning_figure_size) ax1.set_title( "Genetic Algorithms with Population Size 75 & \n Various Mutation Rates - Knapsack", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=axis_label_size) ax1.tick_params(axis="y", labelsize=axis_label_size) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): ga = GARunner( problem=problem, experiment_name="Knapsack GA with Population" + str(entry[0]) + "and Mutation Split " + str(entry[1]), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1500, 25), max_attempts=150, population_sizes=[entry[0]], mutation_rates=[entry[1]]) ga_run_stats, _ = ga.run() fitness = np.asarray(ga_run_stats['Fitness'].tolist()) ga_run_iterations = ga_run_stats['Iteration'].tolist() runs.append(fitness) ga_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(ga_run_iterations, ga_run_average, linestyle=pop_keys[entry[0]], label="MutRate:" + str(entry[1])) ax1.legend(loc='best', fontsize=6, ncol=2) ax1.grid() plt.tight_layout() filename = "population_and_mutation_rates.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) runs = [] times = [] for i in range(5): ga = GARunner(problem=problem, experiment_name="CPP GA After Parameter Tuning" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1500, 25), max_attempts=150, population_sizes=[75], mutation_rates=[0.9]) ga_run_stats, _ = ga.run() ga_run_iterations = ga_run_stats['Iteration'].tolist() times.append(ga_run_stats["Time"].iloc[-1] * 1000) fitness = np.asarray(ga_run_stats['Fitness'].tolist()) runs.append(fitness) ga_run_average = np.mean(np.asarray(runs), axis=0) ga_run_average_time = np.mean(times) if run_mimic: out_dir = KNAPSACK_PATH + MIMIC_PATH if find_params: population_sizes = [25, 50, 100, 150, 200] # Pop size of 100 was best tradeoff with time. population_sizes = [100] keep_percent = [ 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, ] pop_keys = {100: ('solid')} parameters = [] for i in population_sizes: for j in keep_percent: parameters.append((i, j)) fig, (ax1), = plt.subplots(1, 1, figsize=tuning_figure_size) ax1.set_title( "Mimic with 100 Population Size and Various \n Keep Percentages - Knapsack", title_dic) ax1.set_ylabel("Average Fitness Score", title_dic) ax1.tick_params(axis="x", labelsize=axis_label_size) ax1.tick_params(axis="y", labelsize=axis_label_size) ax1.set_xlabel("Iteration", title_dic) for entry in parameters: runs = [] for i in range(5): mimic = MIMICRunner( problem=problem, experiment_name="Knapsack Mimic with Population" + str(entry[0]) + "and Keep Percent " + str(entry[1]) + "run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 50, 1), population_sizes=[entry[0]], keep_percent_list=[entry[1]], max_attempts=5, generate_curves=True, use_fast_mimic=True) df_run_stats, _ = mimic.run() mimic_run_iterations = df_run_stats['Iteration'].tolist() fitness = np.asarray(df_run_stats['Fitness'].tolist()) runs.append(fitness) mimic_run_average = np.mean(np.asarray(runs), axis=0) ax1.plot(mimic_run_iterations, mimic_run_average, linestyle=pop_keys[entry[0]], label="Keep %:" + str(entry[1])) # Put a legend to the right of the current axis ax1.legend(loc='best', fontsize=6) ax1.grid() plt.tight_layout() filename = "population_and_keep_percent_rates.png" filename = os.path.join(out_dir, filename) plt.savefig(filename) runs = [] times = [] for i in range(5): mimic = MIMICRunner(problem=problem, experiment_name="Knapsack Mimic" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 150, 5), population_sizes=[100], keep_percent_list=[0.5], max_attempts=5, generate_curves=True, use_fast_mimic=True) mimic_run_stats, _ = mimic.run() mimic_run_iterations = mimic_run_stats['Iteration'].tolist() times.append(mimic_run_stats["Time"].iloc[-1] * 1000) fitness = np.asarray(mimic_run_stats['Fitness'].tolist()) runs.append(fitness) mimic_run_average = np.mean(np.asarray(runs), axis=0) mimic_run_average_time = np.mean(times) fig, (fitness_cp, time_cp) = plt.subplots(1, 2, figsize=(6, 2)) fitness_cp.set_title("Fitness vs Iterations\n Knapsack N = 100", title_dic) fitness_cp.set_ylabel("Average Fitness Score", title_dic) fitness_cp.tick_params(axis="x", labelsize=axis_label_size) fitness_cp.tick_params(axis="y", labelsize=axis_label_size) fitness_cp.set_xlabel("Iteration", title_dic) fitness_cp.plot(sa_run_iterations, sa_run_average, label="SA") fitness_cp.plot(ga_run_iterations, ga_run_average, label="GA") fitness_cp.plot(rhc_run_iterations, rhc_run_average_restarts, label="RHC") fitness_cp.plot(mimic_run_iterations, mimic_run_average, label="MIMIC") fitness_cp.legend(loc='best', fontsize=6) time_cp.set_xlabel("Log Time (ms)", title_dic) time_cp.set_title("Runtime \n Knapsack N = 100", title_dic) time_cp.set_ylabel("Algorithm", title_dic) time_cp.tick_params(axis="x", labelsize=axis_label_size) time_cp.tick_params(axis="y", labelsize=axis_label_size) x = ['RHC', 'SA', 'GA', 'MIMIC'] x_pos = [i for i, _ in enumerate(x)] time_cp.set_yticks(x_pos) time_cp.set_yticklabels(x) time_cp.set_xscale('log') time_cp.barh(x_pos, [ rhc_run_average_time, sa_run_average_time, ga_run_average_time, mimic_run_average_time ]) time_cp.grid() fitness_cp.grid() plt.tight_layout() path = KNAPSACK_PATH filename = "fitness_vs_iteration.png" filename = os.path.join(path, filename) plt.savefig(filename) fig, (fitness_cp, time_cp) = plt.subplots(1, 2, figsize=(6, 2)) fitness_cp.set_title("Fitness vs Problem Size\n Knapsack Problem", title_dic) fitness_cp.set_ylabel("Average Fitness Score", title_dic) fitness_cp.tick_params(axis="x", labelsize=axis_label_size) fitness_cp.tick_params(axis="y", labelsize=axis_label_size) fitness_cp.set_xlabel("Problem Size(N)", title_dic) # Running on different problem set sizes sizes = [50, 75, 100, 125, 150] rhc_average_size = [] sa_average_size = [] ga_average_size = [] mimic_average_size = [] rhc_average_time = [] sa_average_time = [] ga_average_time = [] mimic_average_time = [] out_dir = KNAPSACK_PATH for size in sizes: problem = CustomKnapSackGenerator.generate( 1, number_of_items_types=size, max_item_count=50, max_weight_per_item=100, max_value_per_item=50, max_weight_pct=0.6, multiply_by_max_item_count=True, crossover=one_point_crossover.OnePointCrossOver, mutator=change_one_mutator.ChangeOneMutator) restarts = [] iterations = [] times = [] for i in range(15): # run RHC runner with param restarts. rhc = RHCRunner(problem=problem, experiment_name="RHC QUEENS" + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1000, 100), max_attempts=500, restart_list=[0]) rhc_run_stats, _ = rhc.run() rhc_run_iterations = rhc_run_stats['Iteration'].tolist() restarts.append(rhc_run_stats['Fitness'].tolist()) times.append(rhc_run_stats["Time"].iloc[-1] * 1000) rhc_run_average_restarts = np.mean(np.asarray(restarts), axis=0) rhc_run_average_time = np.mean(times) rhc_average_size.append(rhc_run_average_restarts[-1]) rhc_average_time.append(rhc_run_average_time) runs = [] times = [] for i in range(5): sa = SARunner( problem=problem, experiment_name= "Knapsack Simulated Annealing after Param Tuning Decay Schedule " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1500, 100), max_attempts=1000, decay_list=[mlrose_hiive.ExpDecay], temperature_list=[3500]) sa_run_stats, _ = sa.run() sa_run_iterations = sa_run_stats['Iteration'].tolist() times.append(sa_run_stats["Time"].iloc[-1] * 1000) runs.append(sa_run_stats['Fitness'].tolist()) sa_run_average = np.mean(np.asarray(runs), axis=0) sa_run_average_time = np.mean(times) sa_average_size.append(sa_run_average[-1]) sa_average_time.append(sa_run_average_time) runs = [] times = [] for i in range(5): ga = GARunner(problem=problem, experiment_name="CPP GA After Parameter Tuning" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 1500, 25), max_attempts=150, population_sizes=[75], mutation_rates=[0.9]) ga_run_stats, _ = ga.run() ga_run_iterations = ga_run_stats['Iteration'].tolist() times.append(ga_run_stats["Time"].iloc[-1] * 1000) runs.append(ga_run_stats['Fitness'].tolist()) ga_run_average = np.mean(np.asarray(runs), axis=0) ga_run_average_time = np.mean(times) ga_average_size.append(ga_run_average[-1]) ga_average_time.append(ga_run_average_time) runs = [] times = [] for i in range(5): mimic = MIMICRunner(problem=problem, experiment_name="Knapsack Mimic" + "Run " + str(i), output_directory=out_dir, seed=np.random.randint(100), iteration_list=np.arange(1, 150, 5), population_sizes=[100], keep_percent_list=[0.5], max_attempts=5, generate_curves=True, use_fast_mimic=True) mimic_run_stats, _ = mimic.run() mimic_run_iterations = mimic_run_stats['Iteration'].tolist() times.append(mimic_run_stats["Time"].iloc[-1] * 1000) runs.append(mimic_run_stats['Fitness'].tolist()) mimic_run_average = np.mean(np.asarray(runs), axis=0) mimic_run_average_time = np.mean(times) mimic_average_size.append(mimic_run_average[-1]) mimic_average_time.append(mimic_run_average_time) fitness_cp.plot(sizes, sa_average_size, label="SA") fitness_cp.plot(sizes, ga_average_size, label="GA") fitness_cp.plot(sizes, rhc_average_size, label="RHC") fitness_cp.plot(sizes, mimic_average_size, label="MIMIC") fitness_cp.legend(loc='best', fontsize=6) time_cp.set_ylabel("Log Time (ms)", title_dic) time_cp.set_title("Runtime vs Problem Size \n Knapsack Problem", title_dic) time_cp.set_xlabel("Problem Size(N)", title_dic) time_cp.tick_params(axis="x", labelsize=axis_label_size) time_cp.tick_params(axis="y", labelsize=axis_label_size) time_cp.set_yscale('log') time_cp.plot(sizes, sa_average_time, label="SA") time_cp.plot(sizes, ga_average_time, label="GA") time_cp.plot(sizes, rhc_average_time, label="RHC") time_cp.plot(sizes, mimic_average_time, label="MIMIC") time_cp.legend(loc='best', fontsize=6) time_cp.grid() fitness_cp.grid() plt.tight_layout() path = KNAPSACK_PATH filename = "fitness_vs_iteration_various_problem_sizes.png" filename = os.path.join(path, filename) plt.savefig(filename)
experiment_name=experiment_name, output_directory=constants.OUTPUT_DIRECTORY, seed=0, iteration_list=constants.iteration_list, max_attempts=2000, temperature_list=[500], decay_list=[ExpDecay]) sa_run_stats, sa_run_curves = sa.run() if algorithm_name == "GA" and False: ga = GARunner(problem=problem, experiment_name=experiment_name, output_directory=constants.OUTPUT_DIRECTORY, seed=0, iteration_list=2**np.arange(16), max_attempts=2000, population_sizes=[10], mutation_rates=[0.05]) ga_run_stats, ga_run_curves = ga.run() if algorithm_name == "MIMIC" and False: mimic = MIMICRunner( problem=problem, experiment_name=experiment_name, output_directory=constants.OUTPUT_DIRECTORY, seed=0, iteration_list=2**np.arange(16), population_sizes=[10], max_attempts=500, keep_percent_list=[0.05], use_fast_mimic=True) mimic_run_stats, mimic_run_curves = mimic.run()