autoeq-optim 0.5.58

AutoEQ objectives, constraints, and optimizer backends
Documentation
use super::super::ObjectiveData;
use super::create::create_de_callback;
use super::create::create_de_objective;
use super::misc::process_de_results;
use super::misc::register_de_constraint;
use super::types::setup_de_common;
use crate::constraints::{
    CeilingConstraintData, MinGainConstraintData, SpacingConstraintData, constraint_ceiling,
    constraint_min_gain, constraint_spacing,
};
use crate::de::init_sobol::init_halton;
use crate::de::{
    CallbackAction, DEConfigBuilder, DEIntermediate, Init, Mutation, ParallelConfig, Strategy,
    differential_evolution,
};
use crate::initial_guess::{SmartInitConfig, create_smart_initial_guesses};
use ndarray::Array1;

/// Optimize filter parameters using AutoEQ custom algorithms
pub fn optimize_filters_autoeq(
    x: &mut [f64],
    lower_bounds: &[f64],
    upper_bounds: &[f64],
    objective_data: ObjectiveData,
    autoeq_name: &str,
    params: &crate::OptimParams,
) -> Result<(String, f64), (String, f64)> {
    // Create the callback with all the logging and user feedback
    let callback = create_de_callback("autoeq::DE", params.quiet);

    // Delegate to the callback-based version
    optimize_filters_autoeq_with_callback(
        x,
        lower_bounds,
        upper_bounds,
        objective_data,
        autoeq_name,
        params,
        callback,
    )
}

