{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "79d922d0-3a5e-478a-bc77-95b6b0da0800",
   "metadata": {},
   "source": [
    "Math 420/620 Quiz 8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a614840-d4ca-42b4-bdef-6069aa165f26",
   "metadata": {},
   "source": [
    "1. Reconsider the war problem of Example 6.1. In this problem we explore\n",
    "the effects of weather on combat. Bad weather and poor visibility decrease\n",
    "the effectiveness of direct fire weapons for both sides. The effectiveness\n",
    "of indirect fire weapons is relatively unaffected by the weather.\n",
    "Represent the effects of bad weather in the model as follows. Let $w$ denote\n",
    "the decrease in weapon effectiveness caused by bad weather conditions.\n",
    "Thus,\n",
    "$$\\eqalign{\n",
    "R_{n+1}&=R_n-w\\lambda (0.05)B_n - \\lambda (0.005)R_n B_n\\cr\n",
    "B_{n+1}&=B_n -w0.05 R_n - 0.005R_n B_n.}\n",
    "$$\n",
    "Here $0 \\le w \\le 1$ with $w = 1$ indicating the best weather\n",
    "and $w = 0$ indicating the worst weather."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28732aaa-0c2a-404c-a1bd-ceba392a12b1",
   "metadata": {},
   "source": [
    "(i) Use a computer to simulate the discrete-time dynamical system given\n",
    "above in the case\n",
    "$\\lambda = 3$. Assume that adverse weather conditions cause a 75\\%\n",
    "decrease in weapon effectiveness for both sides ($w = 0.25$).\n",
    "Who wins the battle,\n",
    "and how long does it take? How many divisions of troops remain\n",
    "on the winning side?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a4d2a33-ef19-4c3c-8b46-bd36ef9c9041",
   "metadata": {},
   "source": [
    "In Example 6.1 it was assumed that $R_0=5$ and $B_0=2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b10eb50e-ab28-4110-942d-40036e09f860",
   "metadata": {},
   "outputs": [],
   "source": [
    "R0=5; B0=2;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0dcd160a-9876-4313-9191-1f8d4f946d2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "battle (generic function with 1 method)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function battle(lambda,w)\n",
    "    dR(R,B)=-w*lambda*0.05*B-lambda*0.005*R*B\n",
    "    dB(R,B)=-w*0.05*R-0.005*R*B\n",
    "    Rn=R0; Bn=B0\n",
    "    nmax=1000\n",
    "    for n=1:nmax\n",
    "        Rn=Rn+dR(Rn,Bn)\n",
    "        Bn=Bn+dB(Rn,Bn)\n",
    "        if Rn<=0 && Bn<=0\n",
    "            return n,\"tie\",max(Rn,Bn)\n",
    "        elseif Rn<=0\n",
    "            return n,\"blue\",Bn\n",
    "        elseif Bn<=0\n",
    "            return n,\"red\",Rn\n",
    "        end\n",
    "    end\n",
    "    return nmax,\"tie\",max(Rn,Bn)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb9ab283-f685-4001-bd04-4233c213a3a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(41, \"red\", 2.1198506097885152)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battle(3,0.25)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a4478a8-2e3f-4ce6-8b8e-b982c7d515ec",
   "metadata": {},
   "source": [
    "The red side wins the battle.  It took $n=41$ hours.\n",
    "\n",
    "There were $2.1198506097885152$ red troops remaining."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49fad78b-461c-4bf6-8445-1c655d75bc59",
   "metadata": {},
   "source": [
    "(ii) Repeat your analysis for $w = 0.1, 0.2, 0.5, 0.75, 0.9$\n",
    "and tabulate your results. Answer the same questions as in part (i)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4138ba8f-414b-4298-a9b0-5d69bfce2c37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×4 Matrix{Any}:\n",
       " 0.1   110  \"red\"  1.22379\n",
       " 0.2    52  \"red\"  1.92336\n",
       " 0.25   41  \"red\"  2.11985\n",
       " 0.5    21  \"red\"  2.59439\n",
       " 0.75   14  \"red\"  2.76393\n",
       " 0.9    12  \"red\"  2.81057"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws=[0.1,0.2,0.25,0.5,0.75,0.9]\n",
    "t1=[]\n",
    "for w in ws\n",
    "    push!(t1,[(w,battle(3,w)...)...;;])\n",
    "end\n",
    "T1=reduce(vcat,t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2d0ba08e-51e4-4786-95ee-891a167a80fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "┌──────┬───────┬────────┬───────────┐\n",
      "│\u001b[1m    w \u001b[0m│\u001b[1m Hours \u001b[0m│\u001b[1m Winner \u001b[0m│\u001b[1m Remaining \u001b[0m│\n",
      "├──────┼───────┼────────┼───────────┤\n",
      "│  0.1 │   110 │    red │   1.22379 │\n",
      "│  0.2 │    52 │    red │   1.92336 │\n",
      "│ 0.25 │    41 │    red │   2.11985 │\n",
      "│  0.5 │    21 │    red │   2.59439 │\n",
      "│ 0.75 │    14 │    red │   2.76393 │\n",
      "│  0.9 │    12 │    red │   2.81057 │\n",
      "└──────┴───────┴────────┴───────────┘\n"
     ]
    }
   ],
   "source": [
    "using PrettyTables\n",
    "pretty_table(T1,column_labels=[\"w\",\"Hours\",\"Winner\",\"Remaining\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6ce663a-78d8-4d5a-a8bb-310fb20a23ed",
   "metadata": {},
   "source": [
    "(iii) Which side benefits from fighting in adverse weather conditions?\n",
    "If you were the blue commander, would you expect red to attack on a\n",
    "sunny day or a rainy day?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6111acac-95d3-4028-ae77-188dc847b6da",
   "metadata": {},
   "source": [
    "Recall that small values of $w$ represent adverse weather conditions.\n",
    "As $w$ decreases red wins but has fewer troops remaining.  Even though\n",
    "blue looses in all cases, they benefit from adverse weather conditions.\n",
    "Therefore, if I were the the blue commander, I would expect red to\n",
    "attack on a sunny day so that red incurs fewer losses."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc4f7876-c7a2-409f-9a44-71348697841c",
   "metadata": {},
   "source": [
    "(iv) Repeat the simulations in parts (i) and (ii)\n",
    "for $\\lambda = 1.5, 2.0, 4.0, 5.0$ and tabulate your\n",
    "results as before. Reconsider your conclusions in part (iii).\n",
    "Are they still valid?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7402136-2213-4d74-a812-5f977887340b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5×6 Matrix{Tuple{Int64, String, Float64}}:\n",
       " (59, \"red\", 3.28006)    (35, \"red\", 3.60113)    …  (10, \"red\", 4.01786)\n",
       " (68, \"red\", 2.65811)    (39, \"red\", 3.08899)       (10, \"red\", 3.65031)\n",
       " (110, \"red\", 1.22379)   (52, \"red\", 1.92336)       (12, \"red\", 2.81057)\n",
       " (98, \"blue\", 0.599858)  (95, \"blue\", 0.288409)     (16, \"red\", 1.69224)\n",
       " (58, \"blue\", 0.97158)   (43, \"blue\", 0.864022)     (18, \"blue\", 0.560668)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lambdas=[1.5,2.0,3.0,4.0,5.0]\n",
    "T2=[battle(lambda,w) for lambda in lambdas,w in ws]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1d1a603d-de0d-41e9-88a9-569209aacee0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "┌───────┬──────────┬──────────┬──────────┬──────────┬─────────┬──────────┐\n",
      "│\u001b[1m λ ∖ w \u001b[0m│\u001b[1m      0.1 \u001b[0m│\u001b[1m      0.2 \u001b[0m│\u001b[1m     0.25 \u001b[0m│\u001b[1m      0.5 \u001b[0m│\u001b[1m    0.75 \u001b[0m│\u001b[1m      0.9 \u001b[0m│\n",
      "├───────┼──────────┼──────────┼──────────┼──────────┼─────────┼──────────┤\n",
      "│\u001b[1m   1.5 \u001b[0m│       59 │       35 │       29 │       16 │      11 │       10 │\n",
      "│\u001b[1m       \u001b[0m│      red │      red │      red │      red │     red │      red │\n",
      "│\u001b[1m       \u001b[0m│  3.28006 │  3.60113 │  3.69186 │  3.91297 │ 3.99453 │  4.01786 │\n",
      "├───────┼──────────┼──────────┼──────────┼──────────┼─────────┼──────────┤\n",
      "│\u001b[1m   2.0 \u001b[0m│       68 │       39 │       32 │       17 │      12 │       10 │\n",
      "│\u001b[1m       \u001b[0m│      red │      red │      red │      red │     red │      red │\n",
      "│\u001b[1m       \u001b[0m│  2.65811 │  3.08899 │  3.21108 │  3.50977 │ 3.61744 │  3.65031 │\n",
      "├───────┼──────────┼──────────┼──────────┼──────────┼─────────┼──────────┤\n",
      "│\u001b[1m   3.0 \u001b[0m│      110 │       52 │       41 │       21 │      14 │       12 │\n",
      "│\u001b[1m       \u001b[0m│      red │      red │      red │      red │     red │      red │\n",
      "│\u001b[1m       \u001b[0m│  1.22379 │  1.92336 │  2.11985 │  2.59439 │ 2.76393 │  2.81057 │\n",
      "├───────┼──────────┼──────────┼──────────┼──────────┼─────────┼──────────┤\n",
      "│\u001b[1m   4.0 \u001b[0m│       98 │       95 │      103 │       30 │      19 │       16 │\n",
      "│\u001b[1m       \u001b[0m│     blue │     blue │      red │      red │     red │      red │\n",
      "│\u001b[1m       \u001b[0m│ 0.599858 │ 0.288409 │ 0.312059 │  1.35226 │ 1.62403 │  1.69224 │\n",
      "├───────┼──────────┼──────────┼──────────┼──────────┼─────────┼──────────┤\n",
      "│\u001b[1m   5.0 \u001b[0m│       58 │       43 │       39 │       27 │      21 │       18 │\n",
      "│\u001b[1m       \u001b[0m│     blue │     blue │     blue │     blue │    blue │     blue │\n",
      "│\u001b[1m       \u001b[0m│  0.97158 │ 0.864022 │ 0.818065 │ 0.654641 │ 0.57854 │ 0.560668 │\n",
      "└───────┴──────────┴──────────┴──────────┴──────────┴─────────┴──────────┘\n"
     ]
    }
   ],
   "source": [
    "using Printf\n",
    "printcell(v,_,_)=@sprintf(\"%d\\n%s\\n%g\",v[1],v[2],v[3])\n",
    "pretty_table(T2,formatters=[printcell],\n",
    "    line_breaks=true,\n",
    "    table_format=TextTableFormat(horizontal_lines_at_data_rows=:all),\n",
    "    stubhead_label=\"λ ∖ w\",\n",
    "    column_labels=ws,row_labels=lambdas)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b265377-58eb-4793-9f7c-d708370402cf",
   "metadata": {},
   "source": [
    "For $\\lambda=1.5$, $2$ and $3$ the advantage to red\n",
    "increases as $w$ increases.  In these cases the blue\n",
    "commander would expect red to attack on a sunny day.\n",
    "\n",
    "For $\\lambda=4$ red wins on a sunny day with the least losses\n",
    "so again the blue commander would expect red to attack on\n",
    "a sunny day.\n",
    "However, if the day is rainy then blue wins.  Therefore,\n",
    "there is a chance that blue might attack red on a rainy day.\n",
    "\n",
    "For $\\lambda=5$ then blue always wins.  In this case I would not\n",
    "expect red to ever attack.  On the other hand, if I were the red\n",
    "commander I would expect blue to attack on a rainy day."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88732bc0-cec5-43a1-b228-2e8e80df4a8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.10",
   "language": "julia",
   "name": "julia-1.10"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
