def __init__(self, hidden_neurons, evolving_iterations, game_iterations, population_size): table = [yamb.Column(i) for i in range(6)] self.best_game = yamb.Yamb(table) self.game_iterations = game_iterations self.population_size = population_size self.iterations = evolving_iterations self.hidden_neurons = hidden_neurons self.population = [NeuralNetwork(self.hidden_neurons, game_iterations) for _ in range(population_size)]
def __init__(self, populationSize): self.__population_size = populationSize self.__networks = [] self.__parameter_list = (3, 8, 3) for i in range(self.__population_size): self.__networks.append(NeuralNetwork(self.__parameter_list)) self.__best_network = self.__networks[0] self.__generation = 1
def breed(individual1, individual2): child = Individual(NeuralNetwork.get_new_model(board)) for layer in range(len(individual1.model.layers)): # Breed the weights w1 = individual1.model.layers[layer].get_weights()[0] w_mask = np.random.randint(0, int(random.random() * w1.size) + 1, size=w1.shape).astype(np.bool) w = np.copy(w1) w[w_mask] = individual2.model.layers[layer].get_weights()[0][w_mask] # Breed the biases b1 = individual1.model.layers[layer].get_weights()[1] b_mask = np.random.randint(0, int(random.random() * b1.size) + 1, size=b1.shape).astype(np.bool) b = np.copy(b1) b[b_mask] = individual2.model.layers[layer].get_weights()[1][b_mask] child.model.layers[layer].set_weights(np.asarray([w, b])) return child
def open(): nn = NeN.NNCreater( keras.models.load_model("AI/NeuralNetwork/SavedModel.h5")) return nn
def populate(individual_class, size=20): models = [] for ignored in range(size): models.append( individual_class(NeuralNetwork.get_new_model(board, random=True))) return models
generations = 20 population_size = 10 max_moves = 300 retain = 0.3 retain_chance = 0.1 mutate_chance = 0.2 mutation_severity = 0.1 if args.load is not None: print("Loading population from file") population = [] try: with open(args.load, "rb") as file: loaded = pickle.load(file) for weights in loaded: model = NeuralNetwork.get_new_model(board) model.set_weights(weights) population.append(Individual(model)) except IOError: print("Error reading file") raise else: population = populate(Individual, population_size) best = [] for i in range(generations): start = time.time() # Get the new parents parents, evaluation_stat = select(population) # Short-circuit if not enough parents
def fill_population(self): while len(self.population) < self.population_size: self.population.append(NeuralNetwork(self.hidden_neurons, self.game_iterations))