{
 "cells": [
  {
   "cell_type": "code",
   "id": "8311f569-6f93-4a3a-ab5f-917c4badc2e0",
   "metadata": {
    "tags": [],
    "ExecuteTime": {
     "end_time": "2024-09-17T20:41:31.731363Z",
     "start_time": "2024-09-17T20:41:14.008637Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import itertools"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Matplotlib is building the font cache; this may take a moment.\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "78b05b39-bad1-4d1c-897f-153e9697f477",
   "metadata": {
    "tags": [],
    "ExecuteTime": {
     "end_time": "2024-09-17T20:41:49.263556Z",
     "start_time": "2024-09-17T20:41:49.253326Z"
    }
   },
   "source": [
    "class ModeleSchelling:\n",
    "    def __init__(self, M, p, L):\n",
    "        self.M = M\n",
    "        self.p = p\n",
    "        self.L = L\n",
    "        self.Ntot = 0\n",
    "        self.grille = self.initialisation_grille()\n",
    "\n",
    "    def initialisation_grille(self):\n",
    "        grille = np.zeros((self.M, self.M), dtype=int)\n",
    "        occupes = np.random.choice(self.M * self.M, size=int((1 - self.p) * self.M * self.M), replace=False)\n",
    "        self.Ntot = len(occupes)\n",
    "        \n",
    "        for occupe in occupes:\n",
    "            i, j = divmod(occupe, self.M)\n",
    "            grille[i, j] = np.random.choice([-1, 1])\n",
    "\n",
    "        return grille\n",
    "\n",
    "    def afficher_grille(self, title):\n",
    "        color = plt.imshow(self.grille, cmap='coolwarm', interpolation='nearest')\n",
    "        plt.colorbar(color)\n",
    "        plt.title(title)\n",
    "        plt.show()\n",
    "\n",
    "    def satisfaisante(self, x, y, agent):\n",
    "        groupe = self.grille[agent[0], agent[1]]\n",
    "        count_similaires = 0\n",
    "        count_differents = 0\n",
    "        \n",
    "        voisins = []\n",
    "        for (i,j) in list(itertools.product([x-1, x, x+1], [y-1, y, y+1])):\n",
    "            if 0 <= i < self.M and 0 <= j < self.M and (i, j) != (x, y):\n",
    "                voisins.append((i, j))\n",
    "                \n",
    "        for voisin in voisins:\n",
    "            i, j = voisin\n",
    "            case = self.grille[i, j]\n",
    "            if case == 0: pass\n",
    "            else:\n",
    "                if case == groupe:\n",
    "                    count_similaires += 1\n",
    "                else: \n",
    "                    count_differents += 1\n",
    "                \n",
    "        if count_similaires+count_differents == 0:\n",
    "            return False\n",
    "        else:\n",
    "            return float(count_similaires/(count_similaires+count_differents)) >= self.L\n",
    "        \n",
    "    def clusters(self):\n",
    "        visited = np.zeros_like(self.grille, dtype=bool)\n",
    "        clusters = []\n",
    "\n",
    "        def dfs(i, j, groupe, cluster): # Depth-First Search \n",
    "            stack = [(i, j)]\n",
    "\n",
    "            while stack:\n",
    "                i, j = stack.pop()\n",
    "\n",
    "                if 0 <= i < self.M and 0 <= j < self.M and not visited[i, j] and self.grille[i, j] == groupe:\n",
    "                    visited[i, j] = True\n",
    "                    cluster.add((i, j))\n",
    "\n",
    "                    for (di, dj) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:\n",
    "                        stack.append((i + di, j + dj))\n",
    "\n",
    "        added_clusters = set()\n",
    "\n",
    "        for groupe in [-1, 1]:\n",
    "            indices = np.argwhere((self.grille == groupe) & (~visited))\n",
    "            for i, j in indices:\n",
    "                current_cluster = set()\n",
    "                dfs(i, j, groupe, current_cluster)\n",
    "                \n",
    "                if current_cluster and frozenset(current_cluster) not in added_clusters:\n",
    "                    clusters.append((groupe, len(current_cluster)))\n",
    "                    added_clusters.add(frozenset(current_cluster))\n",
    "\n",
    "        return clusters\n",
    "    \n",
    "    def coef_segregation(self):\n",
    "        S = 0\n",
    "        clusters = self.clusters()\n",
    "        for i in range(len(clusters)):\n",
    "            S += int(clusters[i][1])**2\n",
    "        return S * 2 / (self.Ntot**2)\n",
    "\n",
    "    def simuler(self, T=400, move_satisfaits=True):\n",
    "        for t in range(1, int((1-self.p)*self.M**2 * T)):\n",
    "            agents = [(i, j) for i, row in enumerate(self.grille) for j, val in enumerate(row) if val != 0]\n",
    "            agent = agents[np.random.randint(0, len(agents) - 1)]\n",
    "            \n",
    "            while not move_satisfaits and self.satisfaisante(agent[0], agent[1], agent):\n",
    "                agents.remove(agent)\n",
    "                if len(agents) == 0:\n",
    "                    break\n",
    "                agent = agents[np.random.randint(0, len(agents) - 1)]\n",
    "            \n",
    "            cases_non_occupees = [(i, j) for i, row in enumerate(self.grille) for j, val in enumerate(row) if val == 0]\n",
    "            nouvelle_case = cases_non_occupees[np.random.randint(0, len(cases_non_occupees) - 1)]\n",
    "            \n",
    "            while not self.satisfaisante(nouvelle_case[0], nouvelle_case[1], agent):\n",
    "                cases_non_occupees.remove(nouvelle_case)\n",
    "                if len(cases_non_occupees) == 0:\n",
    "                    break\n",
    "                nouvelle_case = cases_non_occupees[np.random.randint(0, len(cases_non_occupees) - 1)]\n",
    "                \n",
    "            self.grille[nouvelle_case[0]][nouvelle_case[1]] = self.grille[agent[0]][agent[1]]\n",
    "            self.grille[agent[0]][agent[1]] = 0\n",
    "            cases_non_occupees.append(agent)\n",
    "        self.afficher_grille(f'Configuration Finale de T={T} pour (M, p, L) = ({self.M},{self.p},{self.L})')\n"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "id": "2d3b256a-9934-45bf-b865-815618d9350e",
   "metadata": {
    "tags": [],
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2024-09-17T20:41:52.787394Z"
    }
   },
   "source": [
    "M = 100\n",
    "p = 5/100\n",
    "L = 1/2\n",
    "\n",
    "modele = ModeleSchelling(M, p, L)\n",
    "modele.afficher_grille(\"Configuration Initiale\")\n",
    "\n",
    "for T in [1, 10, 100, 400]:\n",
    "    modele.simuler(T, True)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ],
      "image/png": "[encoded data, 36872 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ],
      "image/png": "[encoded data, 40680 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "eb5223d5-8167-4122-894a-d49ade9bc550",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29768 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 32160 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34236 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33460 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33024 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 32828 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 32780 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30064 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 28760 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30720 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35500 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34672 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33980 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33548 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35176 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33780 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31004 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 29704 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31092 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33332 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35788 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35692 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34244 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34440 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35532 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31568 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30704 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31768 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34080 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36736 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36364 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35004 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34920 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36124 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35592 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31908 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "M = 50\n",
    "T = 100\n",
    "p_list = [2/100, 6/100, 12/100, 18/100]\n",
    "L_list = [1/5, 2/7, 1/3, 5/7, 3/4]\n",
    "\n",
    "for p in p_list:\n",
    "    for L in L_list:\n",
    "        modele = ModeleSchelling(M, p, L)\n",
    "        modele.afficher_grille(f\"Configuration Initiale pour p={p} et L={L}\")\n",
    "        modele.simuler(T, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7791cbab-6526-472f-8823-9088cf0a748a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'random' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[6], line 8\u001B[0m\n\u001B[1;32m      4\u001B[0m L \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m2\u001B[39m\u001B[38;5;241m/\u001B[39m\u001B[38;5;241m7\u001B[39m\n\u001B[1;32m      6\u001B[0m S \u001B[38;5;241m=\u001B[39m []\n\u001B[0;32m----> 8\u001B[0m modele \u001B[38;5;241m=\u001B[39m \u001B[43mModeleSchelling\u001B[49m\u001B[43m(\u001B[49m\u001B[43mM\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mp\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mL\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m      9\u001B[0m modele\u001B[38;5;241m.\u001B[39mafficher_grille(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mConfiguration Initiale\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m     11\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m t \u001B[38;5;129;01min\u001B[39;00m T:\n",
      "Cell \u001B[0;32mIn[5], line 7\u001B[0m, in \u001B[0;36mModeleSchelling.__init__\u001B[0;34m(self, M, p, L)\u001B[0m\n\u001B[1;32m      5\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mL \u001B[38;5;241m=\u001B[39m L\n\u001B[1;32m      6\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mNtot \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m0\u001B[39m\n\u001B[0;32m----> 7\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgrille \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43minitialisation_grille\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n",
      "Cell \u001B[0;32mIn[5], line 16\u001B[0m, in \u001B[0;36mModeleSchelling.initialisation_grille\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m     14\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m occupe \u001B[38;5;129;01min\u001B[39;00m occupes:\n\u001B[1;32m     15\u001B[0m     i, j \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mdivmod\u001B[39m(occupe, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mM)\n\u001B[0;32m---> 16\u001B[0m     grille[i, j] \u001B[38;5;241m=\u001B[39m \u001B[43mrandom\u001B[49m\u001B[38;5;241m.\u001B[39mchoice([\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m])\n\u001B[1;32m     18\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m grille\n",
      "\u001B[0;31mNameError\u001B[0m: name 'random' is not defined"
     ]
    }
   ],
   "source": [
    "M = 50\n",
    "T = [1, 10, 100, 400]\n",
    "p = 5/100\n",
    "L = 2/7\n",
    "\n",
    "S = []\n",
    "\n",
    "modele = ModeleSchelling(M, p, L)\n",
    "modele.afficher_grille(\"Configuration Initiale\")\n",
    "\n",
    "for t in T:\n",
    "    modele.simuler(t, True)\n",
    "    S.append(modele.coef_segregation())\n",
    "    \n",
    "plt.scatter(T, S, label=\"Coefficient de Segrégation\")\n",
    "plt.plot(T, S)\n",
    "plt.legend()\n",
    "plt.xlabel(\"T\")\n",
    "plt.ylabel(\"S\")\n",
    "plt.title(\"Evolution du coefficient de ségrégation S en fonction du temps T\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "f8883cf2-a094-4e74-827d-159c950a13c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26528 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mKeyboardInterrupt\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[95], line 12\u001B[0m\n\u001B[1;32m     10\u001B[0m     modele \u001B[38;5;241m=\u001B[39m ModeleSchelling(M, p, L)\n\u001B[1;32m     11\u001B[0m     modele\u001B[38;5;241m.\u001B[39mafficher_grille(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mConfiguration Initiale\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m---> 12\u001B[0m     \u001B[43mmodele\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msimuler\u001B[49m\u001B[43m(\u001B[49m\u001B[43mT\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m)\u001B[49m\n\u001B[1;32m     13\u001B[0m     S\u001B[38;5;241m.\u001B[39mappend(modele\u001B[38;5;241m.\u001B[39mcoef_segregation())\n\u001B[1;32m     15\u001B[0m plt\u001B[38;5;241m.\u001B[39mscatter(L, S, label\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mCoefficient de Segrégation\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
      "Cell \u001B[0;32mIn[94], line 95\u001B[0m, in \u001B[0;36mModeleSchelling.simuler\u001B[0;34m(self, T, move_satisfaits)\u001B[0m\n\u001B[1;32m     93\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21msimuler\u001B[39m(\u001B[38;5;28mself\u001B[39m, T\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m400\u001B[39m, move_satisfaits\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m):\n\u001B[1;32m     94\u001B[0m     \u001B[38;5;28;01mfor\u001B[39;00m t \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(\u001B[38;5;241m1\u001B[39m, \u001B[38;5;28mint\u001B[39m((\u001B[38;5;241m1\u001B[39m\u001B[38;5;241m-\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mp)\u001B[38;5;241m*\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mM\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m2\u001B[39m \u001B[38;5;241m*\u001B[39m T)):\n\u001B[0;32m---> 95\u001B[0m         agents \u001B[38;5;241m=\u001B[39m [(i, j) \u001B[38;5;28;01mfor\u001B[39;00m i, row \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgrille) \u001B[38;5;28;01mfor\u001B[39;00m j, val \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(row) \u001B[38;5;28;01mif\u001B[39;00m val \u001B[38;5;241m!=\u001B[39m \u001B[38;5;241m0\u001B[39m]\n\u001B[1;32m     96\u001B[0m         agent \u001B[38;5;241m=\u001B[39m agents[random\u001B[38;5;241m.\u001B[39mrandint(\u001B[38;5;241m0\u001B[39m, \u001B[38;5;28mlen\u001B[39m(agents) \u001B[38;5;241m-\u001B[39m \u001B[38;5;241m1\u001B[39m)]\n\u001B[1;32m     98\u001B[0m         \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m move_satisfaits \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39msatisfaisante(agent[\u001B[38;5;241m0\u001B[39m], agent[\u001B[38;5;241m1\u001B[39m], agent):\n",
      "Cell \u001B[0;32mIn[94], line 95\u001B[0m, in \u001B[0;36m<listcomp>\u001B[0;34m(.0)\u001B[0m\n\u001B[1;32m     93\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21msimuler\u001B[39m(\u001B[38;5;28mself\u001B[39m, T\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m400\u001B[39m, move_satisfaits\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m):\n\u001B[1;32m     94\u001B[0m     \u001B[38;5;28;01mfor\u001B[39;00m t \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(\u001B[38;5;241m1\u001B[39m, \u001B[38;5;28mint\u001B[39m((\u001B[38;5;241m1\u001B[39m\u001B[38;5;241m-\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mp)\u001B[38;5;241m*\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mM\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m2\u001B[39m \u001B[38;5;241m*\u001B[39m T)):\n\u001B[0;32m---> 95\u001B[0m         agents \u001B[38;5;241m=\u001B[39m [(i, j) \u001B[38;5;28;01mfor\u001B[39;00m i, row \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgrille) \u001B[38;5;28;01mfor\u001B[39;00m j, val \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28;43menumerate\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mrow\u001B[49m\u001B[43m)\u001B[49m \u001B[38;5;28;01mif\u001B[39;00m val \u001B[38;5;241m!=\u001B[39m \u001B[38;5;241m0\u001B[39m]\n\u001B[1;32m     96\u001B[0m         agent \u001B[38;5;241m=\u001B[39m agents[random\u001B[38;5;241m.\u001B[39mrandint(\u001B[38;5;241m0\u001B[39m, \u001B[38;5;28mlen\u001B[39m(agents) \u001B[38;5;241m-\u001B[39m \u001B[38;5;241m1\u001B[39m)]\n\u001B[1;32m     98\u001B[0m         \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m move_satisfaits \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39msatisfaisante(agent[\u001B[38;5;241m0\u001B[39m], agent[\u001B[38;5;241m1\u001B[39m], agent):\n",
      "\u001B[0;31mKeyboardInterrupt\u001B[0m: "
     ]
    }
   ],
   "source": [
    "M = 50\n",
    "T = 400\n",
    "p_list = [2/100, 6/100, 12/100, 18/100, 25/100]\n",
    "L_list = range(0, 1, 4)\n",
    "\n",
    "S = []\n",
    "\n",
    "for p in p_list:\n",
    "    for L in L_list:\n",
    "        modele = ModeleSchelling(M, p, L)\n",
    "        modele.afficher_grille(\"Configuration Initiale\")\n",
    "        modele.simuler(T, True)\n",
    "        S.append(modele.coef_segregation())\n",
    "        \n",
    "    plt.scatter(L, S, label=\"Coefficient de Segrégation\")\n",
    "    plt.plot(T, S)\n",
    "    plt.legend()\n",
    "    plt.xlabel(\"L\")\n",
    "    plt.ylabel(\"S\")\n",
    "    plt.title(f\"Evolution du coefficient de ségrégation S en fonction de la tolérance L = {L} pour p = {p}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a562854-dc27-44bc-b474-04fc9ca397a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "M = 30\n",
    "T = 400\n",
    "p = 5/100\n",
    "L = 2/7\n",
    "\n",
    "modele = ModeleSchelling(M, p, L)\n",
    "modele.afficher_grille(\"Configuration Initiale\")\n",
    "modele.simuler(T, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0cc31b5b-6fa0-4f7a-9445-ae92acc7e51f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27496 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36552 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "M = 20\n",
    "T = 100\n",
    "p = 5/100\n",
    "L = 2/7\n",
    "\n",
    "modele = ModeleSchelling(M, p, L)\n",
    "modele.afficher_grille(\"Configuration Initiale\")\n",
    "modele.simuler(T, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5ada3b4-dbe1-4df4-a5f1-c34144dffba3",
   "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.10.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
