My NEAT algorithm crashes while training model

41 Views Asked by At

I am trying to build the NEAT agent for gym super mario bros project but it keep crashing after like a minute after I start training model. For NEAT, I have 4 inputs of mario_x, mario_y, object_x, and object_y with 7n outputs of SIMPLE_MOVEMENT = [['NOOP'], ['right'], ['right', 'A'], ['right', 'B'], ['right', 'A', 'B'], ['A'], ['left']].

fitness = 0.0
step = -1
done = False

distances = []

while not done:
    step += 1
    object_checking = None

    if obs is not None:
        (
            mario_position,
            enemy_position,
            block_position,
            item_position,
        ) = object_detection.find_locations(obs, info, step, env, action)
        next_object = object_detection.nearest_object(
            mario_position, enemy_position, block_position, item_position
        )
        object_checking = next_object

        inputs = object_detection.get_values(mario_position, next_object)
        outputs = net.activate(inputs)
        action = outputs.index(max(outputs))
    else:
        action = env.action_space.sample()

    if object_checking == (0, 0) and (action == 0 or action == 5):
        action = 4

    obs, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated
    distance = info["x_pos"]
    distances.append(distance)

    if done:
        env.reset()
        fitness = distance

    fitness = distance
    if fitness < 200:
        fitness = 0
    elif fitness < 300:
        fitness = 100

fitnesses.append(fitness)

return np.max(fitnesses)

I try to modify the get_values from object_detection file to set the value initial location value for mario in case his location is None, and if there is no object then its location is set to (0, 0) as well.

def get_values(mario_position, object_position):
    if mario_position is None:
        mario_x = 42
        mario_y = 193
    else:
        mario_x = mario_position[0]
        mario_y = mario_position[1]

    object_x = object_position[0]
    object_y = object_position[1]

    return [mario_x, mario_y, abs(mario_x - object_x), object_y]
0

There are 0 best solutions below