def print_best(self, gav, iter_num): print("Iteration number " + str(iter_num)) with open("output.txt", 'a') as file: file.write("Iteration number: " + str(iter_num) + "\n") file.write(" Best fitness: " + str(gav[0].fitness) + "\n") overall_value = 0 overall_weight = 0 for i in range(self.items): if gav[0].knapsack[i] == 1: overall_value = overall_value + self.prices[i] overall_weight = overall_weight + self.weights[i] print("Item " + str(i) + " with value " + str(self.prices[i]) + " and weight " + str(self.weights[i])) file.write(" Item " + str(i) + " with value " + str(self.prices[i]) + " and weight " + str(self.weights[i]) + "\n") print("Overall value is " + str(overall_value) + "/" + str(sum(self.prices))) print("Overall weight is " + str(overall_weight) + "/" + str(self.capacity)) print() file.write(" Overall value is " + str(overall_value) + "\n") file.write(" Overall weight is " + str(overall_weight) + "\n") file.write( " Fitness average: " + str(round(utils.average(gav, self.data.ga_popsize), 3)) + "\n") file.write( " Fitness deviation: " + str(round(utils.deviation(gav, self.data.ga_popsize), 3)) + "\n")
def print_best(self, gav, iter_num): print("Best: " + gav[0].str + " (" + str(gav[0].fitness) + ")") with open("output.txt", 'a') as file: file.write("Best: " + gav[0].str + " (" + str(gav[0].fitness) + ")\n") file.write(" Iteration number: " + str(iter_num) + "\n") file.write(" Fitness average: " + str(round(utils.average(gav, self.data.ga_popsize), 3)) + "\n") file.write(" Fitness deviation: " + str(round(utils.deviation(gav, self.data.ga_popsize), 3)) + "\n")
def deviation_local_optima(self, population): window = self.data.ga_popsize / self.data.local_optima_groups_num deviation_list = [] for iteration in range(self.data.local_optima_groups_num): start = int(iteration * window) end = int((iteration + 1) * window) deviation_list.append( round(deviation(population[start:end], int(window)), 3)) self.local_optima_signals.append(deviation_list)