-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·106 lines (72 loc) · 2.52 KB
/
main.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
import logging
import string
import random
import os
import time
import sys
""" Input parameters """
_defaultTestString = "METHINKS IT IS LIKE A WEASEL"
_population_size = 100
_mutation_rate = 0.05
_sleep_time = 0.01
def randomtext(chars, length):
""" Create inital random text """
return ''.join(random.choice(chars) for i in range(length))
def mutatetext(text, mutate=_mutation_rate):
""" mutate the text """
newtext = ''
for i in text:
if random.random() <= mutate:
newtext += random.choice(chars)
else:
newtext += i
return newtext
def scoring(source, target):
""" Score the test """
score = 0
for idx in range(len(target)):
if ord(source[idx]) == ord(target[idx]):
score += 1
return score
def get_chars():
# letters + digits + whitespace + punctuation
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
chars = string.ascii_letters + string.digits + " " + string.punctuation
logging.info("char list is '{}'".format(chars))
return chars
if __name__ == "__main__":
logging.basicConfig(filename="weasel.log", level=logging.INFO,
filemode='w')
if len(sys.argv) == 2 and sys.argv[1]:
targetString = sys.argv[1]
else:
targetString = _defaultTestString
logging.info("Target text is '{}'".format(targetString))
# Create chars
chars = get_chars()
max_score = len(targetString)
logging.info("Max score is {}".format(max_score))
gen_text = randomtext(chars, len(targetString))
logging.info("Inital text: {}".format(gen_text))
gen_score = scoring(gen_text, targetString)
logging.info("'{}' score is {}".format(gen_text, gen_score))
logging.info("Population size: {}".format(_population_size))
logging.info("Mutation rate: {}".format(_mutation_rate))
generation = 0
while (gen_score < max_score):
pop_text = gen_text
generation += 1
for i in range(_population_size):
new_text = mutatetext(pop_text)
new_score = scoring(new_text, targetString)
if new_score > gen_score:
gen_score = new_score
gen_text = new_text
if gen_score == max_score:
break
logging.info("{}. {} score: {}".format(generation,
gen_text, gen_score))
os.system('clear')
print(gen_text)
time.sleep(_sleep_time)