changed signatures to have also a main ingredient set

This commit is contained in:
Jonas Weinz 2019-12-12 09:19:38 +01:00
parent 9c406c3e48
commit 33f22a5050
4 changed files with 5649 additions and 8342 deletions

File diff suppressed because it is too large Load Diff

View File

@ -72,6 +72,12 @@ m_mix = dill.load(open("../RecipeAnalysis/m_mix.dill", "rb"))
m_base_act = dill.load(open("../RecipeAnalysis/m_base_act.dill", "rb"))
m_base_mix = dill.load(open("../RecipeAnalysis/m_base_mix.dill", "rb"))
m_grouped_mix = dill.load(open("../RecipeAnalysis/m_grouped_mix_raw.dill", "rb"))
m_grouped_act = dill.load(open("../RecipeAnalysis/m_grouped_act_raw.dill", "rb"))
m_grouped_base_act = dill.load(open("../RecipeAnalysis/m_grouped_base_act_raw.dill", "rb"))
#m_act.apply_threshold(3)
#m_mix.apply_threshold(3)
#m_base_act.apply_threshold(5)
@ -88,6 +94,10 @@ m_mix.compile()
m_base_act.compile()
m_base_mix.compile()
m_grouped_mix.compile()
m_grouped_act.compile()
m_grouped_base_act.compile()
c_act = m_act._csr
c_mix = m_mix._csr
c_base_act = m_base_act._csr
@ -455,6 +465,9 @@ class RecipeTreeNode(object):
# ### Mix Node
# For the Node Score: just make a simple lookup whether this combination is seen or not. So the node Score is defined as:
#
class MixNode(RecipeTreeNode):
def __init__(self, constant=False):
super().__init__("mix", constant, single_child=False)
@ -536,12 +549,22 @@ class MixNode(RecipeTreeNode):
#s_base += sym_score(ing_a._base_ingredient, ing_b._base_ingredient, m_base_mix, c_base_mix)
#s += sym_score(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)
p1 = sym_p_a_given_b(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)
p2 = sym_p_a_given_b(ing_b.to_json(), ing_a.to_json(), m_mix, c_mix)
s += 0.5 * p1 + 0.5 * p2
# old method:
#p1 = sym_p_a_given_b(ing_a.to_json(), ing_b.to_json(), m_mix, c_mix)
#p2 = sym_p_a_given_b(ing_b.to_json(), ing_a.to_json(), m_mix, c_mix)
#s += 0.5 * p1 + 0.5 * p2
except:
ia = m_mix._label_index[ing_a.to_json()]
ib = m_mix._label_index[ing_b.to_json()]
if c_mix[ia,ib] > 0 or c_mix[ib,ia] > 0:
s += 1
except KeyError as e:
pass
#s_base /= len(pairwise_tuples)
@ -652,7 +675,7 @@ class ActionNode(RecipeTreeNode):
class Tree(object):
@staticmethod
def build_initial_tree(ingredients: list, max_n = 4, wheel_turns = 2):
def build_initial_tree(ingredients: list, main_ingredients: list, max_n = 4, wheel_turns = 2):
'''
# get action sets for ingredients
@ -833,7 +856,7 @@ class Tree(object):
return list(constant_ingredients) + list(additional_ingredients)
@staticmethod
def from_ingredients(ingredients: list, additional_ings=0):
def from_ingredients(ingredients: list, main_ingredients: list, additional_ings=0):
root = None
constant_ingredients = ingredients
@ -997,8 +1020,8 @@ class Tree(object):
# ## Population
class Population(object):
def __init__(self, start_ingredients, n_population = 10, max_additional_ings=0):
self.population = [Tree.from_ingredients(start_ingredients, additional_ings=max_additional_ings) for i in range(n_population)]
def __init__(self, start_ingredients, main_ingredients, n_population = 10, max_additional_ings=0):
self.population = [Tree.from_ingredients(start_ingredients, main_ingredients, additional_ings=max_additional_ings) for i in range(n_population)]
self._n = n_population
self._mix_min = None
self._mix_max = None
@ -1115,3 +1138,19 @@ class Population(object):
display(t.root().dot())
# ## Run Evolutionary Algorithm
#p = Population(["bacon", "tomato", "onion"],['noodle'], max_additional_ings=6)
#p_ingredient_unprepared(list(p.population[0].root().childs())[0]._name) < 0.2
#p.run(100)
#p.plot_population(collect_scores=False)

File diff suppressed because one or more lines are too long

View File

@ -123,7 +123,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5796ec52773740c59e747c0e5f77410e",
"model_id": "affefd1263f44e97815b9c32f49d69d9",
"version_major": 2,
"version_minor": 0
},
@ -149,7 +149,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "92fd11191481475a9c40ae76201b4772",
"model_id": "29990add3612462abfb4dcc60f2ff7ca",
"version_major": 2,
"version_minor": 0
},
@ -163,7 +163,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "618b5a44910843bbaed8b36c3ad2bc46",
"model_id": "4b66883d839f49e9b764f683ba9079c9",
"version_major": 2,
"version_minor": 0
},
@ -189,7 +189,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "301ebb9ed6024493ad85c2b79402345e",
"model_id": "645f200c9fa441f8964683dbcd188ad0",
"version_major": 2,
"version_minor": 0
},
@ -215,7 +215,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c90d303cd2cb43d1aae401ac6226e3a1",
"model_id": "d0c4b6f97b894211a5a1c2cf052e1d58",
"version_major": 2,
"version_minor": 0
},
@ -229,7 +229,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cea1f9de60344298ac8417d755ad74df",
"model_id": "889ed0ea7cc24663b40beb30e8a1abcc",
"version_major": 2,
"version_minor": 0
},
@ -243,7 +243,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3ac8e962dfeb445fa3417dbdbfd5c44c",
"model_id": "fa434784f8364bc692301a162824cb30",
"version_major": 2,
"version_minor": 0
},
@ -321,7 +321,10 @@
"\n",
"w_run_button = widgets.Button(description=\"run EA\")\n",
"\n",
"p = None\n",
"\n",
"def run(e=None):\n",
" global p\n",
" w_result_out.clear_output()\n",
" with w_result_out:\n",
" p = EvolutionaryAlgorithm.Population(\n",