{ "cells": [ { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T07:12:19.852128Z", "start_time": "2018-10-03T07:12:19.644845Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "Dataset has been loaded\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import h5py\n", "from sklearn.model_selection import train_test_split\n", "#import jtplot submodule from jupyterthemes\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", "#now load this dataset \n", "h5f = h5py.File('./datasets/s8_sio2tio2_v2.h5','r')\n", "X = h5f['sizes'][:]\n", "Y = h5f['spectrum'][:]\n", "\n", "#create a train - test split of the dataset\n", "x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=42)\n", "# normalize inputs \n", "x_test = (x_test - 50)/20 \n", "\n", "print(\"Dataset has been loaded\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T07:12:22.108057Z", "start_time": "2018-10-03T07:12:22.084707Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import snlay as snlay\n", "\n", "np.zeros(8)\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T07:12:26.001435Z", "start_time": "2018-10-03T07:12:25.974477Z" } }, "outputs": [], "source": [ "from PyGMO.problem import base\n", "\n", "import snlay as snlay\n", "\n", "mats = np.array([3, 4, 3, 4, 3, 4, 3, 4])\n", "lams = np.linspace(300, 1200, 256)\n", "center = np.zeros(8) + 57\n", "targ_spec = y_test[19]\n", "\n", "def get_best_ans(islets):\n", " champ_fits = np.array([islets[idx].population.champion.f[0] for idx in range(len(islets))])\n", " minidx = np.argmin(champ_fits)\n", " midx = np.asscalar(minidx)\n", " best_x = np.array(archi[midx].population.champion.x)\n", " return best_x, np.amin(champ_fits)\n", "\n", "\n", "def sphere_func(x, dims):\n", " x_np = np.array(x)\n", " spec_ac = snlay.calc_spectrum(x_np, mats, lams)\n", " diff = np.abs(targ_spec - spec_ac)/targ_spec\n", " #print(targ_spec.shape)\n", " #val = np.sum((x_np - center)**2)\n", " val = 100*np.mean(diff)\n", " return val\n", "\n", "\n", "\n", "class my_problem(base):\n", " \"\"\"\n", " \"\"\"\n", " def __init__(self, dim=8):\n", " # First we call the constructor of the base class telling PyGMO\n", " # what kind of problem to expect ('dim' dimensions, 1 objective, 0 contraints etc.)\n", " super(my_problem,self).__init__(dim)\n", " # We set the problem bounds (in this case equal for all components)\n", " self.set_bounds(30, 70)\n", " \n", "\n", " # Reimplement the virtual method that defines the objective function.\n", " def _objfun_impl(self, x):\n", " # Compute the sphere function\n", " f = sphere_func(x,self.dimension)\n", " #f = sum([x[i] ** 2 for i in range(self.dimension)])\n", "\n", " # Note that we return a tuple with one element only. In PyGMO the objective functions\n", " # return tuples so that multi-objective optimization is also possible.\n", " return (f, )\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-01T17:25:37.251830Z", "start_time": "2018-10-01T17:25:37.204999Z" } }, "outputs": [], "source": [ "sphere_func((55, 65, 55, 65, 55, 65, 55, 65), 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2018-10-03T07:11:33.190Z" } }, "outputs": [], "source": [ "import PyGMO as pyg\n", "\n", "prob = my_problem(dim=8) # Create a 10-dimensional problem\n", "\n", "#algo = pyg.algorithm.bee_colony(gen=50) # 500 generations of bee_colony algorithm\n", "\n", "# # # prob = pyg.problem.schwefel(dim = 8)\n", "algo = pyg.algorithm.de(gen = 100)\n", "archi = pyg.archipelago(algo,prob,8,20, topology = pyg.topology.ring())\n", "\n", "epocs = 10\n", "es = np.arange(epocs)\n", "opt_hist = []\n", "for epch in range(epocs):\n", " archi.evolve(1)\n", " x, f = get_best_ans(archi)\n", " opt_hist.append(f)\n", " print(f)\n", "\n", "print(x)\n", "\n", "print(50 + 20*x_test[19])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-01T17:47:25.496340Z", "start_time": "2018-10-01T17:47:25.472208Z" } }, "outputs": [], "source": [ "opt_hist" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig1 = plt.figure(figsize=(5,3))\n", "ax = fig1.add_subplot(1,1,1)\n", "#ax.set_title('silica coated gold')\n", "ax.set_xlabel('Evolution')\n", "ax.set_ylabel('Objective')\n", "ax.semilogy(es, opt_hist)\n", "ax.spines['top'].set_visible(False)\n", "ax.spines['right'].set_visible(False)" ] } ], "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.5.6" } }, "nbformat": 4, "nbformat_minor": 2 }