From 336485b6fcfaa4532730e32980a8dbdf4083e3cc Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 1 Nov 2023 11:43:42 -0700 Subject: [PATCH 1/2] updated estimator_graph_individual_generator to generate random pipelines with user specified sizes (if possible) --- .../graph_pipeline_individual/templates.py | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/tpot2/individual_representations/graph_pipeline_individual/templates.py b/tpot2/individual_representations/graph_pipeline_individual/templates.py index c08d047a..51ca2b2b 100644 --- a/tpot2/individual_representations/graph_pipeline_individual/templates.py +++ b/tpot2/individual_representations/graph_pipeline_individual/templates.py @@ -7,6 +7,7 @@ from tpot2.individual_representations.graph_pipeline_individual.individual import create_node +# will randomly generate individuals (no predefined order) def estimator_graph_individual_generator( root_config_dict, inner_config_dict=None, @@ -22,47 +23,46 @@ def estimator_graph_individual_generator( rng = np.random.default_rng(rng_) - n_nodes = 0 while True: - if n_nodes < max_size: - n_nodes += 1 - - for k in root_config_dict.keys(): - - graph = nx.DiGraph() - root = create_node(config_dict={k:root_config_dict[k]}, rng_=rng) - graph.add_node(root) - - ind = GraphIndividual( rng_=rng, - inner_config_dict=inner_config_dict, - leaf_config_dict=leaf_config_dict, - root_config_dict=root_config_dict, - initial_graph = graph, - - max_size = max_size, - linear_pipeline = linear_pipeline, - hyperparameter_probability = hyperparameter_probability, - hyper_node_probability = hyper_node_probability, - hyperparameter_alpha = hyperparameter_alpha, - - **kwargs, - ) - - starting_ops = [] - if inner_config_dict is not None: - starting_ops.append(ind._mutate_insert_inner_node) - if leaf_config_dict is not None: - starting_ops.append(ind._mutate_insert_leaf) - - if len(starting_ops) > 0: - if n_nodes > 0: - for _ in range(rng.integers(0,min(n_nodes,3))): - func = rng.choice(starting_ops) - func(rng_=rng) - - - yield ind + # if user specified limit, grab a random number between that limit + if max_size is not np.inf: + n_nodes = rng.integers(0,max_size) + # else, grab random number between 0,10 (theaksaini) + else: + n_nodes = rng.integers(0,10) + + graph = nx.DiGraph() + root = create_node(config_dict=root_config_dict, rng_=rng) # grab random root model method + graph.add_node(root) + + ind = GraphIndividual( rng_=rng, + inner_config_dict=inner_config_dict, + leaf_config_dict=leaf_config_dict, + root_config_dict=root_config_dict, + initial_graph = graph, + + max_size = max_size, + linear_pipeline = linear_pipeline, + hyperparameter_probability = hyperparameter_probability, + hyper_node_probability = hyper_node_probability, + hyperparameter_alpha = hyperparameter_alpha, + + **kwargs, + ) + + starting_ops = [] + if inner_config_dict is not None: + starting_ops.append(ind._mutate_insert_inner_node) + if leaf_config_dict is not None: + starting_ops.append(ind._mutate_insert_leaf) + + if len(starting_ops) > 0: + for _ in range(n_nodes-1): + func = rng.choice(starting_ops) + func(rng_=rng) + + yield ind class BaggingCompositeGraphSklearn(): From 18ce1563f20518807b5008b4d33353c8cf145da2 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 1 Nov 2023 13:38:01 -0700 Subject: [PATCH 2/2] fixed range to get correct number of nodes --- .../graph_pipeline_individual/templates.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tpot2/individual_representations/graph_pipeline_individual/templates.py b/tpot2/individual_representations/graph_pipeline_individual/templates.py index 51ca2b2b..cd8015cc 100644 --- a/tpot2/individual_representations/graph_pipeline_individual/templates.py +++ b/tpot2/individual_representations/graph_pipeline_individual/templates.py @@ -27,10 +27,10 @@ def estimator_graph_individual_generator( # if user specified limit, grab a random number between that limit if max_size is not np.inf: - n_nodes = rng.integers(0,max_size) - # else, grab random number between 0,10 (theaksaini) + n_nodes = rng.integers(1,max_size+1) + # else, grab random number between 1,11 (theaksaini) else: - n_nodes = rng.integers(0,10) + n_nodes = rng.integers(1,11) graph = nx.DiGraph() root = create_node(config_dict=root_config_dict, rng_=rng) # grab random root model method @@ -56,6 +56,7 @@ def estimator_graph_individual_generator( starting_ops.append(ind._mutate_insert_inner_node) if leaf_config_dict is not None: starting_ops.append(ind._mutate_insert_leaf) + n_nodes -= 1 if len(starting_ops) > 0: for _ in range(n_nodes-1):