yattom #6295(2008/05/18 13:21 GMT) [ Python ] Rating0/0=0.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import random GOAL = "METHINKSITISAWEASEL" DOMAIN = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" SET_SIZE = 300 MULTIPLY = 5 def create_random(): s = "" for i in range(len(GOAL)): s += random.choice(DOMAIN) return s def initial_set(): s = [] for i in range(SET_SIZE): s += [create_random()] return s def mutate(s): i = random.randrange(0, len(s)) result = s[:i] + random.choice(DOMAIN) + s[i+1:] return result def mutate_set(s): result = [] for e in s: for i in range(MULTIPLY): result.append(mutate(e)) return result def distance(s): d = 0 for c1, c2 in zip(s, GOAL): if c1 != c2: d += 1 return d def sorted_set(s): work = [(distance(e), e) for e in s] def mycmp(e1, e2): return cmp(e1[0], e2[0]) work.sort(mycmp) return work def main(): s = initial_set() i = 0 while True: new_s = mutate_set(s) s = [e for (d,e) in sorted_set(new_s)[:SET_SIZE]] print "%04d: %2d %s"%(i + 1, distance(s[0]), s[0]) if s[0] == GOAL: break i += 1 if __name__=='__main__': main()
Rating0/0=0.00-0+
[ reply ]
yattom
#6295()
[
Python
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]