1

开发了一个展示基本智能的简单算法后,我热衷于进行递归自我改进,但我遇到的问题是由于我对递归缺乏了解。

我知道,如果我有一些东西可以评估我用于改进的算法的“适合性”(我为算法提供了自身的二进制表示),那么它使用它必须生成的新二进制算法的基本智能,比本身)。

但是我需要知道的是,我如何评估算法的适应度?我通常会这样做

if @variable == true
  fitness += 1
end

但是如果适应度是算法本身产生的算法的能力,我怎么能测量这个,因为如果没有产生新算法本身就无法测试产生的算法等等......

干杯马丁

4

2 回答 2

2

Recursion is almost always a bad idea for algorithms that can be implemented using another approach. Ruby has a number of problems when working at extreme stack depths such as increasing cost of garbage collection, significant stack overhead, and a relatively low ceiling which may result in "stack level too deep" errors.

You would probably be better off creating independent objects that do not share state. Instead of recursing, iterate. Where applicable, use your own self-managed stack such as an Array you push and shift elements from.

Some example code might be:

candidate = stack.shift

if (candidate.fitness > minimum_fitness)
  stack.push(candidate.spawn)
end

A method like spawn would do whatever magical GA things you need to do and return a new, independent instance. This could be augmented to combine with another candidate if required.

于 2012-06-07T20:19:44.590 回答
1
def self_improve(level)
  return level if level == 100 #this is the brake
  level += 1
  self_improve(level)
end

p self_improve(1) #=>100
于 2012-06-07T20:18:23.033 回答