{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2018-12-12T12:34:14.020Z"
    }
   },
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import de2 as de\n",
    "import multiprocessing as mp\n",
    "import makeqx as mkq\n",
    "import matplotlib.pyplot as plt\n",
    "from jupyterthemes import jtplot\n",
    "# currently installed theme will be used to\n",
    "# set plot style if no arguments provided\n",
    "jtplot.style()\n",
    "\n",
    "def rndtop5(x):\n",
    "    return np.round(x*2.0)/2\n",
    "\n",
    "def tmm_wrapper2(arg):\n",
    "    args, kwargs = arg\n",
    "    return mkq.vgdr_eval_wsweep(*args, **kwargs)\n",
    "\n",
    "def arc_par(pop, **kwargs):\n",
    "    jobs = []\n",
    "    pool=mp.Pool(90)\n",
    "    num_layers = int(pop[0].size/2)\n",
    "    for indiv in pop:\n",
    "        jobs.append((indiv.reshape(num_layers,2), 0))\n",
    "    arg = [(j, kwargs) for j in jobs]\n",
    "    answ = np.array(pool.map(tmm_wrapper2, arg))\n",
    "    pool.close()\n",
    "    return answ\n",
    "\n",
    "num_layers = 22\n",
    "norlam_min = 0.25   # this is lam/d\n",
    "norlam_max = 2       # this is lam/d\n",
    "lam_pts = 256\n",
    "\n",
    "\n",
    "bnds = [(0, 1)]*num_layers*2\n",
    "its_first = 0\n",
    "psnew = 120\n",
    "islands = 5\n",
    "its_second = 512\n",
    "\n",
    "numgens = 5\n",
    "\n",
    "isl = np.ones((psnew*islands, 2*num_layers))\n",
    "for indiv in range(int(psnew*islands)):\n",
    "    isl[indiv] = np.random.uniform(0,1, 2*num_layers)\n",
    "#     nxhold = isl[indiv][::2]\n",
    "#     nxhold[::2] = 1\n",
    "#     nxhold[1::2] = 0\n",
    "    #isl[indiv] = qn.make_qxn(num_layers, dlevels=2)\n",
    "poplist = np.split(isl, islands)\n",
    "\n",
    "bids = np.zeros(islands, dtype=int)\n",
    "bfits = np.zeros(islands)\n",
    "bests = isl[0:islands]\n",
    "\n",
    "for gen in range(numgens):\n",
    "    print(\"==============================\")\n",
    "    print(\"Epoch #:\" + str(gen + 1))\n",
    "    print(\"==============================\")\n",
    "    \n",
    "    for isln in range(islands):\n",
    "        print(\"----------------- I-\" + str(isln + 1)+ \"---------------------\")\n",
    "        poplist[isln], bids[isln], bfits[isln], bests[isln] = de.de_cp(\n",
    "            fobj=arc_par, \n",
    "            bounds=bnds, \n",
    "            pop=poplist[isln], \n",
    "            history=[], \n",
    "            it_start=its_first, \n",
    "            popsize=psnew, \n",
    "            its=its_second, \n",
    "            lam_low=norlam_min, \n",
    "            lam_high=norlam_max, \n",
    "            lam_pts=lam_pts)\n",
    "        \n",
    "    if its_second > 64:\n",
    "        its_second = int(its_second/2)\n",
    "    \n",
    "    if gen < (numgens - 1):\n",
    "        print(\"Round robin best migration\")\n",
    "        stmp = np.copy(poplist[islands-1][bids[islands-1]])\n",
    "        for isln in range(1, islands):\n",
    "            poplist[isln][bids[isln]] = poplist[isln-1][bids[isln-1]]\n",
    "        poplist[0][bids[0]] = stmp \n",
    "    \n",
    "print(bfits)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-12T09:09:10.997450Z",
     "start_time": "2018-12-12T09:09:08.834305Z"
    }
   },
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import makeqx as mkq\n",
    "import matplotlib.pyplot as plt\n",
    "from jupyterthemes import jtplot\n",
    "# currently installed theme will be used to\n",
    "# set plot style if no arguments provided\n",
    "jtplot.style()\n",
    "\n",
    "qx = bests[np.argmin(bfits)].reshape(num_layers, 2)\n",
    "\n",
    "\n",
    "norlam_min = 0.25   # this is lam/d\n",
    "norlam_max = 2       # this is lam/d\n",
    "lam_pts = 256\n",
    "#lams = np.linspace(lam_low, lam_high, endpoint=True, num=lam_pts)\n",
    "lam_inv = np.linspace(1/norlam_min, 1/norlam_max, num=lam_pts, endpoint=True)\n",
    "lams = 1.0/lam_inv\n",
    "\n",
    "\n",
    "\n",
    "Rs = np.zeros(lams.size)\n",
    "Rs = mkq.tmm_eval_wsweep(qx, 0, lam_low=norlam_min, lam_high=norlam_max, lam_pts=lam_pts)\n",
    "\n",
    "vgdr = 100*np.ones(lam_pts)\n",
    "for idx, lam in enumerate(lams):\n",
    "    if lams[idx] <= 1:\n",
    "        cuts = Rs[np.logical_and(lams >= lam, lams <= 2*lam)]\n",
    "        #print(cuts)\n",
    "        vgdr[idx] = np.mean(cuts)\n",
    "#         print(vgdr)\n",
    "\n",
    "\n",
    "\n",
    "#400.0/np.amin(vgdr)\n",
    "\n",
    "\n",
    "# lams\n",
    "c, cthick = mkq.vgdr2_eval_wsweep(qx = qx, inc_ang=0, lam_low=norlam_min, lam_high=norlam_max, lam_pts=lam_pts)\n",
    "\n",
    "\n",
    "# num_layers = 10\n",
    "# # qx = np.random.uniform(0,1, (num_layers,2))\n",
    "# # qx[:,0] = qx[:,0]/np.sum(qx[:,0])\n",
    "\n",
    "norlam_min = 400.0/cthick   # this is lam/d\n",
    "norlam_max = 800.0/cthick       # this is lam/d\n",
    "lam_pts = 256\n",
    "\n",
    "lam_inv = np.linspace(1/norlam_min, 1/norlam_max, num=lam_pts, endpoint=True)\n",
    "lams = (1.0/lam_inv)\n",
    "lam_ac = np.linspace(400, 800,  num=lam_pts, endpoint=True)\n",
    "lams2 = np.linspace(400, 800, endpoint=True, num = lam_pts)\n",
    "#lams = np.flip(lams, axis=0)\n",
    "\n",
    "# # # for thick in [200, 400, 800]:\n",
    "# # qx[:,1] = mkq.digitize_qx(qx[:,1], dlevels=2)\n",
    "# d_x, n_x = mkq.make_nxdx(qx=qx)\n",
    "\n",
    "Rs2 = mkq.tmm_eval_wsweep(qx = qx, inc_ang=0, lam_low=norlam_min, lam_high=norlam_max, lam_pts=lam_pts)\n",
    "# lams = cthick*lams\n",
    "meansl = np.mean(Rs2)*np.ones_like(lams)\n",
    "# #     #plt.plot(1.0/lams, Rs)\n",
    "# plt.plot( Rs)\n",
    "# #plt.plot(lams, meansl)\n",
    "plt.ylim([0,1])\n",
    "#plt.xlim([0.75,1.1])\n",
    "    \n",
    "\n",
    "plt.plot(lams2, Rs2)\n",
    "plt.plot(lams2, meansl)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-10T13:42:38.721768Z",
     "start_time": "2018-12-10T13:42:37.792591Z"
    }
   },
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import makeqx as mkq\n",
    "import matplotlib.pyplot as plt\n",
    "from jupyterthemes import jtplot\n",
    "# currently installed theme will be used to\n",
    "# set plot style if no arguments provided\n",
    "jtplot.style()\n",
    "\n",
    "\n",
    "num_layers = 50\n",
    "\n",
    "layers = np.arange(num_layers)\n",
    "nlr = np.random.uniform(0,1, num_layers)\n",
    "#nlr = mkq.make_qx(num_layers)\n",
    "#nlr = mkq.digitize_qx(nlr, dlevels=2)\n",
    "#plt.bar(layers, nlr)\n",
    "#plt.ylim([0,1])\n",
    "\n",
    "lam_inv = np.linspace(0.2, 2, num=50, endpoint=True)\n",
    "lams = (1.0/lam_inv)\n",
    "lams = np.flip(lams, axis=0)\n",
    "\n",
    "for thick in [200, 400, 800]:\n",
    "    d_x, n_x = mkq.make_nxdx(qx=nlr, cthick=thick, wavelen=550)\n",
    "    Rs = mkq.tmm_eval_wsweep(qx = nlr, cthick=thick, inc_ang=0, lam_low = 0.5*thick, lam_high=5*thick, lam_pts=50)\n",
    "    #plt.plot(1.0/lams, Rs)\n",
    "    plt.plot(lams, Rs)\n",
    "    plt.ylim([0,100])\n",
    "\n",
    "\n",
    "vgdr = np.zeros(25)\n",
    "for idx, lam in enumerate(lams):\n",
    "    if idx < 25:\n",
    "        cuts = Rs[np.logical_and(lams >= lam, lams <= 2*lam)]\n",
    "        vgdr[idx] = np.mean(cuts)\n",
    "# plt.plot(vgdr)\n",
    "# plt.ylim([0,100])\n",
    "\n",
    "size =  400/lams[np.argmin(vgdr)]\n",
    "val = np.amin(vgdr)\n",
    "size, val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T14:38:47.524293Z",
     "start_time": "2018-12-11T14:38:46.239283Z"
    }
   },
   "outputs": [],
   "source": [
    "#\\][;/++plt.plot(Rs)\n",
    "# for fd in zip(lams, Rs):\n",
    "#     print(fd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T14:57:18.592232Z",
     "start_time": "2018-12-11T14:57:17.305396Z"
    }
   },
   "outputs": [],
   "source": [
    "c "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T14:57:29.843041Z",
     "start_time": "2018-12-11T14:57:28.560532Z"
    }
   },
   "outputs": [],
   "source": [
    "np.mean(Rs2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T14:40:31.683888Z",
     "start_time": "2018-12-11T14:40:30.373859Z"
    }
   },
   "outputs": [],
   "source": [
    "vgdr[178]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-11T14:41:59.272354Z",
     "start_time": "2018-12-11T14:41:57.982383Z"
    }
   },
   "outputs": [],
   "source": [
    "400.0/lams[178]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.0"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}