def test_trace(): from EvoDAG import RootGP from EvoDAG.node import Add y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 Add.nargs = 3 gp = RootGP(generations=np.inf, tournament_size=2, function_set=[Add], classifier=False, early_stopping_rounds=-1, seed=0, popsize=4) gp.X = X[:-10] gp.Xtest = X[-10:] gp.y = y[:-10] gp.create_population() a = gp.random_offspring() gp.population.replace(a) print(a.position, a.variable, a._weight, gp.population.hist[0].variable) s = gp.trace(a) print(len(s), s) assert a._weight.shape[0] + 1 == len(s)
def test_create_population2(): from EvoDAG import RootGP from EvoDAG.node import Function gp = RootGP(generations=1, popsize=10) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() for i in gp.population.population[4:]: assert isinstance(i, Function)
def test_replace_individual(): from EvoDAG import RootGP gp = RootGP(generations=1, tournament_size=2, classifier=False, popsize=5) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() print(gp.population.popsize) a = gp.random_offspring() assert a.position == 0 gp.population.replace(a) assert np.any([x == a for x in gp.population.population]) print(a.position, len(gp.population.population)) assert a.position == len(gp.population.population)
def test_tournament_negative(): from EvoDAG import RootGP gp = RootGP(generations=1, tournament_size=4, classifier=False, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y randint = np.random.randint mock = MagicMock() mock.side_effect = list(range(gp.popsize)) np.random.randint = mock gp.create_population() j = gp.population.tournament(negative=True) index = np.argsort([x.fitness for x in gp.population.population])[0] assert j == index np.random.randint = randint
def test_best_so_far(): from EvoDAG import RootGP gp = RootGP(generations=1, classifier=False, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y # randint = np.random.randint # mock = MagicMock() # mock.side_effect = list(range(gp.popsize)) # np.random.randint = mock gp.create_population() p = gp.population.population index = np.argsort([x.fitness for x in p])[-1] print(p[index].fitness, gp.population.bsf.fitness) assert gp.population.bsf == p[index]
def test_fit_stopping_criteria_estopping(): from EvoDAG import RootGP gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=4, seed=0, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() while not gp.stopping_criteria(): a = gp.random_offspring() gp.population.replace(a) print(len(gp.population.hist) - gp.population.estopping.position) assert (len(gp.population.hist) - gp.population.estopping.position) <= 9
def test_replace_individual(): from EvoDAG import RootGP gp = RootGP(generations=1, tournament_size=2, popsize=5) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() print(gp.population.popsize) a = gp.random_offspring() assert a.position == 0 gp.population.replace(a) assert np.any([x == a for x in gp.population.population]) print(a.position, len(gp.population.population)) assert a.position == len(gp.population.population)
def test_best_so_far(): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y randint = np.random.randint mock = MagicMock() mock.side_effect = list(range(gp.popsize)) np.random.randint = mock gp.create_population() p = gp.population.population index = np.argsort([x.fitness for x in p])[-1] print(p[index].fitness, gp.population.bsf.fitness) assert gp.population.bsf == p[index] np.random.randint = randint
def test_tournament_negative(): from EvoDAG import RootGP gp = RootGP(generations=1, tournament_size=4, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y randint = np.random.randint mock = MagicMock() mock.side_effect = list(range(gp.popsize)) np.random.randint = mock gp.create_population() j = gp.population.tournament(negative=True) index = np.argsort([x.fitness for x in gp.population.population])[0] assert j == index np.random.randint = randint
def test_early_stopping(): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y randint = np.random.randint mock = MagicMock() mock.side_effect = list(range(gp.popsize)) np.random.randint = mock gp.create_population() p = gp.population.population fit = np.array([x.fitness_vs for x in p]) best = fit.max() index = np.where(best == fit)[0][0] assert gp.population.estopping == p[index] np.random.randint = randint
def test_fit_stopping_criteria_estopping(): from EvoDAG import RootGP gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=4, classifier=False, seed=0, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() while not gp.stopping_criteria(): a = gp.random_offspring() gp.population.replace(a) print(len(gp.population.hist) - gp.population.estopping.position) assert (len(gp.population.hist) - gp.population.estopping.position) <= 9
def test_create_population(): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() assert_almost_equals(gp.population.popsize, gp.popsize) # a = map(lambda (x, y): x == y, zip(gp.population.population, # gp.population._hist)) a = [x == y1 for x, y1 in zip(gp.population.population, gp.population._hist)] assert np.all(a) l = [x.variable for x in gp.population.population] l.sort() # assert np.all(map(lambda (x, y): x == y, enumerate(l))) assert np.all([x == y1 for x, y1 in enumerate(l)])
def test_early_stopping(): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4, classifier=False) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y randint = np.random.randint mock = MagicMock() mock.side_effect = list(range(gp.popsize)) np.random.randint = mock gp.create_population() p = gp.population.population fit = np.array([x.fitness_vs for x in p]) best = fit.max() index = np.where(best == fit)[0][0] assert gp.population.estopping == p[index] np.random.randint = randint
def test_height(): from EvoDAG import RootGP from EvoDAG.node import Mul gp = RootGP(generations=1, seed=1, tournament_size=2, popsize=5) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() assert np.all([x.height == 0 for x in gp.population.population[:4]]) n = gp.population.population[-1] assert n.height == 1 args = [3, 4] f = gp._random_offspring(Mul, args) assert f.height == 2
def test_fit_stopping_criteria_gens(): from EvoDAG import RootGP from EvoDAG.node import Add Add.nargs = 2 gp = RootGP(generations=2, early_stopping_rounds=None, tournament_size=2, seed=1, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() for i in range(gp.popsize): assert not gp.stopping_criteria() a = gp.random_offspring() gp.population.replace(a) assert gp.stopping_criteria()
def test_fit_stopping_criteria_gens(): from EvoDAG import RootGP from EvoDAG.node import Add Add.nargs = 2 gp = RootGP(generations=2, early_stopping_rounds=None, tournament_size=2, classifier=False, seed=1, popsize=4) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() for i in range(gp.popsize): assert not gp.stopping_criteria() a = gp.random_offspring() gp.population.replace(a) assert gp.stopping_criteria()
def test_random_offspring(): from EvoDAG import RootGP from EvoDAG.node import Add gp = RootGP(generations=1, seed=1, tournament_size=2, popsize=10) gp.X = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y gp.create_population() randint = np.random.randint mock = MagicMock(return_value=0) np.random.randint = mock a = gp.random_offspring() np.random.randint.assert_called_with(len(gp.function_set)) assert isinstance(a, Add) assert np.isfinite(a.fitness) np.random.randint = randint
def test_trace(): from EvoDAG import RootGP from EvoDAG.node import Add y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 Add.nargs = 3 gp = RootGP(generations=np.inf, tournament_size=2, function_set=[Add], early_stopping_rounds=-1, seed=0, popsize=4) gp.X = X[:-10] gp.Xtest = X[-10:] gp.y = y[:-10] gp.create_population() a = gp.random_offspring() gp.population.replace(a) print(a.position, a.variable, a._weight, gp.population.hist[0].variable) s = gp.trace(a) assert len(s) == 4