在NetLogo中查找位字符串
我想在NetLogo中的一个简单遗传算法模型中搜索一些位字符串,如“101101”。计算适应度中有以下代码:在NetLogo中查找位字符串,netlogo,genetic-algorithm,Netlogo,Genetic Algorithm,我想在NetLogo中的一个简单遗传算法模型中搜索一些位字符串,如“101101”。计算适应度中有以下代码: set fitness length (remove 0 bits) 我可以在这个算法中找到或搜索像“101101”这样的一串位吗。 谢谢 谢谢你的回复! 好的,但是当我运行应用程序时,会出现以下错误: 更新代码: to update-display set winner max-one-of turtles [fitness] ask patches [
set fitness length (remove 0 bits)
我可以在这个算法中找到或搜索像“101101”这样的一串位吗。
谢谢
谢谢你的回复! 好的,但是当我运行应用程序时,会出现以下错误: 更新代码:
to update-display
set winner max-one-of turtles [fitness]
ask patches
[
ifelse item pxcor ([bits] of winner) = 1
[ set pcolor white ]
[ set pcolor black ]
]
end
在“winner”中出现以下错误:
**OF expected input to be a turtle agentset or turtle but got NOBODY instead.**
提前感谢您可以使用
位置
轻松搜索位字符串中的子字符串。然而,简单的遗传算法模型使用列表,而不是字符串。如果愿意,可以使用reduce
将列表快速转换为字符串。例如:
show position "110" reduce word [1 0 1 1 0] ;; => 2
首先,定义这一点:
to-report sublist-position [subl l]
if length l < length subl [ report false ]
if subl = sublist l 0 (length subl) [ report 0 ]
let recurse sublist-position subl butfirst l
if is-number? recurse [ report 1 + recurse ]
report false
end
这适用于任何类型的数据。对于您的特定问题,您知道您只处理0和1,因此我实际上会使用Alan的解决方案,该解决方案更容易理解,而且几乎肯定会运行得更快。请注意,(列表位)
生成一个包含一个元素的列表,即位
。如果位
是一个列表,您只需要减少字位
。此外,如果position
返回false
,您必须决定该怎么办。(请参阅文档。)
to-report sublist-position [subl l]
if length l < length subl [ report false ]
if subl = sublist l 0 (length subl) [ report 0 ]
let recurse sublist-position subl butfirst l
if is-number? recurse [ report 1 + recurse ]
report false
end
observer> show sublist-position [1 1 0] [0 0 0 0 0 1 1]
observer: false
observer> show sublist-position [1 1 0] [0 0 0 0 0 1 1 0 0 0]
observer: 5