best for now RMSE: 30.937

This commit is contained in:
2025-11-07 00:08:08 +03:00
parent cb2b031e9c
commit cfae423f11
4 changed files with 140 additions and 10 deletions

View File

@@ -15,16 +15,21 @@ from gp.fitness import (
RMSEFitness,
)
from gp.ga import GARunConfig, genetic_algorithm
from gp.mutations import grow_mutation, shrink_mutation
from gp.mutations import (
grow_mutation,
hoist_mutation,
node_replacement_mutation,
shrink_mutation,
)
from gp.ops import ADD, COS, DIV, EXP, MUL, NEG, POW, SIN, SQUARE, SUB
from gp.population import ramped_initialization
from gp.primitive import Const, Var
from gp.selection import roulette_selection
from gp.selection import roulette_selection, tournament_selection
NUM_VARS = 9
TEST_POINTS = 10000
MAX_DEPTH = 13
MAX_GENERATIONS = 500
MAX_DEPTH = 15
MAX_GENERATIONS = 200
np.random.seed(17)
random.seed(17)
X = np.random.uniform(-5.536, 5.536, size=(TEST_POINTS, NUM_VARS))
@@ -81,7 +86,7 @@ def target_function(x: NDArray[np.float64]) -> NDArray[np.float64]:
# fitness_function = PenalizedFitness(
# target_function, lambda: X, base_fitness=fitness, lambda_=0.003
# )
fitness_function = HuberFitness(target_function, lambda: X)
fitness_function = RMSEFitness(target_function, lambda: X)
# fitness_function = PenalizedFitness(
# target_function, lambda: X, base_fitness=fitness, lambda_=0.003
# )
@@ -103,9 +108,13 @@ def adaptive_mutation(
r = random.random()
# 50% grow, 50% shrink
if r < 0.5:
if r < 0.4:
return grow_mutation(chromosome, max_depth=max_depth)
elif r < 0.7:
return node_replacement_mutation(chromosome)
elif r < 0.85:
return hoist_mutation(chromosome)
return shrink_mutation(chromosome)
@@ -151,14 +160,15 @@ config = GARunConfig(
mutation_fn=lambda chrom, gen_num: adaptive_mutation(
chrom, gen_num, MAX_GENERATIONS, MAX_DEPTH
),
selection_fn=roulette_selection,
# selection_fn=roulette_selection,
selection_fn=lambda p, f: tournament_selection(p, f, k=3),
init_population=ramped_initialization(
15, [4, 5, 6, 6, 7, 7, 8, 9, 10, 11], terminals, operations
10, [4, 5, 6, 6, 7, 7, 8, 9, 10, 11], terminals, operations
),
seed=17,
pc=0.9,
pm=0.3,
elitism=30,
elitism=10,
max_generations=MAX_GENERATIONS,
log_every_generation=True,
)