diff --git a/EvolutionaryAlgorithm/EvolutionaryAlgorithm.ipynb b/EvolutionaryAlgorithm/EvolutionaryAlgorithm.ipynb index 525b4a6..461dbac 100644 --- a/EvolutionaryAlgorithm/EvolutionaryAlgorithm.ipynb +++ b/EvolutionaryAlgorithm/EvolutionaryAlgorithm.ipynb @@ -1 +1,3104 @@ -{"cells":[{"cell_type":"markdown","metadata":{},"source":"# Evolutionary Algorithm"},{"cell_type":"markdown","metadata":{},"source":"the Evolutionary Algorithm that is supposed to create new recipes based on the Recipe Matrices that are created during the *Recipe Analysis* step.\n\nThe Population of the Evolutional Algorithm consists of a set of recipe trees. Each Recipe Tree consists of several Nodes where each node is of one of the following Types:\n\n* **Ingredient Node:**\n these are the leaf nodes. Containing an ingredient. The score is determined by the actions, that are applied if you follow up the path. At the Moment it measures how many duplicate actions are applied.\n* **Action Node:**\n An Action that is applied on it's child and this child's subtree. Score indicates the average likelihood that this action is applied on the ingredients inside the subtree\n* **Mix Node:**\n Mixing ingredients together. This is also the only Node that can have more than one child. The score is the average of all pairwise likelihoods that two ingredients are mixed togethter"},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"data":{"text/html":" \n "},"metadata":{},"output_type":"display_data"},{"data":{"text/html":" \n "},"metadata":{},"output_type":"display_data"}],"source":"import sys\nsys.path.append(\"../\")\nsys.path.append(\"../RecipeAnalysis/\")\n\nimport settings\n\nimport pycrfsuite\n\nimport json\n\nimport db.db_settings as db_settings\nfrom db.database_connection import DatabaseConnection\n\nfrom Tagging.conllu_generator import ConlluGenerator\nfrom Tagging.crf_data_generator import *\n\nfrom RecipeAnalysis.Recipe import Ingredient\n\nfrom difflib import SequenceMatcher\n\nimport numpy as np\n\nimport plotly.graph_objs as go\nfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\nfrom plotly.subplots import make_subplots\ninit_notebook_mode(connected=True)\n\nfrom graphviz import Digraph\n\nimport itertools\n\nimport random\n\nimport plotly.io as pio\npio.renderers.default = \"jupyterlab\"\n\nfrom IPython.display import Markdown, HTML, display\n\nfrom copy import deepcopy"},{"cell_type":"code","execution_count":59,"metadata":{},"outputs":[],"source":"def gaussian(x, mu, sig):\n return 1./(np.sqrt(2.*np.pi)*sig)*np.exp(-np.power((x - mu)/sig, 2.)/2)"},{"cell_type":"markdown","metadata":{},"source":"## load adjacency matrices"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[],"source":"import dill\nm_act = dill.load(open(\"../RecipeAnalysis/m_act.dill\", \"rb\"))\nm_mix = dill.load(open(\"../RecipeAnalysis/m_mix.dill\", \"rb\"))\nm_base_act = dill.load(open(\"../RecipeAnalysis/m_base_act.dill\", \"rb\"))\nm_base_mix = dill.load(open(\"../RecipeAnalysis/m_base_mix.dill\", \"rb\"))\n\n#m_act.apply_threshold(3)\n#m_mix.apply_threshold(3)\n#m_base_act.apply_threshold(5)\n#m_base_mix.apply_threshold(5)\n\n\n#c_act = m_act.get_csr()\n#c_mix = m_mix.get_csr()\n#c_base_act = m_base_act.get_csr()\n#c_base_mix = m_base_mix.get_csr()\n\nc_act = m_act._csr\nc_mix = m_mix._csr\nc_base_act = m_base_act._csr\nc_base_mix = m_base_mix._csr\n"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"actions = m_act.get_labels()[0]"},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[],"source":"base_ingredients = m_base_mix.get_labels()"},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[],"source":"sym_label_buffer = {}\nfw_label_buffer = {}\nbw_label_buffer = {}"},{"cell_type":"markdown","metadata":{},"source":"### helper functions for adjacency matrices"},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[],"source":"def get_sym_adjacent(key, m, c):\n index = m._label_index[key]\n i1 = c[index,:].nonzero()[1]\n i2 = c[:,index].nonzero()[0]\n \n i = np.concatenate((i1,i2))\n \n if m in sym_label_buffer:\n names = sym_label_buffer[m][i]\n else:\n names = np.array(m.get_labels())\n sym_label_buffer[m] = names\n names = names[i]\n \n counts = np.concatenate((c[index, i1].toarray().flatten(), c[i2, index].toarray().flatten()))\n \n s = np.argsort(-counts)\n \n return names[s], counts[s]"},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[],"source":"def get_forward_adjacent(key, m, c):\n index = m._x_label_index[key]\n i = c[index,:].nonzero()[1]\n \n if m in fw_label_buffer:\n names = fw_label_buffer[m][i]\n else:\n names = np.array(m._y_labels)\n fw_label_buffer[m] = names\n names = names[i]\n \n \n counts = c[index, i].toarray().flatten()\n \n s = np.argsort(-counts)\n \n return names[s], counts[s]"},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[],"source":"def get_backward_adjacent(key, m, c):\n index = m._y_label_index[key]\n i = c[:,index].nonzero()[0]\n \n if m in bw_label_buffer:\n names = bw_label_buffer[m][i]\n else:\n names = np.array(m._x_labels)\n bw_label_buffer[m] = names\n names = names[i]\n \n \n counts = c[i, index].toarray().flatten()\n \n s = np.argsort(-counts)\n \n return names[s], counts[s]"},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"data":{"text/plain":"(array(['heat', 'simmer', 'cook', 'boil', 'bake', 'place', 'slice', 'cut',\n 'chop', 'cool', 'dice', 'refrigerate', 'pour', 'drain', 'brown',\n 'warm', 'blend', 'chill', 'spread', 'thicken', 'grill', 'saute',\n 'peel', 'fry', 'mash', 'whisk', 'break', 'freeze', 'melt'],\n dtype=' 1 and p._single_child)\n \n for c in childs:\n self.remove_child(c)\n \n if p is not None:\n p.remove_child(self)\n \n if self._single_child and self._child is not None and p._name == self._child._name:\n # two adjacent nodes with same name would remain after deletion.\n # merge them! (by adding the child's childs to our parent instead of our childs)\n childs = self._child.childs()\n self._child.remove()\n \n \n for c in childs:\n p.add_child(c)\n \n def insert_before(self, n):\n p = self._parent\n if p is not None:\n p.remove_child(self)\n p.add_child(n)\n n.add_child(self)\n \n def mutate(self):\n n_node = self.n_node_mutate_options()\n n_edge = self.n_edge_mutate_options()\n \n choice = random.choice(range(n_node + n_edge))\n if choice < n_node:\n self.mutate_node()\n else:\n self.mutate_edges()\n \n def mutate_edges(self):\n ings = self.traverse_ingredients()\n ing = random.choice(ings)\n \n a, w = get_backward_adjacent(ing._base_ingredient, m_base_act, c_base_act)\n \n action = random.choices(a, w)[0]\n self.insert_before(ActionNode(action))\n \n def mutate_node(self):\n raise NotImplementedError\n \n def n_node_mutate_options(self):\n \n return 0 if self._constant else 1\n \n def n_edge_mutate_options(self):\n n = 1 if self._parent is not None else 0\n return n\n \n def n_mutate_options(self):\n return self.n_edge_mutate_options() + self.n_node_mutate_options()\n \n def dot_node(self, dot):\n raise NotImplementedError()\n \n def dot(self, d=None):\n if d is None:\n d = Digraph()\n self.dot_node(d)\n \n else:\n self.dot_node(d)\n if self._parent is not None:\n d.edge(self._parent._id, self._id)\n \n \n for c in self.childs():\n c.dot(d)\n \n return d\n \n def serialize(self):\n r = {}\n r['type'] = str(self.__class__.__name__)\n r['id'] = self._id\n r['parent'] = self._parent._id if self._parent is not None else None\n r['name'] = self._name\n r['childs'] = [c._id for c in self.childs()]\n r['constant'] = self._constant\n r['single_child'] = self._single_child\n \n return r\n \n def node_score(self):\n raise NotImplementedError()\n \n"},{"cell_type":"markdown","metadata":{},"source":"### Mix Node"},{"cell_type":"code","execution_count":32,"metadata":{},"outputs":[],"source":"class MixNode(RecipeTreeNode):\n def __init__(self, constant=False):\n super().__init__(\"mix\", constant, single_child=False)\n \n def dot_node(self, dot):\n dot.node(self._id, label=f\"< {self._name}
node score: {self.node_score()}>\", shape=\"diamond\")\n \n def split(self, set_above, set_below, node_between):\n assert len(set_above.difference(self.childs())) == 0\n assert len(set_below.difference(self.childs())) == 0\n \n n_above = MixNode()\n n_below = MixNode()\n \n p = self.parent()\n \n for c in self.childs().copy():\n self.remove_child(c)\n self.remove()\n \n for c in set_below:\n n_below.add_child(c)\n \n for c in set_above:\n n_above.add_child(c)\n \n n_above.add_child(node_between)\n node_between.add_child(n_below)\n \n if p is not None:\n p.add_child(n_above)\n \n # test whether the mix nodes are useless\n if len(n_above.childs()) == 1:\n n_above.remove()\n \n if len(n_below.childs()) == 1:\n n_below.remove()\n \n def n_node_mutate_options(self):\n return 0 if self._constant or len(self.childs()) <= 2 else len(self.childs())\n \n def mutate_node(self):\n \n childs = self.childs()\n \n if len(childs) <= 2:\n print(\"Warning: cannot modify mix node\")\n return\n \n childs = random.sample(childs, len(childs))\n \n n = random.choice(range(1, len(childs)-1))\n \n between_node = ActionNode(random.choice(actions))\n \n self.split(set(childs[:n]), set(childs[n:]), between_node)\n \n \n def node_score(self):\n child_ingredients = [c.traverse_ingredients() for c in self.childs()]\n \n tmp_set = set()\n cumulative_sets = []\n \n pairwise_tuples = []\n \n for c in child_ingredients:\n if len(tmp_set) > 0:\n cumulative_sets.append(tmp_set)\n pairwise_tuples += [x for x in itertools.product(tmp_set, c)]\n tmp_set = tmp_set.union(set(c))\n \n s_base = 0\n s = 0\n \n for ing_a, ing_b in pairwise_tuples:\n try:\n #s_base += sym_score(ing_a._base_ingredient, ing_b._base_ingredient, m_base_mix, c_base_mix)\n \n #s += sym_score(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)\n p1 = sym_p_a_given_b(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)\n p2 = sym_p_a_given_b(ing_b.to_json(), ing_a.to_json(), m_mix, c_mix)\n \n s += 0.5 * p1 + 0.5 * p2\n \n except:\n pass\n \n #s_base /= len(pairwise_tuples)\n s /= len(pairwise_tuples)\n \n #return 0.5 * (s_base + s)\n return s\n \n \n \n"},{"cell_type":"markdown","metadata":{},"source":"### Ingredient Node Class"},{"cell_type":"code","execution_count":163,"metadata":{},"outputs":[],"source":"class IngredientNode(RecipeTreeNode):\n def __init__(self, name, constant=False):\n super().__init__(name, constant, single_child=True)\n \n def get_actions(self):\n a_list = []\n n = self.parent()\n while n is not None:\n if type(n) == ActionNode:\n a_list.append(n.name())\n n = n.parent()\n return a_list\n \n def mutate_node(self):\n self._name = random.choice(base_ingredients)\n #TODO: change w.r.t. mixing probabilities \n \n def traverse_ingredients(self):\n return [Ingredient(self._name)]\n \n def node_score(self):\n actions = self.get_actions()\n \n if len(actions) == 0:\n if p_ingredient_unprepared(self._name) < 0.2:\n return 0\n return 1\n \n seen_actions = set()\n n_duplicates = 0\n for act in actions:\n if act in seen_actions:\n n_duplicates += 1\n else:\n seen_actions.add(act)\n \n duplicate_actions_score = len(seen_actions) / len(actions)\n \n return duplicate_actions_score\n \n \n def dot_node(self, dot):\n dot.node(self._id, label=f\"< {self._name}
node score:{self.node_score()}>\", shape=\"box\")"},{"cell_type":"markdown","metadata":{},"source":"### Action Node Class"},{"cell_type":"code","execution_count":164,"metadata":{},"outputs":[],"source":"class ActionNode(RecipeTreeNode):\n def __init__(self, name, constant=False):\n super().__init__(name, constant, single_child=True)\n \n def n_node_mutate_options(self):\n # beacause we can change or remove ourselve!\n return 0 if self._constant else 2 \n def mutate_node(self):\n if random.choice(range(2)) == 0:\n # change action\n self._name = random.choice(actions)\n else:\n # delete\n self.remove()\n \n def traverse_ingredients(self):\n ingredient_set = super().traverse_ingredients()\n for ing in ingredient_set:\n ing.apply_action(self._name)\n \n return ingredient_set\n \n def node_score(self):\n ings = self.child().traverse_ingredients()\n \n s = 0\n \n for ing in ings:\n try:\n #score = asym_score(self._name, ing.to_json(), m_act, c_act)\n #base_score = asym_score(self._name, ing._base_ingredient, m_base_act, c_base_act)\n \n score = fw_p_a_given_b(self._name, ing._base_ingredient, m_base_act, c_base_act)\n \n s += score\n except KeyError as e:\n pass\n \n \n return s / len(ings)\n \n def dot_node(self, dot):\n dot.node(self._id, label=f\"< {self._name}
node score: {self.node_score()}>\", shape=\"ellipse\")"},{"cell_type":"markdown","metadata":{},"source":"### Tree Class"},{"cell_type":"code","execution_count":166,"metadata":{},"outputs":[],"source":"class Tree(object):\n \n @staticmethod\n def from_ingredients(ingredients: list):\n root = MixNode()\n \n for ing in ingredients:\n root.add_child(IngredientNode(ing, constant=True))\n \n return Tree(root)\n \n @staticmethod\n def from_serialization(s):\n def empty_node(raw_n):\n if raw_n['type'] == \"MixNode\":\n node = MixNode(raw_n['constant'])\n elif raw_n['type'] == \"IngredientNode\":\n node = IngredientNode(raw_n['name'], raw_n['constant'])\n elif raw_n['type'] == \"ActionNode\":\n node = ActionNode(raw_n['name'], raw_n['constant'])\n else:\n print(\"unknown node detected\")\n return\n \n return node\n \n nodes = {}\n for n in s:\n nodes[n['id']] = empty_node(n)\n \n for n in s:\n childs = n['childs']\n id = n['id']\n for c in childs:\n nodes[id].add_child(nodes[c])\n \n return Tree(nodes[s[0]['id']])\n \n \n def __init__(self, root):\n # create a dummy entry node\n self._root = RecipeTreeNode(\"root\", single_child=True)\n self._root.add_child(root)\n \n def root(self):\n return self._root.child()\n \n def mutate(self):\n nodes = self.root().traverse()\n weights = [n.n_mutate_options() for n in nodes]\n \n n = random.choices(nodes, weights)[0]\n \n n.mutate()\n \n def dot(self):\n return self.root().dot()\n \n def serialize(self):\n return [n.serialize() for n in self.root().traverse()]\n \n def structure_score(self):\n n_duplicates = 0\n \n \n def collect_scores(self):\n self._mix_scores = []\n self._act_scores = []\n self._ing_scores = []\n \n nodes = self.root().traverse()\n self._n_mix_nodes = 0\n self._n_act_nodes = 0\n self._n_ing_nodes = 0\n \n s = 0\n for n in nodes:\n if type(n) == MixNode:\n self._mix_scores.append(n.node_score())\n self._n_mix_nodes += 1\n if type(n) == ActionNode:\n self._act_scores.append(n.node_score())\n self._n_act_nodes += 1\n if type(n) == IngredientNode:\n self._ing_scores.append(n.node_score())\n self._n_ing_nodes += 1\n \n self._n_duplicates = 0\n seen_actions = set()\n \n for n in nodes:\n if type(n) == ActionNode:\n if n.name() in seen_actions:\n self._n_duplicates += 1\n else:\n seen_actions.add(n.name())\n \n self._mix_scores = np.array(self._mix_scores)\n self._act_scores = np.array(self._act_scores)\n self._ing_scores = np.array(self._ing_scores)\n \n \n def mix_scores(self):\n return self._mix_scores\n \n def action_scores(self):\n return self._act_scores\n \n def ing_scores(self):\n return self._ing_scores\n \n def bounds_mix_scores(self):\n \n nonzeros = self._mix_scores[self._mix_scores > 0]\n if len(nonzeros) > 0:\n mmax = np.max(nonzeros)\n mmin = np.min(nonzeros)\n return mmin, mmax\n else:\n return None, None\n \n def bounds_act_scores(self):\n \n if len(self._act_scores) == 0:\n return None, None\n nonzeros = self._act_scores[self._act_scores > 0]\n if len(nonzeros) > 0:\n mmax = np.max(nonzeros)\n mmin = np.min(nonzeros)\n return mmin, mmax\n else:\n return None, None\n \n def normalized_mix_scores(self, min, max):\n if (max != min):\n normalized = (self._mix_scores - min)/(max-min)\n normalized[normalized <= 0] = 0\n return normalized\n else:\n return None\n \n def normalized_act_scores(self, min, max):\n if len(self._act_scores) == 0 or max == min:\n return None\n normalized = (self._act_scores - min)/(max-min)\n normalized[normalized <= 0] = 0\n return normalized\n \n def copy(self):\n return Tree.from_serialization(self.serialize())\n"},{"cell_type":"markdown","metadata":{},"source":"## Population"},{"cell_type":"code","execution_count":167,"metadata":{},"outputs":[],"source":"class Population(object):\n def __init__(self, start_ingredients, n_population = 10):\n self.population = [Tree.from_ingredients(start_ingredients) for i in range(n_population)]\n self._n = n_population\n self._mix_min = None\n self._mix_max = None\n self._act_min = None\n self._act_max = None\n self._mix_scores = None\n self._act_scores = None\n self._scores = None\n \n def mutate(self):\n for tree in self.population.copy():\n t_clone = tree.copy()\n t_clone.mutate()\n self.population.append(t_clone)\n \n def pairwise_competition(self):\n new_population = []\n indices = list(range(len(self.population)))\n random.shuffle(indices)\n \n for i in range(len(self.population) // 2):\n i_a = indices[2*i]\n i_b = indices[2*i+1]\n \n if self._scores[i_a] > self._scores[i_b]:\n new_population.append(self.population[i_a])\n else:\n new_population.append(self.population[i_b])\n \n self.population = new_population\n \n def hold_best(self, n=10):\n sorted_indices = np.argsort(-self._scores)\n \n self.population = np.array(self.population)[sorted_indices[:n]].tolist()\n \n def analyse_scores(self):\n for tree in self.population:\n min, max = tree.bounds_mix_scores()\n if min is not None and max is not None:\n if self._mix_min is None or min < self._mix_min:\n self._mix_min = min\n if self._mix_max is None or max > self._mix_max:\n self._mix_max = max\n \n min, max = tree.bounds_act_scores()\n if min is not None and max is not None:\n if self._act_min is None or min < self._act_min:\n self._act_min = min\n if self._act_max is None or max > self._act_max:\n self._act_max = max\n \n def single_score(self, mix_scores, act_scores, ing_scores):\n if mix_scores is None or act_scores is None or ing_scores is None:\n return 0\n score = (0.5 * np.average(mix_scores) + 0.5 * np.average(act_scores)) * np.average(ing_scores)\n # judging also how many actions we have. So far use a gaussian with mean at number of ingredients\n \n score *= gaussian(len(act_scores), len(mix_scores), 1)\n return score\n \n \n \n def collect_scores(self):\n for tree in self.population:\n tree.collect_scores()\n \n self.analyse_scores()\n \n if self._mix_min is not None and self._mix_max is not None:\n self._mix_scores = [t.normalized_mix_scores(self._mix_min, self._mix_max) for t in self.population]\n else:\n # if no normalization can be done, all values are the same or 0.\n # in this case just fill in zeros as score\n self._mix_scores = [np.zeros(shape=t._mix_scores.shape) for t in self.population]\n \n if self._act_min is not None and self._act_max is not None:\n self._act_scores = [t.normalized_act_scores(self._act_min, self._act_max) for t in self.population]\n else:\n self._act_scores = [np.zeros(shape=t._act_scores) for t in self.population]\n \n self._scores = []\n for i in range(len(self._mix_scores)):\n #print (self._mix_scores[i], self._act_scores[i])\n if self._act_scores is None or self._mix_scores is None or self._act_scores[i] is None:\n self._scores.append(0)\n continue\n \n s = self.single_score(self._mix_scores[i], self._act_scores[i], self.population[i].ing_scores())\n self._scores.append(s)\n self._scores = np.array(self._scores)\n \n def run(self, n=50):\n for i in range(n):\n print(i)\n self.mutate()\n self.mutate()\n self.collect_scores()\n #self.pairwise_competition()\n #self.collect_scores()\n self.hold_best(self._n)\n \n \n \n def plot_population(self):\n self.collect_scores()\n #print(self._mix_scores)\n #print(self._act_scores)\n #print(self._scores)\n for i, t in enumerate(self.population):\n display(self._scores[i])\n display(t.root().dot())"},{"cell_type":"markdown","metadata":{},"source":"## Run Evolutionary Algorithm"},{"cell_type":"code","execution_count":176,"metadata":{},"outputs":[],"source":"p = Population([\"noodle\", \"bacon\", \"salt\", \"pepper\", \"tomato\", \"onion\"])"},{"cell_type":"code","execution_count":172,"metadata":{},"outputs":[],"source":"#p_ingredient_unprepared(list(p.population[0].root().childs())[0]._name) < 0.2"},{"cell_type":"code","execution_count":177,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"0\n1\n2\n3\n4\n"}],"source":"p.run(5)"},{"cell_type":"code","execution_count":178,"metadata":{},"outputs":[{"data":{"text/plain":"0.23706667598847336"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n28554\n\n \nheat\nnode score: 0.7713748266936733\n\n\n\n28546\n\n \nmix\nnode score: 0.256507387234994\n\n\n\n28554->28546\n\n\n\n\n\n28548\n\n \ntomato\nnode score:1.0\n\n\n\n28546->28548\n\n\n\n\n\n28552\n\n \nsalt\nnode score:1.0\n\n\n\n28546->28552\n\n\n\n\n\n28547\n\n \nnoodle\nnode score:1.0\n\n\n\n28546->28547\n\n\n\n\n\n28551\n\n \nbacon\nnode score:1.0\n\n\n\n28546->28551\n\n\n\n\n\n28549\n\n \nonion\nnode score:1.0\n\n\n\n28546->28549\n\n\n\n\n\n28550\n\n \npepper\nnode score:1.0\n\n\n\n28546->28550\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.20204020608318787"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29216\n\n \ncool\nnode score: 0.24156450774085103\n\n\n\n29226\n\n \nmix\nnode score: 0.0031457570642523616\n\n\n\n29216->29226\n\n\n\n\n\n29219\n\n \nbacon\nnode score:1.0\n\n\n\n29226->29219\n\n\n\n\n\n29225\n\n \ncook\nnode score: 0.7852322377351193\n\n\n\n29226->29225\n\n\n\n\n\n29221\n\n \ntomato\nnode score:1.0\n\n\n\n29226->29221\n\n\n\n\n\n29220\n\n \nnoodle\nnode score:1.0\n\n\n\n29226->29220\n\n\n\n\n\n29227\n\n \nmix\nnode score: 0.6102252273053411\n\n\n\n29225->29227\n\n\n\n\n\n29218\n\n \nsalt\nnode score:1.0\n\n\n\n29227->29218\n\n\n\n\n\n29222\n\n \npepper\nnode score:1.0\n\n\n\n29227->29222\n\n\n\n\n\n29223\n\n \nonion\nnode score:1.0\n\n\n\n29227->29223\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.17162627779561146"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29095\n\n \nmix\nnode score: 0.0\n\n\n\n29094\n\n \ncook\nnode score: 1.0\n\n\n\n29095->29094\n\n\n\n\n\n29084\n\n \nwhip\nnode score: 0.0026102706158107817\n\n\n\n29095->29084\n\n\n\n\n\n29096\n\n \nmix\nnode score: 0.0\n\n\n\n29094->29096\n\n\n\n\n\n29091\n\n \nheat\nnode score: 0.9351222826086957\n\n\n\n29096->29091\n\n\n\n\n\n29090\n\n \nnoodle\nnode score:1.0\n\n\n\n29096->29090\n\n\n\n\n\n29092\n\n \nonion\nnode score:1.0\n\n\n\n29091->29092\n\n\n\n\n\n29085\n\n \nmix\nnode score: 0.3944813826951458\n\n\n\n29084->29085\n\n\n\n\n\n29088\n\n \npepper\nnode score:1.0\n\n\n\n29085->29088\n\n\n\n\n\n29087\n\n \nsalt\nnode score:1.0\n\n\n\n29085->29087\n\n\n\n\n\n29086\n\n \nbacon\nnode score:1.0\n\n\n\n29085->29086\n\n\n\n\n\n29089\n\n \ntomato\nnode score:1.0\n\n\n\n29085->29089\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.1713840220963046"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29967\n\n \nmix\nnode score: 0.0\n\n\n\n29970\n\n \nnoodle\nnode score:0\n\n\n\n29967->29970\n\n\n\n\n\n29968\n\n \ncook\nnode score: 1.0\n\n\n\n29967->29968\n\n\n\n\n\n29971\n\n \nsimmer\nnode score: 0.41806154430428866\n\n\n\n29967->29971\n\n\n\n\n\n29969\n\n \nonion\nnode score:1.0\n\n\n\n29968->29969\n\n\n\n\n\n29972\n\n \nmix\nnode score: 0.3944813826951458\n\n\n\n29971->29972\n\n\n\n\n\n29973\n\n \nsalt\nnode score:1.0\n\n\n\n29972->29973\n\n\n\n\n\n29976\n\n \nbacon\nnode score:1.0\n\n\n\n29972->29976\n\n\n\n\n\n29974\n\n \npepper\nnode score:1.0\n\n\n\n29972->29974\n\n\n\n\n\n29975\n\n \ntomato\nnode score:1.0\n\n\n\n29972->29975\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.1659860909419401"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n28699\n\n \nmix\nnode score: 0.0\n\n\n\n28690\n\n \nheat\nnode score: 0.9351222826086957\n\n\n\n28699->28690\n\n\n\n\n\n28696\n\n \nnoodle\nnode score:0\n\n\n\n28699->28696\n\n\n\n\n\n28698\n\n \nsimmer\nnode score: 0.4180615443042887\n\n\n\n28699->28698\n\n\n\n\n\n28691\n\n \nonion\nnode score:1.0\n\n\n\n28690->28691\n\n\n\n\n\n28700\n\n \nmix\nnode score: 0.3944813826951458\n\n\n\n28698->28700\n\n\n\n\n\n28692\n\n \nsalt\nnode score:1.0\n\n\n\n28700->28692\n\n\n\n\n\n28695\n\n \npepper\nnode score:1.0\n\n\n\n28700->28695\n\n\n\n\n\n28693\n\n \ntomato\nnode score:1.0\n\n\n\n28700->28693\n\n\n\n\n\n28694\n\n \nbacon\nnode score:1.0\n\n\n\n28700->28694\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.16337906864086424"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29574\n\n \nmix\nnode score: 0.0\n\n\n\n29575\n\n \ncook\nnode score: 1.0\n\n\n\n29574->29575\n\n\n\n\n\n29580\n\n \nwhip\nnode score: 0.0026102706158107817\n\n\n\n29574->29580\n\n\n\n\n\n29576\n\n \nmix\nnode score: 0.0\n\n\n\n29575->29576\n\n\n\n\n\n29577\n\n \nheat\nnode score: 0.9351222826086957\n\n\n\n29576->29577\n\n\n\n\n\n29579\n\n \nnoodle\nnode score:1.0\n\n\n\n29576->29579\n\n\n\n\n\n29578\n\n \nonion\nnode score:1.0\n\n\n\n29577->29578\n\n\n\n\n\n29588\n\n \nmix\nnode score: 0.0\n\n\n\n29580->29588\n\n\n\n\n\n29584\n\n \nbacon\nnode score:1.0\n\n\n\n29588->29584\n\n\n\n\n\n29587\n\n \nplace\nnode score: 0.39444081146694154\n\n\n\n29588->29587\n\n\n\n\n\n29589\n\n \nmix\nnode score: 0.5773519979266957\n\n\n\n29587->29589\n\n\n\n\n\n29582\n\n \npepper\nnode score:1.0\n\n\n\n29589->29582\n\n\n\n\n\n29583\n\n \nsalt\nnode score:1.0\n\n\n\n29589->29583\n\n\n\n\n\n29585\n\n \ntomato\nnode score:1.0\n\n\n\n29589->29585\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.1579882299064732"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29552\n\n \nheat\nnode score: 0.7713748266936734\n\n\n\n29553\n\n \nmix\nnode score: 0.25828931697593543\n\n\n\n29552->29553\n\n\n\n\n\n29559\n\n \npepper\nnode score:1.0\n\n\n\n29553->29559\n\n\n\n\n\n29561\n\n \ncook\nnode score: 1.0\n\n\n\n29553->29561\n\n\n\n\n\n29554\n\n \ntomato\nnode score:1.0\n\n\n\n29553->29554\n\n\n\n\n\n29555\n\n \nsalt\nnode score:1.0\n\n\n\n29553->29555\n\n\n\n\n\n29558\n\n \nonion\nnode score:1.0\n\n\n\n29553->29558\n\n\n\n\n\n29557\n\n \nbacon\nnode score:1.0\n\n\n\n29553->29557\n\n\n\n\n\n29556\n\n \nnoodle\nnode score:1.0\n\n\n\n29561->29556\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.15466055286561975"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29590\n\n \nmix\nnode score: 0.0\n\n\n\n29593\n\n \nnoodle\nnode score:0\n\n\n\n29590->29593\n\n\n\n\n\n29591\n\n \nheat\nnode score: 0.9351222826086957\n\n\n\n29590->29591\n\n\n\n\n\n29594\n\n \nsimmer\nnode score: 0.4180615443042887\n\n\n\n29590->29594\n\n\n\n\n\n29592\n\n \nonion\nnode score:1.0\n\n\n\n29591->29592\n\n\n\n\n\n29602\n\n \nmix\nnode score: 0.07341265771678888\n\n\n\n29594->29602\n\n\n\n\n\n29601\n\n \nheat\nnode score: 1.0\n\n\n\n29602->29601\n\n\n\n\n\n29599\n\n \nbacon\nnode score:1.0\n\n\n\n29602->29599\n\n\n\n\n\n29596\n\n \nsalt\nnode score:1.0\n\n\n\n29602->29596\n\n\n\n\n\n29603\n\n \nmix\nnode score: 0.19925133063788839\n\n\n\n29601->29603\n\n\n\n\n\n29598\n\n \ntomato\nnode score:1.0\n\n\n\n29603->29598\n\n\n\n\n\n29597\n\n \npepper\nnode score:1.0\n\n\n\n29603->29597\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.15272938996506372"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n29059\n\n \nheat\nnode score: 0.7713748266936734\n\n\n\n29069\n\n \nmix\nnode score: 0.29412064068068344\n\n\n\n29059->29069\n\n\n\n\n\n29068\n\n \nslice\nnode score: 0.1757654159815022\n\n\n\n29069->29068\n\n\n\n\n\n29062\n\n \nsalt\nnode score:1.0\n\n\n\n29069->29062\n\n\n\n\n\n29066\n\n \npepper\nnode score:1.0\n\n\n\n29069->29066\n\n\n\n\n\n29070\n\n \nmix\nnode score: 0.06751022103892917\n\n\n\n29068->29070\n\n\n\n\n\n29063\n\n \nnoodle\nnode score:1.0\n\n\n\n29070->29063\n\n\n\n\n\n29064\n\n \nbacon\nnode score:1.0\n\n\n\n29070->29064\n\n\n\n\n\n29065\n\n \nonion\nnode score:1.0\n\n\n\n29070->29065\n\n\n\n\n\n29061\n\n \ntomato\nnode score:1.0\n\n\n\n29070->29061\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":"0.14929647567328216"},"metadata":{},"output_type":"display_data"},{"data":{"image/svg+xml":"\n\n\n\n\n\n%3\n\n\n\n30129\n\n \ncook\nnode score: 0.851970571945862\n\n\n\n30131\n\n \nmix\nnode score: 0.25828931697593543\n\n\n\n30129->30131\n\n\n\n\n\n30137\n\n \ntomato\nnode score:1.0\n\n\n\n30131->30137\n\n\n\n\n\n30132\n\n \nonion\nnode score:1.0\n\n\n\n30131->30132\n\n\n\n\n\n30134\n\n \npepper\nnode score:1.0\n\n\n\n30131->30134\n\n\n\n\n\n30135\n\n \ncook\nnode score: 1.0\n\n\n\n30131->30135\n\n\n\n\n\n30133\n\n \nsalt\nnode score:1.0\n\n\n\n30131->30133\n\n\n\n\n\n30138\n\n \nbacon\nnode score:1.0\n\n\n\n30131->30138\n\n\n\n\n\n30136\n\n \nnoodle\nnode score:0.5\n\n\n\n30135->30136\n\n\n\n\n\n","text/plain":""},"metadata":{},"output_type":"display_data"}],"source":"p.plot_population()"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":""}],"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}} \ No newline at end of file +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Evolutionary Algorithm" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "the Evolutionary Algorithm that is supposed to create new recipes based on the Recipe Matrices that are created during the *Recipe Analysis* step.\n", + "\n", + "The Population of the Evolutional Algorithm consists of a set of recipe trees. Each Recipe Tree consists of several Nodes where each node is of one of the following Types:\n", + "\n", + "* **Ingredient Node:**\n", + " these are the leaf nodes. Containing an ingredient. The score is determined by the actions, that are applied if you follow up the path. At the Moment it measures how many duplicate actions are applied.\n", + "* **Action Node:**\n", + " An Action that is applied on it's child and this child's subtree. Score indicates the average likelihood that this action is applied on the ingredients inside the subtree\n", + "* **Mix Node:**\n", + " Mixing ingredients together. This is also the only Node that can have more than one child. The score is the average of all pairwise likelihoods that two ingredients are mixed togethter" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "sys.path.append(\"../\")\n", + "sys.path.append(\"../RecipeAnalysis/\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": " \n " + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": " \n " + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import settings\n", + "\n", + "import pycrfsuite\n", + "\n", + "import json\n", + "\n", + "import db.db_settings as db_settings\n", + "from db.database_connection import DatabaseConnection\n", + "\n", + "from Tagging.conllu_generator import ConlluGenerator\n", + "from Tagging.crf_data_generator import *\n", + "\n", + "from RecipeAnalysis.Recipe import Ingredient\n", + "\n", + "import ea_tools\n", + "\n", + "from difflib import SequenceMatcher\n", + "\n", + "import numpy as np\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", + "init_notebook_mode(connected=True)\n", + "\n", + "from graphviz import Digraph\n", + "\n", + "import itertools\n", + "\n", + "import random\n", + "\n", + "import plotly.io as pio\n", + "pio.renderers.default = \"jupyterlab\"\n", + "\n", + "from IPython.display import Markdown, HTML, display\n", + "\n", + "from copy import deepcopy" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def gaussian(x, mu, sig):\n", + " return 1./(np.sqrt(2.*np.pi)*sig)*np.exp(-np.power((x - mu)/sig, 2.)/2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## load adjacency matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "import dill\n", + "m_act = dill.load(open(\"../RecipeAnalysis/m_act.dill\", \"rb\"))\n", + "m_mix = dill.load(open(\"../RecipeAnalysis/m_mix.dill\", \"rb\"))\n", + "m_base_act = dill.load(open(\"../RecipeAnalysis/m_base_act.dill\", \"rb\"))\n", + "m_base_mix = dill.load(open(\"../RecipeAnalysis/m_base_mix.dill\", \"rb\"))\n", + "\n", + "#m_act.apply_threshold(3)\n", + "#m_mix.apply_threshold(3)\n", + "#m_base_act.apply_threshold(5)\n", + "#m_base_mix.apply_threshold(5)\n", + "\n", + "\n", + "#c_act = m_act.get_csr()\n", + "#c_mix = m_mix.get_csr()\n", + "#c_base_act = m_base_act.get_csr()\n", + "#c_base_mix = m_base_mix.get_csr()\n", + "\n", + "c_act = m_act._csr\n", + "c_mix = m_mix._csr\n", + "c_base_act = m_base_act._csr\n", + "c_base_mix = m_base_mix._csr\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "actions = m_act.get_labels()[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "base_ingredients = m_base_mix.get_labels()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "sym_label_buffer = {}\n", + "fw_label_buffer = {}\n", + "bw_label_buffer = {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### helper functions for adjacency matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def get_sym_adjacent(key, m, c):\n", + " index = m._label_index[key]\n", + " i1 = c[index,:].nonzero()[1]\n", + " i2 = c[:,index].nonzero()[0]\n", + " \n", + " i = np.concatenate((i1,i2))\n", + " \n", + " if m in sym_label_buffer:\n", + " names = sym_label_buffer[m][i]\n", + " else:\n", + " names = np.array(m.get_labels())\n", + " sym_label_buffer[m] = names\n", + " names = names[i]\n", + " \n", + " counts = np.concatenate((c[index, i1].toarray().flatten(), c[i2, index].toarray().flatten()))\n", + " \n", + " s = np.argsort(-counts)\n", + " \n", + " return names[s], counts[s]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def get_forward_adjacent(key, m, c):\n", + " index = m._x_label_index[key]\n", + " i = c[index,:].nonzero()[1]\n", + " \n", + " if m in fw_label_buffer:\n", + " names = fw_label_buffer[m][i]\n", + " else:\n", + " names = np.array(m._y_labels)\n", + " fw_label_buffer[m] = names\n", + " names = names[i]\n", + " \n", + " \n", + " counts = c[index, i].toarray().flatten()\n", + " \n", + " s = np.argsort(-counts)\n", + " \n", + " return names[s], counts[s]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def get_backward_adjacent(key, m, c):\n", + " index = m._y_label_index[key]\n", + " i = c[:,index].nonzero()[0]\n", + " \n", + " if m in bw_label_buffer:\n", + " names = bw_label_buffer[m][i]\n", + " else:\n", + " names = np.array(m._x_labels)\n", + " bw_label_buffer[m] = names\n", + " names = names[i]\n", + " \n", + " \n", + " counts = c[i, index].toarray().flatten()\n", + " \n", + " s = np.argsort(-counts)\n", + " \n", + " return names[s], counts[s]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": "(array(['heat', 'simmer', 'cook', 'boil', 'bake', 'place', 'slice', 'cut',\n 'chop', 'cool', 'dice', 'refrigerate', 'pour', 'drain', 'brown',\n 'warm', 'blend', 'chill', 'spread', 'thicken', 'grill', 'saute',\n 'peel', 'fry', 'mash', 'whisk', 'break', 'freeze', 'melt'],\n dtype=' 1 and p._single_child)\n", + " \n", + " for c in childs:\n", + " self.remove_child(c)\n", + " \n", + " if p is not None:\n", + " p.remove_child(self)\n", + " \n", + " if self._single_child and self._child is not None and p._name == self._child._name:\n", + " # two adjacent nodes with same name would remain after deletion.\n", + " # merge them! (by adding the child's childs to our parent instead of our childs)\n", + " childs = self._child.childs()\n", + " self._child.remove()\n", + " \n", + " \n", + " for c in childs:\n", + " p.add_child(c)\n", + " \n", + " def insert_before(self, n):\n", + " p = self._parent\n", + " if p is not None:\n", + " p.remove_child(self)\n", + " p.add_child(n)\n", + " n.add_child(self)\n", + " \n", + " def mutate(self):\n", + " n_node = self.n_node_mutate_options()\n", + " n_edge = self.n_edge_mutate_options()\n", + " \n", + " choice = random.choice(range(n_node + n_edge))\n", + " if choice < n_node:\n", + " self.mutate_node()\n", + " else:\n", + " self.mutate_edges()\n", + " \n", + " def mutate_edges(self):\n", + " ings = self.traverse_ingredients()\n", + " ing = random.choice(ings)\n", + " \n", + " a, w = get_backward_adjacent(ing._base_ingredient, m_base_act, c_base_act)\n", + " \n", + " action = random.choices(a, w)[0]\n", + " self.insert_before(ActionNode(action))\n", + " \n", + " def mutate_node(self):\n", + " raise NotImplementedError\n", + " \n", + " def n_node_mutate_options(self):\n", + " \n", + " return 0 if self._constant else 1\n", + " \n", + " def n_edge_mutate_options(self):\n", + " n = 1 if self._parent is not None else 0\n", + " return n\n", + " \n", + " def n_mutate_options(self):\n", + " return self.n_edge_mutate_options() + self.n_node_mutate_options()\n", + " \n", + " def dot_node(self, dot):\n", + " raise NotImplementedError()\n", + " \n", + " def dot(self, d=None):\n", + " if d is None:\n", + " d = Digraph()\n", + " self.dot_node(d)\n", + " \n", + " else:\n", + " self.dot_node(d)\n", + " if self._parent is not None:\n", + " d.edge(self._parent._id, self._id)\n", + " \n", + " \n", + " for c in self.childs():\n", + " c.dot(d)\n", + " \n", + " return d\n", + " \n", + " def serialize(self):\n", + " r = {}\n", + " r['type'] = str(self.__class__.__name__)\n", + " r['id'] = self._id\n", + " r['parent'] = self._parent._id if self._parent is not None else None\n", + " r['name'] = self._name\n", + " r['childs'] = [c._id for c in self.childs()]\n", + " r['constant'] = self._constant\n", + " r['single_child'] = self._single_child\n", + " \n", + " return r\n", + " \n", + " def node_score(self):\n", + " raise NotImplementedError()\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Mix Node" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "class MixNode(RecipeTreeNode):\n", + " def __init__(self, constant=False):\n", + " super().__init__(\"mix\", constant, single_child=False)\n", + " \n", + " def dot_node(self, dot):\n", + " dot.node(self._id, label=f\"< {self._name}
node score: {self.node_score():.4f}>\", shape=\"diamond\", style=\"filled\", color=\"#d5e8d4\")\n", + " \n", + " def split(self, set_above, set_below, node_between):\n", + " assert len(set_above.difference(self.childs())) == 0\n", + " assert len(set_below.difference(self.childs())) == 0\n", + " \n", + " n_above = MixNode()\n", + " n_below = MixNode()\n", + " \n", + " p = self.parent()\n", + " \n", + " for c in self.childs().copy():\n", + " self.remove_child(c)\n", + " self.remove()\n", + " \n", + " for c in set_below:\n", + " n_below.add_child(c)\n", + " \n", + " for c in set_above:\n", + " n_above.add_child(c)\n", + " \n", + " n_above.add_child(node_between)\n", + " node_between.add_child(n_below)\n", + " \n", + " if p is not None:\n", + " p.add_child(n_above)\n", + " \n", + " # test whether the mix nodes are useless\n", + " if len(n_above.childs()) == 1:\n", + " n_above.remove()\n", + " \n", + " if len(n_below.childs()) == 1:\n", + " n_below.remove()\n", + " \n", + " def n_node_mutate_options(self):\n", + " return 0 if self._constant or len(self.childs()) <= 2 else len(self.childs())\n", + " \n", + " def mutate_node(self):\n", + " \n", + " childs = self.childs()\n", + " \n", + " if len(childs) <= 2:\n", + " print(\"Warning: cannot modify mix node\")\n", + " return\n", + " \n", + " childs = random.sample(childs, len(childs))\n", + " \n", + " n = random.choice(range(1, len(childs)-1))\n", + " \n", + " between_node = ActionNode(random.choice(actions))\n", + " \n", + " self.split(set(childs[:n]), set(childs[n:]), between_node)\n", + " \n", + " \n", + " def node_score(self):\n", + " child_ingredients = [c.traverse_ingredients() for c in self.childs()]\n", + " \n", + " tmp_set = set()\n", + " cumulative_sets = []\n", + " \n", + " pairwise_tuples = []\n", + " \n", + " for c in child_ingredients:\n", + " if len(tmp_set) > 0:\n", + " cumulative_sets.append(tmp_set)\n", + " pairwise_tuples += [x for x in itertools.product(tmp_set, c)]\n", + " tmp_set = tmp_set.union(set(c))\n", + " \n", + " s_base = 0\n", + " s = 0\n", + " \n", + " for ing_a, ing_b in pairwise_tuples:\n", + " try:\n", + " #s_base += sym_score(ing_a._base_ingredient, ing_b._base_ingredient, m_base_mix, c_base_mix)\n", + " \n", + " #s += sym_score(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)\n", + " p1 = sym_p_a_given_b(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)\n", + " p2 = sym_p_a_given_b(ing_b.to_json(), ing_a.to_json(), m_mix, c_mix)\n", + " \n", + " s += 0.5 * p1 + 0.5 * p2\n", + " \n", + " except:\n", + " pass\n", + " \n", + " #s_base /= len(pairwise_tuples)\n", + " s /= len(pairwise_tuples)\n", + " \n", + " #return 0.5 * (s_base + s)\n", + " return s\n", + " \n", + " \n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Ingredient Node Class" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "class IngredientNode(RecipeTreeNode):\n", + " def __init__(self, name, constant=False):\n", + " super().__init__(name, constant, single_child=True)\n", + " \n", + " def get_actions(self):\n", + " a_list = []\n", + " n = self.parent()\n", + " while n is not None:\n", + " if type(n) == ActionNode:\n", + " a_list.append(n.name())\n", + " n = n.parent()\n", + " return a_list\n", + " \n", + " def mutate_node(self):\n", + " self._name = random.choice(base_ingredients)\n", + " #TODO: change w.r.t. mixing probabilities \n", + " \n", + " def traverse_ingredients(self):\n", + " return [Ingredient(self._name)]\n", + " \n", + " def node_score(self):\n", + " actions = self.get_actions()\n", + " \n", + " if len(actions) == 0:\n", + " if p_ingredient_unprepared(self._name) < 0.2:\n", + " return 0\n", + " return 1\n", + " \n", + " seen_actions = set()\n", + " n_duplicates = 0\n", + " for act in actions:\n", + " if act in seen_actions:\n", + " n_duplicates += 1\n", + " else:\n", + " seen_actions.add(act)\n", + " \n", + " duplicate_actions_score = len(seen_actions) / len(actions)\n", + " \n", + " return duplicate_actions_score\n", + " \n", + " \n", + " def dot_node(self, dot):\n", + " dot.node(self._id, label=f\"< {self._name}
node score:{self.node_score():.4f}>\", shape=\"box\", style=\"filled\", color=\"#ffe6cc\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Action Node Class" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "class ActionNode(RecipeTreeNode):\n", + " def __init__(self, name, constant=False):\n", + " super().__init__(name, constant, single_child=True)\n", + " \n", + " def n_node_mutate_options(self):\n", + " # beacause we can change or remove ourselve!\n", + " return 0 if self._constant else 2 \n", + " def mutate_node(self):\n", + " if random.choice(range(2)) == 0:\n", + " # change action\n", + " self._name = random.choice(actions)\n", + " else:\n", + " # delete\n", + " self.remove()\n", + " \n", + " def traverse_ingredients(self):\n", + " ingredient_set = super().traverse_ingredients()\n", + " for ing in ingredient_set:\n", + " ing.apply_action(self._name)\n", + " \n", + " return ingredient_set\n", + " \n", + " def node_score(self):\n", + " ings = self.child().traverse_ingredients()\n", + " \n", + " s = 0\n", + " \n", + " for ing in ings:\n", + " try:\n", + " #score = asym_score(self._name, ing.to_json(), m_act, c_act)\n", + " #base_score = asym_score(self._name, ing._base_ingredient, m_base_act, c_base_act)\n", + " \n", + " score = fw_p_a_given_b(self._name, ing._base_ingredient, m_base_act, c_base_act)\n", + " \n", + " s += score\n", + " except KeyError as e:\n", + " pass\n", + " \n", + " \n", + " return s / len(ings)\n", + " \n", + " def dot_node(self, dot):\n", + " dot.node(self._id, label=f\"< {self._name}
node score: {self.node_score():.4f}>\", shape=\"ellipse\", style=\"filled\", color=\"#dae8fc\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tree Class" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "class Tree(object):\n", + " @staticmethod\n", + " def build_initial_tree(ingredients: list):\n", + " # get action sets for ingredients\n", + " possible_actions = {}\n", + " for ing in ingredients:\n", + " action_set, action_weights = m_base_act.get_backward_adjacent(ing)\n", + " possible_actions[ing] = set(action_set.tolist()[:5])\n", + " \n", + " # now find actions with the same subset\n", + " \n", + " ings_for_acts = {}\n", + " \n", + " for ing, acts in possible_actions.items():\n", + " for a in acts:\n", + " if a not in ings_for_acts:\n", + " ings_for_acts[a] = set()\n", + " \n", + " ings_for_acts[a].add(ing)\n", + " \n", + " # now looking for the largest subset and choose one of them randomly\n", + " \n", + " action_keys = np.array(list(ings_for_acts.keys()))\n", + " set_lengths = np.array([len(ings_for_acts[a]) for a in action_keys])\n", + " \n", + " # sort lengths\n", + " sorted_length_indices = np.argsort(-set_lengths)\n", + " \n", + " # now perform the following steps:\n", + " # * go through all unprocessed ingredients\n", + " # * for each ing: find largest action set that is suitable\n", + " # * perform this action on all it's ingredients.\n", + " # * continue until no ingredient is left\n", + " \n", + " unprocessed_ings = set(ingredients)\n", + " unprocessed_actions = set(ings_for_acts.keys())\n", + " highest_node_for_ingredient = {}\n", + " \n", + " \n", + " \n", + " # create ingredient nodes:\n", + " for ing in ingredients:\n", + " highest_node_for_ingredient[ing] = IngredientNode(ing, constant=True)\n", + " \n", + " while len(unprocessed_ings) > 0:\n", + " # select random ingredient:\n", + " ing = np.random.choice(list(unprocessed_ings))\n", + " \n", + " sorted_actions = action_keys[sorted_length_indices]\n", + " selected_action = None\n", + " \n", + " for action in sorted_actions:\n", + " if ing in ings_for_acts[action]:\n", + " selected_action = action\n", + " break\n", + " \n", + " # found best action. apply it to all matching ingredients\n", + " if selected_action is not None:\n", + " matching_ingredients = ings_for_acts[selected_action]\n", + " \n", + " \n", + " if len(matching_ingredients) == 1:\n", + " ing = list(matching_ingredients)[0]\n", + " ing_node = highest_node_for_ingredient[ing]\n", + " action_node = ActionNode(selected_action)\n", + " action_node.add_child(ing_node)\n", + " unprocessed_ings.remove(ing)\n", + " highest_node_for_ingredient[ing] = action_node\n", + " display(action_node.dot())\n", + " \n", + " else:\n", + " \n", + " nodes_to_mix = set()\n", + "\n", + " mix_node = MixNode()\n", + " action_node = ActionNode(selected_action)\n", + " action_node.add_child(mix_node)\n", + " \n", + " for ing in matching_ingredients:\n", + " nodes_to_mix.add(highest_node_for_ingredient[ing])\n", + "\n", + " # update reference to highest node in tree\n", + " highest_node_for_ingredient[ing] = action_node\n", + " \n", + " if ing in unprocessed_ings:\n", + " unprocessed_ings.remove(ing)\n", + "\n", + " for node in nodes_to_mix:\n", + " mix_node.add_child(node)\n", + " #display(action_node.dot())\n", + " \n", + " root_layer = set(highest_node_for_ingredient.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", + " return root_layer_without_parents[0]\n", + " \n", + " root_node = MixNode()\n", + " for r in root_layer_without_parents:\n", + " root_node.add_child(r)\n", + " \n", + " return root_node\n", + " \n", + " \n", + " \n", + "\n", + " @staticmethod\n", + " def from_ingredients(ingredients: list):\n", + " root = MixNode()\n", + " \n", + " for ing in ingredients:\n", + " root.add_child(IngredientNode(ing, constant=True))\n", + " \n", + " return Tree(root)\n", + " \n", + " @staticmethod\n", + " def from_serialization(s):\n", + " def empty_node(raw_n):\n", + " if raw_n['type'] == \"MixNode\":\n", + " node = MixNode(raw_n['constant'])\n", + " elif raw_n['type'] == \"IngredientNode\":\n", + " node = IngredientNode(raw_n['name'], raw_n['constant'])\n", + " elif raw_n['type'] == \"ActionNode\":\n", + " node = ActionNode(raw_n['name'], raw_n['constant'])\n", + " else:\n", + " print(\"unknown node detected\")\n", + " return\n", + " \n", + " return node\n", + " \n", + " nodes = {}\n", + " for n in s:\n", + " nodes[n['id']] = empty_node(n)\n", + " \n", + " for n in s:\n", + " childs = n['childs']\n", + " id = n['id']\n", + " for c in childs:\n", + " nodes[id].add_child(nodes[c])\n", + " \n", + " return Tree(nodes[s[0]['id']])\n", + " \n", + " \n", + " def __init__(self, root):\n", + " # create a dummy entry node\n", + " self._root = RecipeTreeNode(\"root\", single_child=True)\n", + " self._root.add_child(root)\n", + " \n", + " def root(self):\n", + " return self._root.child()\n", + " \n", + " def mutate(self):\n", + " nodes = self.root().traverse()\n", + " weights = [n.n_mutate_options() for n in nodes]\n", + " \n", + " n = random.choices(nodes, weights)[0]\n", + " \n", + " n.mutate()\n", + " \n", + " def dot(self):\n", + " return self.root().dot()\n", + " \n", + " def serialize(self):\n", + " return [n.serialize() for n in self.root().traverse()]\n", + " \n", + " def structure_score(self):\n", + " n_duplicates = 0\n", + " \n", + " \n", + " def collect_scores(self):\n", + " self._mix_scores = []\n", + " self._act_scores = []\n", + " self._ing_scores = []\n", + " \n", + " nodes = self.root().traverse()\n", + " self._n_mix_nodes = 0\n", + " self._n_act_nodes = 0\n", + " self._n_ing_nodes = 0\n", + " \n", + " s = 0\n", + " for n in nodes:\n", + " if type(n) == MixNode:\n", + " self._mix_scores.append(n.node_score())\n", + " self._n_mix_nodes += 1\n", + " if type(n) == ActionNode:\n", + " self._act_scores.append(n.node_score())\n", + " self._n_act_nodes += 1\n", + " if type(n) == IngredientNode:\n", + " self._ing_scores.append(n.node_score())\n", + " self._n_ing_nodes += 1\n", + " \n", + " self._n_duplicates = 0\n", + " seen_actions = set()\n", + " \n", + " for n in nodes:\n", + " if type(n) == ActionNode:\n", + " if n.name() in seen_actions:\n", + " self._n_duplicates += 1\n", + " else:\n", + " seen_actions.add(n.name())\n", + " \n", + " self._mix_scores = np.array(self._mix_scores)\n", + " self._act_scores = np.array(self._act_scores)\n", + " self._ing_scores = np.array(self._ing_scores)\n", + " \n", + " \n", + " def mix_scores(self):\n", + " return self._mix_scores\n", + " \n", + " def action_scores(self):\n", + " return self._act_scores\n", + " \n", + " def ing_scores(self):\n", + " return self._ing_scores\n", + " \n", + " def bounds_mix_scores(self):\n", + " \n", + " nonzeros = self._mix_scores[self._mix_scores > 0]\n", + " if len(nonzeros) > 0:\n", + " mmax = np.max(nonzeros)\n", + " mmin = np.min(nonzeros)\n", + " return mmin, mmax\n", + " else:\n", + " return None, None\n", + " \n", + " def bounds_act_scores(self):\n", + " \n", + " if len(self._act_scores) == 0:\n", + " return None, None\n", + " nonzeros = self._act_scores[self._act_scores > 0]\n", + " if len(nonzeros) > 0:\n", + " mmax = np.max(nonzeros)\n", + " mmin = np.min(nonzeros)\n", + " return mmin, mmax\n", + " else:\n", + " return None, None\n", + " \n", + " def normalized_mix_scores(self, min, max):\n", + " if (max != min):\n", + " normalized = (self._mix_scores - min)/(max-min)\n", + " normalized[normalized <= 0] = 0\n", + " return normalized\n", + " else:\n", + " return None\n", + " \n", + " def normalized_act_scores(self, min, max):\n", + " if len(self._act_scores) == 0 or max == min:\n", + " return None\n", + " normalized = (self._act_scores - min)/(max-min)\n", + " normalized[normalized <= 0] = 0\n", + " return normalized\n", + " \n", + " def copy(self):\n", + " return Tree.from_serialization(self.serialize())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "base_ings = [\"noodle\", \"bacon\", \"salt\", \"pepper\", \"tomato\", \"onion\"]\n", + "#test_ings = [Ingredient(i) for i in base_ings]\n", + "\n", + "#test_ings[0].__dir__()\n", + "\n", + "n = Tree.build_initial_tree(base_ings)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": "\n\n\n\n\n\n%3\n\n\n\n7\n\n \nheat\nnode score: 0.7714\n\n\n\n6\n\n \nmix\nnode score: 0.2565\n\n\n\n7->6\n\n\n\n\n\n5\n\n \nonion\nnode score:1.0000\n\n\n\n6->5\n\n\n\n\n\n3\n\n \npepper\nnode score:1.0000\n\n\n\n6->3\n\n\n\n\n\n1\n\n \nbacon\nnode score:1.0000\n\n\n\n6->1\n\n\n\n\n\n4\n\n \ntomato\nnode score:1.0000\n\n\n\n6->4\n\n\n\n\n\n0\n\n \nnoodle\nnode score:1.0000\n\n\n\n6->0\n\n\n\n\n\n2\n\n \nsalt\nnode score:1.0000\n\n\n\n6->2\n\n\n\n\n\n", + "text/plain": "" + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "n.dot()" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": "(array(['cook', 'bake', 'drain', 'heat', 'place', 'boil', 'cut', 'pour'],\n dtype=' self._scores[i_b]:\n", + " new_population.append(self.population[i_a])\n", + " else:\n", + " new_population.append(self.population[i_b])\n", + " \n", + " self.population = new_population\n", + " \n", + " def hold_best(self, n=10):\n", + " sorted_indices = np.argsort(-self._scores)\n", + " \n", + " self.population = np.array(self.population)[sorted_indices[:n]].tolist()\n", + " \n", + " def analyse_scores(self):\n", + " for tree in self.population:\n", + " min, max = tree.bounds_mix_scores()\n", + " if min is not None and max is not None:\n", + " if self._mix_min is None or min < self._mix_min:\n", + " self._mix_min = min\n", + " if self._mix_max is None or max > self._mix_max:\n", + " self._mix_max = max\n", + " \n", + " min, max = tree.bounds_act_scores()\n", + " if min is not None and max is not None:\n", + " if self._act_min is None or min < self._act_min:\n", + " self._act_min = min\n", + " if self._act_max is None or max > self._act_max:\n", + " self._act_max = max\n", + " \n", + " def single_score(self, mix_scores, act_scores, ing_scores):\n", + " if mix_scores is None or act_scores is None or ing_scores is None:\n", + " return 0\n", + " score = (0.5 * np.average(mix_scores) + 0.5 * np.average(act_scores)) * np.average(ing_scores)\n", + " # judging also how many actions we have. So far use a gaussian with mean at number of ingredients\n", + " \n", + " score *= gaussian(len(act_scores), len(mix_scores), 1)\n", + " return score\n", + " \n", + " \n", + " \n", + " def collect_scores(self):\n", + " for tree in self.population:\n", + " tree.collect_scores()\n", + " \n", + " self.analyse_scores()\n", + " \n", + " if self._mix_min is not None and self._mix_max is not None:\n", + " self._mix_scores = [t.normalized_mix_scores(self._mix_min, self._mix_max) for t in self.population]\n", + " else:\n", + " # if no normalization can be done, all values are the same or 0.\n", + " # in this case just fill in zeros as score\n", + " self._mix_scores = [np.zeros(shape=t._mix_scores.shape) for t in self.population]\n", + " \n", + " if self._act_min is not None and self._act_max is not None:\n", + " self._act_scores = [t.normalized_act_scores(self._act_min, self._act_max) for t in self.population]\n", + " else:\n", + " self._act_scores = [np.zeros(shape=t._act_scores) for t in self.population]\n", + " \n", + " self._scores = []\n", + " for i in range(len(self._mix_scores)):\n", + " #print (self._mix_scores[i], self._act_scores[i])\n", + " if self._act_scores is None or self._mix_scores is None or self._act_scores[i] is None:\n", + " self._scores.append(0)\n", + " continue\n", + " \n", + " s = self.single_score(self._mix_scores[i], self._act_scores[i], self.population[i].ing_scores())\n", + " self._scores.append(s)\n", + " self._scores = np.array(self._scores)\n", + " \n", + " def run(self, n=50):\n", + " for i in range(n):\n", + " print(i)\n", + " self.mutate()\n", + " self.mutate()\n", + " self.collect_scores()\n", + " #self.pairwise_competition()\n", + " #self.collect_scores()\n", + " self.hold_best(self._n)\n", + " \n", + " \n", + " \n", + " def plot_population(self):\n", + " self.collect_scores()\n", + " #print(self._mix_scores)\n", + " #print(self._act_scores)\n", + " #print(self._scores)\n", + " for i, t in enumerate(self.population):\n", + " display(self._scores[i])\n", + " display(t.root().dot())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Run Evolutionary Algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "p = Population([\"noodle\", \"bacon\", \"salt\", \"pepper\", \"tomato\", \"onion\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "#p_ingredient_unprepared(list(p.population[0].root().childs())[0]._name) < 0.2" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "p.run(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.15600101279542286" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "152\n", + "\n", + " \n", + "mix\n", + "node score: 0.2583\n", + "\n", + "\n", + "\n", + "154\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->154\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "153\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->153\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "158\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "152->158\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "157\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->157\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "155\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "152->155\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "160\n", + "\n", + " \n", + "cook\n", + "node score: 1.0000\n", + "\n", + "\n", + "\n", + "152->160\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "156\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "160->156\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.15600101279542283" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "263\n", + "\n", + " \n", + "mix\n", + "node score: 0.2583\n", + "\n", + "\n", + "\n", + "265\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "263->265\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "264\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "263->264\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "269\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "263->269\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "267\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "263->267\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "268\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "263->268\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "271\n", + "\n", + " \n", + "cook\n", + "node score: 1.0000\n", + "\n", + "\n", + "\n", + "263->271\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "266\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "271->266\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.14231469422656626" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "367\n", + "\n", + " \n", + "cool\n", + "node score: 0.2416\n", + "\n", + "\n", + "\n", + "358\n", + "\n", + " \n", + "mix\n", + "node score: 0.2583\n", + "\n", + "\n", + "\n", + "367->358\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "364\n", + "\n", + " \n", + "cook\n", + "node score: 1.0000\n", + "\n", + "\n", + "\n", + "358->364\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "363\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "358->363\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "362\n", + "\n", + " \n", + "onion\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "358->362\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "361\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "358->361\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "360\n", + "\n", + " \n", + "bacon\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "358->360\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "359\n", + "\n", + " \n", + "tomato\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "358->359\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "365\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "364->365\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.13079458634574578" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "291\n", + "\n", + " \n", + "cook\n", + "node score: 0.8520\n", + "\n", + "\n", + "\n", + "282\n", + "\n", + " \n", + "mix\n", + "node score: 0.2421\n", + "\n", + "\n", + "\n", + "291->282\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "283\n", + "\n", + " \n", + "cut\n", + "node score: 0.2756\n", + "\n", + "\n", + "\n", + "282->283\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "289\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "282->289\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "288\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "282->288\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "287\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "282->287\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "286\n", + "\n", + " \n", + "onion\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "282->286\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "285\n", + "\n", + " \n", + "bacon\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "282->285\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "284\n", + "\n", + " \n", + "tomato\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "283->284\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.11722113709725497" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "125\n", + "\n", + " \n", + "mix\n", + "node score: 0.2402\n", + "\n", + "\n", + "\n", + "130\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "125->130\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "129\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "125->129\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "127\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "125->127\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "133\n", + "\n", + " \n", + "bake\n", + "node score: 0.6603\n", + "\n", + "\n", + "\n", + "125->133\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "126\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "125->126\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "128\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "125->128\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "131\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "133->131\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.11055457053445297" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "356\n", + "\n", + " \n", + "mix\n", + "node score: 0.0000\n", + "\n", + "\n", + "\n", + "350\n", + "\n", + " \n", + "simmer\n", + "node score: 0.4219\n", + "\n", + "\n", + "\n", + "356->350\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "355\n", + "\n", + " \n", + "place\n", + "node score: 0.3497\n", + "\n", + "\n", + "\n", + "356->355\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "353\n", + "\n", + " \n", + "noodle\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "356->353\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "348\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "356->348\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "351\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "350->351\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "357\n", + "\n", + " \n", + "mix\n", + "node score: 0.4135\n", + "\n", + "\n", + "\n", + "355->357\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "352\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "357->352\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "347\n", + "\n", + " \n", + "tomato\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "357->347\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "349\n", + "\n", + " \n", + "onion\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "357->349\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.10984776980514128" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "333\n", + "\n", + " \n", + "warm\n", + "node score: 0.1375\n", + "\n", + "\n", + "\n", + "324\n", + "\n", + " \n", + "mix\n", + "node score: 0.2402\n", + "\n", + "\n", + "\n", + "333->324\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "326\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "324->326\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "325\n", + "\n", + " \n", + "bacon\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "324->325\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "331\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "324->331\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "330\n", + "\n", + " \n", + "onion\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "324->330\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "328\n", + "\n", + " \n", + "bake\n", + "node score: 0.6603\n", + "\n", + "\n", + "\n", + "324->328\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "327\n", + "\n", + " \n", + "tomato\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "324->327\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "329\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "328->329\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.07842295704725359" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "89\n", + "\n", + " \n", + "mix\n", + "node score: 0.2421\n", + "\n", + "\n", + "\n", + "97\n", + "\n", + " \n", + "cut\n", + "node score: 0.2756\n", + "\n", + "\n", + "\n", + "89->97\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "94\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "89->94\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "95\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "89->95\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "93\n", + "\n", + " \n", + "noodle\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "89->93\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "92\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "89->92\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "91\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "89->91\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "90\n", + "\n", + " \n", + "tomato\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "97->90\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.07810123770938873" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "212\n", + "\n", + " \n", + "mix\n", + "node score: 0.2520\n", + "\n", + "\n", + "\n", + "216\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "212->216\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "218\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "212->218\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "214\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "212->214\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "213\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "212->213\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "220\n", + "\n", + " \n", + "slice\n", + "node score: 0.2504\n", + "\n", + "\n", + "\n", + "212->220\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "217\n", + "\n", + " \n", + "noodle\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "212->217\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "215\n", + "\n", + " \n", + "bacon\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "220->215\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.06936547614329233" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "98\n", + "\n", + " \n", + "mix\n", + "node score: 0.1990\n", + "\n", + "\n", + "\n", + "104\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "98->104\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "103\n", + "\n", + " \n", + "noodle\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "98->103\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "100\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "98->100\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "101\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "98->101\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "106\n", + "\n", + " \n", + "fry\n", + "node score: 0.2833\n", + "\n", + "\n", + "\n", + "98->106\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "99\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "98->99\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "102\n", + "\n", + " \n", + "bacon\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "106->102\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "p.plot_population()" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "%3\n", + "\n", + "\n", + "\n", + "152\n", + "\n", + " \n", + "mix\n", + "node score: 0.2583\n", + "\n", + "\n", + "\n", + "154\n", + "\n", + " \n", + "tomato\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->154\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "153\n", + "\n", + " \n", + "bacon\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->153\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "158\n", + "\n", + " \n", + "salt\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "152->158\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "157\n", + "\n", + " \n", + "onion\n", + "node score:0.0000\n", + "\n", + "\n", + "\n", + "152->157\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "155\n", + "\n", + " \n", + "pepper\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "152->155\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "160\n", + "\n", + " \n", + "cook\n", + "node score: 1.0000\n", + "\n", + "\n", + "\n", + "152->160\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "156\n", + "\n", + " \n", + "noodle\n", + "node score:1.0000\n", + "\n", + "\n", + "\n", + "160->156\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p.population[0].dot()" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "an = list(p1.root().childs())[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "an.remove()" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'init.dot.pdf'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p1.dot().render(filename=\"init.dot\")" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": "'init2.dot.pdf'" + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "n.dot().render(filename=\"init2.dot\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +} \ No newline at end of file diff --git a/EvolutionaryAlgorithm/ea_tools.ipynb b/EvolutionaryAlgorithm/ea_tools.ipynb new file mode 100644 index 0000000..8397e8a --- /dev/null +++ b/EvolutionaryAlgorithm/ea_tools.ipynb @@ -0,0 +1,74 @@ +{ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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": [ + "def wheel_of_fortune_selection(items: list, item_scores:list):\n", + " ordering = np.argsort(item_scores)\n", + " ordering = ordering + 1\n", + "\n", + " wheel_weights = wheel_of_fortune(ordering, len(ordering))\n", + "\n", + " return np.random.choice(items, p=wheel_weights)\n" + ] + } + ] +} \ No newline at end of file diff --git a/EvolutionaryAlgorithm/ea_tools.py b/EvolutionaryAlgorithm/ea_tools.py new file mode 100644 index 0000000..975ff7e --- /dev/null +++ b/EvolutionaryAlgorithm/ea_tools.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# coding: utf-8 + +# # Statistical Tools + +import numpy as np + + +# * Helper function to calculate the wheel of fortune + +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 + + wheel_weights = wheel_of_fortune(ordering, len(ordering)) + + return np.random.choice(items, p=wheel_weights) +