def setUp(self): """Setup Tests""" # Setup up variable and truth configuration eugene.Config.VAR['x'] = np.linspace(0, 8.0 * np.pi, 1024) eugene.Config.TRUTH = eugene.Config.VAR['x'] * np.sin(eugene.Config.VAR['x']) + eugene.Config.VAR['x']/2.0 + 1.61 def error_and_complexity(gene_expression, scale): """user fitness function, weighted combination of error and complexity""" weights = np.array([0.95, 0.025, 0.025]) scaled_gene_expression = 1.0 / (gene_expression / scale) return np.dot(scaled_gene_expression, weights) # Setup Populations self.P1 = Population( init_population_size=100, objective_function=error_and_complexity, max_generations=100, init_tree_size=2, target=eugene.Config.TRUTH, pruning=False )
class PopulationTests(unittest.TestCase): """ Test the Population Module. """ def setUp(self): """Setup Tests""" # Setup up variable and truth configuration eugene.Config.VAR['x'] = np.linspace(0, 8.0 * np.pi, 1024) eugene.Config.TRUTH = eugene.Config.VAR['x'] * np.sin(eugene.Config.VAR['x']) + eugene.Config.VAR['x']/2.0 + 1.61 def error_and_complexity(gene_expression, scale): """user fitness function, weighted combination of error and complexity""" weights = np.array([0.95, 0.025, 0.025]) scaled_gene_expression = 1.0 / (gene_expression / scale) return np.dot(scaled_gene_expression, weights) # Setup Populations self.P1 = Population( init_population_size=100, objective_function=error_and_complexity, max_generations=100, init_tree_size=2, target=eugene.Config.TRUTH, pruning=False ) def test_29_initialize_population(self): """Initialize a Random Population""" self.P1.initialize() def test_30_initialize_population(self): """Initialize a seeded Population""" inds = [Individual(Tree( Node('n_add', Node('n_pow', Node('x'), Node(2.0) ), Node('n_pow', Node('y'), Node(2.0) ) ) ))] self.P1.initialize(inds) def test_31_display_population(self): """Display Population Info""" self.P1.initialize() self.P1.describe() def test_32_run_population_roulette(self): """Run Population for 10 generations with roulette selection""" self.P1.initialize() self.P1.run(10) def test_33_run_population_pruning(self): """Run Population for 10 generations with pruning""" self.P1.pruning = True self.P1.initialize() self.P1.run(10) def test_34_run_population_stochastic(self): """Run Population for 10 generations with stochastic selection""" self.P1.selectmethod = 'stochastic' self.P1.initialize() self.P1.run(10) def test_35_run_population_tournament(self): """Run Population for 10 generations with tournament selection""" self.P1.selectmethod = 'tournament' self.P1.initialize() self.P1.run(10) def test_36_run_population_rank_roulette(self): """Run Population for 10 generations with rank roulette selection""" self.P1.selectmethod = 'rank_roulette' self.P1.initialize() self.P1.run(10)
from eugene.Population import Population # Setup up variable and truth configuration eugene.Config.VAR['x'] = np.linspace(0, 8.0 * np.pi, 1024) eugene.Config.TRUTH = eugene.Config.VAR['x'] * np.sin(eugene.Config.VAR['x']) + eugene.Config.VAR['x']/2.0 + 1.61 # @profile def error_and_complexity(gene_expression, scale): """user fitness function, weighted combination of error and complexity""" weights = np.array([0.95, 0.025, 0.025]) scaled_gene_expression = 1.0 / (gene_expression / scale) return np.dot(scaled_gene_expression, weights) # Setup Population P = Population( init_population_size=1000, objective_function=error_and_complexity, max_generations=100, init_tree_size=2, target=eugene.Config.TRUTH, pruning=False ) # Initialize Population P.initialize() # Run the Population P.run(20)