{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c0e2a42d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from core.lottery import *\n",
    "from core.strategy import random_strategy\n",
    "from core.constants import *\n",
    "from pid.pid_base import *\n",
    "from draw import draw\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c6c8b3b",
   "metadata": {},
   "source": [
    "# controller gains search space simulation.  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "414e5569",
   "metadata": {},
   "source": [
    "crawler converges fast towrds highest accuracy, here is how it works:\n",
    "## for all controller N pramaters:\n",
    "* start with some hueristic state $s_i | 0 < i <= N$ \n",
    "* look on the right, and left of s at random, within certain dynamic range, and step size that are changed every loop to either zoom in/out to converge fast as possible, and avoid getting stuck in local minima/maxima.\n",
    "* if new record is hit, or range is exhusted, move to new next controller parameter/dimension state $s_{i+1}$.\n",
    "* if a loop went by without hitting any new record (high controller accuracy), search space is scrutinized with smaller step, and larger space around $s_i$ in next round.\n",
    "* otherwise space range is decreased, and step is increased."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49dc7d66",
   "metadata": {},
   "source": [
    "## auto crawler discrete controller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38e77bc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!python secondary_discrete_auto_crawler.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "10a81f04",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!cat highest_gain.txt "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2fc307d",
   "metadata": {},
   "source": [
    "## auto crawler takahashi controller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6baf77e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!python secondary_takahashi_auto_crawler.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3f55d753",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!cat highest_gain_takahashi.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caf6731c",
   "metadata": {},
   "source": [
    "# controller parameters results\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beff0f57",
   "metadata": {},
   "source": [
    "the controller results shows that optimal gains are as follows: accuracy:0.7568862275449102, kp: -0.03999999999998902, ki:-0.005999999985257798, kd:0.01299999999999478\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "45e85da8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def vesting_instance(initial_distribution, vesting):\n",
    "    os.system(\"rm log/*_feedback.hist; rm log/*_output.hist log/darkie* log/rewards.log\")\n",
    "    RUNNING_TIME = len(next(iter(vesting.values())))*28800\n",
    "    #RUNNING_TIME = 10000\n",
    "    print('running time: {}'.format(RUNNING_TIME))\n",
    "    total_vesting = 0\n",
    "    if __name__ == \"__main__\":\n",
    "        darkies = []\n",
    "        id = 0\n",
    "        for name, distrib in vesting.items():\n",
    "            darkies += [Darkie(initial_distribution[id], vesting=distrib, strategy=random_strategy(EPOCH_LENGTH))]\n",
    "            id+=1\n",
    "            total_vesting+=distrib[-1]\n",
    "        airdrop = 0\n",
    "        for darkie in darkies:\n",
    "            airdrop+=darkie.stake\n",
    "        print(\"network airdrop: {}/{}% on {} nodes\".format(airdrop, airdrop/ERC20DRK*100, len(darkies)))\n",
    "        print('total vesting: {}/{}%'.format(total_vesting, total_vesting/ERC20DRK*100))\n",
    "        dt = DarkfiTable(airdrop, RUNNING_TIME, kp=-0.010399999999938556, ki=-0.0365999996461878, kd=0.03840000000000491,  r_kp=-2.53, r_ki=29.5, r_kd=53.77)\n",
    "        for darkie in darkies:\n",
    "            dt.add_darkie(darkie)\n",
    "        dt.background(rand_running_time=False)\n",
    "        dt.write()\n",
    "    return RUNNING_TIME"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dd77352",
   "metadata": {},
   "source": [
    "# run lottery instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d83e8a1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "vesting = {}\n",
    "with open('vested_distribution.csv') as f:\n",
    "    for node  in f.readlines():\n",
    "        keyval = node.split(',')\n",
    "        key = keyval[0]\n",
    "        val = ','.join(keyval[1:])\n",
    "        vesting[keyval[0]] = eval(eval(val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1741af9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "running time: 1728000\n",
      "network airdrop: 1070/5.095238095238095e-05% on 1070 nodes\n",
      "total vesting: 111747850.0/5.32132619047619%\n",
      "secondary min/max : 0.0001/0.9999\n",
      "primary min/max : 1/1000\n"
     ]
    }
   ],
   "source": [
    "nodes = len(vesting)\n",
    "# stakers intial distribution\n",
    "genesis_distribution = [1 for _ in range(nodes)]\n",
    "running_time = vesting_instance(genesis_distribution, vesting)\n",
    "draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2be550bd",
   "metadata": {},
   "source": [
    "# average APR"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7464623d",
   "metadata": {},
   "source": [
    "### NOTE! to estimate APR using only initial distribution run avg_apr in core/lottery.py\n",
    "### NOTE! for apr estimation with vesting, analyse every node stake under log with it's id log/darkie[id].log\n",
    "the following is rough average estimation of apr for all nodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ce426c2",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
