def experiment_optimal_ga(self): prob_length = 20 fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() pop_size = 1200 rand_state = 42 max_attempt = 70 max_iter = 1000 mutation_prob = 0.1 start = time.time() alg = GA(problem, rand_state, max_attempt, max_iter, pop_size, mutation_prob) best_fitness = alg.optimize() end = time.time() diff = abs(end - start) print('time taken for GA- Knapsack: ' + str(diff))
def experiment_ga_1(self): prob_lengths = np.arange(7, 30) result = np.zeros((len(self.rand_seeds), len(prob_lengths))) pop_size = 200 mutation_prob = 0.1 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 = GA(problem, rand_state, 10, 1000, pop_size, mutation_prob) best_fitness = alg.optimize() result[i][j] = best_fitness print(str(result)) avg_result = np.mean(result, axis=0) print('avg result for varying input size' + str(avg_result)) title = self.problem_type + ' with GA - Input Size Variation' plot_curve(prob_lengths, avg_result, title, 'Input Size', 'Best Score')
def experiment_ga_5(self): mutation_probs = np.arange(0.1, 1, 0.1) result = np.zeros((len(self.rand_seeds), len(mutation_probs))) pop_size = 1000 max_iter = np.inf for i in range(len(self.rand_seeds)): rand_state = self.rand_seeds[i] for j in range(len(mutation_probs)): prob_length = 20 fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() max_attempt = 60 mutation_prob = mutation_probs[j].item() alg = GA(problem, rand_state, max_attempt, max_iter, pop_size, mutation_prob) best_fitness = alg.optimize() result[i][j] = best_fitness avg_result = np.mean(result, axis=0) print('avg result ' + str(avg_result)) title = self.problem_type + ' with GA - Mutation Prob Variation' plot_curve(mutation_probs, avg_result, title, 'Mutation Prob', 'Best Score')
def experiment_ga_4(self): pop_sizes = np.arange(50, 1000, 100) result = np.zeros((len(self.rand_seeds), len(pop_sizes))) mutation_prob = 0.1 max_iter = np.inf for i in range(len(self.rand_seeds)): rand_state = self.rand_seeds[i] for j in range(len(pop_sizes)): prob_length = 20 fl = CustomProblem(prob_length, self.problem_type) problem = fl.create_problem() max_attempt = 20 pop_size = pop_sizes[j].item() alg = GA(problem, rand_state, max_attempt, max_iter, pop_size, mutation_prob) best_fitness = alg.optimize() result[i][j] = best_fitness avg_result = np.mean(result, axis=0) print('avg result ' + str(avg_result)) title = self.problem_type + ' with GA - Population Size Variation' plot_curve(pop_sizes, avg_result, title, 'Population Size', 'Best Score')
def experiment_ga_3(self): max_iters = np.arange(500, 3000, 400) result = np.zeros((len(self.rand_seeds), len(max_iters))) pop_size = 200 mutation_prob = 0.1 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 = 20 max_iter = max_iters[j].item() alg = GA(problem, rand_state, max_attempt, max_iter, pop_size, mutation_prob) best_fitness = alg.optimize() result[i][j] = best_fitness avg_result = np.mean(result, axis=0) print('avg result ' + str(avg_result)) title = self.problem_type + ' with GA - Max Iterations Variation' plot_curve(max_iters, avg_result, title, 'Max Iterations', 'Best Score')
def experiment_ga(self): fl = FlipFlop(7) problem = fl.create_problem() alg = GA(problem, 42, 10, 1000, 200, 0.1) alg.optimize()