def experiment_sa_4(self): init_state = None schedule_var = 1 best_state = None max_attempts = np.array([5, 10, 15, 40, 60, 80, 100, 150, 200]) result = np.zeros((len(self.rand_seeds), len(max_attempts))) for i in range(len(self.rand_seeds)): rand_state = self.rand_seeds[i] for j in range(len(max_attempts)): prob_length = 20 max_iter = np.inf fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() max_attempt = max_attempts[j].item() alg = SA(problem, init_state, rand_state, schedule_var, max_attempt, max_iter) best_state, best_fitness = alg.optimize() result[i][j] = best_fitness avg_result = np.mean(result, axis=0) print('avg result ' + str(avg_result)) print('best_state') print(best_state) title = self.problem_type + ' with SA - Max Attempts Variation -Geom' plot_curve(max_attempts, avg_result, title, 'Max Attempts', 'Best Score')
def experiment_sa_7(self): init_state = None schedule_var = 2 best_state = None max_iters = np.arange(100, 5000, 100) result = np.zeros((len(self.rand_seeds), len(max_iters))) for i in range(len(self.rand_seeds)): rand_state = self.rand_seeds[i] for j in range(len(max_iters)): prob_length = 20 fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() max_attempt = 200 max_iter = max_iters[j].item() alg = SA(problem, init_state, rand_state, schedule_var, max_attempt, max_iter) best_state, best_fitness = alg.optimize() result[i][j] = best_fitness avg_result = np.mean(result, axis=0) print('best_state') print(best_state) print('avg result ' + str(avg_result)) title = self.problem_type + ' with SA - Max Iter Variation - Arith' plot_curve(max_iters, avg_result, title, 'Max Iterations', 'Best Score')
def experiment_sa_11(self): init_state = None prob_lengths = np.arange(7, 30) schedule_var = 0 best_state = None result = np.zeros((len(self.rand_seeds), len(prob_lengths))) best_state = None for i in range(len(self.rand_seeds)): rand_state = self.rand_seeds[i] for j in range(len(prob_lengths)): prob_length = prob_lengths[j] fl = CustomProblem(prob_length.item(), self.problem_type) problem = fl.create_problem() alg = SA(problem, init_state, rand_state, schedule_var, 10, 1000) best_state, best_fitness = alg.optimize() result[i][j] = best_fitness print(str(result)) print('best_state') print(best_state) avg_result = np.mean(result, axis=0) print('avg result for varying input size' + str(avg_result)) title = self.problem_type + ' with SA - Input Size Variation' plot_curve(prob_lengths, avg_result, title, 'Input Size', 'Best Score')
def experiment_optimal_sa(self): prob_length = 20 init_state = None schedule_var = 0 rand_state = 42 max_attempt = 110 fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() start = time.time() alg = SA(problem, init_state, rand_state, schedule_var, max_attempt, 1000) best_score, best_fitness = alg.optimize() end = time.time() diff = abs(end - start) print('time taken for SA - Knapsack: ' + str(diff))
def experiment_sa(self): fl = FlipFlop(7) problem = fl.create_problem() alg = SA(problem, 42, 0, 10, 1000) alg.optimize()