hi, can this train some fighting games such as the epic Street Fight 2 ?
drizzt00s opened this issue · 2 comments
Issue summary
[Put a detailed description of the issue here.]
System information
- [Operating system]
- [Python version]
- [Gym Retro version]
bro take this
import tensorflow as tf
import numpy as np
import cv2
import neat
import pickle
import retro
import os
Prompt the user to enter the game and level information
game_name = input("Enter the game name: ")
level_name = input("Enter the level name: ")
env = retro.make(game_name, level_name)
Define a function to preprocess the game frames
def preprocess_frame(frame):
# Resize the frame
frame = cv2.resize(frame, (int(frame.shape[1]/8), int(frame.shape[0]/8)))
# Convert the frame to grayscale
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Reshape the frame
frame = np.reshape(frame, (frame.shape[0], frame.shape[1], 1))
return frame
def check_stuck(action_history, action_count_threshold):
# Check if the AI has taken the same action multiple times
action_counts = np.unique(action_history, return_counts=True)[1]
if action_counts.size == 0:
return False
elif action_counts.max() >= action_count_threshold:
return True
else:
return False
def eval_genomes(genomes, config):
for genome_id, genome in genomes:
ob = env.reset()
ac = env.action_space.sample()
inx, iny, inc = env.observation_space.shape
inx = int(inx/8)
iny = int(iny/8)
net = neat.nn.recurrent.RecurrentNetwork.create(genome, config)
current_max_fitness = 0
fitness_current = 0
frame = 0
counter = 0
xpos = 0
xpos_max = 0
action_history = []
done = False
while not done:
env.render()
frame += 1
ob = preprocess_frame(ob)
imgarray = np.ndarray.flatten(ob)
nnOutput = net.activate(imgarray.astype(np.float32))
ob, rew, done, info = env.step(nnOutput)
fitness_current += rew
# Penalize the fitness score if the AI is stuck in a particular position
if check_stuck(action_history, 30):
fitness_current -= 10
action_history.append(np.argmax(nnOutput))
if fitness_current > current_max_fitness:
current_max_fitness = fitness_current
counter = 0
else:
counter += 1
if counter == 250:
done = True
if done:
done = True
print(genome_id, fitness_current)
genome.fitness = fitness_current
# Save the progress of the AI every 10 generations
if genome_id % 100 == 0:
with open(f'neat-checkpoint-{genome_id}.pkl', 'wb') as output:
pickle.dump((p, stats, genome), output, 1)
print(frame)
Load the saved progress of the AI
if os.path.exists('neat-checkpoint'):
p = neat.Checkpointer.restore_checkpoint('neat-checkpoint')
else:
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
'config-feedforward')
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(10, filename_prefix='neat-checkpoint-'))
winner = p.run(eval_genomes)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(10, filename_prefix='neat-checkpoint-'))
winner = p.run(eval_genomes)
with open('winner.pkl', 'wb') as output:
pickle.dump(winner, output, 1)
cv2.destroyAllWindows()