mutation
This commit is contained in:
52
lab4/main.py
52
lab4/main.py
@@ -15,10 +15,11 @@ from gp.fitness import (
|
||||
)
|
||||
from gp.ga import GARunConfig, genetic_algorithm
|
||||
from gp.mutations import (
|
||||
grow_mutation,
|
||||
hoist_mutation,
|
||||
node_replacement_mutation,
|
||||
shrink_mutation,
|
||||
CombinedMutation,
|
||||
GrowMutation,
|
||||
HoistMutation,
|
||||
NodeReplacementMutation,
|
||||
ShrinkMutation,
|
||||
)
|
||||
from gp.ops import ADD, COS, DIV, EXP, MUL, NEG, POW, SIN, SQUARE, SUB
|
||||
from gp.population import ramped_initialization
|
||||
@@ -36,7 +37,6 @@ X = np.random.uniform(-5.536, 5.536, size=(TEST_POINTS, NUM_VARS))
|
||||
# axes = [np.linspace(-5.536, 5.536, TEST_POINTS) for _ in range(NUM_VARS)]
|
||||
# X = np.array(np.meshgrid(*axes)).T.reshape(-1, NUM_VARS)
|
||||
operations = [SQUARE, SIN, COS, EXP, ADD, SUB, MUL, DIV, POW]
|
||||
# operations = [SQUARE, ADD, SUB, MUL]
|
||||
terminals = [Var(f"x{i}") for i in range(1, NUM_VARS + 1)]
|
||||
|
||||
|
||||
@@ -53,36 +53,16 @@ def target_function(x: NDArray[np.float64]) -> NDArray[np.float64]:
|
||||
return np.sum(prefix_sums, axis=1)
|
||||
|
||||
|
||||
# fitness_function = MSEFitness(target_function, lambda: X)
|
||||
# fitness_function = HuberFitness(target_function, lambda: X, delta=0.5)
|
||||
# fitness_function = PenalizedFitness(
|
||||
# target_function, lambda: X, base_fitness=fitness, lambda_=0.1
|
||||
# )
|
||||
# fitness_function = NRMSEFitness(target_function, lambda: X)
|
||||
fitness_function = RMSEFitness(target_function, lambda: X)
|
||||
|
||||
# fitness_function = PenalizedFitness(
|
||||
# target_function, lambda: X, base_fitness=fitness_function, lambda_=0.0001
|
||||
# )
|
||||
|
||||
|
||||
def adaptive_mutation(
|
||||
chromosome: Chromosome,
|
||||
generation: int,
|
||||
max_generations: int,
|
||||
max_depth: int,
|
||||
) -> Chromosome:
|
||||
r = random.random()
|
||||
|
||||
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)
|
||||
|
||||
combined_mutation = CombinedMutation(
|
||||
mutations=[
|
||||
GrowMutation(max_depth=MAX_DEPTH),
|
||||
NodeReplacementMutation(),
|
||||
HoistMutation(),
|
||||
ShrinkMutation(),
|
||||
],
|
||||
probs=[0.4, 0.3, 0.15, 0.15],
|
||||
)
|
||||
|
||||
init_population = ramped_initialization(
|
||||
20, [i for i in range(MAX_DEPTH - 9, MAX_DEPTH + 1)], terminals, operations
|
||||
@@ -93,9 +73,7 @@ print("Population size:", len(init_population))
|
||||
config = GARunConfig(
|
||||
fitness_func=fitness_function,
|
||||
crossover_fn=lambda p1, p2: crossover_subtree(p1, p2, max_depth=MAX_DEPTH),
|
||||
mutation_fn=lambda chrom, gen_num: adaptive_mutation(
|
||||
chrom, gen_num, MAX_GENERATIONS, MAX_DEPTH
|
||||
),
|
||||
mutation_fn=combined_mutation,
|
||||
# selection_fn=roulette_selection,
|
||||
selection_fn=lambda p, f: tournament_selection(p, f, k=3),
|
||||
init_population=init_population,
|
||||
|
||||
Reference in New Issue
Block a user