/// AutoEQ DE optimization with external progress callback
pub fn optimize_filters_autoeq_with_callback(
    x: &mut [f64],
    lower_bounds: &[f64],
    upper_bounds: &[f64],
    objective_data: ObjectiveData,
    _autoeq_name: &str,
    params: &crate::OptimParams,
    mut callback: Box<dyn FnMut(&DEIntermediate) -> CallbackAction + Send>,
) -> Result<(String, f64), (String, f64)> {
    // Extract parameters from args
    let population = params.population;
    let maxeval = params.maxeval;

    // Reuse same setup as standard AutoEQ DE
    let setup = setup_de_common(
        lower_bounds,
        upper_bounds,
        objective_data.clone(),
        population,
        maxeval,
        params.quiet,
    );
    let base_objective_fn = create_de_objective(setup.penalty_data.clone());

    // Create smart initialization based on frequency response analysis
    // Skip for drivers-flat loss as it uses a different parameter layout
    let smart_guesses = if matches!(
        setup.penalty_data.loss_type,
        crate::LossType::DriversFlat | crate::LossType::MultiSubFlat
    ) {
        Vec::new()
    } else {
        let params_per_filter = crate::param_utils::params_per_filter(params.peq_model);
        let num_filters = x.len() / params_per_filter;
        // If the caller (typically roomeq's `prepare_single_channel_eq`)
        // already detected high-quality room-mode problems via SSIR /
        // decomposed correction, feed them into the smart-guess
        // generator instead of letting it run its own cruder
        // find_peaks over the smoothed deviation. Empty list → fall
        // back to the legacy auto-detection.
        let pre_detected_problems = setup.penalty_data.detected_problems.clone();
        if !pre_detected_problems.is_empty() && !params.quiet {
            log::debug!(
                "🎯 Seeding smart initial guesses with {} pre-detected problem(s) from upstream analysis",
                pre_detected_problems.len()
            );
        }
        let smart_config = SmartInitConfig {
            seed: params.seed, // Pass seed for deterministic initialization
            pre_detected_problems,
            ..SmartInitConfig::default()
        };

        // Use the deviation curve (target - measurement) to identify problems.
        // Positive deviation = needs boost, negative = needs cut.
        let target_response = &setup.penalty_data.deviation;
        let freq_grid = &setup.penalty_data.freqs;

        if !params.quiet {
            log::debug!(
                "🧠 Generating smart initial guesses based on frequency response analysis..."
            );
        }
        let guesses = create_smart_initial_guesses(
            target_response,
            freq_grid,
            num_filters,
            &setup.bounds,
            &smart_config,
            params.peq_model,
        );

        if !params.quiet {
            log::debug!("📊 Generated {} smart initial guesses", guesses.len());
        }
        guesses
    };

    // Generate Sobol quasi-random population for better space coverage
    let sobol_samples = init_halton(
        x.len(),
        setup.population_size.saturating_sub(smart_guesses.len()),
        &setup.bounds,
    );

    if !params.quiet {
        log::debug!(
            "🎯 Generated {} Sobol quasi-random samples",
            sobol_samples.len()
        );
    }

    // Use the best smart guess as initial x0, fall back to Sobol initialization
    let best_initial_guess = if !smart_guesses.is_empty() {
        // Use the first (best) smart guess
        Array1::from(smart_guesses[0].clone())
    } else if !sobol_samples.is_empty() {
        // Fallback to the first Sobol sample if no smart guesses
        Array1::from(sobol_samples[0].clone())
    } else {
        // Ultimate fallback: use current x as initial guess
        Array1::from(x.to_vec())
    };

    if !params.quiet {
        log::debug!("🚀 Using smart initial guess with Sobol population initialization");
    }

    // Parse strategy from CLI args
    use std::str::FromStr;
    let strategy = Strategy::from_str(&params.strategy).unwrap_or_else(|_| {
        if !params.quiet {
            log::debug!(
                "⚠️ Warning: Invalid strategy '{}', falling back to CurrentToBest1Bin",
                params.strategy
            );
        }
        Strategy::CurrentToBest1Bin
    });

    // Set up adaptive configuration if using adaptive strategies
    let adaptive_config = if matches!(strategy, Strategy::AdaptiveBin | Strategy::AdaptiveExp) {
        Some(crate::de::AdaptiveConfig {
            adaptive_mutation: true,
            wls_enabled: false,                    // Disable WLS for stability
            w_max: 0.8,                            // Reduce max weight for more stability
            w_min: 0.2,                            // Increase min weight for more stability
            w_f: params.adaptive_weight_f * 0.5,   // Make adaptation even more conservative
            w_cr: params.adaptive_weight_cr * 0.5, // Make adaptation even more conservative
            f_m: 0.6,                              // Start with slightly higher F
            cr_m: 0.5,                             // Start with slightly lower CR
            wls_prob: 0.0,                         // Completely disable WLS
            wls_scale: 0.0,                        // Completely disable WLS
        })
    } else {
        None
    };

    // Adjust tolerance for adaptive strategies (they need much more relaxed convergence)
    let (tolerance, atolerance) =
        if matches!(strategy, Strategy::AdaptiveBin | Strategy::AdaptiveExp) {
            // Use much more relaxed tolerances for adaptive strategies - they converge differently
            (params.tolerance * 10.0, params.atolerance * 10.0)
        } else {
            (params.tolerance, params.atolerance)
        };

    // Use constraint helpers for nonlinear constraints
    let mut config_builder = DEConfigBuilder::new()
        .maxiter(setup.max_iter)
        .popsize(setup.pop_multiplier)
        .tol(tolerance)
        .atol(atolerance)
        .strategy(strategy)
        .mutation(Mutation::Range { min: 0.4, max: 1.2 })
        .recombination(params.recombination)
        .init(Init::LatinHypercube) // Use Latin Hypercube sampling for population
        .x0(best_initial_guess) // Use smart guess as initial best individual
        .disp(false)
        .callback(Box::new(move |intermediate| callback(intermediate)));

    // Add seed if provided for deterministic results
    if let Some(seed_value) = params.seed {
        config_builder = config_builder.seed(seed_value);
        if !params.quiet {
            log::debug!("🎲 Using deterministic seed: {}", seed_value);
        }
    }

    // Add adaptive configuration if present
    if let Some(adaptive_cfg) = adaptive_config {
        config_builder = config_builder.adaptive(adaptive_cfg);
    }

    // Configure parallel evaluation
    let parallel_config = ParallelConfig {
        enabled: !params.no_parallel,
        num_threads: if params.parallel_threads == 0 {
            None // Use all available cores
        } else {
            Some(params.parallel_threads)
        },
    };
    config_builder = config_builder.parallel(parallel_config);

    if !params.no_parallel && !params.quiet {
        log::debug!(
            "🚄 Parallel evaluation enabled with {} threads",
            if params.parallel_threads.eq(&0) {
                "all available".to_string()
            } else {
                params.parallel_threads.to_string()
            }
        );
    }

    // Add native nonlinear constraints
    let mut config = config_builder
        .build()
        .map_err(|e| (format!("DE config build failed: {:?}", e), f64::INFINITY))?;

    // Register nonlinear constraints using helper
    if setup.penalty_data.max_db > 0.0 {
        register_de_constraint(
            &mut config,
            constraint_ceiling,
            CeilingConstraintData {
                freqs: setup.penalty_data.freqs.clone(),
                srate: setup.penalty_data.srate,
                max_db: setup.penalty_data.max_db,
                peq_model: setup.penalty_data.peq_model,
            },
        );
    }

    if setup.penalty_data.min_db > 0.0 {
        register_de_constraint(
            &mut config,
            constraint_min_gain,
            MinGainConstraintData {
                min_db: setup.penalty_data.min_db,
                peq_model: setup.penalty_data.peq_model,
            },
        );
    }

    if setup.penalty_data.min_spacing_oct > 0.0 {
        register_de_constraint(
            &mut config,
            constraint_spacing,
            SpacingConstraintData {
                min_spacing_oct: setup.penalty_data.min_spacing_oct,
                peq_model: setup.penalty_data.peq_model,
            },
        );
    }

    let result = differential_evolution(&base_objective_fn, &setup.bounds, config)
        .map_err(|e| (format!("DE optimization failed: {:?}", e), f64::INFINITY))?;
    process_de_results(x, result, "AutoDE")
}