def test_multiple_variables(): import numpy as np from EvoDAG.population import Inputs from EvoDAG.cython_utils import SelectNumbers from EvoDAG import EvoDAG from SparseArray import SparseArray y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.X = X gp.X[-1]._eval_tr = SparseArray.fromlist( [0 for x in range(gp.X[-1].hy.size())]) gp.nclasses(y) gp.y = y inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))])) inputs._func = [inputs._func[-1]] inputs._nfunc = 1 v = inputs.input() assert v is not None mask = np.array(gp._mask[0].full_array(), dtype=np.bool) D = np.array([x.hy.full_array() for x in gp.X]).T b = np.array(gp._ytr[0].full_array()) coef = np.linalg.lstsq(D[mask], b[mask])[0] for a, b in zip(coef, v.weight[0]): assert_almost_equals(a, b)
def test_inputs(): from EvoDAG.population import Inputs from EvoDAG.cython_utils import SelectNumbers from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.X = X gp.nclasses(y) gp.y = y inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))])) func = inputs._func for f in func: inputs._func = [f] inputs._nfunc = 1 v = inputs.input() assert v is not None inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))]))
def test_all_variables_inputs(): from EvoDAG.population import Inputs from EvoDAG.cython_utils import SelectNumbers from EvoDAG import EvoDAG y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, use_all_vars_input_functions=True, popsize=5, share_inputs=True) gp.X = X gp.nclasses(y) gp.y = y inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))])) func = inputs._func print(func, gp.nvar) for f in func: v = inputs.all_variables() assert v is not None assert isinstance(v, f) assert inputs._all_variables_index == len(func)
def test_restrictions(): from EvoDAG.population import Inputs from EvoDAG.cython_utils import SelectNumbers from EvoDAG import EvoDAG from SparseArray import SparseArray y = cl.copy() gp = EvoDAG(classifier=True, multiple_outputs=True, popsize=5, share_inputs=True) gp.X = X gp.X[-1]._eval_tr = SparseArray.fromlist( [0 for x in range(gp.X[-1].hy.size())]) gp.nclasses(y) gp.y = y for c in [True, False]: gp._classifier = c tag = 'classification' if c else 'regression' inputs = Inputs(gp, SelectNumbers([x for x in range(len(gp.X))])) for x in inputs._funcs: print(x, tag) assert getattr(x, tag)
def test_selectNumbers(): from EvoDAG.cython_utils import SelectNumbers s = SelectNumbers([x for x in range(100)]) a = s.get(10) print(a, len(a)) assert len(a) == 10 a = s.get(91) assert len(a) == 90 s.pos -= 10 a = s.get(10) assert len(a) == 10 assert s.pos == 100 assert s.empty() s.pos = 0 while not s.empty(): s.get_one() s = SelectNumbers([]) assert s.empty()