{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "technological-script",
   "metadata": {},
   "source": [
    "## Start and Manage a New Experiment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "immediate-daily",
   "metadata": {},
   "source": [
    "### 1. Initialize Tuner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "formed-grounds",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nni.algorithms.hpo.gridsearch_tuner import GridSearchTuner\n",
    "tuner = GridSearchTuner()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reported-somerset",
   "metadata": {},
   "source": [
    "### 2. Configure Search Space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "potential-williams",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_space = {\n",
    "    \"C\": {\"_type\":\"quniform\",\"_value\":[0.1, 1, 0.1]},\n",
    "    \"kernel\": {\"_type\":\"choice\",\"_value\":[\"linear\", \"rbf\", \"poly\", \"sigmoid\"]},\n",
    "    \"degree\": {\"_type\":\"choice\",\"_value\":[1, 2, 3, 4]},\n",
    "    \"gamma\": {\"_type\":\"quniform\",\"_value\":[0.01, 0.1, 0.01]},\n",
    "    \"coef0\": {\"_type\":\"quniform\",\"_value\":[0.01, 0.1, 0.01]}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greek-archive",
   "metadata": {},
   "source": [
    "### 3. Configure Experiment "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fiscal-expansion",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nni.experiment import Experiment\n",
    "experiment = Experiment(tuner, 'local')\n",
    "experiment.config.experiment_name = 'test'\n",
    "experiment.config.trial_concurrency = 2\n",
    "experiment.config.max_trial_number = 5\n",
    "experiment.config.search_space = search_space\n",
    "experiment.config.trial_command = 'python3 main.py'\n",
    "experiment.config.trial_code_directory = './'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "received-tattoo",
   "metadata": {},
   "source": [
    "### 4. Start Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "pleasant-patent",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-02-22 12:27:11] Creating experiment, Experiment ID: bj025qo4\n",
      "[2021-02-22 12:27:11] Connecting IPC pipe...\n",
      "[2021-02-22 12:27:15] Statring web server...\n",
      "[2021-02-22 12:27:16] Setting up...\n",
      "[2021-02-22 12:27:16] Dispatcher started\n",
      "[2021-02-22 12:27:16] Web UI URLs: http://127.0.0.1:8081 http://10.0.1.5:8081 http://172.17.0.1:8081\n"
     ]
    }
   ],
   "source": [
    "experiment.start(8081)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "miniature-prison",
   "metadata": {},
   "source": [
    "### 5. Experiment View & Control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "animated-english",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'RUNNING'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "alpha-ottawa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[TrialResult(parameter={'coef0': 0.01, 'gamma': 0.01, 'degree': 1, 'kernel': 'linear', 'C': 0.1}, value=0.9866666666666667, trialJobId='B55mT'),\n",
       " TrialResult(parameter={'coef0': 0.02, 'gamma': 0.01, 'degree': 1, 'kernel': 'linear', 'C': 0.1}, value=0.9866666666666667, trialJobId='QkhD0')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.export_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "unique-rendering",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'B55mT': [TrialMetricData(timestamp=1613996853005, trialJobId='B55mT', parameterId='0', type='FINAL', sequence=0, data=0.9866666666666667)],\n",
       " 'QkhD0': [TrialMetricData(timestamp=1613996853843, trialJobId='QkhD0', parameterId='1', type='FINAL', sequence=0, data=0.9866666666666667)]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_job_metrics()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welsh-difference",
   "metadata": {},
   "source": [
    "### 6. Stop Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "technological-cleanup",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-02-22 12:28:16] Stopping experiment, please wait...\n",
      "[2021-02-22 12:28:16] Dispatcher exiting...\n",
      "[2021-02-22 12:28:17] Experiment stopped\n",
      "[2021-02-22 12:28:19] Dispatcher terminiated\n"
     ]
    }
   ],
   "source": [
    "experiment.stop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nni-dev",
   "language": "python",
   "name": "nni-dev"
  },
  "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
