-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,347 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Base agents\n", | ||
"\n", | ||
"> To be written." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| default_exp agents.base" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import logging\n", | ||
"logging_level = logging.DEBUG" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| hide\n", | ||
"from nbdev.showdoc import *" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"\n", | ||
"\n", | ||
"from abc import ABC, abstractmethod\n", | ||
"from typing import Union, Optional, List\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"from ddopnew.envs.base import BaseEnvironment\n", | ||
"from ddopnew.utils import MDPInfo\n", | ||
"\n", | ||
"# # TEMPORARY\n", | ||
"# from sklearn.utils.validation import check_array\n", | ||
"# import numbers" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"\n", | ||
"class BaseAgent():\n", | ||
"\n", | ||
" train_mode = \"direct_fit\" # or \"epochs_fit\" or \"env_interaction\"\n", | ||
" \n", | ||
" def __init__(self,\n", | ||
" environment_info: MDPInfo,\n", | ||
" preprocessors: Optional[List[object]] = None, # default is empty list\n", | ||
" postprocessors: Optional[List[object]] = None # default is empty list\n", | ||
" ):\n", | ||
" \n", | ||
" \"\"\"\n", | ||
" Initialize a BaseAgent.\n", | ||
"\n", | ||
" Args:\n", | ||
" environment_info (MDPInfo): Information about the environment (MDP).\n", | ||
" preprocessors (Optional[List[object]]): A list of preprocessors to apply to input data.\n", | ||
" postprocessors (Optional[List[object]]): A list of postprocessors to apply to output data.\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" self.preprocessors = preprocessors or []\n", | ||
" self.postprocessors = postprocessors or []\n", | ||
"\n", | ||
" self.environment_info = environment_info\n", | ||
" self.mode = \"train\"\n", | ||
" self.print = False # Can be used for debugging\n", | ||
" self.receive_batch_dim = False\n", | ||
"\n", | ||
" @abstractmethod\n", | ||
" def draw_action_(self, observation):\n", | ||
" pass\n", | ||
"\n", | ||
" def draw_action(self, observation):\n", | ||
"\n", | ||
" observation = self.add_batch_dim(observation)\n", | ||
"\n", | ||
" for preprocessor in self.preprocessors:\n", | ||
" observation = preprocessor(observation)\n", | ||
"\n", | ||
" action = self.draw_action_(observation)\n", | ||
" \n", | ||
" for postprocessor in self.postprocessors:\n", | ||
" action = postprocessor(action)\n", | ||
" return action\n", | ||
"\n", | ||
" def add_preprocessor(self, preprocessor):\n", | ||
" self.preprocessors.append(preprocessor)\n", | ||
" \n", | ||
" def add_postprocessor(self, postprocessor):\n", | ||
" self.postprocessors.append(postprocessor)\n", | ||
"\n", | ||
" def train(self):\n", | ||
" self.mode = \"train\"\n", | ||
" \n", | ||
" def eval(self):\n", | ||
" self.mode = \"eval\"\n", | ||
" \n", | ||
" def add_batch_dim(self, input: np.ndarray) -> np.ndarray:\n", | ||
" \n", | ||
" \"\"\"\n", | ||
" Add a batch dimension to the input array if it doesn't already have one.\n", | ||
"\n", | ||
" Args:\n", | ||
" input (np.ndarray): The input array that may need a batch dimension added.\n", | ||
"\n", | ||
" Returns:\n", | ||
" np.ndarray: The input array with an added batch dimension, if required.\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" if self.receive_batch_dim:\n", | ||
" # If the batch dimension is expected, return the input as is\n", | ||
" return input\n", | ||
" else:\n", | ||
" # Add a batch dimension by expanding the dimensions of the input\n", | ||
" return np.expand_dims(input, axis=0)\n", | ||
" \n", | ||
" def flatten_X(self, X):\n", | ||
"\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" Function to flatten the time-dimension of the feature matrix\n", | ||
" for agents that require a 2D input.\n", | ||
" \n", | ||
" \n", | ||
" Args:\n", | ||
" X (np.ndarray): The input data to be flattened.\n", | ||
"\n", | ||
" Returns:\n", | ||
" _type_: _description_\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" if X.ndim == 3:\n", | ||
" return X.reshape(X.shape[0], -1)\n", | ||
" else:\n", | ||
" return X\n", | ||
" \n", | ||
" def save(self):\n", | ||
" raise NotImplementedError(\"This agent does not have a save method implemented.\")\n", | ||
"\n", | ||
" def load(self):\n", | ||
" raise NotImplementedError(\"This agent does not have a load method implemented.\")\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# #| export\n", | ||
"\n", | ||
"# def check_cu_co(cu, co, n_outputs):\n", | ||
"# \"\"\"Validate under- and overage costs.\n", | ||
"\n", | ||
"# Parameters\n", | ||
"# ----------\n", | ||
"# cu : {ndarray, Number or None}, shape (n_outputs,)\n", | ||
"# The underage costs per unit. Passing cu=None will output an array of ones.\n", | ||
"# co : {ndarray, Number or None}, shape (n_outputs,)\n", | ||
"# The overage costs per unit. Passing co=None will output an array of ones.\n", | ||
"# n_outputs : int\n", | ||
"# The number of outputs.\n", | ||
"# Returns\n", | ||
"# -------\n", | ||
"# cu : ndarray, shape (n_outputs,)\n", | ||
"# Validated underage costs. It is guaranteed to be \"C\" contiguous.\n", | ||
"# co : ndarray, shape (n_outputs,)\n", | ||
"# Validated overage costs. It is guaranteed to be \"C\" contiguous.\n", | ||
"# \"\"\"\n", | ||
"# costs = [[cu, \"cu\"], [co, \"co\"]]\n", | ||
"# costs_validated = []\n", | ||
"# for c in costs:\n", | ||
"# if c[0] is None:\n", | ||
"# cost = np.ones(n_outputs, dtype=np.float64)\n", | ||
"# elif isinstance(c[0], numbers.Number):\n", | ||
"# cost = np.full(n_outputs, c[0], dtype=np.float64)\n", | ||
"# else:\n", | ||
"# cost = check_array(\n", | ||
"# c[0], accept_sparse=False, ensure_2d=False, dtype=np.float64,\n", | ||
"# order=\"C\"\n", | ||
"# )\n", | ||
"# if cost.ndim != 1:\n", | ||
"# raise ValueError(c[1], \"must be 1D array or scalar\")\n", | ||
"\n", | ||
"# if cost.shape != (n_outputs,):\n", | ||
"# raise ValueError(\"{}.shape == {}, expected {}!\"\n", | ||
"# .format(c[1], cost.shape, (n_outputs,)))\n", | ||
"# costs_validated.append(cost)\n", | ||
"# cu = costs_validated[0]\n", | ||
"# co = costs_validated[1]\n", | ||
"# return cu, co\n", | ||
"\n", | ||
"# class NewsvendorSAAagentOLD(BaseAgent):\n", | ||
"\n", | ||
"# def __init__(self, environment_info, cu, co):\n", | ||
"# self.cu = cu\n", | ||
"# self.co = co\n", | ||
"\n", | ||
"# self.sl = cu / (cu + co)\n", | ||
"\n", | ||
"# self.quantiles = np.array([0.0])\n", | ||
"\n", | ||
"# super().__init__(environment_info)\n", | ||
"\n", | ||
"# self.fitted = False\n", | ||
"\n", | ||
"# def _calc_weights(self):\n", | ||
"# weights = np.full(self.n_samples_, 1 / self.n_samples_)\n", | ||
"# return weights\n", | ||
"\n", | ||
"# def fit(self, Y, X, mask=None):\n", | ||
"\n", | ||
"# demand = Y\n", | ||
"# features = X\n", | ||
"\n", | ||
"# self.mask=mask\n", | ||
"# y=demand\n", | ||
"\n", | ||
"# if mask is not None:\n", | ||
"# if demand.shape != mask.shape:\n", | ||
"# if demand.shape[1]==1 & len(mask.shape)==1:\n", | ||
"# mask = mask.reshape((-1,1))\n", | ||
"# self.mask = mask\n", | ||
"# if demand.shape != mask.shape:\n", | ||
"# raise ValueError(\"Shapes of mask and demand do not match\")\n", | ||
"# # check if 1 either in mask.shape or demand.shape, if yes squeeze\n", | ||
"# demand=demand*mask\n", | ||
" \n", | ||
"# y = check_array(y, ensure_2d=False, accept_sparse='csr')\n", | ||
"\n", | ||
"# if y.ndim == 1:\n", | ||
"# y = np.reshape(y, (-1, 1))\n", | ||
"\n", | ||
"# # Training data\n", | ||
"# self.y_ = y\n", | ||
"# self.n_samples_ = y.shape[0]\n", | ||
"\n", | ||
"# # Determine output settings\n", | ||
"# self.n_outputs_ = y.shape[1]\n", | ||
"\n", | ||
"# # Check and format under- and overage costs\n", | ||
"# self.cu_, self.co_ = check_cu_co(self.cu, self.co, self.n_outputs_)\n", | ||
"\n", | ||
"# self.q_star = np.array(self._findQ(self._calc_weights()))\n", | ||
"\n", | ||
"# self.fitted=True\n", | ||
"\n", | ||
"# return self\n", | ||
"\n", | ||
"# def _findQ(self, weights):\n", | ||
"# \"\"\"Calculate the optimal order quantity q\"\"\"\n", | ||
"\n", | ||
"# y = self.y_\n", | ||
"# q = []\n", | ||
"\n", | ||
"# for k in range(self.n_outputs_):\n", | ||
"# alpha = self.cu_[k] / (self.cu_[k] + self.co_[k])\n", | ||
"# y_product = y[:,k]\n", | ||
"# if self.mask is not None:\n", | ||
"# mask_product = self.mask[:,k]\n", | ||
"# y_product = y_product[mask_product.astype(bool)]\n", | ||
"# # print(y_product.shape)\n", | ||
"# q.append(np.quantile(y_product, alpha, interpolation=\"higher\"))\n", | ||
"# print(\"found optimal q:\", q)\n", | ||
"# return q\n", | ||
"\n", | ||
"# def draw_action(self, *args, **kwargs):\n", | ||
"\n", | ||
"# if self.fitted:\n", | ||
"# pred = self.q_star\n", | ||
" \n", | ||
"# else:\n", | ||
"# pred = np.random.rand(1) \n", | ||
"# return pred" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| hide\n", | ||
"import nbdev; nbdev.nbdev_export()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "python3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |