{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from random import random\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.animation import FuncAnimation\n",
    "\n",
    "def decide (prob):\n",
    "    return 1 if random() <= prob else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pS activity: 0.016666666666666666\n",
      "pA activity: 0.06666666666666667\n"
     ]
    }
   ],
   "source": [
    "# Question 5.3.5\n",
    "\n",
    "L = 100\n",
    "dt = .01\n",
    "T = 60\n",
    "\n",
    "pA_rate = .1\n",
    "pS_rate = .05\n",
    "\n",
    "pA = np.zeros(L)\n",
    "pS = np.zeros(L)\n",
    "\n",
    "pS_counts = 0\n",
    "pA_counts = 0\n",
    "\n",
    "\n",
    "def check_collisions ():\n",
    "    collisions = np.where((pA == 1) & (pS == 1))\n",
    "    pA[collisions] = 0\n",
    "    pS[collisions] = 0\n",
    "\n",
    "\n",
    "    \n",
    "for n in range(0,int(T / dt)):\n",
    "    if pA[0] == 0:\n",
    "        pA[0] = decide(dt * pA_rate)\n",
    "\n",
    "    if pS[L-1] == 0:\n",
    "        pS[L-1] = decide(dt * pS_rate)\n",
    "\n",
    "\n",
    "    if pA[L-1] == 1 and decide(.4):\n",
    "        pA[L-1] = 0\n",
    "        pA_counts += 1\n",
    "    \n",
    "    if pS[0] == 1 and decide(.4):\n",
    "        pS[0] = 0\n",
    "        pS_counts += 1\n",
    "\n",
    "    for i in range(1,L):\n",
    "        if pA[L-1-i] == 1 and decide(.4) and pA[L-i] == 0:\n",
    "            pA[L-1-i] = 0\n",
    "            pA[L-i] = 1\n",
    "        check_collisions()\n",
    "        \n",
    "        if pS[i] == 1 and decide(.4) and pS[i-1] == 0:\n",
    "            pS[i-1] = 1\n",
    "            pS[i] = 0\n",
    "        check_collisions()\n",
    "\n",
    "\n",
    "print(f\"pS activity: {pS_counts / T}\")\n",
    "print(f\"pA activity: {pA_counts / T}\")\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
