-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasciigenalg.py
147 lines (114 loc) · 4.35 KB
/
asciigenalg.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import random
import sys
SPACE = ' '
START_CHA_ORD = ord(SPACE)
END_CHAR_ORD = 126
END_LINE = '\n'
POPULATION_SIZE = 50
ITERATION = 5000
TOP = 10
CROSSOVER_SOLUTION = 10
MUTATION_SOLUTION = 10
NEW_SOLUTION = 5
BOT = CROSSOVER_SOLUTION + MUTATION_SOLUTION + NEW_SOLUTION
MUTATION_EDGE = 0.8
CROSSOVER_EDGE = 0.8
SHOW_ITERATION = 1000
class Solution:
score = -1
chromosome = []
def __init__(self, chromosome=[], score=-1):
self.score = score
self.chromosome = chromosome
def print_score_solution(self):
print(self.score)
print_solution(self.chromosome)
def main(argv):
template = []
line_size = 0
lines_count = 0
with open(argv[1]) as f:
origin = list(f)
line_size = max(map(lambda x: len(x), origin))
lines_count = len(origin)
for l in origin:
striped = l.replace(END_LINE, SPACE);
new_list = list(striped)
new_list.extend([SPACE] * (line_size-len(striped)))
template.append(new_list)
best_score = score_solution(template, template)
population = create_population(line_size, lines_count, POPULATION_SIZE, template)
show = 0
for i in range(ITERATION):
if i == show:
show = 2 * show + 1
print("\n\n\n\n\nIteration: ", i)
population[0].print_score_solution()
population.sort(key=lambda x: x.score, reverse=True)
if population[0].score == best_score:
print("\n\n\n\n\nBest solution found. Iteration: ", i)
break
new_solutions = []
for _ in range(CROSSOVER_SOLUTION):
new_solutions.append(crossover(population[random.randint(0, TOP - 1)], population[random.randint(0, POPULATION_SIZE - 1)], template))
for _ in range(MUTATION_SOLUTION):
new_solutions.append(mutation(population[random.randint(0, POPULATION_SIZE - 1)], template))
for _ in range(NEW_SOLUTION):
new_solutions.append(create_solution(line_size, lines_count, template))
del population[-BOT:]
population.extend(new_solutions)
population[0].print_score_solution()
def create_population(line_size, lines_count, population_size, template):
population = []
for _ in range(population_size):
population.append(create_solution(line_size, lines_count, template))
return population
def create_solution(line_size, lines_count, template):
chromosome = []
# for _ in range(lines_count):
# solution.chromosome.append([chr(random.randrange(START_CHA_ORD, END_CHAR_ORD + 1, 1)) for _ in range (line_size)])
for _ in range(lines_count):
l = []
for _ in range(line_size):
l.append(template[random.randint(0, lines_count-1)][random.randint(0, line_size-1)])
chromosome.append(l)
solution = Solution(chromosome, score_solution(chromosome, template))
return solution
def score_solution(solution, template):
score = 0
for x in range(len(solution)):
for y in range(len(solution[x])):
if solution[x][y] == template[x][y]:
score += 1
return score
def mutation(solution, template):
chromosome = []
for l in solution.chromosome:
new_l = []
for i in l:
if random.random() > MUTATION_EDGE:
# new_l.append(chr(random.randint(START_CHA_ORD, END_CHAR_ORD)))
new_l.append(template[random.randint(0, len(template)-1)][random.randint(0, len(template[0])-1)])
else:
new_l.append(i)
chromosome.append(new_l)
new_solution = Solution(chromosome, score_solution(chromosome, template))
return new_solution
def crossover(top_solution, other_solution, template):
chromosome = []
for x in range(len(top_solution.chromosome)):
line = []
for y in range(len(top_solution.chromosome[x])):
if random.random() > CROSSOVER_EDGE:
line.append(other_solution.chromosome[x][y])
else:
line.append(top_solution.chromosome[x][y])
chromosome.append(line)
new_solution = Solution(chromosome, score_solution(chromosome, template))
return new_solution
def print_solution(solution):
for l in solution:
print(''.join(l))
if __name__ == "__main__":
main(sys.argv)