我正在建立一种遗传算法,可以在符号回归分析中进行时间序列预测。我正在尝试找到一种算法,以找到与数据的潜在趋势相匹配的方程式。 (预测啤酒的月销量)
想法是使用类似lisp的表达式,它将等式写入树中。这允许在交叉/配合阶段进行分支交换。
5* (5 +5)
写为:
X = '(mul 5 (add 5 5))'
Y = parser(X)
y = ['mul', 5, ['add', 5, 5]]
我想知道如何创建一个初始种群集,其中的个体会自动代表不同的表情。 “适合度”与每个方程式与基本趋势的匹配程度有关。
例如,一个人可能是:'(加100(mul x(sin(mul x 3))))''其中x是以月为单位的时间。
如何自动为我的人群生成表达式?我不知道该怎么做,任何帮助将不胜感激。
您可以轻松地通过递归和一个随机数生成器random()
解决此问题,该生成器返回0到1之间的(伪)随机float
。这是一些伪代码:
randomExp() {
// Choose a function(like mul or add):
func = getRandomFunction() // Just choose one of your functions randomly.
arg1 = ""
rand1 = random()
// Choose the arguments. You may choose other percentages here depending how deep you want it to be and how many 'x' you want to have.
if(rand1 < 0.2)
arg1 = randomExp() // Here add a new expression
else if(rand1 < 0.5)
arg1 = "x"
else
arg1 = randomConstant() // Get a random constant in a predefined range.
// Do the same for the second argument:
arg2 = ""
…
…
// Put everything together and return it:
return "("+func+" "+arg1+" "+arg2+")"
}
您可能还希望限制递归深度,因为这可能会给您返回一个理论上无限长的表达式。