-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (83 loc) · 3.25 KB
/
main.py
File metadata and controls
114 lines (83 loc) · 3.25 KB
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
import model
import model as md
import math_lib as ml
import matplotlib.pyplot as plt
import math
import functions
import csv_module as csvm
def create_model():
print('Введите параметры модели:')
print('Размер популяции = ',end = '')
pop_count = int(input())
print('количество узлов сетки = 2^', end = '')
gr_count = int(input())
print('номер функции = ', end = '')
func_num = int(input())
func = functions.func_dict[func_num]
model = md.Model(pop_count, gr_count, func[0], func[1])
print('Изменить доп. параметры? [y/n] ', end='')
if input()=='y':
print(f'Вероятность скрещивания (по умолчанию {model.PR_CROSSOVER}) = ', end = '')
model.PR_CROSSOVER = float(input())
print(f'Вероятность мутации (по умолчанию {model.PR_MUTATION}) = ', end = '')
model.PR_MUTATION = float(input())
print(f'Изменить интервал (по умолчанию {model.interval}) ? [y/n] ', end='')
if input() == 'y':
print('Начало = ', end = '')
a = float(input())
print('Конец = ', end = '')
b = float(input())
model.interval = (a,b)
return model
def draw_graph(model):
dots_x = []
dots_y = []
for ind in m.current_population:
dots_y.append(ind.fitness)
dots_x.append(ml.gene_to_global_coords(model, ind.gene))
plt.scatter(dots_x, dots_y, edgecolors='red')
x = []
for index in range(-2,2**model.grid_count+2):
x.append(model.interval[0] + index*model.grid_step)
y = [model.function(fx) for fx in x]
plt.plot(x,y)
plt.grid()
plt.title(f'Поколение № {model.generation_number}')
plt.show()
def print_model_generaiton(model):
print('-----------------------------')
print(f'номер поколения:{m.generation_number}')
for ind in model.current_population:
print(f'ген: {ind.gene}| индекс: {ml.grey_to_num(ind.gene)}| глобальная X: {ml.gene_to_global_coords(m, ind.gene)}| фенотип: {ind.fitness}')
print('-----------------------------')
def save_model_generation(model):
for ind in model.current_population:
csvm.history.append({
'gene': ind.gene,
'index': ml.grey_to_num(ind.gene),
'global_X': ml.gene_to_global_coords(m, ind.gene),
'phenotype': ind.fitness,
'generation': model.generation_number
})
def draw_stats(model):
x = [i for i in range(1,model.generation_number+1)]
y = [fit for fit in model.avg_fitness_in_gen]
plt.grid()
plt.plot(x,y)
plt.scatter(x,y)
plt.xlabel('Номер поколения')
plt.ylabel('Среднее значение минимума ф-ии')
plt.show()
while True:
m = create_model()
print('Введите макс. вол-во поколений:', end='')
for i in range(int(input())):
m.next()
draw_graph(m)
print_model_generaiton(m)
save_model_generation(m)
draw_stats(m)
print('Название файла:', end='')
fn = input()
csvm.save_csv(fn)
print('------------')