Хромосомы для лаб4

This commit is contained in:
2025-10-21 12:26:43 +03:00
parent 268c4cf4a1
commit afd7a700ca
7 changed files with 273 additions and 0 deletions

23
lab4/main.py Normal file
View File

@@ -0,0 +1,23 @@
from gp import Chromosome, Terminal, ops
from gp.population import ramped_initialization
operations = ops.ALL
terminals = [Terminal(f"x{i}") for i in range(1, 9)]
chrom = Chromosome(operations, terminals, 8, "grow")
print("Depth:", chrom.depth)
print("Formula:", chrom)
print("Tree:\n", chrom.str_tree())
values = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
print("Value for ", values, ":", chrom.eval(values))
population = ramped_initialization(
100,
[3, 4, 5, 6, 7, 8],
lambda depth, method: Chromosome(operations, terminals, depth, method),
)
print("Population size:", len(population))
print("Population:")
[print(str(chrom)) for chrom in population]