Aquí tienes un ejemplo básico de cómo crear un modelo de Deep Learning en Python utilizando PyTorch y entrenar a un agente de aprendizaje por refuerzo para jugar al tic-tac-toe (tres en raya) contra sí mismo:
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
# Define the tic-tac-toe environment
class TicTacToe:
def __init__(self):
self.board = [0] * 9
self.current_player = 1
self.winning_combos = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
[0, 4, 8], [2, 4, 6] # diagonals
]
def reset(self):
self.board = [0] * 9
self.current_player = 1
def is_game_over(self):
for combo in self.winning_combos:
if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != 0:
return True
return 0 not in self.board
def make_move(self, position):
if self.board[position] == 0:
self.board[position] = self.current_player
self.current_player = -1 if self.current_player == 1 else 1
def get_state(self):
return np.array(self.board)
def get_valid_moves(self):
return [i for i in range(9) if self.board[i] == 0]
# Define the deep Q-network
class DQN(nn.Module):
def __init__(self):
super(DQN, self).__init__()
self.fc1 = nn.Linear(9, 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, 64)
self.fc4 = nn.Linear(64, 64)
self.fc5 = nn.Linear(64, 64)
self.fc6 = nn.Linear(64, 64)
self.fc7 = nn.Linear(64, 64)
self.fc8 = nn.Linear(64, 64)
self.fc9 = nn.Linear(64, 64)
self.fc10 = nn.Linear(64, 64)
self.fc11 = nn.Linear(64, 64)
self.fc12 = nn.Linear(64, 64)
self.fc13 = nn.Linear(64, 9)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = torch.relu(self.fc3(x))
x = torch.relu(self.fc4(x))
x = torch.relu(self.fc5(x))
x = torch.relu(self.fc6(x))
x = torch.relu(self.fc7(x))
x = torch.relu(self.fc8(x))
x = torch.relu(self.fc9(x))
x = torch.relu(self.fc10(x))
x = torch.relu(self.fc11(x))
x = torch.relu(self.fc12(x))
x = self.fc13(x)
return x
# Define the agent
class Agent:
def __init__(self):
self.model = DQN()
self.optimizer = optim.Adam(self.model.parameters(), lr=0.001)
self.loss_fn = nn.MSELoss()
self.gamma = 0.99
self.epsilon = 1.0
self.epsilon_decay = 0.999
self.epsilon_min = 0.1
def train(self, num_episodes):
env = TicTacToe()
for episode in range(num_episodes):
env.reset()
state = env.get_state()
done = False
while not done:
# Choose an action
if np.random.rand() < self.epsilon:
action = np.random.choice(env.get_valid_moves())
else:
q_values = self.model(torch.tensor(state, dtype=torch.float32))
valid_moves = np.array(env.get_valid_moves())
q_values = q_values.detach().numpy()[valid_moves]
action = valid_moves[np.argmax(q_values)]
# Take the chosen action
env.make_move(action)
next_state = env.get_state()
reward = 0
# Update the reward and check if the game is over
if env.is_game_over():
done = True
winner = env.current_player
if winner != 0:
reward = 1 if winner == 1 else -1
# Update the Q-values
target = reward + self.gamma * np.max(self.model(torch.tensor(next_state, dtype=torch.float32)).detach().numpy())
q_values = self.model(torch.tensor(state, dtype=torch.float32))
q_values[action] = target
# Backpropagation
self.optimizer.zero_grad()
loss = self.loss_fn(q_values, self.model(torch.tensor(state, dtype=torch.float32)))
loss.backward()
self.optimizer.step()
state = next_state
# Decay the exploration rate
self.epsilon = max(self.epsilon * self.epsilon_decay, self.epsilon_min)
# Update other hyperparameters
self.gamma *= 0.999
self.epsilon_decay *= 0.999
self.epsilon_min *= 0.999
# Train the agent
agent = Agent()
agent.train(num_episodes=7000)
Una vez finalizada la fase de entrenamiento del modelo, puedes jugar con él utilizando el siguiente código:
def play_game(agent):
env = TicTacToe()
while not env.is_game_over():
print("Current Board:")
print("-------------")
for i in range(0, 9, 3):
print("|", env.board[i], "|", env.board[i+1], "|", env.board[i+2], "|")
print("-------------")
if env.current_player == 1:
# Agent's turn
state = env.get_state()
q_values = agent.model(torch.tensor(state, dtype=torch.float32))
valid_moves = np.array(env.get_valid_moves())
q_values = q_values.detach().numpy()[valid_moves]
action = valid_moves[np.argmax(q_values)]
print("Agent chooses position:", action)
else:
# Player's turn
while True:
try:
action = int(input("Your turn. Choose a position (0-8): "))
if action not in env.get_valid_moves():
raise ValueError
break
except ValueError:
print("Invalid move. Please choose a valid position.")
env.make_move(action)
# Print final board state
print("Final Board:")
print("-------------")
for i in range(0, 9, 3):
print("|", env.board[i], "|", env.board[i+1], "|", env.board[i+2], "|")
print("-------------")
# Determine the result
if env.is_game_over():
winner = env.current_player
if winner == 1:
print("Agent wins!")
elif winner == -1:
print("You win!")
else:
print("It's a tie!")
else:
print("Game ended in a tie.")
# Usage:
play_game(agent)def play_game(agent):
env = TicTacToe()
while not env.is_game_over():
print("Current Board:")
print("-------------")
for i in range(0, 9, 3):
print("|", env.board[i], "|", env.board[i+1], "|", env.board[i+2], "|")
print("-------------")
if env.current_player == 1:
# Agent's turn
state = env.get_state()
q_values = agent.model(torch.tensor(state, dtype=torch.float32))
valid_moves = np.array(env.get_valid_moves())
q_values = q_values.detach().numpy()[valid_moves]
action = valid_moves[np.argmax(q_values)]
print("Agent chooses position:", action)
else:
# Player's turn
while True:
try:
action = int(input("Your turn. Choose a position (0-8): "))
if action not in env.get_valid_moves():
raise ValueError
break
except ValueError:
print("Invalid move. Please choose a valid position.")
env.make_move(action)
# Print final board state
print("Final Board:")
print("-------------")
for i in range(0, 9, 3):
print("|", env.board[i], "|", env.board[i+1], "|", env.board[i+2], "|")
print("-------------")
# Determine the result
if env.is_game_over():
winner = env.current_player
if winner == 1:
print("Agent wins!")
elif winner == -1:
print("You win!")
else:
print("It's a tie!")
else:
print("Game ended in a tie.")
# Usage:
play_game(agent)
Cuando ejecutes este código, el programa mostrará el tablero actual del juego y te pedirá que ingreses una posición para realizar tu movimiento. Las posiciones van del 0 al 8 y corresponden a las casillas del tablero, como se muestra a continuación:
| 0 | 1 | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
Si es tu turno, deberás ingresar un número del 0 al 8 y presionar Enter. Si el movimiento es válido, el programa actualizará el tablero con tu movimiento y mostrará el tablero actualizado.
Luego, el agente tomará su turno y elegirá la posición en la que desea jugar. El programa mostrará la posición elegida por el agente.
El juego continuará alternando entre tus turnos y los del agente hasta que el juego termine. Al finalizar, se mostrará el estado final del tablero y el resultado del juego (si el agente ganó, si tú ganaste o si el juego terminó en empate).