kaggle 泰坦尼克号 GP 是什么?

数据挖掘 卡格尔 基因编程
2022-03-08 04:56:49

有时我会看到带有 GP 编程的内核。
但没有解释,他们放了一些带有未知方程的随机数
这是 GP 编程的一部分

gp <-function(data)
{
  p<-0.200000*tanh(((((31.006277) * ((((((data$Embarked) + (data$Cabin))/2.0)) + (((((sin((tanh((data$Parch))))) * (3.0))) - (data$Pclass))))))) * 2.0)) +
    0.200000*tanh(((31.006277) * (((((data$Age) * (data$Cabin))) + (((((0.318310) - (data$Pclass))) + (pmin((2.0), (((data$Parch) * 2.0)))))))))) +
    0.200000*tanh(ifelse(rep(1.,NROW(data))*(ifelse(rep(1.,NROW(data))*(data$SibSp>0.),data$Cabin,sin((data$Parch)))>0.),(7.90205097198486328),(((((((data$Cabin) + (data$Fare))/2.0)) - (9.869604))) - (31.006277)))) +
    0.200000*tanh(((((((((((tanh(((((0.636620) < (data$Parch))*1.)))) * 2.0)) - (data$Pclass))) + (((data$Embarked) + (sin((data$Pclass))))))) * 2.0)) * 2.0)) +

完整代码在这里https://www.kaggle.com/scirpus/my-first-gp-in-r

有人可以解释这个 GP 是什么意思以及这个数字和方程式是如何得出的吗?

我的猜测和神经网络一样,对吧?

2个回答

GP也被称为基因编程它是一种受自然选择(适者生存)启发的算法,旨在找到执行某些任务的理想算法。这些算法在每一代中创建具有编码行为作为一组基因的个体。选择在每一代中最好地执行任务的个体进行轻微排列并进入下一代。这导致后几代的解决方案空间在某个局部最小值附近变窄。


例子

让我们看一个简单的例子,看看它如何用于找到从 -100 到 100 的 2D 函数的最小值。

这包括 4 个关键步骤:初始化、评估、选择和组合。

初始化

种群中的每个个体都由一些基因编码。在我们的例子中,基因代表我们的值。然后,我们将针对这个特定问题将搜索范围设置为 [0, 1000]。通常你会根据你的问题知道什么是自然可能的。例如,您应该知道自然界中可能的土壤密度范围。我们将在我们的人口中创造 100 个人。[x,y]

体能评估

这一步只是要求您将值放入您的函数并获得其结果。很标准的东西。[x,y]

选择

您可以通过多种方式选择父母。我将永远保留阿尔法男性。种群中最好的个体,他将被克隆到下一个。然后我将使用锦标赛选择。我们将重复以下内容,直到下一代人口满为止。随机选择四个父母,从前两个中选出最好的,从后两个中选出最好的。这将是我们的下一个后代的两个父母。

组合

值的二进制值为孩子构建新的基因组通过均匀随机从两个亲本基因中选择子基因组中每个密码子的所得二进制值。[x,y]

import numpy as np

class Genetic(object):

    def __init__(self, f, pop_size = 1000, n_variables = 2):
        self.f = f
        self.minim = -100
        self.maxim = 100
        self.pop_size = pop_size
        self.n_variables = n_variables
        self.population = self.initializePopulation()
        self.evaluatePopulation()

    def initializePopulation(self):
        return [np.random.randint(self.minim, self.maxim, size=(self.n_variables)) 
                           for i in range(self.pop_size)]

    def evaluatePopulation(self):
        return [self.f(i[0], i[1]) for i in self.population]

    def nextGen(self):
        results = self.evaluatePopulation()
        children = [self.population[np.argmin(results)]]

        while len(children) < self.pop_size:
            # Tournament selection
            randA, randB = np.random.randint(0, self.pop_size), \
                           np.random.randint(0, self.pop_size)
            if results[randA] < results[randB]: p1 = self.population[randA]
            else: p1 = self.population[randB]

            randA, randB = np.random.randint(0, self.pop_size), \
                           np.random.randint(0, self.pop_size)  
            if results[randA] < results[randB]: p2 = self.population[randA]
            else: p2 = self.population[randB]   

            signs = []
            for i in zip(p1, p2):
                if i[0] < 0 and i[1] < 0: signs.append(-1)
                elif i[0] >= 0 and i[1] >= 0: signs.append(1)
                else: signs.append(np.random.choice([-1,1]))

            # Convert values to binary
            p1 = [format(abs(i), '010b') for i in p1]
            p2 = [format(abs(i), '010b') for i in p2]

            # Recombination
            child = []
            for i, j in zip(p1, p2):
                for k, l in zip(i, j):
                    if k == l: child.append(k)
                    else: child.append(str(np.random.randint(min(k, l), 
                                                             max(k,l))))

            child = ''.join(child)
            g1 = child[0:len(child)//2] 
            g2 = child[len(child)//2:len(child)]
            children.append(np.asarray([signs[0]*int(g1, 2), 
                                        signs[1]*int(g2, 2)]))
        self.population = children

    def run(self):
        ix = 0
        while ix < 1000:
            ix += 1
            self.nextGen()
        return self.population[0]

要使用此算法,您可以定义一些函数,然后运行以下命令。

f = lambda x, y: (x-7)**2 + y**2
gen = Genetic(f)
minim = gen.run()

print('Minimum found      X =', minim[0], ', Y =', minim[1])

“GP”代表遗传编程。这是一个人工智能领域。遗传程序将生成一个由一串数字和函数组成的树结构。树是经过几代进化过程后最适合问题的解决方案。

我建议你阅读遗传编程。遗传编程的创始人是 John Koza,他关于遗传编程的第一本书名为“Genetic Programming: On the Programming of Computers by Means of Natural Selection (Complex Adaptive Systems), 1992”。

Scirpus 用 C++ 编写了自己的 GP 代码,并用它来生成树结构并将其粘贴到他的 R 程序中。您可以使用流行的库(如Python 中的分布式进化算法 (DEAP))了解如何自己完成