mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-03-16 05:11:40 +01:00
94 lines
2.4 KiB
Plaintext
94 lines
2.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "7e37429a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch\n",
|
|
"import numpy as np\n",
|
|
"import pickle\n",
|
|
"from pathlib import Path\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "85ff0eb4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Agent:\n",
|
|
" \"\"\"Base class for reinforcement learning agents.\"\"\"\n",
|
|
"\n",
|
|
" def __init__(self, action_space: int) -> None:\n",
|
|
" \"\"\"Initialize the agent.\"\"\"\n",
|
|
" self.action_space = action_space\n",
|
|
"\n",
|
|
" def get_action(self, observation: np.ndarray, epsilon: float = 0.0):\n",
|
|
" \"\"\"Select an action based on the current observation.\"\"\"\n",
|
|
" raise NotImplementedError\n",
|
|
"\n",
|
|
" def update(self, state: np.ndarray, action: int, reward: float, next_state: np.ndarray, done: bool):\n",
|
|
" \"\"\"Update the agent's knowledge based on the experience tuple.\"\"\"\n",
|
|
" pass\n",
|
|
"\n",
|
|
" def save(self, filename: str) -> None:\n",
|
|
" \"\"\"Save the agent's state to a file.\"\"\"\n",
|
|
" with Path(filename).open(\"wb\") as f:\n",
|
|
" pickle.dump(self.__dict__, f)\n",
|
|
"\n",
|
|
" def load(self, filename: str) -> None:\n",
|
|
" \"\"\"Load the agent's state from a file.\"\"\"\n",
|
|
" with Path(filename).open(\"rb\") as f:\n",
|
|
" self.__dict__.update(pickle.load(f)) # noqa: S301\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2459be52",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Random Agent"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "89633751",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class RandomAgent(Agent):\n",
|
|
" \"\"\"A simple agent that selects actions randomly.\"\"\"\n",
|
|
" def get_action(self, observation, epsilon=0.0):\n",
|
|
" return self.action_space.sample()\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "studies (3.13.9)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|