在Lua递归函数中重置变量

我正在使用Lua编写一个非常简单的程序,以更深入地了解遗传编程。下面是一个突变函数,用于在树(pop)中前往编号节点(nodeNum)并将其替换为随机数字1-100,或 交换用减法替换加法(或反之亦然):

local count = 0
function mutate(pop, nodeNum)
    for k,v in ipairs(pop) do
        if type(v) == "table" then
            mutate(v, nodeNum)
        else
            count = count + 1
        end

        if count == nodeNum and k == 1 then -- correct node
            if type(v) == "function" then
                if v == add then
                    pop[k] = sub
                else
                    pop[k] = add
                end
            else
                pop[k] = math.random(100)
            end
        end
    end
end

我的问题出现在count上。调用这个函数很麻烦,因为必须每次重置count:

-- mutate the first 3 elements in program tree t
mutate(t,1)
count = 0
mutate(t, 2)
count = 0
mutate(t, 3)

我尝试使用do ... end的变化,如下:

do
    local count
    function mutate(pop, nodeNum)
    if not count then
        count = 0
    ...
end

我还尝试为突变函数添加一个额外的参数mutate(pop, nodeNum, count),并使用mutate(t, 1, 0)调用它,但我不能使这两种方法都正常工作。

我可能忽略了一些非常明显的东西,有人能看到更优雅的解决方案吗?

点赞
用户1640834
用户1640834

我并不怎么使用 Lua,但是,最简单的方式可能是创建另一个函数来调用这个函数,在当前方法返回后将计数器设置为零。例如:

function mutateExample(pop, nodeNum)
    mutate(pop, nodeNum)
    count = 0;
end

这样,mutate 函数可以递归调用自己,直到需要结束时计数器就会被重置。

2012-09-01 22:24:12
用户734069
用户734069

count需要是函数内部的局部变量,而不是函数外部的变量。它是一个堆栈变量,所以将它放在函数的堆栈中:

function mutate(pop, nodeNum)
    local count = 0

    for k,v in ipairs(pop) do
        if type(v) == "table" then
            mutate(v, nodeNum)
        else
            count = count + 1
        end

        if count == nodeNum and k == 1 then -- correct node
            if type(v) == "function" then
                if v == add then
                    pop[k] = sub
                else
                    pop[k] = add
                end
            else
                pop[k] = math.random(100)
            end
        end
    end
end

只有在希望多个函数调用共享变量时,才使用外部的local变量。

2012-09-01 22:29:37
用户501459
用户501459
```lua
function mutate(pop, nodeNum, count)
    count = count or 0

    for k,v in ipairs(pop) do
        if type(v) == "table" then
            mutate(v, nodeNum, count)
        else
            count = count + 1
        end

        if count == nodeNum and k == 1 then -- correct node
            if type(v) == "function" then
                if v == add then
                    pop[k] = sub
                else
                    pop[k] = add
                end
            else
                pop[k] = math.random(100)
            end
        end
    end
end

将给定的 population 中的第 nodeNum 个节点进行变异,将其替换为 add 或 sub,或替换为随机整数。若该节点为一个表,则递归进一步操作。

代码中的 count 参数记录已经处理的节点数,如果该参数为 nil,则将其初始化为 0。

在遍历 pop 的过程中,对于每一个可以找到的节点,均将 count 增加 1。如果 count 等于 nodeNum,且当前节点是 pop 中第一个找到的节点,则将其进行变异。如果当前节点是一个函数,则将其替换为相反的运算符(add -> sub 或 sub -> add),否则替换为随机整数。

2012-09-02 03:43:37