def run(): agentDQN = DeepQNetworkAgent() current_episode = 0 max_episode = 401 for current_episode in range(max_episode): gameInit(agentDQN, showGui=showGui) #change boolean to display UI #Decrease exploration epsilon after every episode agentDQN.explorationEpsilonDecay() while not snakeML.gameEnded: state_1 = np.reshape(snakeML.getInputLayer(), (1, SIZE_INPUT_LAYER)) if np.random.random_sample() < agentDQN.explorationEpsilon: action = np.random.randint(0, 3) else: prediction = agentDQN.model.predict(np.reshape(state_1, (1, SIZE_INPUT_LAYER))) action = np.argmax(prediction) foodAte, gameEnded = snakeML.nextStep(action) reward = agentDQN.setReward(foodAte, gameEnded) state_2 = np.reshape(snakeML.getInputLayer(), (1, SIZE_INPUT_LAYER)) agentDQN.train_short_memory(state_1, action, reward, state_2, gameEnded) agentDQN.remember(state_1, action, reward, state_2, gameEnded) agentDQN.replay_memory() print(f"Game {current_episode}, Score={snakeML.getSnakeLength()-1}") snakeML.exit() if((current_episode % 10)==0): agentDQN.model.save(f"model/episode_{current_episode:03d}.hdf5")
def showSnake(modelName): model = tf.keras.models.load_model(modelName) snakeML.newGameML() snakeML.startGameML() game_ended = False num_steps = 0 while not game_ended: input_layer = snakeML.getInputLayer() output = model.predict(np.matrix(input_layer)) # predict output output = np.argmax(output) len, game_ended = snakeML.nextStep(output) num_steps += 1 snakeML.exit()
weights = reshapeWeights(population[j], input_layer_units, hidden_layers_units, output_layer_units) model.set_weights(weights) snakeML.newGameML() snakeML.startGameML(showGui=False) game_ended = False num_steps = 0 while not game_ended and (num_steps < max_steps): input_layer = snakeML.getInputLayer() output = model.predict(np.matrix(input_layer)) # predict output output = np.argmax(output) len[j], game_ended = snakeML.nextStep(output) num_steps += 1 fitness[j] = calcFitness(len[j], num_steps) snakeML.exit() updateCanvas(txtbox_training_id, i, j, fitness[j], len[j]) best_person = np.argmax(fitness) print( f"Generation {i}, Top fitness = {fitness[best_person]}, Length = {len[j]}" ) updateCanvas(txtbox_trained_id, i, "Null", fitness[best_person], len[j]) weights = reshapeWeights(population[best_person], input_layer_units, hidden_layers_units, output_layer_units) model.set_weights(weights) model.save(f"model/model_generation_{i}_best_fitness.h5") print(f"Generation {i} best model saved.") print("Creating next generation. This might take a while.") population = GeneticAlgo.getNextGeneration(population, fitness) df = pd.DataFrame(population)