new initialization functions in test notebook

This commit is contained in:
Jonas Weinz 2019-12-27 11:52:10 +01:00
parent 33f22a5050
commit b4aae0ba02
3 changed files with 639 additions and 685 deletions

View File

@ -91,7 +91,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jonas/.local/lib/python3.7/site-packages/ipykernel_launcher.py:37: TqdmExperimentalWarning:\n",
"/home/jonas/.local/lib/python3.7/site-packages/ipykernel_launcher.py:39: 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"
@ -119,6 +119,8 @@
"\n",
"import numpy as np\n",
"\n",
"import ActionGroups as AG\n",
"\n",
"import plotly.graph_objs as go\n",
"from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n",
"from plotly.subplots import make_subplots\n",
@ -459,14 +461,92 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def prepare_ratio(ing:str):\n",
" keys, values = m_grouped_act.get_backward_adjacent(Ingredient(ing).to_json())\n",
" action_dict = dict(zip(keys,values))\n",
" return action_dict['prepare'] / action_dict['heat']"
" return action_dict['prepare'] / action_dict['heat']\n",
"\n",
"def random_prepare(ing:str):\n",
" \"\"\"\n",
" returns randomly a boolean value if ing should be prepared, w.r.t. the prepare_ration function\n",
" \"\"\"\n",
" \n",
" return prepare_ratio(ing) > np.random.normal(0.35,0.1)\n",
"\n",
"def random_heated(ingredient:str):\n",
" action_set, action_weights = m_grouped_base_act.get_backward_adjacent(ingredient)\n",
" d = dict(zip(action_set, action_weights))\n",
" ratio = 1 - d['prepare'] / d['heat']\n",
" \n",
" return ratio > np.random.normal(0.65,0.15)\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def relative_action_rank(ingredient:str, action:str):\n",
" action_set, action_weights = m_base_act.get_backward_adjacent(ingredient)\n",
" if action not in action_set or len(action_set) <= 1:\n",
" return 0\n",
" return 1 - action_set.tolist().index(action) / (len(action_set) - 1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def filter_set_by_group(act_set, act_w, group):\n",
" new_act_set = []\n",
" new_act_w = []\n",
" for i in range(len(act_set)):\n",
" if act_set[i] in AG.inverse_groups[group]:\n",
" new_act_set.append(act_set[i])\n",
" new_act_w.append(act_w[i])\n",
" return np.array(new_act_set), np.array(new_act_w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## better normalized scores:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def normalized_score(key, matrix):\n",
" sum_key = matrix.get_sum(key)\n",
" keys, values = matrix.get_adjacent(key)\n",
" normalized_values = np.array([(values[i] / matrix.get_sum(keys[i])) * (values[i] / sum_key) for i in range(len(keys))])\n",
" sort = np.argsort(-normalized_values)\n",
" return keys[sort], normalized_values[sort]\n",
"\n",
"def forward_normalized_score(key, matrix):\n",
" sum_key = matrix.get_fw_sum(key)\n",
" keys, values = matrix.get_forward_adjacent(key)\n",
" normalized_values = np.array([(values[i] / matrix.get_bw_sum(keys[i])) * (values[i] / sum_key) for i in range(len(keys))])\n",
" sort = np.argsort(-normalized_values)\n",
" return keys[sort], normalized_values[sort]\n",
"\n",
"def backward_normalized_score(key, matrix):\n",
" sum_key = matrix.get_bw_sum(key)\n",
" keys, values = matrix.get_backward_adjacent(key)\n",
" normalized_values = np.array([(values[i] / matrix.get_fw_sum(keys[i])) * (values[i] / sum_key) for i in range(len(keys))])\n",
" sort = np.argsort(-normalized_values)\n",
" return keys[sort], normalized_values[sort]"
]
},
{
@ -479,7 +559,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
@ -681,7 +761,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
@ -803,7 +883,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
@ -861,7 +941,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
@ -919,7 +999,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@ -946,6 +1026,7 @@
" ings_for_acts[a].add(ing)\n",
"\n",
" '''\n",
" \n",
" # choose randomly an action for each ingredient by the \"wheel of fortune\" method\n",
" actions_for_ing = {}\n",
" for ing in ingredients:\n",
@ -1267,6 +1348,441 @@
" return Tree.from_serialization(self.serialize())\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"ingredients = [\"onion\", \"tomato\", \"rice\", \"salt\"] \n",
"main_ingredients = [\"rice\"]\n",
"\n",
"assert set(main_ingredients).issubset(set(ingredients))\n",
"\n",
"max_n = 5\n",
"wheel_turns = 2\n",
"\n",
"def does_action_match(ingredient:str, action:str, t = 0.6):\n",
" return relative_action_rank(ingredient, action) > t\n",
"\n",
"\n",
"# choose randomly an action for each ingredient by the \"wheel of fortune\" method\n",
"actions_for_ing = {}\n",
"for ing in ingredients:\n",
" actions_for_ing[ing] = set()\n",
" action_set, action_weights = m_base_act.get_backward_adjacent(ing)\n",
" if random_heated(ing):\n",
" #print(action_set)\n",
" action_set, action_weights = filter_set_by_group(action_set, action_weights, \"heat\")\n",
" #print(action_set)\n",
" for i in range(wheel_turns):\n",
" if ing in main_ingredients:\n",
" # if main ingredient: choose by action probability\n",
" w = np.array(list(action_weights), dtype=float)\n",
" w *= (1.0 / np.sum(w))\n",
" action = np.random.choice(list(action_set), size=1, replace=False, p=w)[0]\n",
" else:\n",
" # else: choose rank based\n",
" action = ea_tools.wheel_of_fortune_selection(action_set[:max_n], action_weights[:max_n])\n",
" actions_for_ing[ing].add(action)\n",
" #print(f\"action {action} for ing {ing}\")\n",
" #print(ing, action)\n",
" \n",
"# create ingredient nodes:\n",
"ingredient_nodes = {}\n",
"\n",
"# create ingredient nodes:\n",
"for ing in ingredients:\n",
" new_node = IngredientNode(ing, constant=True)\n",
" \n",
" # check if we should do a preparation step\n",
" if random_prepare(ing):\n",
" # choose a preparation cooking action\n",
" action_set, action_weights = m_act.get_backward_adjacent(Ingredient(ing).to_json())\n",
" action_set, action_weights = filter_set_by_group(action_set, action_weights, \"prepare\")\n",
" if len(action_set) > 0:\n",
" action = ea_tools.wheel_of_fortune_selection(action_set[:max_n], action_weights[:max_n])\n",
" act_node = ActionNode(action)\n",
" act_node.add_child(new_node)\n",
" new_node = act_node\n",
" \n",
" \n",
" ingredient_nodes[ing] = new_node\n",
"\n",
"# starting now with the actions found for the main ingredients and try to match all ingredients together\n",
"# with that:\n",
"\n",
"unprocessed_ings = set(filter(lambda x: len(actions_for_ing[x]) > 0, ingredients))\n",
"unprocessed_main_ings = set(filter(lambda x: len(actions_for_ing[x]) > 0, main_ingredients))\n",
"\n",
"while len(unprocessed_main_ings) > 0:\n",
" main_ing = unprocessed_main_ings.pop()\n",
" \n",
" # random action for that ing:\n",
" act = actions_for_ing[main_ing].pop()\n",
" \n",
" act_node = ActionNode(act)\n",
" mix_node = MixNode()\n",
" mix_node.add_child(ingredient_nodes[main_ing])\n",
" act_node.add_child(mix_node)\n",
" ingredient_nodes[main_ing] = act_node\n",
" \n",
" unprocessed_ings.remove(main_ing)\n",
" \n",
" for ing in unprocessed_ings.copy():\n",
" if does_action_match(ing, act):\n",
" mix_node.add_child(ingredient_nodes[ing])\n",
" ingredient_nodes[ing] = act_node\n",
" unprocessed_ings.remove(ing)\n",
" if ing in unprocessed_main_ings:\n",
" unprocessed_main_ings.remove(ing)\n",
" \n",
" if len(mix_node.childs()) == 1:\n",
" mix_node.remove()\n",
"\n",
"# now make the same with all remaining ingredients:\n",
"while len(unprocessed_ings) > 0:\n",
" current_ing = unprocessed_ings.pop() \n",
" \n",
" # random action for that ing:\n",
" act = actions_for_ing[current_ing].pop()\n",
" \n",
" act_node = ActionNode(act)\n",
" mix_node = MixNode()\n",
" mix_node.add_child(ingredient_nodes[current_ing])\n",
" act_node.add_child(mix_node)\n",
" \n",
" ingredient_nodes[current_ing] = act_node\n",
" \n",
" \n",
" for ing in unprocessed_ings.copy():\n",
" if does_action_match(ing, act):\n",
" mix_node.add_child(ingredient_nodes[ing])\n",
" ingredient_nodes[ing] = act_node\n",
" unprocessed_ings.remove(ing)\n",
" \n",
" if len(mix_node.childs()) == 1:\n",
" mix_node.remove()\n",
"\n",
"\n",
"root_layer = set([n.root() for n in ingredient_nodes.values()])\n",
"\n",
"root_layer_without_parents = []\n",
"for node in root_layer:\n",
" if node.parent() is None:\n",
" root_layer_without_parents.append(node)\n",
"\n",
"if len(root_layer_without_parents) == 1:\n",
" root_node = root_layer_without_parents[0]\n",
"\n",
"else:\n",
" root_node = MixNode()\n",
" for r in root_layer_without_parents:\n",
" root_node.add_child(r)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
" -->\n",
"<!-- Title: %3 Pages: 1 -->\n",
"<svg width=\"613pt\" height=\"434pt\"\n",
" viewBox=\"0.00 0.00 612.85 433.82\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 429.8234)\">\n",
"<title>%3</title>\n",
"<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-429.8234 608.8528,-429.8234 608.8528,4 -4,4\"/>\n",
"<!-- 10 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>10</title>\n",
"<polygon fill=\"#d5e8d4\" stroke=\"#d5e8d4\" points=\"272.8528,-425.8234 152.8528,-389.8234 272.8528,-353.8234 392.8528,-389.8234 272.8528,-425.8234\"/>\n",
"<text text-anchor=\"start\" x=\"259.3528\" y=\"-393.6234\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"263.3528\" y=\"-393.6234\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">mix</text>\n",
"<text text-anchor=\"start\" x=\"220.8528\" y=\"-379.6234\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 0.0000</text>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>8</title>\n",
"<ellipse fill=\"#dae8fc\" stroke=\"#dae8fc\" cx=\"84.8528\" cy=\"-292.3675\" rx=\"84.7059\" ry=\"25.4118\"/>\n",
"<text text-anchor=\"start\" x=\"68.8528\" y=\"-296.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"72.8528\" y=\"-296.1675\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">bake</text>\n",
"<text text-anchor=\"start\" x=\"32.8528\" y=\"-282.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 1.0000</text>\n",
"</g>\n",
"<!-- 10&#45;&gt;8 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>10&#45;&gt;8</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M228.7752,-366.9743C201.1586,-352.6584 165.4433,-334.1442 136.5007,-319.1409\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"137.8495,-315.8978 127.3606,-314.4028 134.6279,-322.1124 137.8495,-315.8978\"/>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"#dae8fc\" stroke=\"#dae8fc\" cx=\"272.8528\" cy=\"-292.3675\" rx=\"84.7059\" ry=\"25.4118\"/>\n",
"<text text-anchor=\"start\" x=\"261.3528\" y=\"-296.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"265.3528\" y=\"-296.1675\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">cut</text>\n",
"<text text-anchor=\"start\" x=\"220.8528\" y=\"-282.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 0.2505</text>\n",
"</g>\n",
"<!-- 10&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>10&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M272.8528,-353.8113C272.8528,-345.4239 272.8528,-336.496 272.8528,-328.1199\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"276.3529,-327.8873 272.8528,-317.8874 269.3529,-327.8874 276.3529,-327.8873\"/>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"#dae8fc\" stroke=\"#dae8fc\" cx=\"464.8528\" cy=\"-292.3675\" rx=\"84.7059\" ry=\"25.4118\"/>\n",
"<text text-anchor=\"start\" x=\"453.8528\" y=\"-296.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"457.8528\" y=\"-296.1675\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">fry</text>\n",
"<text text-anchor=\"start\" x=\"412.8528\" y=\"-282.1675\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 0.0859</text>\n",
"</g>\n",
"<!-- 10&#45;&gt;6 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>10&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M317.8683,-366.9743C346.1945,-352.5964 382.8636,-333.9838 412.4895,-318.9462\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"414.1077,-322.05 421.4406,-314.4028 410.9394,-315.808 414.1077,-322.05\"/>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>5</title>\n",
"<polygon fill=\"#ffe6cc\" stroke=\"#ffe6cc\" points=\"142.8528,-212.9117 26.8528,-212.9117 26.8528,-176.9117 142.8528,-176.9117 142.8528,-212.9117\"/>\n",
"<text text-anchor=\"start\" x=\"72.3528\" y=\"-198.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"76.3528\" y=\"-198.7117\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">salt</text>\n",
"<text text-anchor=\"start\" x=\"34.8528\" y=\"-184.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score:1.0000</text>\n",
"</g>\n",
"<!-- 8&#45;&gt;5 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>8&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M84.8528,-266.7622C84.8528,-253.4123 84.8528,-237.0481 84.8528,-223.3105\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"88.3529,-222.9641 84.8528,-212.9642 81.3529,-222.9642 88.3529,-222.9641\"/>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>2</title>\n",
"<polygon fill=\"#ffe6cc\" stroke=\"#ffe6cc\" points=\"328.8528,-212.9117 212.8528,-212.9117 212.8528,-176.9117 328.8528,-176.9117 328.8528,-212.9117\"/>\n",
"<text text-anchor=\"start\" x=\"247.8528\" y=\"-198.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"251.8528\" y=\"-198.7117\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">tomato</text>\n",
"<text text-anchor=\"start\" x=\"220.8528\" y=\"-184.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score:1.0000</text>\n",
"</g>\n",
"<!-- 3&#45;&gt;2 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>3&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M272.3273,-266.7622C272.0534,-253.4123 271.7175,-237.0481 271.4356,-223.3105\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"274.9278,-222.8902 271.2233,-212.9642 267.9293,-223.0339 274.9278,-222.8902\"/>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>7</title>\n",
"<polygon fill=\"#d5e8d4\" stroke=\"#d5e8d4\" points=\"466.8528,-230.9117 346.8528,-194.9117 466.8528,-158.9117 586.8528,-194.9117 466.8528,-230.9117\"/>\n",
"<text text-anchor=\"start\" x=\"453.3528\" y=\"-198.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"457.3528\" y=\"-198.7117\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">mix</text>\n",
"<text text-anchor=\"start\" x=\"414.8528\" y=\"-184.7117\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 1.0000</text>\n",
"</g>\n",
"<!-- 6&#45;&gt;7 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>6&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M465.3783,-266.7622C465.5397,-258.8985 465.7225,-249.989 465.9032,-241.1865\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"469.4068,-241.0411 466.1128,-230.9713 462.4083,-240.8974 469.4068,-241.0411\"/>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node8\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"#dae8fc\" stroke=\"#dae8fc\" cx=\"385.8528\" cy=\"-97.4558\" rx=\"84.7059\" ry=\"25.4118\"/>\n",
"<text text-anchor=\"start\" x=\"371.3528\" y=\"-101.2558\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"375.3528\" y=\"-101.2558\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">slice</text>\n",
"<text text-anchor=\"start\" x=\"333.8528\" y=\"-87.2558\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score: 0.1501</text>\n",
"</g>\n",
"<!-- 7&#45;&gt;1 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>7&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M442.7695,-165.9356C433.3727,-154.6298 422.5567,-141.6164 412.8939,-129.9906\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"415.5038,-127.655 406.4202,-122.2016 410.1204,-132.1293 415.5038,-127.655\"/>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node10\" class=\"node\">\n",
"<title>4</title>\n",
"<polygon fill=\"#ffe6cc\" stroke=\"#ffe6cc\" points=\"604.8528,-115.4558 488.8528,-115.4558 488.8528,-79.4558 604.8528,-79.4558 604.8528,-115.4558\"/>\n",
"<text text-anchor=\"start\" x=\"533.8528\" y=\"-101.2558\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"537.8528\" y=\"-101.2558\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">rice</text>\n",
"<text text-anchor=\"start\" x=\"496.8528\" y=\"-87.2558\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score:1.0000</text>\n",
"</g>\n",
"<!-- 7&#45;&gt;4 -->\n",
"<g id=\"edge9\" class=\"edge\">\n",
"<title>7&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M490.6389,-165.9356C501.7,-152.4609 514.7524,-136.5605 525.4944,-123.4746\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"528.2061,-125.6875 531.8458,-115.7374 522.7956,-121.246 528.2061,-125.6875\"/>\n",
"</g>\n",
"<!-- 0 -->\n",
"<g id=\"node9\" class=\"node\">\n",
"<title>0</title>\n",
"<polygon fill=\"#ffe6cc\" stroke=\"#ffe6cc\" points=\"443.8528,-36 327.8528,-36 327.8528,0 443.8528,0 443.8528,-36\"/>\n",
"<text text-anchor=\"start\" x=\"367.3528\" y=\"-21.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\"> </text>\n",
"<text text-anchor=\"start\" x=\"371.3528\" y=\"-21.8\" font-family=\"Times,serif\" font-weight=\"bold\" font-size=\"14.00\" fill=\"#000000\">onion</text>\n",
"<text text-anchor=\"start\" x=\"335.8528\" y=\"-7.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">node score:1.0000</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;0 -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>1&#45;&gt;0</title>\n",
"<path fill=\"none\" stroke=\"#000000\" d=\"M385.8528,-71.8782C385.8528,-63.7122 385.8528,-54.6289 385.8528,-46.2824\"/>\n",
"<polygon fill=\"#000000\" stroke=\"#000000\" points=\"389.3529,-46.2287 385.8528,-36.2288 382.3529,-46.2288 389.3529,-46.2287\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.dot.Digraph at 0x7ff425fc1a90>"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"root_node.dot()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"constant_ingredients = [\"noodle\", \"onion\", \"tomato\"]\n",
"main_ingredients = ['noodle']\n",
"min_additional = 3\n",
"max_additional = 6\n",
"top_ings =3"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['noodle',\n",
" 'onion',\n",
" 'tomato',\n",
" 'salt',\n",
" 'cheese',\n",
" 'garlic clove',\n",
" 'olive oil',\n",
" 'mozzarella cheese']"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"seen_items = set(constant_ingredients)\n",
"\n",
"items = []\n",
"scores = []\n",
"\n",
"assert set(main_ingredients).issubset(set(constant_ingredients))\n",
"\n",
"# additional ingredients are choosen w.r.t all given ingredients\n",
"n_additional_ings = np.random.randint(min_additional, max_additional + 1)\n",
"\n",
"# extra ings are ingredients choosen specially for the main ingredient\n",
"n_extra_ings = int((len(main_ingredients) / len(constant_ingredients)) * n_additional_ings)\n",
"\n",
"if n_extra_ings > n_additional_ings:\n",
" n_extra_ings = n_additional_ings\n",
"\n",
" \n",
"# choose extra ingredients\n",
"extra_candidates = []\n",
"extra_weights = []\n",
"\n",
"for ing in main_ingredients:\n",
" candidates, weights = normalized_score(ing, m_base_mix)\n",
" extra_candidates.append(candidates[:10])\n",
" extra_weights.append(weights[:10])\n",
"\n",
"extra_ingredients = ea_tools.combined_wheel_of_fortune_selection(extra_candidates,\n",
" extra_weights,\n",
" n_extra_ings)\n",
"\n",
"for ing in constant_ingredients:\n",
" # find best matching ingredients\n",
" best_items = []\n",
" best_scores = []\n",
"\n",
" candidates, weights = m_base_mix.get_adjacent(ing)\n",
" i = 0\n",
" while i < len(candidates) and len(best_items) < top_ings:\n",
" if candidates[i] not in seen_items:\n",
" best_items.append(candidates[i])\n",
" best_scores.append(weights[i])\n",
" i += 1\n",
"\n",
" items.append(best_items)\n",
" scores.append(best_scores)\n",
"\n",
"#TODO: error handling if too few options are availabale!\n",
"\n",
"additional_ingredients = ea_tools.combined_wheel_of_fortune_selection(items,\n",
" scores,\n",
" n_additional_ings - n_extra_ings)\n",
"list(constant_ingredients) + list(additional_ingredients) + list(extra_ingredients)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array(['ricotta cheese', 'spaghetti sauce', 'mozzarella cheese', 'cheese',\n",
" 'spinach', 'sausage', 'ground beef', 'tomato sauce', 'onion',\n",
" 'basil', 'broccoli', 'parsley', 'mushroom soup', 'mushroom',\n",
" 'sauce', 'egg', 'garlic clove', 'chicken', 'milk', 'water', 'salt',\n",
" 'zucchini', 'tomato', 'pepper', 'seasoning', 'green pepper',\n",
" 'shrimp', 'soy sauce', 'butter', 'red pepper', 'oregano',\n",
" 'clove garlic', 'olive oil', 'pork', 'carrot', 'green onion',\n",
" 'cream cheese', 'garlic', 'chicken broth', 'tablespoon butter',\n",
" 'red bell pepper', 'flour', 'cream', 'black pepper',\n",
" 'vegetable oil', 'chicken breast', 'sugar'], dtype='<U86'),\n",
" array([1.72269296e-03, 1.36100201e-03, 4.55169243e-04, 1.77893267e-04,\n",
" 1.24020302e-04, 1.23572299e-04, 1.15856830e-04, 8.13045041e-05,\n",
" 7.44682982e-05, 6.11712938e-05, 5.25617916e-05, 4.61949373e-05,\n",
" 4.57840602e-05, 4.48640553e-05, 3.80872894e-05, 3.73615068e-05,\n",
" 3.41135643e-05, 2.86899933e-05, 2.60745654e-05, 2.58892962e-05,\n",
" 2.39238665e-05, 2.25048416e-05, 2.02862719e-05, 1.98958546e-05,\n",
" 1.98476423e-05, 1.70669883e-05, 1.64057568e-05, 1.63032289e-05,\n",
" 1.57316299e-05, 1.57055922e-05, 1.50292158e-05, 1.42549883e-05,\n",
" 1.39543420e-05, 1.13377515e-05, 1.08577156e-05, 1.06498185e-05,\n",
" 1.04539527e-05, 1.01201500e-05, 9.80997203e-06, 9.75022935e-06,\n",
" 8.94143133e-06, 8.23586373e-06, 6.82070606e-06, 5.09612356e-06,\n",
" 4.75313364e-06, 4.31247765e-06, 3.80137782e-06]))"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"normalized_score(\"noodle\", m_base_mix)"
]
},
{
"cell_type": "markdown",
"metadata": {},

File diff suppressed because one or more lines are too long

View File

@ -135,7 +135,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5rc1"
"version": "3.7.5"
},
"mimetype": "text/x-python",
"name": "python",