{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ee4c71e9-e325-494b-a787-ac1d5d27ebe3",
   "metadata": {},
   "source": [
    "# The Photo booth "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a7f2ce5-b90a-46e7-8968-e98f96909d90",
   "metadata": {},
   "source": [
    "You need a new passport photo, and you go to the photo booth to make one. \n",
    "\n",
    "In the photo booth, you can make several shots that are, independently, \"bad\" with probability $p[0]$, \"average\" with probability $p[1]$ and \"superb\" with probability $p[2]$.\n",
    "\n",
    "Bad shots are valued $V[0]$, averages shots $V[1]$ and superb shots $V[2]$.\n",
    "\n",
    "After each of the $T-1$ first shots, you have the possibility to *stop* (and the last shot is printed) or to *continue* (and a new independent shot is taken, erasing the previous ones). If you did not stop before, the $T$-th shot is the final one and it is printed whatsoever.\n",
    "\n",
    "You want to determine the strategy that maximizes the value of the printed picture. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4c9770dd-e5e9-4b89-9b01-5bbca2419949",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5d2aabea-79d1-4f2b-9106-4562b79b5afa",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Agent:    \n",
    "    def __init__(self, T, p, V):\n",
    "        self.T = T\n",
    "        self.p = p\n",
    "        self.V = V\n",
    "    \n",
    "    def decideStop(self, r):\n",
    "        if r != 0:\n",
    "            return True # keep the picture\n",
    "        else:\n",
    "            return False # try another shot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4e2c4666-6823-4599-9edb-a23effa8f6ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "average value:  2.438\n"
     ]
    }
   ],
   "source": [
    "T = 4\n",
    "p = [0.5, 0.3, 0.2]\n",
    "V = [0, 1, 5]\n",
    "\n",
    "N = 1000\n",
    "res = []\n",
    "agent = Agent(T, p, V)\n",
    "for _ in range(N):\n",
    "    r = 0\n",
    "    for t in range(T):\n",
    "        r = np.random.choice(V, p=p)\n",
    "        if agent.decideStop(r):\n",
    "            break\n",
    "    res.append(r)\n",
    "\n",
    "print(\"average value: \", np.mean(res))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82a6552b-53d2-4906-a757-2d649e727c82",
   "metadata": {},
   "source": [
    "## Evaluation: \n",
    "\n",
    "Upload into https://plmbox.math.cnrs.fr/u/d/537c87e08ca745acae29/\n",
    "  * a file <Yourname.py> containing a class \"Agent\" respecting the template above ; it will be test in various conditions (different values of T, p, and V)\n",
    "  * a file <Yourname.pdf> with your mathematical analysis of the problem\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25d5643c-1c87-4c95-ba06-51f077b55b1e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python3UMPA",
   "language": "python",
   "name": "python3umpa"
  },
  "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.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
