{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b27a7c4-9ce3-494d-ad53-5dbc394b3d93",
   "metadata": {},
   "source": [
    "# Bread Production"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bff17c2-39ba-41b5-bfb5-769a63a4cdd7",
   "metadata": {},
   "source": [
    "You are a baker, you choose how many breads you produce. \n",
    "Each bread you sell yields a gain of $g$, while each bread produced but not sold yields a loss of $l$.\n",
    "The demand for each day is an independent random draw of a distribution D."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b67246c-3e82-42c9-9c44-ffbc503bb8ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:01.886616Z",
     "iopub.status.busy": "2026-08-28T14:50:01.886341Z",
     "iopub.status.idle": "2026-08-28T14:50:01.927979Z",
     "shell.execute_reply": "2026-08-28T14:50:01.927632Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#import matplotlib.pyplot as plt #%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0027baab-af20-40cd-ac12-acd38f01a5aa",
   "metadata": {},
   "source": [
    "## When the law of the demand is known"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39bd4688-42b6-4dd9-87b2-8848d57ab47f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:01.930532Z",
     "iopub.status.busy": "2026-08-28T14:50:01.930418Z",
     "iopub.status.idle": "2026-08-28T14:50:01.932319Z",
     "shell.execute_reply": "2026-08-28T14:50:01.932052Z"
    }
   },
   "outputs": [],
   "source": [
    "g = 1 # gain per unit sold\n",
    "l = 0.5 # loss per unit produced but not sold\n",
    "demandDist = lambda: np.random.geometric(0.1) # random demand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "afa53960-2e1b-45a9-9c84-f0ad49f06093",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:01.934529Z",
     "iopub.status.busy": "2026-08-28T14:50:01.934455Z",
     "iopub.status.idle": "2026-08-28T14:50:01.937033Z",
     "shell.execute_reply": "2026-08-28T14:50:01.936466Z"
    }
   },
   "outputs": [],
   "source": [
    "class knownDemandAgent:\n",
    "    \n",
    "    def __init__(self, g, l, demandDist):\n",
    "        self.m = np.mean([demandDist() for k in range(1000)])\n",
    "\n",
    "    def receiveReward(self, reward):\n",
    "        pass\n",
    "    \n",
    "    def decisionRule(self):\n",
    "        return self.m\n",
    "\n",
    "myAgent = knownDemandAgent(g, l, demandDist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "44847acc-3cab-4290-9aaa-959f7923fda7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:01.938761Z",
     "iopub.status.busy": "2026-08-28T14:50:01.938544Z",
     "iopub.status.idle": "2026-08-28T14:50:01.941374Z",
     "shell.execute_reply": "2026-08-28T14:50:01.941055Z"
    }
   },
   "outputs": [],
   "source": [
    "from common import evaluatePolicy  # shared with evaluator.py / evaluatorPublic.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7b036efe-d3bf-4709-b9ad-49824b894de6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:01.943062Z",
     "iopub.status.busy": "2026-08-28T14:50:01.942881Z",
     "iopub.status.idle": "2026-08-28T14:50:02.010431Z",
     "shell.execute_reply": "2026-08-28T14:50:02.010185Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1742.255"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T = 365\n",
    "N = 100\n",
    "evaluatePolicy(T, N, myAgent, g, l, demandDist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89a37bcc",
   "metadata": {},
   "source": [
    "*Food for thought: this agent produces the **average** daily demand. Since overproducing (loss $l$) and underproducing (missed gain $g$) are not penalized the same way, is the mean really the production level that maximizes expected profit?*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3925755c-3e4e-454d-9961-6e50cd09a89e",
   "metadata": {},
   "source": [
    "## When the law of the demand is unknown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a625148a-e206-4cc5-a0d2-f5e1be1f50d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:02.011955Z",
     "iopub.status.busy": "2026-08-28T14:50:02.011883Z",
     "iopub.status.idle": "2026-08-28T14:50:02.013845Z",
     "shell.execute_reply": "2026-08-28T14:50:02.013590Z"
    }
   },
   "outputs": [],
   "source": [
    "g = 1 # gain per unit sold\n",
    "l = 0.5 # loss per unit produced but not sold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8b6685aa-f384-4415-a265-e681d49ef3ef",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:02.014959Z",
     "iopub.status.busy": "2026-08-28T14:50:02.014893Z",
     "iopub.status.idle": "2026-08-28T14:50:02.017140Z",
     "shell.execute_reply": "2026-08-28T14:50:02.016914Z"
    }
   },
   "outputs": [],
   "source": [
    "class unknownDemandAgent:\n",
    "    \n",
    "    def __init__(self, g, l):\n",
    "        self.g = g\n",
    "        self.l = l \n",
    "        self.n = 0 # number of days observed\n",
    "        self.s = 0 # sum of the demands so far\n",
    "        self.lastProd = 0\n",
    "        \n",
    "    def receiveReward(self, reward):\n",
    "        self.n += 1\n",
    "        if reward == self.g * self.lastProd:\n",
    "            self.s += self.lastProd + 1\n",
    "        else: \n",
    "            self.s += (reward + self.l * self.lastProd) / (self.g + self.l)\n",
    "        \n",
    "    def decisionRule(self):\n",
    "        if self.n > 0:\n",
    "            self.lastProd = self.s / self.n\n",
    "        else:\n",
    "            self.lastProd = 10 #default for the first day\n",
    "        return self.lastProd\n",
    "\n",
    "myAgent = unknownDemandAgent(g, l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c64b3a41-3650-48bc-8dc2-6ece3fb35027",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-28T14:50:02.018246Z",
     "iopub.status.busy": "2026-08-28T14:50:02.018175Z",
     "iopub.status.idle": "2026-08-28T14:50:02.101929Z",
     "shell.execute_reply": "2026-08-28T14:50:02.101637Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "939.29"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T = 365\n",
    "N = 100\n",
    "evaluatePolicy(T, N, myAgent, g, l, demandDist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "135b52ef",
   "metadata": {},
   "source": [
    "*Food for thought: on a day when the bakery sells out, the true demand is never observed — only a lower bound on it. Does naively averaging the reconstructed demand values account for that?*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "505b6b88-ee64-47df-b35a-fbe0dcf777a3",
   "metadata": {},
   "source": [
    "## Evaluation: \n",
    "\n",
    "upload a file <yourname.py> into https://plmbox.math.cnrs.fr/u/d/5c666340912f40d4bd2c/  containing:\n",
    "  \n",
    "  * a class \"Yourname_K\" (replace \"yourname\" with your name...) implementing the agent with known demand\n",
    "  * a class \"Yourname_U\" implementing the agent with unknown demand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b7d47fa-c46d-4d17-bd75-a408fcb737d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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
}
