{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to make and use the test problems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Currently supported:\n",
    "* ZDT Problems- ZDT1-4, ZDT6\n",
    "* DTLZ Problems- DTLZ1-7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the test problem  builder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from desdeo_problem.testproblems.TestProblems import test_problem_builder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `test_problem_builder` to build the necessary `MOProblem` instance, which can be used by methods in desdeo-emo and desdeo-mcdm to solve multiobjective optimization problems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function test_problem_builder in module desdeo_problem.testproblems.TestProblems:\n",
      "\n",
      "test_problem_builder(name: str, n_of_variables: int = None, n_of_objectives: int = None) -> desdeo_problem.problem.Problem.MOProblem\n",
      "    Build test problems. Currently supported: ZDT1-4, ZDT6, and DTLZ1-7.\n",
      "    \n",
      "    Args:\n",
      "        name (str): Name of the problem in all caps. For example: \"ZDT1\", \"DTLZ4\", etc.\n",
      "        n_of_variables (int, optional): Number of variables. Required for DTLZ problems,\n",
      "            but can be skipped for ZDT problems as they only support one variable value.\n",
      "        n_of_objectives (int, optional): Required for DTLZ problems,\n",
      "            but can be skipped for ZDT problems as they only support one variable value.\n",
      "    \n",
      "    Raises:\n",
      "        ProblemError: When one of many issues occur while building the MOProblem\n",
      "            instance.\n",
      "    \n",
      "    Returns:\n",
      "        MOProblem: The test problem object\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(test_problem_builder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<desdeo_problem.problem.Problem.MOProblem at 0x1f33b279ac8>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zdt1 = test_problem_builder(\"ZDT1\")\n",
    "zdt1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<desdeo_problem.problem.Problem.MOProblem at 0x1f33b279208>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dtlz3 = test_problem_builder(\"DTLZ3\", n_of_objectives= 3, n_of_variables=20)\n",
    "dtlz3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How to use these instances for other purposes, such as generating data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate input data as desired:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.9566665  0.12642707 0.75401858 0.03765694 0.01986561 0.48660617\n",
      "  0.49280935 0.85344899 0.27133411 0.93323257 0.84133493 0.20475801\n",
      "  0.92905088 0.06490354 0.8570188  0.83492128 0.62833644 0.99593786\n",
      "  0.81635487 0.82580931 0.56251793 0.97574662 0.47558831 0.3939823\n",
      "  0.27397178 0.7496003  0.04909389 0.08239682 0.34656906 0.49915204]\n",
      " [0.74919761 0.28625722 0.67908382 0.6106337  0.6950148  0.25785019\n",
      "  0.61746779 0.76319615 0.89890242 0.75963628 0.98161652 0.67291301\n",
      "  0.79612155 0.52273917 0.20450823 0.7952598  0.60585745 0.4121897\n",
      "  0.05809478 0.34800526 0.58840432 0.18724738 0.68237086 0.61657321\n",
      "  0.3096879  0.32458604 0.16036243 0.82480997 0.17956196 0.01421743]\n",
      " [0.9957692  0.19364135 0.11009589 0.63606894 0.92162454 0.95342228\n",
      "  0.88665615 0.74501953 0.09816078 0.48951933 0.76896919 0.64603171\n",
      "  0.90088292 0.26154581 0.91006787 0.89883207 0.45426937 0.47012129\n",
      "  0.01451065 0.40256939 0.2019439  0.45817166 0.56534801 0.18641038\n",
      "  0.91780371 0.19666782 0.83473067 0.15496044 0.01478023 0.85545543]]\n"
     ]
    }
   ],
   "source": [
    "number_of_samples = 3\n",
    "zdt_data = np.random.random((number_of_samples, 30))  # 30 is the number of variables in the ZDT1 problem\n",
    "print(zdt_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "dtlz_data = np.random.random((number_of_samples, 20)) # We put the number of variables earlier as 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`<MOProblem object>.evaluate(data)` returns a tuple containing the objective values and constraint violations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "EvaluationResults(objectives=array([[0.9566665 , 3.42361516],\n",
       "       [0.74919761, 3.55955498],\n",
       "       [0.9957692 , 3.31853043]]), fitness=array([[0.9566665 , 3.42361516],\n",
       "       [0.74919761, 3.55955498],\n",
       "       [0.9957692 , 3.31853043]]), constraints=None, uncertainity=array([[nan, nan],\n",
       "       [nan, nan],\n",
       "       [nan, nan]]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zdt_obj_val = zdt1.evaluate(zdt_data)\n",
    "zdt_obj_val"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are no constraints in the zdt or dtlz problems, hence cons_val is None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "EvaluationResults(objectives=array([[1307.49928399, 1214.1266996 ,  555.10066581],\n",
       "       [ 248.94975775,  162.82774202, 2199.31133262],\n",
       "       [1052.58290325, 1701.71639663,  600.67385014]]), fitness=array([[1307.49928399, 1214.1266996 ,  555.10066581],\n",
       "       [ 248.94975775,  162.82774202, 2199.31133262],\n",
       "       [1052.58290325, 1701.71639663,  600.67385014]]), constraints=None, uncertainity=array([[nan, nan, nan],\n",
       "       [nan, nan, nan],\n",
       "       [nan, nan, nan]]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dtlz_obj_val = dtlz3.evaluate(dtlz_data)\n",
    "dtlz_obj_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "desdeo-problem",
   "language": "python",
   "name": "desdeo-problem"
  },
  "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
