Skip to content

Config Objects

sklearn-genetic-opt groups advanced settings into four configuration objects. Passing None (the default) uses sensible defaults for each.

EvolutionConfig

Controls the core evolutionary algorithm parameters.

python
from sklearn_genetic import EvolutionConfig

EvolutionConfig(
    population_size=50,
    generations=80,
    crossover_probability=0.8,
    mutation_probability=0.1,
    tournament_size=3,
    elitism=True,
    keep_top_k=1,
    criteria="max",
    algorithm="eaMuPlusLambda",
)
ParameterTypeDefaultDescription
population_sizeint50Number of individuals per generation
generationsint80Number of generations to run
crossover_probabilityfloat or adapter0.8Probability of crossover between two parents. Can be a fixed float or an adapter schedule
mutation_probabilityfloat or adapter0.1Per-individual mutation probability. Can be a fixed float or an adapter schedule
tournament_sizeint3Number of individuals competing in tournament selection
elitismboolTrueCarry the best individual unchanged to the next generation
keep_top_kint1Size of the hall of fame — best k individuals are preserved across generations
criteria"max" or "min""max"Whether to maximize or minimize the scoring metric
algorithmstr"eaMuPlusLambda"DEAP algorithm: "eaMuPlusLambda", "eaMuCommaLambda", or "eaSimple"

PopulationConfig

Controls how the initial population is created and seeded.

python
from sklearn_genetic import PopulationConfig

PopulationConfig(
    initializer="smart",
    warm_start_configs=None,
)
ParameterTypeDefaultDescription
initializer"smart" or "random""smart"Population initialization strategy. "smart" uses Latin hypercube sampling, estimator defaults, stratified categoricals, and duplicate avoidance
warm_start_configslist of dictsNoneSpecific parameter configurations to include in the initial population. Each dict must have keys matching param_grid. Invalid configs are silently skipped

RuntimeConfig

Controls parallelism, caching, and runtime behavior.

python
from sklearn_genetic import RuntimeConfig

RuntimeConfig(
    n_jobs=None,
    pre_dispatch="2*n_jobs",
    error_score=np.nan,
    return_train_score=False,
    use_cache=True,
    parallel_backend="auto",
    verbose=True,
)
ParameterTypeDefaultDescription
n_jobsint or NoneNoneNumber of parallel jobs. None = 1, -1 = all available cores
pre_dispatchstr or int"2*n_jobs"Number of jobs pre-dispatched for parallel execution
error_scorefloat or "raise"np.nanScore to assign when a candidate's fit raises an exception. Use "raise" to surface exceptions during debugging
return_train_scoreboolFalseInclude training scores in cv_results_
use_cacheboolTrueCache evaluated candidates and reuse scores when the same configuration appears again
parallel_backendstr"auto"Parallelism strategy: "auto" or "population" (parallel across candidates in each generation), "cv" (parallel across CV folds within each candidate)
verboseboolTruePrint the per-generation log during fit

Nested parallelism

If your estimator already uses parallelism (e.g., RandomForestClassifier(n_jobs=-1)), set parallel_backend="cv" or set the estimator's n_jobs=1 to avoid oversubscribing the CPU.

OptimizationConfig

Optional quality controls for the main GA loop. All features here are disabled by default except diversity_control.

python
from sklearn_genetic import OptimizationConfig

OptimizationConfig(
    # Diversity control (enabled by default)
    diversity_control=True,
    diversity_threshold=0.25,
    diversity_stagnation_generations=5,
    diversity_mutation_boost=2.0,
    random_immigrants_fraction=0.1,

    # Adaptive tournament selection
    adaptive_selection=False,
    selection_pressure_min=2,
    selection_pressure_max=None,
    offspring_diversity_retries=0,

    # Fitness sharing
    fitness_sharing=False,
    sharing_radius=0.2,
    sharing_alpha=1.0,

    # Local search
    local_search=False,
    local_search_top_k=1,
    local_search_steps=1,
    local_search_radius=0.1,

    # Final re-evaluation
    final_selection=False,
    final_selection_top_k=3,
    final_selection_cv=None,
)

Diversity Control

ParameterTypeDefaultDescription
diversity_controlboolTrueMonitor population diversity and inject random immigrants when it drops below diversity_threshold
diversity_thresholdfloat0.25Genotype diversity level below which intervention triggers. Values 0.1–0.3 are typical
diversity_stagnation_generationsint5Also trigger diversity control after this many stagnant generations, even if diversity is above the threshold
diversity_mutation_boostfloat2.0Multiplier applied to mutation_probability when diversity control triggers. Values 1.5–2.5 are typical
random_immigrants_fractionfloat0.1Fraction of offspring replaced with random individuals when diversity control triggers

Adaptive Selection

ParameterTypeDefaultDescription
adaptive_selectionboolFalseAdjust tournament size based on current diversity and stagnation
selection_pressure_minint2Minimum tournament size when diversity is low
selection_pressure_maxint or NoneNoneMaximum tournament size. None uses tournament_size from EvolutionConfig
offspring_diversity_retriesint0Number of times to retry generating offspring that are distinct from parents

Fitness Sharing

ParameterTypeDefaultDescription
fitness_sharingboolFalseApply niche-aware selection pressure: reduce the effective fitness of candidates that are too similar to each other
sharing_radiusfloat0.2Niche radius — normalized distance below which two individuals share fitness. Values 0.15–0.35 are typical
sharing_alphafloat1.0Sharing function exponent. Higher values narrow the sharing effect to very similar individuals
ParameterTypeDefaultDescription
local_searchboolFalseAfter the genetic search, evaluate neighbors of the top local_search_top_k hall-of-fame individuals
local_search_top_kint1Number of hall-of-fame individuals to refine
local_search_stepsint1Number of neighbor evaluations per hall-of-fame individual. Each step adds cross-validation calls
local_search_radiusfloat0.1Step size as a fraction of the parameter range. Values 0.05–0.15 keep refinement local

Final Selection

ParameterTypeDefaultDescription
final_selectionboolFalseRe-evaluate the top final_selection_top_k candidates after the GA and select the best before refitting
final_selection_top_kint3Number of top candidates to re-evaluate
final_selection_cvCV splitter or NoneNoneCross-validation strategy for final selection. None reuses the main cv

See Also

Released under the MIT License.