Esempio n. 1
0
    X = q1[:, :-1]
    y = q1[:, -1]
    X = np.concatenate((X, (X[:, 1] / X[:, 0]**2).reshape(-1, 1)), axis=1)

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.20,
                                                        random_state=0)

    store = Store()

    # symbolset
    pset0 = SymbolSet()
    pset0.add_features(X_train, y_train)
    pset0.add_constants(c=[
        1,
    ])

    pset0.add_operations(
        power_categories=(2, ),
        categories=("Add", "exp", "Neg"),
    )
    h_bgp = 3

    # stop = None
    # This random_state is under Linux system. For others system ,the random_state maybe different。
    # try with different random_state.
    stop = lambda ind: ind.fitness.values[0] >= 0.99
    sl = SymbolLearning(loop='MultiMutateLoop',
                        pset=pset0,
                        gen=10,
Esempio n. 2
0
    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)
Esempio n. 3
0
class MyTestgp(unittest.TestCase):
    def setUp(self):
        self.SymbolTree = SymbolTree
        self.pset = SymbolSet()

        from sklearn.datasets import fetch_california_housing

        data = fetch_california_housing()
        x = data["data"][:100]
        y = data["target"][:100]

        self.x = x
        self.y = y
        # self.pset.add_features(x, y, )
        self.pset.add_features(x, y, x_group=[[1, 2], [4, 5]])
        self.pset.add_constants([6, 3, 4],
                                c_dim=[dless, dless, dless],
                                c_prob=None)
        self.pset.add_operations(power_categories=(2, 3, 0.5),
                                 categories=("Add", "Mul", "Neg", "Abs"),
                                 self_categories=None)

        from sklearn.metrics import r2_score, mean_squared_error

        self.cp = CalculatePrecisionSet(self.pset,
                                        scoring=[r2_score, mean_squared_error],
                                        score_pen=[1, -1],
                                        dim_type=None,
                                        filter_warning=True)

    def test_gp_flow(self):
        from numpy import random
        random.seed(1)
        cpset = self.cp
        # def Tree
        from deap.base import Fitness

        Fitness_ = newclass.create("Fitness_", Fitness, weights=(1, -1))
        PTree_ = newclass.create("PTrees_", SymbolTree, fitness=Fitness_)

        # def selection
        toolbox = Toolbox()

        # toolbox.register("select", selTournament, tournsize=3)
        toolbox.register("select", selKbestDim, dim_type=dless)
        # selBest
        toolbox.register("mate", cxOnePoint)
        # def mutate
        toolbox.register("generate", genGrow, pset=cpset, min_=2, max_=3)
        # toolbox.register("mutate", mutUniform, expr=toolbox.generate, pset=cpset)
        # toolbox.register("mutate", mutNodeReplacement, pset=cpset)
        toolbox.register("mutate", mutShrink, pset=cpset)

        toolbox.decorate(
            "mate", staticLimit(key=operator.attrgetter("height"),
                                max_value=10))
        toolbox.decorate(
            "mutate",
            staticLimit(key=operator.attrgetter("height"), max_value=10))
        # def elaluate

        # toolbox.register("evaluate", cpset.parallelize_calculate, n_jobs=4, add_coef=True,
        # inter_add=False, inner_add=False)

        # toolbox.register("parallel", parallelize, n_jobs=1, func=toolbox.evaluate, respective=False, tq=False)

        population = [PTree_.genGrow(cpset, 3, 4) for _ in range(10)]
        # si = sys.getsizeof(cpset)
        for i in range(5):
            invalid_ind = [ind for ind in population if not ind.fitness.valid]
            invalid_ind_score = cpset.parallelize_score(inds=invalid_ind)

            for ind, score in zip(invalid_ind, invalid_ind_score):
                ind.fitness.values = score[0]
                ind.y_dim = score[1]
            # si2 = sys.getsizeof(invalid_ind[0])
            # invalid_ind=[i.compress() for i in invalid_ind]
            # si3 = sys.getsizeof(invalid_ind[0])
            # print(si3,si2,si)
            population = toolbox.select(population, len(population))
            offspring = varAnd(population, toolbox, 1, 1)
            population[:] = offspring
Esempio n. 4
0
        y, y_dim = Dim.convert_xi(y, y_u)
        c, c_dim = Dim.convert_x(c, c_u)

        scal = MagnitudeTransformer(tolerate=1)
        x, y = scal.fit_transform_all(x, y, group=2)
        c = scal.fit_transform_constant(c)

        # symbolset
        pset0 = SymbolSet()
        pset0.add_features(x,
                           y,
                           x_dim=x_dim,
                           y_dim=y_dim,
                           x_group=2,
                           feature_name=fea_name)
        pset0.add_constants(c, c_dim=c_dim, c_prob=0.05)
        pset0.add_operations(power_categories=(2, 3, 0.5),
                             categories=("Add", "Mul", "Sub", "Div", "exp",
                                         "ln"),
                             self_categories=None)

        # a = time.time()
        dicts = {}
        for i in range(12):
            bl = MultiMutateLoop(pset=pset0,
                                 gen=20,
                                 pop=1000,
                                 hall=1,
                                 batch_size=40,
                                 re_hall=3,
                                 n_jobs=12,
Esempio n. 5
0
class MyTestbase(unittest.TestCase):

    def setUp(self):
        self.SymbolTree = SymbolTree
        self.pset = SymbolSet()

        from sklearn.datasets import fetch_california_housing

        data = fetch_california_housing()
        x = data["data"][:100]
        y = data["target"][:100]
        # No = Normalizer()
        # y=y/max(y)
        # x = No.fit_transform(x)
        self.x = x
        self.y = y
        # self.pset.add_features(x, y, )
        self.pset.add_features(x, y, x_group=[[1, 2], [4, 5]])
        self.pset.add_constants([6, 3, 4], c_dim=[dless, dless, dless], c_prob=None)
        self.pset.add_operations(power_categories=(2, 3, 0.5),
                                 categories=("Add", "Mul", "Self", "Abs"),
                                 self_categories=None)

        from sklearn.metrics import r2_score, mean_squared_error
        self.cp = CalculatePrecisionSet(self.pset, scoring=[r2_score, mean_squared_error],
                                        score_pen=[1, -1],
                                        filter_warning=True)

    def test_pset_passed_to_cpset_will_change(self):
        cp = CalculatePrecisionSet(self.pset)
        self.assertNotEqual(cp, self.cp)

    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)
        # self.assertNotEqual(repr(sl), str(sl))

    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_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)

    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)