karoo_gp as jupyter notebook
parent
f7720564e0
commit
fd5d54122e
|
@ -0,0 +1,284 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Karoo GP (desktop + server combined)\n",
|
||||
"# Use Genetic Programming for Classification and Symbolic Regression\n",
|
||||
"# by Kai Staats, MSc with TensorFlow support provided by Iurii Milovanov; see LICENSE.md\n",
|
||||
"# version 2.3 for Python 3.6\n",
|
||||
"\n",
|
||||
"'''\n",
|
||||
"A word to the newbie, expert, and brave--\n",
|
||||
"Even if you are highly experienced in Genetic Programming, it is recommended that you review the 'Karoo User Guide' \n",
|
||||
"before running this application. While your computer will not burst into flames nor will the sun collapse into a black \n",
|
||||
"hole if you do not, you will likely find more enjoyment of this particular flavour of GP with a little understanding \n",
|
||||
"of its intent and design.\n",
|
||||
"\n",
|
||||
"Without any command line arguments, Karoo GP relies upon user settings and the datasets located in karoo_gp/files/.\n",
|
||||
"\n",
|
||||
"\t$ python karoo_gp_main.py\n",
|
||||
"\t\n",
|
||||
"\n",
|
||||
"If you include the path to an external dataset, it will auto-load at launch:\n",
|
||||
"\n",
|
||||
"\t$ python karoo_gp_main.py /[path]/[to_your]/[filename].csv\n",
|
||||
"\t\n",
|
||||
"\n",
|
||||
"If you include one or more additional arguments, they will override the default values, as follows:\n",
|
||||
"\n",
|
||||
"\t-ker [r,c,m]\t\t\tfitness function: (r)egression, (c)lassification, or (m)atching\n",
|
||||
"\t-typ [f,g,r]\t\t\tTree type: (f)ull, (g)row, or (r)amped half/half\n",
|
||||
"\t-bas [3...10]\t\t\tmaximum Tree depth for initial population\n",
|
||||
"\t-max [3...10]\t\t\tmaximum Tree depth for entire run\n",
|
||||
"\t-min [3 to 2^(bas +1) - 1]\tminimum number of nodes\n",
|
||||
"\t-pop [10...1000]\t\tnumber of trees in each generational population\n",
|
||||
"\t-gen [1...100]\t\t\tnumber of generations\n",
|
||||
"\t-tor [7 per 100]\t\tnumber of trees selected for tournament\n",
|
||||
"\t-evr [0.0...1.0] \t\tdecimal percent of pop generated through Reproduction\n",
|
||||
"\t-evp [0.0...1.0] \t\tdecimal percent of pop generated through Point Mutation\n",
|
||||
"\t-evb [0.0...1.0] \t\tdecimal percent of pop generated through Branch Mutation\n",
|
||||
"\t-evc [0.0...1.0] \t\tdecimal percent of pop generated through Crossover\n",
|
||||
"\t\n",
|
||||
"If you include any of the above flags, then you *must* also include a flag to load an external dataset.\n",
|
||||
"\n",
|
||||
"\t-fil [path]/[to]/[data].csv\tan external dataset\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"An example is given, as follows:\n",
|
||||
"\n",
|
||||
"\t$ python karoo_gp_server.py -ker c -typ r -bas 4 -fil [path]/[to]/[data].csv\n",
|
||||
"\n",
|
||||
"'''\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import sys; sys.path.append('modules/') # add directory 'modules' to the current path\n",
|
||||
"import argparse\n",
|
||||
"import karoo_gp_base_class; gp = karoo_gp_base_class.Base_GP()\n",
|
||||
"\n",
|
||||
"os.system('clear')\n",
|
||||
"print ('\\n\\033[36m\\033[1m')\n",
|
||||
"print ('\\t ** ** ****** ***** ****** ****** ****** ******')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ** ** ** ** ** ** **')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ** ** ** ** ** ** **')\n",
|
||||
"print ('\\t **** ******** ****** ** ** ** ** ** *** *******')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ** ** ** ** ** ** **')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ** ** ** ** ** ** **')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ** ** ** ** ** ** **')\n",
|
||||
"print ('\\t ** ** ** ** ** ** ****** ****** ****** **')\n",
|
||||
"print ('\\033[0;0m')\n",
|
||||
"print ('\\t\\033[36m Genetic Programming in Python with TensorFlow - by Kai Staats, version 2.3\\033[0;0m')\n",
|
||||
"print ('')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"# User Interface for Configuation |\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"\n",
|
||||
"if len(sys.argv) < 3: # either no command line argument, or only a filename is provided\n",
|
||||
"\n",
|
||||
"\twhile True:\n",
|
||||
"\t\ttry:\n",
|
||||
"\t\t\tquery = input('\\t Select (c)lassification, (r)egression, (m)atching, or (p)lay (default m): ')\n",
|
||||
"\t\t\tif query in ['c','r','m','p','']: kernel = query or 'm'; break\n",
|
||||
"\t\t\telse: raise ValueError()\n",
|
||||
"\t\texcept ValueError: print ('\\t\\033[32m Select from the options given. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\n",
|
||||
"\tif kernel == 'p': # play mode\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Select (f)ull or (g)row (default g): ')\n",
|
||||
"\t\t\t\tif query in ['f','g','']: tree_type = query or 'f'; break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Select from the options given. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter the depth of the Tree (default 1): ')\n",
|
||||
"\t\t\t\tif query == '': tree_depth_base = 1; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(1,11)): tree_depth_base = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from 1 including 10. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\ttree_depth_max = tree_depth_base\n",
|
||||
"\t\ttree_depth_min = 3\n",
|
||||
"\t\ttree_pop_max = 1\n",
|
||||
"\t\tgen_max = 1\n",
|
||||
"\t\ttourn_size = 0\n",
|
||||
"\t\tdisplay = 'm'\n",
|
||||
"\t\t#\tevolve_repro, evolve_point, evolve_branch, evolve_cross, tourn_size, precision, filename are not required\n",
|
||||
"\t\n",
|
||||
"\telse: # if any other kernel is selected\n",
|
||||
"\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Select (f)ull, (g)row, or (r)amped 50/50 method (default r): ')\n",
|
||||
"\t\t\t\tif query in ['f','g','r','']: tree_type = query or 'r'; break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Select from the options given. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter depth of the \\033[3minitial\\033[0;0m population of Trees (default 3): ')\n",
|
||||
"\t\t\t\tif query == '': tree_depth_base = 3; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(1,11)): tree_depth_base = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from 1 including 10. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter maximum Tree depth (default %s): ' %str(tree_depth_base))\n",
|
||||
"\t\t\t\tif query == '': tree_depth_max = tree_depth_base; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(tree_depth_base,11)): tree_depth_max = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from %s including 10. Try again ...\\n\\033[0;0m' %str(tree_depth_base))\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\tmax_nodes = 2**(tree_depth_base+1)-1 # calc the max number of nodes for the given depth\n",
|
||||
"\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter minimum number of nodes for any given Tree (default 3; max %s): ' %str(max_nodes))\n",
|
||||
"\t\t\t\tif query == '': tree_depth_min = 3; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(3,max_nodes + 1)): tree_depth_min = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from 3 including %s. Try again ...\\n\\033[0;0m' %str(max_nodes))\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\t#while True:\n",
|
||||
"\t\t\t#try:\n",
|
||||
"\t\t\t\t#query = input('\\t Select (p)artial or (f)ull operator inclusion (default p): ')\n",
|
||||
"\t\t\t\t#if query == '': swim = 'p'; break\n",
|
||||
"\t\t\t\t#elif query in ['p','f']: swim = query; break\n",
|
||||
"\t\t\t\t#else: raise ValueError()\n",
|
||||
"\t\t\t#except ValueError: print ('\\t\\033[32m Select from the options given. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\t#except KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter number of Trees in each population (default 100): ')\n",
|
||||
"\t\t\t\tif query == '': tree_pop_max = 100; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(1,1001)): tree_pop_max = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from 1 including 1000. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\t# calculate the tournament size\n",
|
||||
"\t\ttourn_size = int(tree_pop_max * 0.07) # default 7% can be changed by selecting (g)eneration and then 'ts'\n",
|
||||
"\t\tif tourn_size < 2: tourn_size = 2 # forces some diversity for small populations\n",
|
||||
"\t\tif tree_pop_max == 1: tourn_size = 1 # in theory, supports the evolution of a single Tree - NEED TO FIX 2018 04/19\n",
|
||||
"\t\t\n",
|
||||
"\t\twhile True:\n",
|
||||
"\t\t\ttry:\n",
|
||||
"\t\t\t\tquery = input('\\t Enter max number of generations (default 10): ')\n",
|
||||
"\t\t\t\tif query == '': gen_max = 10; break\n",
|
||||
"\t\t\t\telif int(query) in list(range(1,101)): gen_max = int(query); break\n",
|
||||
"\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\texcept ValueError: print ('\\t\\033[32m Enter a number from 1 including 100. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\n",
|
||||
"\t\tif gen_max > 1:\n",
|
||||
"\t\t\twhile True:\n",
|
||||
"\t\t\t\ttry:\n",
|
||||
"\t\t\t\t\tquery = input('\\t Display (i)nteractive, (g)eneration, (m)iminal, (s)ilent, or (d)e(b)ug (default m): ')\n",
|
||||
"\t\t\t\t\tif query in ['i','g','m','s','db','']: display = query or 'm'; break\n",
|
||||
"\t\t\t\t\telse: raise ValueError()\n",
|
||||
"\t\t\t\texcept ValueError: print ('\\t\\033[32m Select from the options given. Try again ...\\n\\033[0;0m')\n",
|
||||
"\t\t\t\texcept KeyboardInterrupt: sys.exit()\n",
|
||||
"\t\t\t\t\n",
|
||||
"\t\telse: display = 's' # display mode is not used, but a value must be passed\n",
|
||||
"\t\t\t\t\n",
|
||||
"\t### additional configuration parameters ###\n",
|
||||
"\t\n",
|
||||
"\tevolve_repro = int(0.1 * tree_pop_max) # quantity of a population generated through Reproduction\n",
|
||||
"\tevolve_point = int(0.0 * tree_pop_max) # quantity of a population generated through Point Mutation\n",
|
||||
"\tevolve_branch = int(0.2 * tree_pop_max) # quantity of a population generated through Branch Mutation\n",
|
||||
"\tevolve_cross = int(0.7 * tree_pop_max) # quantity of a population generated through Crossover\n",
|
||||
"\tfilename = '' # not required unless an external file is referenced\n",
|
||||
"\tprecision = 6 # number of floating points for the round function in 'fx_fitness_eval'\n",
|
||||
"\tswim = 'p' # require (p)artial or (f)ull set of features (operators) for each Tree entering the gene_pool\n",
|
||||
"\tmode = 'd' # pause at the (d)esktop when complete, awaiting further user interaction; or terminate in (s)erver mode\n",
|
||||
"\t\n",
|
||||
"\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"# Command Line for Configuation |\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"\n",
|
||||
"else: # 2 or more command line arguments are provided\n",
|
||||
"\n",
|
||||
"\tap = argparse.ArgumentParser(description = 'Karoo GP Server')\n",
|
||||
"\tap.add_argument('-ker', action = 'store', dest = 'kernel', default = 'c', help = '[c,r,m] fitness function: (r)egression, (c)lassification, or (m)atching')\n",
|
||||
"\tap.add_argument('-typ', action = 'store', dest = 'type', default = 'r', help = '[f,g,r] Tree type: (f)ull, (g)row, or (r)amped half/half')\n",
|
||||
"\tap.add_argument('-bas', action = 'store', dest = 'depth_base', default = 4, help = '[3...10] maximum Tree depth for the initial population')\n",
|
||||
"\tap.add_argument('-max', action = 'store', dest = 'depth_max', default = 4, help = '[3...10] maximum Tree depth for the entire run')\n",
|
||||
"\tap.add_argument('-min', action = 'store', dest = 'depth_min', default = 3, help = 'minimum nodes, from 3 to 2^(base_depth +1) - 1')\n",
|
||||
"\tap.add_argument('-pop', action = 'store', dest = 'pop_max', default = 100, help = '[10...1000] number of trees per generation')\n",
|
||||
"\tap.add_argument('-gen', action = 'store', dest = 'gen_max', default = 10, help = '[1...100] number of generations')\n",
|
||||
"\tap.add_argument('-tor', action = 'store', dest = 'tor_size', default = 7, help = '[7 for each 100] recommended tournament size')\n",
|
||||
"\tap.add_argument('-evr', action = 'store', dest = 'evo_r', default = 0.1, help = '[0.0-1.0] decimal percent of pop generated through Reproduction')\n",
|
||||
"\tap.add_argument('-evp', action = 'store', dest = 'evo_p', default = 0.0, help = '[0.0-1.0] decimal percent of pop generated through Point Mutation')\n",
|
||||
"\tap.add_argument('-evb', action = 'store', dest = 'evo_b', default = 0.2, help = '[0.0-1.0] decimal percent of pop generated through Branch Mutation')\n",
|
||||
"\tap.add_argument('-evc', action = 'store', dest = 'evo_c', default = 0.7, help = '[0.0-1.0] decimal percent of pop generated through Crossover')\n",
|
||||
"\tap.add_argument('-fil', action = 'store', dest = 'filename', default = '', help = '/path/to_your/[data].csv')\n",
|
||||
"\t\n",
|
||||
"\targs = ap.parse_args()\n",
|
||||
"\n",
|
||||
"\t# pass the argparse defaults and/or user inputs to the required variables\n",
|
||||
"\tkernel = str(args.kernel)\n",
|
||||
"\ttree_type = str(args.type)\n",
|
||||
"\ttree_depth_base = int(args.depth_base)\n",
|
||||
"\ttree_depth_max = int(args.depth_max)\n",
|
||||
"\ttree_depth_min = int(args.depth_min)\n",
|
||||
"\ttree_pop_max = int(args.pop_max)\n",
|
||||
"\tgen_max = int(args.gen_max)\n",
|
||||
"\ttourn_size = int(args.tor_size)\n",
|
||||
"\tevolve_repro = int(float(args.evo_r) * tree_pop_max)\n",
|
||||
"\tevolve_point = int(float(args.evo_p) * tree_pop_max)\n",
|
||||
"\tevolve_branch = int(float(args.evo_b) * tree_pop_max)\n",
|
||||
"\tevolve_cross = int(float(args.evo_c) * tree_pop_max)\n",
|
||||
"\tfilename = str(args.filename)\n",
|
||||
"\t\n",
|
||||
"\tdisplay = 's' # display mode is set to (s)ilent\n",
|
||||
"\tprecision = 6 # number of floating points for the round function in 'fx_fitness_eval'\n",
|
||||
"\tswim = 'p' # require (p)artial or (f)ull set of features (operators) for each Tree entering the gene_pool\n",
|
||||
"\tmode = 's' # pause at the (d)esktop when complete, awaiting further user interaction; or terminate in (s)erver mode\n",
|
||||
"\t\n",
|
||||
"\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"# Conduct the GP run |\n",
|
||||
"#++++++++++++++++++++++++++++++++++++++++++\n",
|
||||
"\n",
|
||||
"gp.fx_karoo_gp(kernel, tree_type, tree_depth_base, tree_depth_max, tree_depth_min, tree_pop_max, gen_max, tourn_size, filename, evolve_repro, evolve_point, evolve_branch, evolve_cross, display, precision, swim, mode)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"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.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
Loading…
Reference in New Issue