def test_depart_tree(self): from numpy import random random.seed(1) for i in range(10): sl = SymbolTree.genGrow(self.pset, 5, 6) sl_departs = sl.depart() for i in sl_departs: cpsl = self.cp.calculate_simple(i) self.assertIsNotNone(cpsl.y_dim) self.assertIsNotNone(cpsl.expr) self.assertIsNone(cpsl.p_name)
def test_barch_tree(self): from numpy import random random.seed(1) for i in range(10): sl = SymbolTree.genGrow(self.pset, 3, 4) cpsl = self.cp.calculate_detail(sl) self.assertIsNotNone(cpsl.y_dim) self.assertIsNotNone(cpsl.expr) self.assertIsNone(cpsl.p_name) if cpsl.pre_y is not None: self.assertIsInstance(cpsl.pre_y, numpy.ndarray) self.assertEqual(cpsl.pre_y.shape, self.y.shape) print(cpsl.coef_pre_y[:3]) print(cpsl.pre_y[:3]) print(cpsl.coef_score) print(cpsl.coef_expr) print(cpsl.pure_expr)
pset.add_operations(categories=("Add", "Mul", "Sub", "Div")) s = pset.free_symbol[1] ss = [] for si in s: if isinstance(si, sympy.Symbol): ss.append(si) else: ss.extend(si) target = (ss[0] + ss[1]) * (ss[2] - ss[3]) target = sympy.simplify(target) # a = time.time() random.seed(4) population = [ SymbolTree.genFull(pset, int(height - 1), int(height - 1) + 1) for _ in range(5000) ] for n, i in enumerate(population): i = compile_context(i, pset.context, pset.gro_ter_con, simplify=True) expr = general_expr_dict(i, pset.expr_init_map, pset.free_symbol, pset.gsym_map, simplifying=True) # print(expr) if expr == target: print(n) break ####GP pset = SymbolSet()
x_u = [kg] * 13 y_u = kg c_u = [dless, dless, dless] x, x_dim = Dim.convert_x(x, x_u, target_units=None, unit_system="SI") y, y_dim = Dim.convert_xi(y, y_u) c, c_dim = Dim.convert_x(c, c_u) t = time.time() # symbolset pset0 = SymbolSet() pset0.add_features(x, y, x_dim=x_dim, y_dim=y_dim, x_group=[[1, 2], [3, 4, 5]]) pset0.add_constants(c, c_dim=c_dim, c_prob=None) pset0.add_operations(power_categories=(2, 3, 0.5), categories=("Add", "Mul", "Sub", "Div", "exp"), self_categories=None) random.seed(0) z = time.time() sl = [SymbolTree.genGrow(pset0, 3, 4) for _ in range(100)] a = time.time() sl = [compile_context(sli, pset0.context, pset0.gro_ter_con) for sli in sl] b = time.time() print(b - a, a - z)
def test_add_tree_back(self): from numpy import random random.seed(1) sl = SymbolTree.genGrow(self.pset, 3, 4) self.pset.add_tree_to_features(sl)
def test_tree_gengrow_repr_and_str_different(self): from numpy import random random.seed(1) sl = SymbolTree.genGrow(self.pset, 3, 4) print(sl)