master-thesis/EvolutionaryAlgorithm/ea_tools.ipynb

149 lines
3.6 KiB
Plaintext
Raw Normal View History

2019-11-08 10:47:58 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Statistical Tools"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
2019-12-01 14:04:07 +01:00
"import numpy as np\n",
"import scipy.stats"
2019-11-08 10:47:58 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Helper function to calculate the wheel of fortune"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def wheel_of_fortune(rank_i,n):\n",
" return rank_i / (0.5 * n * (n + 1))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
2019-12-01 14:04:07 +01:00
"def wheel_of_fortune_weights(items:list, item_scores:list):\n",
" rank = scipy.stats.rankdata(item_scores)\n",
"\n",
" n = len(items)\n",
2019-11-08 10:47:58 +01:00
"\n",
2019-12-01 14:04:07 +01:00
" 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",
2019-11-08 10:47:58 +01:00
"\n",
2019-12-01 14:04:07 +01:00
" 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",
" "
2019-11-08 10:47:58 +01:00
]
}
2019-12-01 14:04:07 +01:00
],
"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.5"
2019-12-01 14:04:07 +01:00
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 4
}