예제 #1
0
파일: main.py 프로젝트: KokJianYu/Snake_ML
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")
예제 #2
0
파일: main.py 프로젝트: KokJianYu/Snake_ML
def gameInit(agentDQN, showGui=False):
    snakeML.newGameML()
    snakeML.startGameML(showGui=showGui)
    state_1 = np.reshape(snakeML.getInputLayer(), (1, SIZE_INPUT_LAYER))
    action = 1
    foodAte, gameEnded = snakeML.nextStep(1)
    state_2 = np.reshape(snakeML.getInputLayer(), (1, SIZE_INPUT_LAYER))
    reward = agentDQN.setReward(foodAte, gameEnded)
    agentDQN.remember(state_1, action, reward, state_2, gameEnded)
    agentDQN.replay_memory()
예제 #3
0
파일: main.py 프로젝트: samueljz/Snake_ML
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()
예제 #4
0
파일: main.py 프로젝트: samueljz/Snake_ML
    fitness = np.empty(n_population)
    len = np.empty(n_population)
    for j in range(0, n_population):
        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.")
예제 #5
0
episode_plot = []
for i in range(len(files)):
    weightsToUse = f"{PATH}{files[i]}"
    #model.load_model(weightsToUse)
    model = keras.models.load_model(weightsToUse)
    totalScore = 0
    for j in range(GAMES_PER_EPISODE):
        score = 0
        counter = 0
        snakeML.newGameML()
        snakeML.startGameML(showGui=SHOW_GUI)
        while not snakeML.gameEnded and counter < 200:
            state = np.reshape(snakeML.getInputLayer(), (1, SIZE_INPUT_LAYER))
            output = model.predict(state)
            action = np.argmax(output)
            snakeML.nextStep(action)
            if (score == snakeML.snake.length - 1):
                counter += 1
            else:
                score = snakeML.snake.length - 1
                counter = 0
        totalScore += score
        snakeML.exit()
    score_plot.append(totalScore / GAMES_PER_EPISODE)
    episode_plot.append(i * 10)
    print(f"File={files[i]} Score={totalScore / GAMES_PER_EPISODE}")


def plot_seaborn(array_counter, array_score):
    episodes = np.array(array_counter)
    scores = np.array(array_score)