diff --git a/karoo-gp.py b/karoo-gp.py index 8c24ed4..ddecbd7 100644 --- a/karoo-gp.py +++ b/karoo-gp.py @@ -332,9 +332,9 @@ '(c)lassification, or (m)atching') ap.add_argument('-typ', action='store', dest='type', default='r', help='[f,g,r] Tree type: (f)ull, (g)row, or (r)amped half/half') - ap.add_argument('-bas', action='store', dest='depth_base', default=4, + ap.add_argument('-bas', action='store', dest='depth_base', type=int, default=4, help='[3...10] maximum Tree depth for the initial population') - ap.add_argument('-max', action='store', dest='depth_max', default=4, + ap.add_argument('-max', action='store', dest='depth_max', type=int, default=None, help='[3...10] maximum Tree depth for the entire run') ap.add_argument('-min', action='store', dest='depth_min', default=3, help='minimum nodes, from 3 to 2^(base_depth +1) - 1') @@ -368,8 +368,8 @@ # pass the argparse defaults and/or user inputs to the required variables kernel = str(args.kernel) tree_type = str(args.type) - tree_depth_base = int(args.depth_base) - tree_depth_max = int(args.depth_max) + tree_depth_base = args.depth_base + tree_depth_max = args.depth_max tree_depth_min = int(args.depth_min) tree_pop_max = int(args.pop_max) gen_max = int(args.gen_max) diff --git a/karoo_gp/base_class.py b/karoo_gp/base_class.py index 8b095b6..d8b30e7 100644 --- a/karoo_gp/base_class.py +++ b/karoo_gp/base_class.py @@ -105,7 +105,7 @@ class BaseGP(object): """ def __init__( - self, tree_type='r', tree_depth_base=3, tree_depth_max=3, + self, tree_type='r', tree_depth_base=3, tree_depth_max=None, tree_depth_min=1, tree_pop_max=100, gen_max=10, tourn_size=7, filename='', output_dir='', evolve_repro=0.1, evolve_point=0.1, evolve_branch=0.2, evolve_cross=0.6, display='s', precision=None, @@ -159,7 +159,15 @@ def __init__( self.X_hash = None # hash of last-used fit data self.gen_max = gen_max # number of generations to evolve self.swim = swim # culling method - self.tree_depth_max = tree_depth_max # max allowed depth + + if tree_depth_max is None: + self.tree_depth_max = tree_depth_base + elif tree_depth_max >= tree_depth_base: + self.tree_depth_max = tree_depth_max + else: + raise ValueError(f'Max depth ({tree_depth_max}) must be greater ' + f'or equal to base depth ({tree_depth_base})') + self.tree_depth_min = tree_depth_min # min allowed number of nodes self.tourn_size = tourn_size # number of Trees per tournament self.evolve_repro = evolve_repro # ratio of next_gen reproduced diff --git a/karoo_gp/test/data/results[c-f].json b/karoo_gp/test/data/results[c-f].json index 86c2ff0..506c164 100644 --- a/karoo_gp/test/data/results[c-f].json +++ b/karoo_gp/test/data/results[c-f].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-15-25-662209", + "launched": "2022-07-06_20-39-53-790684", "dataset": "karoo_gp/files/data_CLASSIFY.csv", "config": { "kernel": "c", "precision": 6, "tree_type": "f", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,41 +21,41 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 5, - "expression": "pl**2/sw - sl - pw/(pl**2*sw)" + "id": 10, + "expression": "pl*sl/sw - pl/pw - sl" }, "score": { - "fitness": 26.0, + "fitness": 23.0, "classification_report": { "0.0": { - "precision": 1.0, + "precision": 0.6, "recall": 1.0, - "f1-score": 1.0, + "f1-score": 0.7499999999999999, "support": 9 }, "1.0": { "precision": 1.0, - "recall": 0.6666666666666666, - "f1-score": 0.8, + "recall": 0.4166666666666667, + "f1-score": 0.5882352941176471, "support": 12 }, "2.0": { - "precision": 0.6923076923076923, + "precision": 0.9, "recall": 1.0, - "f1-score": 0.8181818181818181, + "f1-score": 0.9473684210526316, "support": 9 }, - "accuracy": 0.8666666666666667, + "accuracy": 0.7666666666666667, "macro avg": { - "precision": 0.8974358974358975, - "recall": 0.8888888888888888, - "f1-score": 0.8727272727272727, + "precision": 0.8333333333333334, + "recall": 0.8055555555555557, + "f1-score": 0.7618679050567595, "support": 30 }, "weighted avg": { - "precision": 0.9076923076923077, - "recall": 0.8666666666666667, - "f1-score": 0.8654545454545455, + "precision": 0.85, + "recall": 0.7666666666666667, + "f1-score": 0.7445046439628483, "support": 30 } }, @@ -66,9 +66,9 @@ 0 ], [ - 0, - 8, - 4 + 6, + 5, + 1 ], [ 0, diff --git a/karoo_gp/test/data/results[c-g].json b/karoo_gp/test/data/results[c-g].json index 147b9c6..c3cb0d4 100644 --- a/karoo_gp/test/data/results[c-g].json +++ b/karoo_gp/test/data/results[c-g].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-16-20-639880", + "launched": "2022-07-06_20-39-14-375734", "dataset": "karoo_gp/files/data_CLASSIFY.csv", "config": { "kernel": "c", "precision": 6, "tree_type": "g", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,41 +21,41 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 9, - "expression": "pl*pw**2 + pw*sw - sl" + "id": 10, + "expression": "2*pl - sw**2 + 1" }, "score": { - "fitness": 19.0, + "fitness": 20.0, "classification_report": { "0.0": { - "precision": 1.0, + "precision": 0.9, "recall": 1.0, - "f1-score": 1.0, + "f1-score": 0.9473684210526316, "support": 9 }, "1.0": { "precision": 1.0, - "recall": 0.08333333333333333, - "f1-score": 0.15384615384615385, + "recall": 0.16666666666666666, + "f1-score": 0.2857142857142857, "support": 12 }, "2.0": { - "precision": 0.45, + "precision": 0.5, "recall": 1.0, - "f1-score": 0.6206896551724138, + "f1-score": 0.6666666666666666, "support": 9 }, - "accuracy": 0.6333333333333333, + "accuracy": 0.6666666666666666, "macro avg": { - "precision": 0.8166666666666668, - "recall": 0.6944444444444443, - "f1-score": 0.5915119363395225, + "precision": 0.7999999999999999, + "recall": 0.7222222222222223, + "f1-score": 0.633249791144528, "support": 30 }, "weighted avg": { - "precision": 0.8350000000000001, - "recall": 0.6333333333333333, - "f1-score": 0.5477453580901857, + "precision": 0.8200000000000001, + "recall": 0.6666666666666666, + "f1-score": 0.5984962406015039, "support": 30 } }, @@ -66,9 +66,9 @@ 0 ], [ - 0, 1, - 11 + 2, + 9 ], [ 0, diff --git a/karoo_gp/test/data/results[c-r].json b/karoo_gp/test/data/results[c-r].json index db561d3..eb5690a 100644 --- a/karoo_gp/test/data/results[c-r].json +++ b/karoo_gp/test/data/results[c-r].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-16-39-210454", + "launched": "2022-07-06_20-38-32-741216", "dataset": "karoo_gp/files/data_CLASSIFY.csv", "config": { "kernel": "c", "precision": 6, "tree_type": "r", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,41 +21,41 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 3, - "expression": "pl + pw - sl" + "id": 6, + "expression": "pw - 1" }, "score": { - "fitness": 18.0, + "fitness": 27.0, "classification_report": { "0.0": { - "precision": 0.5625, + "precision": 1.0, "recall": 1.0, - "f1-score": 0.72, + "f1-score": 1.0, "support": 9 }, "1.0": { - "precision": 0.5, - "recall": 0.25, - "f1-score": 0.3333333333333333, + "precision": 0.9090909090909091, + "recall": 0.8333333333333334, + "f1-score": 0.8695652173913043, "support": 12 }, "2.0": { - "precision": 0.75, - "recall": 0.6666666666666666, - "f1-score": 0.7058823529411765, + "precision": 0.8, + "recall": 0.8888888888888888, + "f1-score": 0.8421052631578948, "support": 9 }, - "accuracy": 0.6, + "accuracy": 0.9, "macro avg": { - "precision": 0.6041666666666666, - "recall": 0.6388888888888888, - "f1-score": 0.5864052287581699, + "precision": 0.903030303030303, + "recall": 0.9074074074074074, + "f1-score": 0.9038901601830663, "support": 30 }, "weighted avg": { - "precision": 0.59375, - "recall": 0.6, - "f1-score": 0.5610980392156864, + "precision": 0.9036363636363636, + "recall": 0.9, + "f1-score": 0.9004576659038901, "support": 30 } }, @@ -66,14 +66,14 @@ 0 ], [ - 7, - 3, + 0, + 10, 2 ], [ 0, - 3, - 6 + 1, + 8 ] ] } diff --git a/karoo_gp/test/data/results[m-f].json b/karoo_gp/test/data/results[m-f].json index e4db7ff..fdf8ee0 100644 --- a/karoo_gp/test/data/results[m-f].json +++ b/karoo_gp/test/data/results[m-f].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-17-46-867022", + "launched": "2022-07-06_20-37-24-150346", "dataset": "karoo_gp/files/data_MATCH.csv", "config": { "kernel": "m", "precision": 6, "tree_type": "f", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -19,12 +19,5 @@ "population": 50, "number_of_generations": 10 }, - "outcome": "SUCCESS", - "fittest_tree": { - "id": 45, - "expression": "-a + 3*b + c - 2" - }, - "score": { - "fitness": 10.0 - } + "outcome": "FAILURE" } diff --git a/karoo_gp/test/data/results[m-g].json b/karoo_gp/test/data/results[m-g].json index ceb51c5..3946680 100644 --- a/karoo_gp/test/data/results[m-g].json +++ b/karoo_gp/test/data/results[m-g].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-18-20-189034", + "launched": "2022-07-06_20-36-33-411609", "dataset": "karoo_gp/files/data_MATCH.csv", "config": { "kernel": "m", "precision": 6, "tree_type": "g", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,7 +21,7 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 25, + "id": 27, "expression": "a + b + c" }, "score": { diff --git a/karoo_gp/test/data/results[m-r].json b/karoo_gp/test/data/results[m-r].json index 1f55ef6..b457825 100644 --- a/karoo_gp/test/data/results[m-r].json +++ b/karoo_gp/test/data/results[m-r].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-18-39-427568", + "launched": "2022-07-06_20-32-50-913856", "dataset": "karoo_gp/files/data_MATCH.csv", "config": { "kernel": "m", "precision": 6, "tree_type": "r", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -19,12 +19,5 @@ "population": 10, "number_of_generations": 10 }, - "outcome": "SUCCESS", - "fittest_tree": { - "id": 4, - "expression": "a + b + c" - }, - "score": { - "fitness": 10.0 - } + "outcome": "FAILURE" } diff --git a/karoo_gp/test/data/results[r-f].json b/karoo_gp/test/data/results[r-f].json index 43eb620..785d2bc 100644 --- a/karoo_gp/test/data/results[r-f].json +++ b/karoo_gp/test/data/results[r-f].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-17-01-295356", + "launched": "2022-07-06_20-42-30-394614", "dataset": "karoo_gp/files/data_REGRESS.csv", "config": { "kernel": "r", "precision": 6, "tree_type": "f", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,11 +21,11 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 6, - "expression": "1 - t/r**2" + "id": 3, + "expression": "1" }, "score": { - "fitness": 5.90544, - "mean_squared_error": 0.66168 + "fitness": 0.05, + "mean_squared_error": 7.77778e-05 } } diff --git a/karoo_gp/test/data/results[r-g].json b/karoo_gp/test/data/results[r-g].json index 13c9f98..4c74f25 100644 --- a/karoo_gp/test/data/results[r-g].json +++ b/karoo_gp/test/data/results[r-g].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-17-17-516625", + "launched": "2022-07-06_20-41-43-634215", "dataset": "karoo_gp/files/data_REGRESS.csv", "config": { "kernel": "r", "precision": 6, "tree_type": "g", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,11 +21,11 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 10, + "id": 8, "expression": "1" }, "score": { "fitness": 0.05, - "mean_squared_error": 7.77778e-5 + "mean_squared_error": 7.77778e-05 } } diff --git a/karoo_gp/test/data/results[r-r].json b/karoo_gp/test/data/results[r-r].json index 8ae2973..ba384f1 100644 --- a/karoo_gp/test/data/results[r-r].json +++ b/karoo_gp/test/data/results[r-r].json @@ -1,13 +1,13 @@ { "package": "Karoo GP", - "launched": "2022-06-12_06-17-33-410486", + "launched": "2022-07-06_20-40-55-255266", "dataset": "karoo_gp/files/data_REGRESS.csv", "config": { "kernel": "r", "precision": 6, "tree_type": "r", "tree_depth_base": 3, - "tree_depth_max": 4, + "tree_depth_max": 3, "min_node_count": 3, "genetic_operators": { "reproduction": 0.1, @@ -21,7 +21,7 @@ }, "outcome": "SUCCESS", "fittest_tree": { - "id": 2, + "id": 10, "expression": "0" }, "score": {