def test_predict(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 function_set = [ x for x in RootGP()._function_set if x.regression and x.nargs ] gp = RootGP(generations=np.inf, tournament_size=2, function_set=function_set, early_stopping_rounds=-1, classifier=False, remove_raw_inputs=False, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) es = gp.population.estopping assert gp.decision_function().SSE(es.hy_test) == 0 hy_test = es.hy_test assert gp.decision_function(X=X[-10:]).SSE(hy_test) == 0 hy = gp.decision_function(X=X[-10:]) _ = gp.predict(X=X[-10:]) assert SparseArray.fromlist(_).SSE(hy) == 0 assert len(gp.Xtest)
def test_regression_y(): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=4, classifier=False) assert not gp._classifier gp.X = X gp.y = cl.copy() assert gp._ytr.SSE(gp.y) > 0 gp = RootGP(generations=1, popsize=4, classifier=False, tr_fraction=1.0) gp.X = X gp.y = cl.copy() assert gp._ytr.SSE(gp.y) == 0
def test_model_graphviz(): from EvoDAG import RootGP from EvoDAG.node import Function import tempfile y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=3, tournament_size=2, early_stopping_rounds=-1, classifier=False, pr_variable=1, seed=0, popsize=10).fit(X, y) m = gp.model() print(m._hist) print(m._hist[-1].position, m._hist[-1]._variable) with tempfile.TemporaryFile('w+') as io: m.graphviz(io) io.seek(0) l = io.readlines() cnt = len(m._hist) for k in m._hist: if isinstance(k, Function): v = k._variable if isinstance(v, list): cnt += len(v) else: cnt += 1 print("".join(l)) print(cnt, len(l)) assert 2 + cnt == len(l)
def test_X_sparse(): from EvoDAG import RootGP gp = RootGP(generations=1, tournament_size=2, popsize=10) X1 = list(map(SparseArray.fromlist, X.T)) gp.X = X1
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_ensemble_model(): from EvoDAG import RootGP from EvoDAG.model import Ensemble y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gps = [ RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, classifier=False, seed=seed, popsize=10).fit(X[:-10], y[:-10], test_set=X) for seed in range(3) ] ens = Ensemble([gp.model() for gp in gps]) res = [gp.decision_function() for gp in gps] res = np.median([x.full_array() for x in res], axis=0) res = SparseArray.fromlist(res) print(res) # res = Add.cumsum(res) * (1 / 3.) r2 = ens.decision_function(None) print(res.full_array()[:10], r2.full_array()[:10]) print(res.SSE(r2)) assert res.SSE(r2) == 0
def test_fit(): from EvoDAG import RootGP function_set = [x for x in RootGP()._function_set if x.regression] y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, function_set=function_set, classifier=False, seed=0, popsize=4).fit(X, y, test_set=X) assert np.isfinite(gp.population.estopping.fitness) assert np.isfinite(gp.population.estopping.fitness_vs) assert gp.population.estopping.hy.isfinite() assert len(gp.population.hist) > 4
def create_problem_node2(nargs=4, seed=0): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=nargs, multiple_outputs=True, seed=seed) gp.X = X gp.Xtest = X y = cl.copy() gp.nclasses(y) gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_features(): from EvoDAG import RootGP gp = RootGP(generations=1) gp.X = X assert gp.nvar == 4 print(gp.X) assert isinstance(gp.X[0], Variable) assert isinstance(gp.X[0].hy, SparseArray) gp.Xtest = X assert gp.X[0].hy.SSE(gp.X[0].hy_test) == 0
def test_fname(): from EvoDAG.node import Add from EvoDAG import RootGP x = np.linspace(-1, 1, 100) y = 4.3 * x**2 + 3.2 * x - 3.2 Add.nargs = 10 gp = RootGP(classifier=False, popsize=10, generations=2).fit([SparseArray.fromlist(x)], y, test_set=[SparseArray.fromlist(x)]) assert gp.signature.count('Ad10') == 1
def test_fitness(): 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 l = gp.random_leaf() assert l.fitness < 0
def create_problem_node(nargs=4, seed=0): from EvoDAG import RootGP gp = RootGP(generations=1, popsize=nargs, classifier=False, seed=seed) gp.X = X gp.Xtest = X y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_logging(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, classifier=False, seed=0, popsize=10).fit(X, y, test_set=X)
def test_get_params(): from EvoDAG import RootGP # y = cl.copy() gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=0, popsize=100) p = gp.get_params() assert p['generations'] == np.inf assert p['popsize'] == 100 assert p['seed'] == 0
def test_random_selection(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 RootGP(generations=np.inf, tournament_size=1, early_stopping_rounds=-1, classifier=False, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:])
def test_get_clone(): from EvoDAG import RootGP # y = cl.copy() gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, seed=0, popsize=100) gp1 = gp.clone() print(gp.popsize, gp1.popsize) assert gp.popsize == gp1.popsize assert gp._generations == gp1._generations assert gp._seed == gp1._seed
def test_classification_y(): from EvoDAG import RootGP gp = RootGP(generations=1, multiple_outputs=True, popsize=4) assert gp._classifier gp.X = X y = cl.copy() gp.nclasses(y) gp.y = y print(gp._ytr, gp.y) for a, b in zip(gp._ytr, gp.y): assert a.SSE(b) > 0 assert a.sum() == 0 assert b.sum() < 0
def test_fit_stopping_criteria_estopping(): from EvoDAG import RootGP function_set = [x for x in RootGP()._function_set if x.regression] gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=4, function_set=function_set, 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_es_extra_test(): from EvoDAG import RootGP x = np.linspace(-1, 1, 100) y = 4.3 * x**2 + 3.2 * x - 3.2 es_extra_test = RootGP.es_extra_test RootGP.es_extra_test = MagicMock(side_effect=RuntimeError('Mock')) try: RootGP(classifier=False, popsize=10, generations=2).fit([SparseArray.fromlist(x)], y, test_set=[SparseArray.fromlist(x)]) assert False except RuntimeError: RootGP.es_extra_test = es_extra_test
def test_class_values(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 0 y[~mask] = -1 gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, classifier=True, multiple_outputs=True, seed=0, popsize=10).fit(X[:-10], y[:-10], test_set=X[-10:]) assert np.all(gp._labels == np.array([-1, 0]))
def create_problem_node2(nargs=4, seed=0): from EvoDAG import RootGP from test_root import X, cl import numpy as np gp = RootGP(generations=1, popsize=nargs, multiple_outputs=True, seed=seed) X1 = np.concatenate((X, np.atleast_2d(np.zeros(X.shape[0])).T), axis=1) for i in range(10, 20): X1[i, -1] = 1 gp.X = X1 gp.Xtest = X1 y = cl.copy() gp.nclasses(y) gp.y = y return gp, [gp.X[x] for x in range(nargs)]
def test_regression(): from EvoDAG import RootGP x = np.linspace(-1, 1, 100) y = 4.3 * x**2 + 3.2 * x - 3.2 gp = RootGP(classifier=False, popsize=10, generations=2).fit([SparseArray.fromlist(x)], y, test_set=[SparseArray.fromlist(x)]) model = gp.model() yh = gp.predict() assert not model._classifier yh1 = model.predict(X=[SparseArray.fromlist(x)]) spf = SparseArray.fromlist assert spf(yh).SSE(spf(yh1)) == 0
def test_fit_stopping_criteria_gens(): from EvoDAG import RootGP from EvoDAG.node import Add Add.nargs = 2 function_set = [x for x in RootGP()._function_set if x.regression] gp = RootGP(generations=2, early_stopping_rounds=None, tournament_size=2, function_set=function_set, 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_infite_evolution(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 y[~mask] = -1 try: RootGP(generations=np.inf, tournament_size=2, tr_fraction=1, early_stopping_rounds=-1, seed=0, popsize=10).fit(X, y, test_set=X) assert False except RuntimeError: pass
def test_unique(): from EvoDAG import RootGP mock = MagicMock(side_effect=RuntimeError('Mock')) ui = RootGP.unique_individual RootGP.unique_individual = mock gp = RootGP(generations=np.inf, tournament_size=2, unique_individuals=True, multiple_outputs=True, early_stopping_rounds=-1, seed=0, popsize=100) try: gp.fit(X, cl) except RuntimeError: pass RootGP.unique_individual = ui
def test_labels(): from EvoDAG import RootGP y = cl.copy() mask = y == 0 y[mask] = 1 gp = RootGP(generations=np.inf, tournament_size=2, early_stopping_rounds=-1, multiple_outputs=True, seed=0, popsize=100).fit(X, y, test_set=X) m = gp.model() hy = m.predict(X=X) print(np.unique(hy), np.unique(y)) print(np.array([1, 2])) for k in np.unique(hy): assert k in [1, 2]
def test_regression(): from EvoDAG import RootGP from EvoDAG.model import Ensemble x = np.linspace(-1, 1, 100) y = 4.3 * x**2 + 3.2 * x - 3.2 gps = [ RootGP(classifier=False, seed=seed, popsize=10, generations=2).fit([SparseArray.fromlist(x)], y, test_set=[SparseArray.fromlist(x)]) for seed in range(3) ] ens = Ensemble([gp.model() for gp in gps]) hy = np.median([gp.predict() for gp in gps], axis=0) hy1 = ens.predict(X=[SparseArray.fromlist(x)]) print(hy, hy1) assert np.all(hy == hy1)
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]