def test_lexicase_shapes(): """test_selection.py: lexicase selection returns correct shape""" few = FEW(seed_with_ml=False, population_size=257) few.term_set = [node('x', loc=0)] pop = few.init_pop() offspring, locs = few.lexicase(pop.individuals) assert len(offspring) == 257 # smaller popsize than tournament size few = FEW(seed_with_ml=False, population_size=2) few.term_set = [node('x', loc=0)] pop = few.init_pop() offspring, locs = few.lexicase(pop.individuals) assert len(offspring) == 2
def test_epsilon_lexicase_shapes(): """test_selection.py: epsilon lexicase selection returns correct shape""" np.random.seed(42) few = FEW(seed_with_ml=False, population_size=257, lex_size=False) few.term_set = [node('x', loc=0)] pop = few.init_pop() offspring = few.epsilon_lexicase(np.random.rand(257, 100), []) assert len(offspring) == 257 # smaller popsize than tournament size few = FEW(seed_with_ml=False, population_size=2, lex_size=False) few.term_set = [node('x', loc=0)] pop = few.init_pop() offspring = few.epsilon_lexicase(np.random.rand(2, 100), []) assert len(offspring) == 2
def test_out_shapes(): """test_evaluation.py: program output is correct size """ # load test data set boston = load_boston() # boston.data = boston.data[::10] # boston.target = boston.target[::10] n_features = boston.data.shape[1] # function set # terminal set term_set = [] # numbers represent column indices of features for i in np.arange(n_features): term_set.append(node('x', loc=i)) # features # term_set.append(('k',0,np.random.rand())) # ephemeral random constants # initialize population pop_size = 5 few = FEW(population_size=pop_size, seed_with_ml=False) few.term_set = term_set few.n_features = n_features pop = few.init_pop() pop.X = np.asarray( list(map(lambda I: few.out(I, boston.data), pop.individuals))) #pop.X = out(pop.individuals[0],boston.data,boston.target) print("pop.X.shape:", pop.X.shape) print("boston.target.shape", boston.target.shape) assert pop.X.shape == (pop_size, boston.target.shape[0])
def test_calc_fitness_shape(): """test_evaluation.py: calc_fitness correct shapes """ # load test data set boston = load_boston() # boston.data = boston.data[::10] # boston.target = boston.target[::10] n_features = boston.data.shape[1] # terminal set term_set = [] # numbers represent column indices of features for i in np.arange(n_features): term_set.append(node('x', loc=i)) # features # term_set.append(('k',0,np.random.rand())) # ephemeral random constants # initialize population pop_size = 5 few = FEW(population_size=pop_size, seed_with_ml=False) few.term_set = term_set few.n_features = n_features pop = few.init_pop() pop.X = np.asarray( list(map(lambda I: few.out(I, boston.data), pop.individuals))) fitnesses = few.calc_fitness(pop.X, boston.target, 'mse', 'tournament') assert len(fitnesses) == len(pop.individuals) # test vectorized fitnesses vec_fitnesses = few.calc_fitness(pop.X, boston.target, 'mse', 'lexicase') fitmat = np.asarray(vec_fitnesses) print("fitmat.shape:", fitmat.shape) assert fitmat.shape == (len(pop.individuals), boston.target.shape[0])
def test_lexicase_survival_shapes(): """test_selection.py: lexicase survival returns correct shape""" # func_set = [node('+'), node('-'), node('*'), node('/'), node('sin'), # node('cos'), node('exp'),node('log'), node('^2'), # node('^3'), node('sqrt')] # terminal set term_set = [] n_features = 3 # numbers represent column indices of features # for i in np.arange(n_features): # term_set.append(node('x',loc=i)) # features term_set = [node('x', loc=i) for i in np.arange(n_features)] # term_set.append(('erc',0,np.random.rand())) # ephemeral random constants few = FEW(seed_with_ml=False, population_size=257) few.term_set = term_set pop = few.init_pop() for i in pop.individuals: i.fitness_vec = list(np.random.rand(10, 1)) offspring, locs = few.lexicase(pop.individuals, num_selections=100, survival=True) assert len(offspring) == 100 # smaller popsize than tournament size ew = FEW(seed_with_ml=False, population_size=2) few.term_set = term_set pop = few.init_pop() for i in pop.individuals: i.fitness_vec = np.random.rand(10, 1) offspring, locs = few.lexicase(pop.individuals, num_selections=1, survival=True) assert len(offspring) == 1
def test_pop_init(): """test_population.py: population initialization makes valid trees """ # define function set # function set # func_set = [('+',2),('-',2),('*',2),('/',2),('sin',1),('cos',1),('exp',1),('log',1)] # terminal set term_set = [] n_features = 3 # numbers represent column indices of features term_set = [node('x',loc=i) for i in np.arange(n_features)] # term_set.append(('erc',0,np.random.rand())) # ephemeral random constants few = FEW(seed_with_ml=False) few.term_set = term_set few.n_features=n_features pop = few.init_pop() for I in pop.individuals: assert is_valid_program(I.stack)