Skip to content

Commit 0487d5b

Browse files
committed
output refactor
1 parent 03bfee3 commit 0487d5b

File tree

7 files changed

+48
-48
lines changed

7 files changed

+48
-48
lines changed

src/base_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class BaseClass:
22
def __init__(self: object):
3-
self.should_print = False
3+
self.should_print = True

src/codification/encode.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ def __init__(
1313
self.upper_bound = upper_bound
1414
self.precision = precision
1515
super().__init__()
16-
print(self.should_print)
1716

1817
def encode(self: object, number: float) -> str:
1918
interval_lower_bound = self.__get_interval_lower_bound(number)
2019
binary = self.__convert_to_binary(interval_lower_bound)
21-
if self.should_print:
22-
print(f'INTERVAL {interval_lower_bound} Encoded {number} to {binary}')
2320
return binary
2421

2522
def __get_bin_length(self: object):

src/combination/combination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __mix_cromozoms(self: object, c1: Cromosome, c2: Cromosome) -> None:
2626
c2.update_binary(new_c2)
2727
if self.should_print:
2828
print(f'Combined {c1.id} with {c2.id} at point {point}')
29+
print(f'Result: {new_c1} {new_c2}')
2930

3031
def __get_cromosomes_for_combination(self: object) -> list[Cromosome]:
3132
return [c for c in self.population if self.__should_combine()]
@@ -48,4 +49,3 @@ def __get_possible_combinations(self: object) -> list[tuple[Cromosome, Cromosome
4849
second = cromosomes.pop()
4950
result.append((first, second))
5051
return result
51-

src/cromosome.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,16 @@ def __repr__(self: object) -> str:
3434
return f'{self.id}'
3535

3636
def show(self: object):
37-
print(f'{self.id}: {self.value} x= {self.binary} f={self.fitness}')
37+
print(f'{self.id:<3} {self.value:<10} x={self.binary:<30} f={self.fitness:<10}')
3838

3939
def update_value(self: object, value: float) -> None:
4040
self.value = value
4141
self.binary = Cromosome.__CODER.encoder.encode(value)
4242
self.fitness = Cromosome.__FUNCTION(value)
43-
if self.should_print:
44-
print(f'Updated value for {self.id}')
4543

4644
def update_binary(self: object, binary: str) -> None:
4745
self.binary = binary
4846
value = Cromosome.__CODER.decoder.decode(binary)
4947
value = round(value, Cromosome.__CODER.precision)
5048
self.value = value
5149
self.fitness = Cromosome.__FUNCTION(self.value)
52-
if self.should_print:
53-
print(f'Updated binary for {self.id}')

src/main.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,19 @@
66
from src.selection.selector import Selector
77
from src.combination.combination import Combinator
88
from src.mutation.mutation import Mutator
9+
import random
910

1011
if __name__ == '__main__':
11-
values = [
12-
-0.914592,
13-
-0.516787,
14-
-0.246207,
15-
1.480791,
16-
0.835307,
17-
1.229633,
18-
0.133068,
19-
-0.897179,
20-
0.100578,
21-
-0.311975,
22-
1.411980,
23-
0.404924,
24-
1.954865,
25-
0.359503,
26-
1.255452,
27-
1.124764,
28-
1.527482,
29-
1.573845,
30-
-0.562311,
31-
1.191435
32-
]
12+
population_size = 20
13+
generations = 50
3314
lower_bound = -1
3415
upper_bound = 2
3516
precision = 6
3617
f = lambda x: -(x ** 2) + x + 2
3718
combination_rate = 0.25
38-
mutation_rate = 0.1
19+
mutation_rate = 0.01
20+
21+
values = [round(random.uniform(lower_bound, upper_bound), precision) for _ in range(population_size)]
3922
coder = coder.Coder(lower_bound, upper_bound, precision)
4023
binaries = [coder.encoder.encode(x)for x in values]
4124
fitnesses = [f(x) for x in values]
@@ -46,30 +29,39 @@
4629
combinator = Combinator(population, combination_rate)
4730
mutator = Mutator(population, mutation_rate)
4831

49-
print('Initial population:')
32+
SEPARATOR = '----------------------------------------\n'
33+
34+
print('Initial population:\n')
5035
for c in population:
5136
c.show()
52-
53-
selected = selector.select()
37+
print(SEPARATOR)
5438

55-
print('\nSelected population:')
56-
for c in selected:
39+
population = selector.select()
40+
print('\nSelected population:\n')
41+
for c in population:
5742
c.show()
58-
population = selected
43+
print(SEPARATOR)
5944

60-
print(f'Combination rate: {combination_rate}')
45+
print(f'Combination rate: {combination_rate}\n')
6146
population = combinator.combine()
6247

63-
print('\nAfter combination:')
48+
print('\nAfter combination:\n')
6449
for c in population:
6550
c.show()
51+
print(SEPARATOR)
6652

67-
print(f'Mutation rate: {mutation_rate}')
53+
print(f'Mutation rate: {mutation_rate}\n')
6854
population = mutator.mutate()
6955

70-
print('\nAfter mutation:')
56+
print('\nAfter mutation:\n')
7157
for c in population:
7258
c.show()
59+
print(SEPARATOR)
60+
61+
coder.should_print = False
62+
selector.should_print = False
63+
combinator.should_print = False
64+
mutator.should_print = False
7365

7466
print('\n\n\n')
7567
for i in range(49):

src/mutation/mutation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def __init__(
1515
def mutate(self: object) -> list[Cromosome]:
1616
for c in self.population:
1717
if self.__should_mutate():
18+
if self.should_print:
19+
print(f'Mutating {c.id}')
1820
self.__mutate_cromozom(c)
1921
return self.population
2022

src/selection/selector.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ def __init__(
1313
super().__init__()
1414

1515
def select(self: object) -> list[Cromosome]:
16-
selected = []
16+
selected = [self.__get_the_best_cromozom()]
1717
if self.should_print:
18-
print('Intervals:')
19-
for index, interval in enumerate(self.intervals):
20-
print(f'Interval {index}: {interval}')
18+
self.__print_cromosomes_probabilities()
19+
print()
20+
self.__print_intervals()
21+
print()
2122
while len(selected) < len(self.population):
2223
selected.append(self.__select_cromozom())
2324
return selected
@@ -48,6 +49,18 @@ def __select_cromozom(self: object) -> Cromosome:
4849
number = self.__generate_random_number()
4950
index = self.__get_index_of_interval(number)
5051
if self.should_print:
51-
print(f'Selected index: {index}')
52+
print(f'u= {number} selected {self.population[index]}')
5253
return self.population[index]
5354

55+
def __print_cromosomes_probabilities(self: object):
56+
print('Selection probabilities:')
57+
for c in self.population:
58+
print(f'Probability for {c}: {self.__get_probability_for_cromozom(c)}')
59+
60+
def __print_intervals(self: object):
61+
print('Intervals:')
62+
for i in range(len(self.intervals) - 1):
63+
print(f'Interval {i}: {self.intervals[i]} - {self.intervals[i + 1]}')
64+
65+
def __get_the_best_cromozom(self: object) -> Cromosome:
66+
return max(self.population, key=lambda c: c.fitness)

0 commit comments

Comments
 (0)