master-thesis/EvolutionaryAlgorithm/InteractiveVersion.ipynb

375 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# User Interface for the Evolutionary Algorithm"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
" if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (typeof require !== 'undefined') {\n",
" require.undef(\"plotly\");\n",
" requirejs.config({\n",
" paths: {\n",
" 'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
" window._Plotly = Plotly;\n",
" });\n",
" }\n",
" </script>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
" if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (typeof require !== 'undefined') {\n",
" require.undef(\"plotly\");\n",
" requirejs.config({\n",
" paths: {\n",
" 'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
" window._Plotly = Plotly;\n",
" });\n",
" }\n",
" </script>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jonas/Dokumente/gitRepos/master_thesis/EvolutionaryAlgorithm/EvolutionaryAlgorithm.py:58: TqdmExperimentalWarning:\n",
"\n",
"Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
"\n"
]
}
],
"source": [
"import EvolutionaryAlgorithm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display, HTML, Markdown"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# user widgets\n",
"w_result_out = widgets.Output()\n",
"w_ing_list_out = widgets.Output()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**setup input ingredients:**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"**number of input ingredients:**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "affefd1263f44e97815b9c32f49d69d9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButtons(index=3, options=('1', '2', '3', '4', '5', '6', '7', '8', '9', '10'), style=ToggleButtonsStyle(b…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"**maximum number of additional ingredients:**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "29990add3612462abfb4dcc60f2ff7ca",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButtons(index=3, options=('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), style=ToggleButtonsStyle(bu…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4b66883d839f49e9b764f683ba9079c9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"**number of evolutionary cycles:**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "645f200c9fa441f8964683dbcd188ad0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButtons(index=1, options=('0', '5', '10', '15', '20', '25', '30', '35', '40', '45'), style=ToggleButtons…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"**population size:**"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d0c4b6f97b894211a5a1c2cf052e1d58",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ToggleButtons(index=1, options=('5', '10', '15', '20', '25', '30', '35', '40', '45', '50'), style=ToggleButton…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "889ed0ea7cc24663b40beb30e8a1abcc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(description='run EA', style=ButtonStyle())"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fa434784f8364bc692301a162824cb30",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w_number_input_ings = widgets.ToggleButtons(\n",
" options = [str(i+1) for i in range(10)],\n",
" value='4')\n",
"w_number_input_ings.style.button_width=\"10px\"\n",
"w_number_additional_ings = widgets.ToggleButtons(options=[str(i) for i in range(10)], value='3')\n",
"w_number_additional_ings.style.button_width=\"10px\"\n",
"\n",
"'''\n",
"containers = [\n",
" widgets.Combobox(\n",
" # value='John',\n",
" placeholder='Choose Ingredient',\n",
" options=EvolutionaryAlgorithm.m_base_mix.get_labels(),\n",
" description=f'Ingredient {i}',\n",
" ensure_option=True,\n",
" disabled=False\n",
" )\n",
"\n",
" for i in range(10)]\n",
"'''\n",
"\n",
"containers = [\n",
" widgets.Text(\n",
" # value='John',\n",
" placeholder='Choose Ingredient',\n",
" description=f'Ingredient {i}',\n",
" disabled=False\n",
" )\n",
"\n",
" for i in range(10)]\n",
"\n",
"ingredients = []\n",
"w_ing_container = widgets.VBox(ingredients)\n",
"\n",
"display(Markdown(\"**number of input ingredients:**\"))\n",
"display(w_number_input_ings)\n",
"display(Markdown(\"**maximum number of additional ingredients:**\"))\n",
"display(w_number_additional_ings)\n",
"\n",
"def update_ings(e=None):\n",
" if len(w_ing_container.children) == int(w_number_input_ings.value):\n",
" return\n",
" \n",
" w_ing_list_out.clear_output()\n",
" with w_ing_list_out:\n",
" display(widgets.VBox([containers[i] for i in range(int(w_number_input_ings.value))]))\n",
"\n",
"update_ings()\n",
"display(w_ing_list_out)\n",
"\n",
"# control evo cycle:\n",
"w_number_cycles = widgets.ToggleButtons(options=[str(i*5) for i in range(10)], value='5')\n",
"w_number_cycles.style.button_width=\"10px\"\n",
"\n",
"w_population_size = widgets.ToggleButtons(options=[str((i+1)*5) for i in range(10)], value='10')\n",
"w_population_size.style.button_width=\"10px\"\n",
"\n",
"display(Markdown(\"**number of evolutionary cycles:**\"))\n",
"display(w_number_cycles)\n",
"display(Markdown(\"**population size:**\"))\n",
"display(w_population_size)\n",
"\n",
"\n",
"w_run_button = widgets.Button(description=\"run EA\")\n",
"\n",
"p = None\n",
"\n",
"def run(e=None):\n",
" global p\n",
" w_result_out.clear_output()\n",
" with w_result_out:\n",
" p = EvolutionaryAlgorithm.Population(\n",
" [containers[i].value for i in range(int(w_number_input_ings.value))],\n",
" max_additional_ings=int(w_number_additional_ings.value)\n",
" )\n",
" p.run(int(w_number_cycles.value))\n",
" display(Markdown(\"**Population after running EA:**\"))\n",
" p.plot_population(collect_scores=int(w_population_size.value)>0)\n",
" \n",
"display(w_run_button)\n",
"display(w_result_out)\n",
"w_run_button.on_click(run)\n",
"\n",
"w_number_input_ings.observe(update_ings)"
]
},
{
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}