Reputation: 29
I want to Search Some bits string like "101101" in a Simple Genetic Algorithm Model in NetLogo . There is this code in the Calculate-fitness:
set fitness length (remove 0 bits)
Can I find or search a string of Bits Like "101101" in this Algorithm . Thanks
thanks for your Reply ! ok , but when I run the application , this error appear : ...
to calculate-fitness
set fitness position "101101110" reduce word(list bits)
end
and for update code :
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
and in "winner" appear this error :
**OF expected input to be a turtle agentset or turtle but got NOBODY instead.**
Thanks in advance
Upvotes: 1
Views: 161
Reputation: 30498
First define this:
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
Now you can do:
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
This works on any kind of data. For your particular problem, you know you are only dealing with 0's and 1's, so I would actually go with Alan's solution, which is easier to understand and will almost certainly run faster, too.
Upvotes: 1
Reputation: 9620
You can readily search for substrings in a bit string with position
. However, the Simple Genetic Algorithm Model uses lists, not strings. If you wish, you can convert a list to a string quite quickly with reduce
. E.g.,
show position "110" reduce word [1 0 1 1 0] ;; => 2
Upvotes: 1