Lua 3点交叉帮助开始



我想为遗传编程实现一个 3 点交叉,但我不知道该怎么做以及从哪里开始。

我的意见是:

a = {(first pair), (second pair), ... etc.} 

例如a = {(12345,67890), (09876,54321)}(这些是数字,而不是字符串)

输出:像这样:

示例:a_1 = {(12895), (67340)}也是数字。

感谢您的回复,对不起我的英语不好。

这是我对整数的 k 点交叉的快速实现,主要使用整数算术。从这个开始,您可以使用循环将其扩展到交叉许多整数对的染色体。

math.randomseed(111)
-- math.randomseed(os.time())
a = 12345
b = 67890
len = 5 -- number of digits
function split(mum, dad, len, base)
    local split = math.pow(base, math.random(len))
    local son = math.floor(dad / split) * split + mum % split
    local daughter = math.floor(mum / split) * split + dad % split
    return son, daughter
end
function kpoint(mum, dad, len, base, k)
    for i=1, k do
      mum, dad = split(mum, dad, len, base)
    end
    return mum, dad
end
s, d = kpoint(a, b, len, 10, 3) -- 3 point crossover in base 10
print(s) -- 67395
print(d) -- 12840
-- binary, (crossover binary representation)
s, d = kpoint(tonumber("10001", 2), tonumber("10110", 2), 5, 2, 3)
print(s) -- 23  which is (10111) in base 2
print(d) -- 16  which is (10000) in base 2

-- binary, (crossover base 10, but interpret as binary)
s, d = kpoint(1101, 1010, 4, 10, 3)
print(s) -- 1001
print(d) -- 1110

最新更新