-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataclass_test.py
More file actions
96 lines (90 loc) · 2.59 KB
/
dataclass_test.py
File metadata and controls
96 lines (90 loc) · 2.59 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
import ungenetico as ung
import math
from statistics import mean
# def obj_expression(z, x, y, m):
# return sum(x) * y * z + m
#
#
# a = ung.GeneInt('x', min_val=1, max_val=11, length=10, mutation_operator=ung.MutationNotUniform())
# print(a)
# a.name = 'y'
# print(a)
# a2 = ung.GeneInt('y', min_val=1.1, max_val=10.9, mutation_operator=ung.MutationUniform())
# a3 = ung.GeneBool('z')
# a4 = ung.GeneFloat('m', min_val=1.1, max_val=10.9, mutation_operator=ung.MutationNotUniform())
# print(a2)
# print(a3)
# print(a3.min_val)
# print(a3.max_val)
# print(a4)
#
# ga = ung.GeneticAlgorithm(objective_function=obj_expression)
#
# print(ga)
# ga.add_gen(a)
# ga.add_gen(a2)
# ga.add_gen(a3)
# ga.add_gen(a4)
# ga.create_population(3)
# for ind in ga.actual_generation.population:
# print(ind)
# for gen in ind.genome:
# print(gen.value)
#
# ga.mutate()
#
# ga.calculate_objective_function()
# for ind in ga.actual_generation.population:
# for gen in ind.genome:
# print(gen.value)
# print(f'objective: {ind.objective_value}')
# #
# for _ in range(10):
# a3.mutate(ga)
# print(a3)
#
# print(ga.actual_generation.size)
# ga.assign_probability()
# ga.actual_generation.sort_population('descending', 'survival_probability')
# for ind in ga.actual_generation.population:
# print(ind.survival_probability)
# #
# ga.select()
# ga.match()
# print(ga.actual_generation)
# ga.reproduce()
# print(ga.actual_generation)
# #print(ga.actual_generation)
# -------------------
def obj_expression(x):
of = 0
for xi in x:
of += xi
return of
ga = ung.GeneticAlgorithm(objective_function=obj_expression,
optimization='maximization',
generation_max=100,
generation_size=100,
probability_operator=ung.ProbabilityLineal(),
pairing_operator=ung.PairingExtremes(),
reproduction_operator=ung.ReproductionBestBetweenParentsChildren())
ga.add_gen(ung.GeneFloat('x', -10, 10, length=25, crossover_operator=ung.CrossoverBLX()))
ga.optimize()
# ga.create_population(10)
# ga.mutate()
# # print(ga)
# for _ in range(3):
# print(f'----------{_}')
# ga.calculate_objective_function()
# ga.assign_probability()
# ga.select()
# ga.match()
# ga.reproduce()
# ga.calculate_objective_function()
# print(ga.objective_mean)
# # ga.mutate()
# # ga.calculate_objective_function()
# # #of = [ind.objective_value for ind in ga.actual_generation.population]
# # #print(of)
# # print(ga.objective_mean)
# #print(ga)