Better Initialization

This commit is contained in:
Jonas Weinz
2019-12-01 14:04:07 +01:00
parent 020685c86a
commit 9c406c3e48
14 changed files with 15621 additions and 1647 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,371 @@
{
"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": "5796ec52773740c59e747c0e5f77410e",
"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": "92fd11191481475a9c40ae76201b4772",
"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": "618b5a44910843bbaed8b36c3ad2bc46",
"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": "301ebb9ed6024493ad85c2b79402345e",
"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": "c90d303cd2cb43d1aae401ac6226e3a1",
"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": "cea1f9de60344298ac8417d755ad74df",
"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": "3ac8e962dfeb445fa3417dbdbfd5c44c",
"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",
"def run(e=None):\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
}

View File

@ -1,28 +1,8 @@
{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {
"language_info": {
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": 3
}
},
"orig_nbformat": 2,
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"cells": [
{
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Statistical Tools"
]
@ -33,14 +13,13 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
"import numpy as np\n",
"import scipy.stats"
]
},
{
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"* Helper function to calculate the wheel of fortune"
]
@ -61,14 +40,109 @@
"metadata": {},
"outputs": [],
"source": [
"def wheel_of_fortune_selection(items: list, item_scores:list):\n",
" ordering = np.argsort(item_scores)\n",
" ordering = ordering + 1\n",
"def wheel_of_fortune_weights(items:list, item_scores:list):\n",
" rank = scipy.stats.rankdata(item_scores)\n",
"\n",
" wheel_weights = wheel_of_fortune(ordering, len(ordering))\n",
" n = len(items)\n",
"\n",
" return np.random.choice(items, p=wheel_weights)\n"
" return wheel_of_fortune(rank, n)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def wheel_of_fortune_selection(items: list, item_scores:list, num_choices=1):\n",
" \n",
" wheel_weights = wheel_of_fortune_weights(items, item_scores)\n",
" \n",
" n = min(len(items), num_choices)\n",
" \n",
" choice = np.random.choice(items, size=n, replace=False, p=wheel_weights)\n",
" \n",
" if num_choices == 1:\n",
" return choice[0]\n",
"\n",
" return choice\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def combined_wheel_of_fortune_selection(items_list:list, item_scores_list:list, num_choices=1):\n",
" \n",
" scores = {}\n",
" \n",
" for i in range(len(items_list)):\n",
" items = items_list[i]\n",
" item_scores = item_scores_list[i]\n",
" \n",
" w = wheel_of_fortune_weights(items, item_scores)\n",
" #print(items, item_scores)\n",
" #print(w)\n",
" \n",
" for j, item in enumerate(items):\n",
" if item in scores:\n",
" scores[item] += w[j]\n",
" else:\n",
" scores[item] = w[j]\n",
" \n",
" combined_items = []\n",
" combined_scores = []\n",
" \n",
" for i,s in scores.items():\n",
" combined_items.append(i)\n",
" combined_scores.append(s)\n",
" \n",
" combined_scores = np.array(combined_scores)\n",
" \n",
" #print(combined_scores)\n",
" #print(np.sum(combined_scores))\n",
" \n",
" combined_scores /= len(items_list)\n",
" \n",
" #print(combined_scores)\n",
" \n",
" #print(np.sum(combined_scores))\n",
" \n",
" n = min(len(combined_items), num_choices)\n",
" \n",
" return np.random.choice(combined_items, size=n, replace=False, p=combined_scores)\n",
" \n",
" "
]
}
]
}
],
"metadata": {
"file_extension": ".py",
"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.5rc1"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@ -4,6 +4,7 @@
# # Statistical Tools
import numpy as np
import scipy.stats
# * Helper function to calculate the wheel of fortune
@ -12,11 +13,67 @@ def wheel_of_fortune(rank_i,n):
return rank_i / (0.5 * n * (n + 1))
def wheel_of_fortune_selection(items: list, item_scores:list):
ordering = np.argsort(item_scores)
ordering = ordering + 1
def wheel_of_fortune_weights(items:list, item_scores:list):
rank = scipy.stats.rankdata(item_scores)
wheel_weights = wheel_of_fortune(ordering, len(ordering))
n = len(items)
return np.random.choice(items, p=wheel_weights)
return wheel_of_fortune(rank, n)
def wheel_of_fortune_selection(items: list, item_scores:list, num_choices=1):
wheel_weights = wheel_of_fortune_weights(items, item_scores)
n = min(len(items), num_choices)
choice = np.random.choice(items, size=n, replace=False, p=wheel_weights)
if num_choices == 1:
return choice[0]
return choice
def combined_wheel_of_fortune_selection(items_list:list, item_scores_list:list, num_choices=1):
scores = {}
for i in range(len(items_list)):
items = items_list[i]
item_scores = item_scores_list[i]
w = wheel_of_fortune_weights(items, item_scores)
#print(items, item_scores)
#print(w)
for j, item in enumerate(items):
if item in scores:
scores[item] += w[j]
else:
scores[item] = w[j]
combined_items = []
combined_scores = []
for i,s in scores.items():
combined_items.append(i)
combined_scores.append(s)
combined_scores = np.array(combined_scores)
#print(combined_scores)
#print(np.sum(combined_scores))
combined_scores /= len(items_list)
#print(combined_scores)
#print(np.sum(combined_scores))
n = min(len(combined_items), num_choices)
return np.random.choice(combined_items, size=n, replace=False, p=combined_scores)