import gymnasium as gym import math import random import matplotlib import matplotlib.pyplot as plt from collections import namedtuple, deque from itertools import count import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F env = gym.make("CartPole-v1") # set up matplotlib is_ipython = 'inline' in matplotlib.get_backend() if is_ipython: from IPython import display plt.ion() # if GPU is to be used [device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device "torch.device") = [torch.device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device "torch.device")( "cuda" if [torch.cuda.is_available](https://docs.pytorch.org/docs/stable/generated/torch.cuda.is_available.html#torch.cuda.is_available "torch.cuda.is_available")() else "mps" if [torch.backends.mps.is_available](https://docs.pytorch.org/docs/stable/backends.html#torch.backends.mps.is_available "torch.backends.mps.is_available")() else "cpu" ) # To ensure reproducibility during training, you can fix the random seeds # by uncommenting the lines below. This makes the results consistent across # runs, which is helpful for debugging or comparing different approaches. # # That said, allowing randomness can be beneficial in practice, as it lets # the model explore different training trajectories. # seed = 42 # random.seed(seed) # torch.manual_seed(seed) # env.reset(seed=seed) # env.action_space.seed(seed) # env.observation_space.seed(seed) # if torch.cuda.is_available(): # torch.cuda.manual_seed(seed)