def four_peaks(length, random_seeds): # Define Four Peaks objective function and problem four_fitness = FourPeaks(t_pct=0.1) problem = DiscreteOpt(length=length, fitness_fn=four_fitness, maximize=True, max_val=2) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for RHC, SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=100, sa_max_iters=1000, ga_max_iters=50, mimic_max_iters=50, sa_init_temp=100, sa_decay_rates=np.arange(0.002, 0.04, 0.002), sa_min_temp=0.001, ga_pop_size=1000, mimic_pop_size=1000, ga_keep_pct=0.1, mimic_keep_pct=0.2, pop_sizes=np.arange(100, 1001, 100), keep_pcts=np.arange(0.1, 0.51, 0.1), plot_name='Four Peaks', plot_ylabel='Fitness') # Plot performances for RHC, SA, GA and MIMIC print('\nPlot performances for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=1000, sa_max_iters=5000, ga_max_iters=100, mimic_max_iters=100, sa_init_temp=100, sa_exp_decay_rate=0.02, sa_min_temp=0.001, ga_pop_size=1000, ga_keep_pct=0.1, mimic_pop_size=1000, mimic_keep_pct=0.2, plot_name='Four Peaks', plot_ylabel='Fitness')
def Knapsack_f(length, weights, values, random_seeds): Knapsack_objective = Knapsack(weights, values, max_weight_pct=0.35) problem = DiscreteOpt(length=length, fitness_fn=Knapsack_objective, maximize=True, max_val=2) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for RHC, SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=1000, sa_max_iters=1000, ga_max_iters=250, mimic_max_iters=50, sa_init_temp=100, sa_decay_rates=np.arange(0.05, 2.01, 0.05), sa_min_temp=0.001, ga_pop_size=300, mimic_pop_size=1500, ga_keep_pct=0.2, mimic_keep_pct=0.4, pop_sizes=np.arange(100, 2001, 200), keep_pcts=np.arange(0.1, 0.81, 0.1), plot_name='Knapsack', plot_ylabel='Fitness') # Plot performances for RHC, SA, GA and MIMIC print('\nPlot performances for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=1000, sa_max_iters=1000, ga_max_iters=500, mimic_max_iters=100, sa_init_temp=100, sa_exp_decay_rate=0.020, sa_min_temp=0.001, ga_pop_size=100, ga_keep_pct=0.2, mimic_pop_size=300, mimic_keep_pct=0.4, plot_name='Knapsack', plot_ylabel='Fitness')
def travel_salesman(length, distances, random_seeds): # Define Travel Salesman objective function and problem fitness_dists = TravellingSales(distances=distances) # Define optimization problem object problem = TSPOpt(length=length, fitness_fn=fitness_dists, maximize=False) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=500, sa_max_iters=500, ga_max_iters=50, mimic_max_iters=50, sa_init_temp=100, sa_decay_rates=np.arange(0.005, 0.05, 0.005), sa_min_temp=0.001, ga_pop_size=100, mimic_pop_size=100, ga_keep_pct=0.2, mimic_keep_pct=0.2, pop_sizes=np.arange(100, 401, 100), keep_pcts=np.arange(0.1, 0.51, 0.1), plot_name='TSP', plot_ylabel='Fitness') #Plot performances for RHC, SA, GA and MIMIC print('\nPlot Performances for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=500, sa_max_iters=500, ga_max_iters=500, mimic_max_iters=500, sa_init_temp=100, sa_exp_decay_rate=0.02, sa_min_temp=0.001, ga_pop_size=100, ga_keep_pct=0.15, mimic_pop_size=300, mimic_keep_pct=0.5, plot_name='TSP', plot_ylabel='Fitness')
def travel_salesman(length, distances, random_seeds): """Define and experiment the Travel Salesman optimization problem. The Travel Salesman is a famous optimization problem presenting whose purpose is to find the shortest tout of N cities and visit each exactly once. Args: length (int): problem length. distances(list of tuples): list of inter-distances between each pair of cities. random_seeds (list or ndarray): random seeds for get performances over multiple random runs. Returns: None. """ # Define Travel Salesman objective function and problem tsp_objective = TravellingSales(distances=distances) problem = TSPOpt(length=length, fitness_fn=tsp_objective, maximize=False) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=500, sa_max_iters=500, ga_max_iters=50, mimic_max_iters=10, sa_init_temp=100, sa_decay_rates=np.arange(0.005, 0.05, 0.005), sa_min_temp=0.001, ga_pop_size=100, mimic_pop_size=700, ga_keep_pct=0.2, mimic_keep_pct=0.2, pop_sizes=np.arange(100, 1001, 100), keep_pcts=np.arange(0.1, 0.81, 0.1), plot_name='TSP', plot_ylabel='Cost') # Plot performances for RHC, SA, GA and MIMIC print('\nPlot Optimizations for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=500, sa_max_iters=500, ga_max_iters=50, mimic_max_iters=10, sa_init_temp=100, sa_exp_decay_rate=0.03, sa_min_temp=0.001, ga_pop_size=100, ga_keep_pct=0.2, mimic_pop_size=700, mimic_keep_pct=0.2, plot_name='TSP', plot_ylabel='Cost')
def four_peaks(length, random_seeds): """Define and experiment Four Peaks optimization problem. Four Peaks is an optimization problem presenting 2 global optima and 2 local optima. Args: length (int): problem length. random_seeds (list or ndarray): random seeds for get performances over multiple random runs. Returns: None. """ # Define Four Peaks objective function and problem four_fitness = FourPeaks(t_pct=0.1) problem = DiscreteOpt(length=length, fitness_fn=four_fitness, maximize=True, max_val=2) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for RHC, SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=10000, sa_max_iters=10000, ga_max_iters=250, mimic_max_iters=250, sa_init_temp=100, sa_decay_rates=np.arange(0.002, 0.1, 0.002), sa_min_temp=0.001, ga_pop_size=1000, mimic_pop_size=1000, ga_keep_pct=0.1, mimic_keep_pct=0.2, pop_sizes=np.arange(100, 1001, 100), keep_pcts=np.arange(0.1, 0.81, 0.1), plot_name='Four Peaks', plot_ylabel='Fitness') # Plot performances for RHC, SA, GA and MIMIC print('\nPlot Optimizations for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=7000, sa_max_iters=7000, ga_max_iters=250, mimic_max_iters=250, sa_init_temp=100, sa_exp_decay_rate=0.02, sa_min_temp=0.001, ga_pop_size=1000, ga_keep_pct=0.1, mimic_pop_size=1000, mimic_keep_pct=0.2, plot_name='Four Peaks', plot_ylabel='Fitness')
def flip_plop(length, random_seeds): """Define and experiment Flip Flop optimization problem. Flip Flop is an optimization problem whose purpose is to optimize the number of alternations in as vector of bits. Args: length (int): problem length. random_seeds (list or ndarray): random seeds for get performances over multiple random runs. Returns: None. """ # Define Flip Flop objective function and problem flip_flop_objective = FlipFlop() problem = DiscreteOpt(length=length, fitness_fn=flip_flop_objective, maximize=True, max_val=2) problem.set_mimic_fast_mode(True) # set fast MIMIC # Plot optimizations for RHC, SA, GA and MIMIC print('\nPlot Optimizations for SA, GA and MIMIC') ro.plot_optimizations(problem=problem, random_seeds=random_seeds, rhc_max_iters=1000, sa_max_iters=1000, ga_max_iters=250, mimic_max_iters=50, sa_init_temp=100, sa_decay_rates=np.arange(0.05, 2.01, 0.05), sa_min_temp=0.001, ga_pop_size=300, mimic_pop_size=1500, ga_keep_pct=0.2, mimic_keep_pct=0.4, pop_sizes=np.arange(100, 2001, 200), keep_pcts=np.arange(0.1, 0.81, 0.1), plot_name='Flip Flop', plot_ylabel='Fitness') # Plot performances for RHC, SA, GA and MIMIC print('\nPlot Optimizations for RHC, SA, GA and MIMIC') ro.plot_performances(problem=problem, random_seeds=random_seeds, rhc_max_iters=1000, sa_max_iters=1000, ga_max_iters=500, mimic_max_iters=100, sa_init_temp=100, sa_exp_decay_rate=0.3, sa_min_temp=0.001, ga_pop_size=300, ga_keep_pct=0.2, mimic_pop_size=1500, mimic_keep_pct=0.4, plot_name='Flip Flop', plot_ylabel='Fitness')