Note that there are some explanatory texts on larger screens.

plurals
  1. POEvolutionary Algorithm to guess a string, messed up by replication
    text
    copied!<p>i am working on a python script to test out genetic programming. As an exercise i have made a simple Script that tries to guess a string without the whole population part. </p> <p>My Code is:</p> <pre><code># acts as a gene # it has three operations: # Mutation : One character is changed # Replication: a sequencepart is duplicated # Extinction : A sequencepart is lost # Crossover : the sequence is crossed with another Sequence import random class StringGene: def __init__(self, s): self.sequence = s self.allowedChars = "ABCDEFGHIJKLMOPQRSTUVWXYZ/{}[]*()+-" def __str__(self): return self.sequence def Mutation(self): x = random.randint(0, len(self.sequence)-1) r = random.randint(0, len(self.allowedChars)-1) d = self.sequence self.sequence = d[:x-1]+ self.allowedChars[r] + d[x:] def Replication(self): x1 = random.randint(0, len(self.sequence)-1) x2 = random.randint(0, len(self.sequence)-1) self.sequence =self.sequence[:x1]+ self.sequence[x1:x2] + self.sequence[x2:] self.sequence = self.sequence[:32] def Extinction(self): x1 = random.randint(0, len(self.sequence)-1) x2 = random.randint(0, len(self.sequence)-1) self.sequence = self.sequence[:x1] + self.sequence[x2:] def CrossOver(self, s): x1 = random.randint(0, len(self.sequence)-1) x2 = random.randint(0, len(s)-1) self.sequence = self.sequence[:x1+1]+ s[x2:] #x1 = random.randint(0, len(self.sequence)-1) #self.sequence = s[:x2 ] + self.sequence[x1+1:] if __name__== "__main__": import itertools def hamdist(str1, str2): if (len(str2)&gt;len(str1)): str1, str2 = str2, str1 str2 = str2.ljust(len(str1)) return sum(itertools.imap(str.__ne__, str1, str2)) g = StringGene("Hi there, Hello World !") g.Mutation() print "gm: " + str(g) g.Replication() print "gr: " + str(g) g.Extinction() print "ge: " + str(g) h = StringGene("Hello there, partner") print "h: " + str(h) g.CrossOver(str(h)) print "gc: " + str(g) change = 0 oldres = 100 solutionstring = "Hello Daniel. Nice to meet you." best = StringGene("") res = 100 print solutionstring while (res &gt; 0): g.Mutation() g.Replication() g.Extinction() res = hamdist(str(g), solutionstring) if res&lt;oldres: print "'"+ str(g) + "'" print "'"+ str(best) + "'" best = g oldres = res else : g = best change = change + 1 print "Solution:" + str(g)+ " " + str(hamdist(solutionstring, str(g))) + str (change) </code></pre> <p>I have a crude hamming distance as a measure how far the solution string differs from the current one. However i want to be able to have a varying length in the guessing, so i introduced replication and deletion of parts of the string. </p> <p>Now, however the string grows infinitely and the Solution String is never found. Can you point out, where i went wrong?</p> <p>Can you suggest improvements?</p> <p>cheers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload