遺伝的アルゴリズムがどのように機能するかを理解しようとしています。何かを書いてみることで学んだことはすべてそうですが、私の知識は非常に限られており、これが正しいかどうか確信が持てません。
このアルゴリズムの目的は、人口の半分がすでに感染している場合、群れの半分が病気に感染するのにどれくらいの時間がかかるかを調べることです. これは私の頭の中で思いついた例にすぎないので、これが実行可能な例であるかどうかはわかりません.
どうすれば自分の知識を向上させることができるかについて、いくつかのフィードバックをいただければ幸いです。
コードは次のとおりです。
import random
def disease():
herd = []
generations = 0
pos = 0
for x in range(100):
herd.append(random.choice('01'))
print herd
same = all(x == herd[0] for x in herd)
while same == False:
same = all(x == herd[0] for x in herd)
for animal in herd:
try:
if pos != 0:
after = herd[pos+1]
before = herd[pos-1]
if after == before and after == '1' and before == '1' and animal == '0':
print "infection at", pos
herd[pos] = '1'
#print herd
pos += 1
except IndexError:
pass
pos = 0
generations += 1
random.shuffle(herd)
#print herd
print "Took",generations,"generations to infect all members of herd."
if __name__ == "__main__":
disease()