support for argparse added

pull/4/head
Kai Staats 2016-07-13 22:43:06 -06:00
parent 6d1358fb5d
commit caafb5660e
4 changed files with 137 additions and 84 deletions

View File

@ -2,7 +2,7 @@
# Define the methods and global variables used by Karoo GP
# by Kai Staats, MSc UCT / AIMS
# Much thanks to Emmanuel Dufourq and Arun Kumar for their support, guidance, and free psychotherapy sessions
# version 0.9.1.5
# version 0.9.1.6
'''
A NOTE TO THE NEWBIE, EXPERT, AND BRAVE
@ -16,7 +16,6 @@ import os
import sys
import time
import argparse
import numpy as np
import pprocess as pp
import sklearn.metrics as skm
@ -36,7 +35,8 @@ class Base_GP(object):
will 'print' to screen.
The categories (denoted by #+++++++ banners) are as follows:
'fx_karoo_' Methods to Run Karoo GP (with the exception of top-level 'karoo_gp' itself)
'karoo_gp' A single, top-level method which conducts an entire run. Used by karoo_gp_server.py
'fx_karoo_' Methods to Run Karoo GP
'fx_gen_' Methods to Generate a Tree
'fx_eval_' Methods to Evaluate a Tree
'fx_fitness_' Methods to Evaluate Tree Fitness
@ -114,6 +114,7 @@ class Base_GP(object):
self.fittest_dict = {} # temp store all Trees which share the best fitness score
self.gene_pool = [] # temp store all Tree IDs for use by Tournament
self.core_count = pp.get_number_of_cores() # pprocess
self.class_labels = 0 # temp set a variable which will be assigned the number of class labels (data_y)
return
@ -122,20 +123,19 @@ class Base_GP(object):
# Methods to Run Karoo GP |
#++++++++++++++++++++++++++++++++++++++++++
def karoo_gp(self, run, tree_type, tree_depth_base):
def karoo_gp(self, tree_type, tree_depth_base, filename):
'''
This is single method enables the engagement of the entire Karoo GP application. It is used by
karoo_gp_server.py and the future, single command line executable, but not by karoo_gp_main.py which engages
each of the included functions sequentially.
This single method enables the engagement of the entire Karoo GP application. It is used by karoo_gp_server.py
for both scripted and command line execution, but not by karoo_gp_main.py.
Arguments required: run, tree_type, tree_depth_base
Arguments required: tree_type, tree_depth_base
'''
self.karoo_banner(run)
self.karoo_banner()
# construct first generation of Trees
self.fx_karoo_data_load()
self.fx_karoo_data_load(tree_type, tree_depth_base, filename)
self.generation_id = 1 # set initial generation ID
self.population_a = ['Karoo GP by Kai Staats, Generation ' + str(self.generation_id)] # a list which will store all Tree arrays, one generation at a time
self.fx_karoo_construct(tree_type, tree_depth_base) # construct the first population of Trees
@ -167,16 +167,12 @@ class Base_GP(object):
return
def karoo_banner(self, run):
def karoo_banner(self):
'''
This method makes Karoo GP look old-school cool!
While the banner remains the same, it presents a configuration request unique to a 'server' run. At the time
of this writing, the only options are 'server' or 'main' where 'main' defaults to requests for feedback based
upon the display mode selected by the user. See 'fx_karoo_construct' for examples.
Arguments required: run
Arguments required: none
'''
os.system('clear')
@ -191,20 +187,12 @@ class Base_GP(object):
print '\t ** ** ** ** ** ** ** ** ** ** ** ** **'
print '\t ** ** ** ** ** ** ****** ****** ****** **'
print '\033[0;0m'
print '\t\033[36m Genetic Programming in Python - by Kai Staats, version 0.9\033[0;0m'
if run == 'server':
print '\n\t Type \033[1m?\033[0;0m to configure Karoo GP before your run, or \033[1mENTER\033[0;0m to continue.\033[0;0m'
self.fx_karoo_pause(0)
elif run == 'main': pass
else: pass
print '\t\033[36m Genetic Programming in Python - by Kai Staats, version 0.9.1.6\033[0;0m'
return
def fx_karoo_data_load(self):
def fx_karoo_data_load(self, tree_type, tree_depth_base, filename):
'''
The data and function .csv files are loaded according to the fitness function kernel selected by the user. An
@ -221,9 +209,10 @@ class Base_GP(object):
func_dict = {'b':'files/functions_BOOL.csv', 'c':'files/functions_CLASSIFY.csv', 'r':'files/functions_REGRESS.csv', 'm':'files/functions_MATCH.csv', 'p':'files/functions_PLAY.csv'}
fitt_dict = {'b':'max', 'c':'max', 'r':'min', 'm':'max', 'p':''}
if len(sys.argv) == 1: # load data in the karoo_gp/files/ directory
if len(sys.argv) == 1: # load data from the default karoo_gp/files/ directory
data_x = np.loadtxt(data_dict[self.kernel], skiprows = 1, delimiter = ',', dtype = float); data_x = data_x[:,0:-1] # load all but the right-most column
data_y = np.loadtxt(data_dict[self.kernel], skiprows = 1, usecols = (-1,), delimiter = ',', dtype = float) # load only right-most column (class labels)
self.class_labels = len(np.unique(data_y))
header = open(data_dict[self.kernel],'r')
self.terminals = header.readline().split(','); self.terminals[-1] = self.terminals[-1].replace('\n','') # load the variables across the top of the .csv
@ -232,9 +221,9 @@ class Base_GP(object):
self.fitness_type = fitt_dict[self.kernel]
elif len(sys.argv) == 2: # load an external data file
print '\n\t\033[36m You have opted to load an alternative dataset:', sys.argv[1], '\033[0;0m'
data_x = np.loadtxt(sys.argv[1], skiprows = 1, delimiter = ',', dtype = float); data_x = data_x[:,0:-1] # load all but the right-most column
data_y = np.loadtxt(sys.argv[1], skiprows = 1, usecols = (-1,), delimiter = ',', dtype = float) # load only right-most column (class labels)
self.class_labels = len(np.unique(data_y))
header = open(sys.argv[1],'r')
self.terminals = header.readline().split(','); self.terminals[-1] = self.terminals[-1].replace('\n','') # load the variables across the top of the .csv
@ -242,7 +231,17 @@ class Base_GP(object):
self.functions = np.loadtxt(func_dict[self.kernel], delimiter=',', skiprows=1, dtype = str) # load the user defined functions (operators)
self.fitness_type = fitt_dict[self.kernel]
else: print '\n\t\033[31mERROR! You have assigned too many command line arguments at launch. Try again ...\033[0;0m'; sys.exit()
elif len(sys.argv) > 2: # receive filename and additional flags from karoo_gp_server.py via argparse
data_x = np.loadtxt(filename, skiprows = 1, delimiter = ',', dtype = float); data_x = data_x[:,0:-1] # load all but the right-most column
data_y = np.loadtxt(filename, skiprows = 1, usecols = (-1,), delimiter = ',', dtype = float) # load only right-most column (class labels)
self.class_labels = len(np.unique(data_y))
header = open(filename,'r')
self.terminals = header.readline().split(','); self.terminals[-1] = self.terminals[-1].replace('\n','') # load the variables across the top of the .csv
self.functions = np.loadtxt(func_dict[self.kernel], delimiter=',', skiprows=1, dtype = str) # load the user defined functions (operators)
self.fitness_type = fitt_dict[self.kernel]
### 2) from the dataset, generate TRAINING and TEST data ###
@ -356,7 +355,7 @@ class Base_GP(object):
def fx_karoo_construct(self, tree_type, tree_depth_base):
'''
As used by the method 'fx_karoo_gp', this method constructs the initial population based upon the user-defined
As used by the method 'karoo_gp', this method constructs the initial population based upon the user-defined
Tree type and initial, maximum Tree depth. "Ramped half/half" is currently not ramped, rather split 50/50
Full/Grow. This will be updated with a future version of Karoo GP.
@ -366,11 +365,7 @@ class Base_GP(object):
if self.display == 'i' or self.display == 'g':
print '\n\t Type \033[1m?\033[0;0m at any (pause) to review your options, or \033[1mENTER\033[0;0m to continue.\033[0;0m'
self.fx_karoo_pause(0)
if self.display == 's':
print '\n\t Type \033[1m?\033[0;0m to configure Karoo GP before your run, or \033[1mENTER\033[0;0m to continue.\033[0;0m'
self.fx_karoo_pause(0)
if tree_type == 'r': # Ramped 50/50
for TREE_ID in range(1, int(self.tree_pop_max / 2) + 1):
self.fx_gen_tree_build(TREE_ID, 'f', tree_depth_base) # build 1/2 of the 1st generation of Trees as Full
@ -537,7 +532,7 @@ class Base_GP(object):
print '\t\033[36m\033[1m i \t\033[0;0m interactive display mode'
print '\t\033[36m\033[1m m \t\033[0;0m minimal display mode'
print '\t\033[36m\033[1m g \t\033[0;0m generation display mode'
print '\t\033[36m\033[1m s \t\033[0;0m silent (server) display mode'
print '\t\033[36m\033[1m s \t\033[0;0m silent display mode'
print '\t\033[36m\033[1m db \t\033[0;0m debug display mode'
print '\t\033[36m\033[1m t \t\033[0;0m timer display mode'
print ''
@ -566,7 +561,7 @@ class Base_GP(object):
elif pause == 'i': self.display = 'i'; print '\t Interactive mode engaged (for control freaks)'
elif pause == 'm': self.display = 'm'; print '\t Minimal mode engaged (for recovering control freaks)'
elif pause == 'g': self.display = 'g'; print '\t Generation mode engaged (for GP gurus)'
elif pause == 's': self.display = 's'; print '\t Server mode engaged (for zen masters)'
# elif pause == 's': self.display = 's'; print '\t Server mode engaged (for zen masters)'
elif pause == 'db': self.display = 'db'; print '\t Debug mode engaged (for vouyers)'
elif pause == 't': self.display = 't'; print '\t Timer mode engaged (for managers)'

View File

@ -2,16 +2,36 @@
# Use Genetic Programming for Classification and Symbolic Regression
# by Kai Staats, MSc UCT / AIMS
# Much thanks to Emmanuel Dufourq and Arun Kumar for their support, guidance, and free psychotherapy sessions
# version 0.9.1.5
# version 0.9.1.6
'''
A NOTE TO THE NEWBIE, EXPERT, AND BRAVE
Even if you are highly experienced in Genetic Programming, it is recommended that you review the 'Karoo Quick Start' before running
this application. While your computer will not burst into flames nor will the sun collapse into a black hole if you do not, you will
likely find more enjoyment of this particular flavour of GP with a little understanding of its intent and design.
A word to the newbie, expert, and brave--
Even if you are highly experienced in Genetic Programming, it is recommended that you review the 'Karoo Quick Start'
before running this application. While your computer will not burst into flames nor will the sun collapse into a black
hole if you do not, you will likely find more enjoyment of this particular flavour of GP with a little understanding
of its intent and design.
KAROO GP DESKTOP
This is the Karoo GP desktop application. It presents a simple yet functional user interface for configuring each
Karoo GP run. While this can be launched on a remote server, you may find that once you get the hang of using Karoo,
and are in more of a production mode than one of experimentation, using karoo_gp_server.py is more to your liking as
it provides both a scripted and/or command-line launch vehicle.
To launch Karoo GP desktop:
$ python karoo_gp_main.py
(or from iPython)
$ run karoo_gp_main.py
If you include the path to an external dataset, it will auto-load at launch:
$ python karoo_gp_main.py /[path]/[to_your]/[filename].csv
'''
import sys # sys.path.append('modules/') # add the directory 'modules' to the current path
import sys # sys.path.append('modules/') to add the directory 'modules' to the current path
import karoo_gp_base_class; gp = karoo_gp_base_class.Base_GP()
#++++++++++++++++++++++++++++++++++++++++++
@ -26,7 +46,7 @@ Future versions will enable all of these parameters to be configured via an exte
command-line arguments passed at launch.
'''
gp.karoo_banner('main')
gp.karoo_banner()
print ''
@ -39,17 +59,6 @@ while True:
except ValueError: print '\t\033[32m Select from the options given. Try again ...\n\033[0;0m'
except KeyboardInterrupt: sys.exit()
if gp.kernel == 'c': # if the Classification kernel is selected (above)
menu = range(1,101)
while True:
try:
gp.class_labels = raw_input('\t Enter the number of class labels (default 3): ')
if gp.class_labels not in str(menu) or gp.class_labels == '0': raise ValueError()
gp.class_labels = gp.class_labels or 3; gp.class_labels = int(gp.class_labels); break
except ValueError: print '\t\033[32m Select from the options given. Try again ...\n\033[0;0m'
except KeyboardInterrupt: sys.exit()
menu = ['f','g','r','']
while True:
try:
@ -117,10 +126,10 @@ else: # if any other kernel is selected
except ValueError: print '\t\033[32m Enter a number from 1 including 100. Try again ...\n\033[0;0m'
except KeyboardInterrupt: sys.exit()
menu = ['i','m','g','s','db','t','']
menu = ['i','g','m','s','db','t','']
while True:
try:
gp.display = raw_input('\t Display (i)nteractive, (m)iminal, (g)eneration, or (s)ilent (default m): ')
gp.display = raw_input('\t Display (i)nteractive, (g)eneration, (m)iminal, or (s)ilent (default m): ')
if gp.display not in menu: raise ValueError()
gp.display = gp.display or 'm'; break
except ValueError: print '\t\033[32m Select from the options given. Try again ...\n\033[0;0m'
@ -128,18 +137,15 @@ else: # if any other kernel is selected
# define the ratio between types of mutation, where all sum to 1.0; can be adjusted in 'i'nteractive mode
gp.evolve_repro = int(0.0 * gp.tree_pop_max) # percentage of subsequent population to be generated through Reproduction
gp.evolve_point = int(0.0 * gp.tree_pop_max) # percentage of subsequent population to be generated through Point Mutation
gp.evolve_branch = int(0.0 * gp.tree_pop_max) # percentage of subsequent population to be generated through Branch Mutation
gp.evolve_cross = int(1.0 * gp.tree_pop_max) # percentage of subsequent population to be generated through Crossover Reproduction
gp.evolve_repro = int(0.1 * gp.tree_pop_max) # percentage of subsequent population to be generated through Reproduction
gp.evolve_point = int(0.1 * gp.tree_pop_max) # percentage of subsequent population to be generated through Point Mutation
gp.evolve_branch = int(0.2 * gp.tree_pop_max) # percentage of subsequent population to be generated through Branch Mutation
gp.evolve_cross = int(0.6 * gp.tree_pop_max) # percentage of subsequent population to be generated through Crossover
gp.tourn_size = 10 # qty of individuals entered into each tournament (standard 10); can be adjusted in 'i'nteractive mode
gp.cores = 1 # replace '1' with 'int(gp.core_count)' to auto-set to max; can be adjusted in 'i'nteractive mode
gp.precision = 4 # the number of floating points for the round function in 'fx_fitness_eval'; hard coded
# if len(sys.argv) == 2: # look for an argument when Karoo GP is launched
# gp.data_load = int(sys.argv[1]) # assign file for the data load method in karoo_base_class
#++++++++++++++++++++++++++++++++++++++++++
# Construct First Generation of Trees |
@ -152,7 +158,8 @@ constructed from scratch. All parameters which define the Trees were set by the
If the user has selected 'Play' mode, this is the only generation to be constructed, and then GP Karoo terminates.
'''
gp.fx_karoo_data_load()
filename = '' # temp place holder
gp.fx_karoo_data_load(tree_type, tree_depth_base, filename)
gp.generation_id = 1 # set initial generation ID
gp.population_a = ['Karoo GP by Kai Staats, Generation ' + str(gp.generation_id)] # an empty list which will store all Tree arrays, one generation at a time

Binary file not shown.

View File

@ -2,37 +2,88 @@
# Use Genetic Programming for Classification and Symbolic Regression
# by Kai Staats, MSc UCT / AIMS
# Much thanks to Emmanuel Dufourq and Arun Kumar for their support, guidance, and free psychotherapy sessions
# version 0.9.1.5
# version 0.9.1.6
'''
A NOTE TO THE NEWBIE, EXPERT, AND BRAVE
Even if you are highly experienced in Genetic Programming, it is recommended that you review the 'Karoo Quick Start' before running
this application. While your computer will not burst into flames nor will the sun collapse into a black hole if you do not, you will
likely find more enjoyment of this particular flavour of GP with a little understanding of its intent and design.
A word to the newbie, expert, and brave--
Even if you are highly experienced in Genetic Programming, it is recommended that you review the 'Karoo Quick Start'
before running this application. While your computer will not burst into flames nor will the sun collapse into a black
hole if you do not, you will likely find more enjoyment of this particular flavour of GP with a little understanding
of its intent and design.
KAROO GP SERVER
This is the Karoo GP server application. It can be internally scripted, fully command-line configured, or a combination
of both. If this is your first time using Karoo GP, please run the desktop application karoo_gp_main.py first in order
that you come to understand its full functionality.
To launch Karoo GP server:
$ python karoo_gp_server.py
(or from iPython)
$ run karoo_gp_server.py
Without any arguments, Karoo GP relies entirely upon the scripted settings and the datasets located in karoo_gp/files/.
If you include the path to an external dataset, it will auto-load at launch:
$ python karoo_gp_server.py /[path]/[to_your]/[filename].csv
You can include a number of additional arguments which override the default values, as follows:
-ker [r,c,m] fitness function: (r)egression, (c)lassification, or (m)atching
-typ [f,g,r] Tree type: (f)ull, (g)row, or (r)amped half/half
-bas [3...10] maximum Tree depth for the initial population
-max [3...10] maximum Tree depth for the entire run
-min [3...100] minimum number of nodes
-pop [10...1000] maximum population
-gen [1...100] number of generations
Note that if you include any of the above flags, then you must also include a flag to load an external dataset:
$ python karoo_gp_server.py -ker c -typ r -bas 4 -fil /[path]/[to_your]/[filename].csv
'''
import sys # sys.path.append('modules/') # add the directory 'modules' to the current path
import sys # sys.path.append('modules/') to add the directory 'modules' to the current path
import argparse
import karoo_gp_base_class; gp = karoo_gp_base_class.Base_GP()
# parameters configuration
gp.kernel = 'c' # ['r','c','m'] fitness function: (r)egression, (c)lassification, or (m)atching
gp.class_labels = 3 # [2,3, ...] number of class labels in the feature set
tree_type = 'r' # ['f','g','r'] Tree (t)ype: (f)ull, (g)row, or (r)amped half/half
tree_depth_base = 3 # [3,10] maximum Tree depth for the initial population, where nodes = 2^(depth + 1) - 1
gp.tree_depth_max = 3 # [3,10] maximum Tree depth for the entire run; introduces potential bloat
gp.tree_depth_min = 3 # [3,100] minimum number of nodes
gp.tree_pop_max = 100 # [10,1000] maximum population
gp.generation_max = 10 # [1,100] number of generations
gp.display = 'm' # ['i','m','g','s'] display mode: (i)nteractive, (m)inimal, (g)enerational, or (s)erver
ap = argparse.ArgumentParser(description = 'Karoo GP Server')
ap.add_argument('-ker', action = 'store', dest = 'kernel', default = 'm', help = '[r,c,m] fitness function: (r)egression, (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 = 3, help = '[3...10] maximum Tree depth for the initial population')
ap.add_argument('-max', action = 'store', dest = 'depth_max', default = 3, help = '[3...10] maximum Tree depth for the entire run')
ap.add_argument('-min', action = 'store', dest = 'depth_min', default = 3, help = '[3...100] minimum number of nodes')
ap.add_argument('-pop', action = 'store', dest = 'pop_max', default = 100, help = '[10...1000] maximum population')
ap.add_argument('-gen', action = 'store', dest = 'gen_max', default = 10, help = '[1...100] number of generations')
ap.add_argument('-fil', action = 'store', dest = 'filename', default = 'files/data_MATCH.csv', help = '/path/to_your/data.csv')
args = ap.parse_args()
# set the same parameters found in the Karoo GP desktop application, but potentially passed from the command line
gp.kernel = str(args.kernel)
tree_type = str(args.type)
tree_depth_base = int(args.depth_base)
gp.tree_depth_max = int(args.depth_max)
gp.tree_depth_min = int(args.depth_min)
gp.tree_pop_max = int(args.pop_max)
gp.generation_max = int(args.gen_max)
filename = str(args.filename)
gp.display = 'n' # display mode is set to (s)ilent
gp.evolve_repro = int(0.1 * gp.tree_pop_max) # percentage of subsequent population to be generated through Reproduction
gp.evolve_point = int(0.1 * gp.tree_pop_max) # percentage of subsequent population to be generated through Point Mutation
gp.evolve_branch = int(0.2 * gp.tree_pop_max) # percentage of subsequent population to be generated through Branch Mutation
gp.evolve_cross = int(0.6 * gp.tree_pop_max) # percentage of subsequent population to be generated through Crossover Reproduction
gp.evolve_cross = int(0.6 * gp.tree_pop_max) # percentage of subsequent population to be generated through Crossover
gp.tourn_size = 10 # qty of individuals entered into each tournament (standard 10); can be adjusted in 'i'nteractive mode
gp.cores = 1 # replace '1' with 'int(gp.core_count)' to auto-set to max; can be adjusted in 'i'nteractive mode
gp.precision = 4 # the number of floating points for the round function in 'fx_fitness_eval'; hard coded
# run Karoo GP
gp.karoo_gp('server', tree_type, tree_depth_max)
gp.karoo_gp(tree_type, tree_depth_base, filename)