qchen-fdii-cardc/nlopt4m/search does not have a README file

    EvaluationContextError

    pub(all) suberror EvaluationContextError {
    SearchStopped(SearchTermination)
    SearchFailure(source~ : Error, last_point~ : EvaluatedPoint, evaluations~ : Int, gradient_evaluations~ : Int)
    } derive(
    Debug
    )

    A stopped context rejects further evaluations; a search failure can carry the original error and a best-point checkpoint.

    EvaluatedPoint

    pub(all) struct EvaluatedPoint {
    x : Array[Double]
    value : Double
    } derive(
    Debug
    )

    A checked value-only point retained by an evaluation context.

    EvaluationContext

    pub struct EvaluationContext {
    objective : (ArrayView[Double], Bool) -> (Double, Array[Double]?) raise
    has_gradient : Bool
    dimension : Int
    max_evaluations : Int
    target_value : Double?
    observer : (SearchEvent) -> SearchControl raise?
    evaluations : Int
    gradient_evaluations : Int
    best : EvaluatedPoint?
    cancelled : Bool
    }

    Evaluation budget with best-so-far recovery and observation.

    EvaluationContext::best

    Return an owned copy of the best valid point, if one has been observed.

    EvaluationContext::evaluate

    fn EvaluationContext::evaluate(self : EvaluationContext, x : ArrayView[Double]) -> Double raise

    Check the point, charge the attempt, evaluate, then update the checkpoint before invoking the observer. Callback errors propagate and the checkpoint remains available through best.

    EvaluationContext::evaluate_with_gradient

    fn EvaluationContext::evaluate_with_gradient(self : EvaluationContext, x : ArrayView[Double]) -> (Double, Array[Double]) raise

    Evaluate the value and gradient, validating both before updating the checkpoint or notifying the observer. Attempts count as both objective and gradient evaluations.

    EvaluationContext::evaluations

    fn EvaluationContext::evaluations(self : EvaluationContext) -> Int

    Number of attempted objective callbacks, including callbacks that failed.

    EvaluationContext::failure

    fn EvaluationContext::failure(self : EvaluationContext, source : Error) -> Error

    Attach a copy of the best valid point and attempted-work counts to an error. If no valid point has been evaluated, return the original error.

    test {
    let context = @search.EvaluationContext::new(x => x[0] * x[0], 1)
    ignore(context.evaluate([2.0]))
    let failure = context.failure(@nlopt4m.OptimizeError::LineSearchFailed)
    guard failure
    is @search.EvaluationContextError::SearchFailure(last_point~, ..) else {
    fail("expected a checkpoint")
    }
    assert_eq(last_point.value, 4.0)
    }

    EvaluationContext::gradient_evaluations

    fn EvaluationContext::gradient_evaluations(self : EvaluationContext) -> Int

    Number of attempted evaluations requesting a gradient, including failures.

    EvaluationContext::new

    fn EvaluationContext::new(objective : (ArrayView[Double]) -> Double raise, dimension : Int, max_evaluations? : Int, target_value? : Double, observer? : (SearchEvent) -> SearchControl raise) -> EvaluationContext raise
    OptimizeError

    Construct a context without evaluating the objective.

    test {
    let context = @search.EvaluationContext::new(
    x => x[0] * x[0],
    1,
    max_evaluations=2,
    )
    assert_eq(context.evaluate([2.0]), 4.0)
    assert_eq(context.best().unwrap().value, 4.0)
    }

    EvaluationContext::new_with_gradient

    fn EvaluationContext::new_with_gradient(objective : (ArrayView[Double], Bool) -> (Double, Array[Double]?) raise, dimension : Int, max_evaluations? : Int, target_value? : Double, observer? : (SearchEvent) -> SearchControl raise) -> EvaluationContext raise
    OptimizeError

    Construct a context whose callback can optionally supply a gradient. The callback's Boolean argument is true when a gradient is requested. In that case it must return Some finite vector matching the dimension.

    test {
    let context = @search.EvaluationContext::new_with_gradient(
    (x, need_gradient) => {
    (x[0] * x[0], if need_gradient { Some([2.0 * x[0]]) } else { None })
    },
    1,
    )
    let (value, gradient) = context.evaluate_with_gradient([2.0])
    assert_eq(value, 4.0)
    assert_eq(gradient, [4.0])
    }

    EvaluationContext::stop_reason

    Cancellation takes precedence over target attainment and budget exhaustion.

    GlobalSolution

    pub(all) struct GlobalSolution {
    x : Array[Double]
    value : Double
    generations : Int
    evaluations : Int
    seed : UInt64
    diversity : Double
    termination : SearchTermination
    } derive(
    Debug
    )

    Result of a seeded population search. Generations count fully completed generations; evaluations include initialization and partial generations. Diversity is the maximum normalized coordinate range of evaluated points in the current population.

    SearchControl

    pub(all) enum SearchControl {
    Continue
    Stop
    } derive(Eq,
    Debug
    )

    An observer's instruction after a successful objective evaluation.

    SearchEvent

    pub(all) struct SearchEvent {
    x : Array[Double]
    value : Double
    best_value : Double
    evaluations : Int
    } derive(
    Debug
    )

    A checked value-only evaluation, after updating the best-so-far value.

    SearchSolution

    pub(all) struct SearchSolution {
    x : Array[Double]
    value : Double
    iterations : Int
    evaluations : Int
    termination : SearchTermination
    } derive(
    Debug
    )

    Best point evaluated by a value-only search. Iterations count completed polls or simplex iterations; evaluations include all attempted trials.

    SearchTermination

    pub(all) enum SearchTermination {
    TargetReached
    Stagnated
    MaxIterations
    MaxEvaluations
    Cancelled
    } derive(Eq,
    Debug
    )

    A value-only search stops on a target, a limit, or mesh stagnation. Stagnation does not certify an optimum.

    differential_evolution

    fn differential_evolution(objective : (ArrayView[Double]) -> Double raise, lower : ArrayView[Double], upper : ArrayView[Double], seed? : UInt64, population_size? : Int, max_generations? : Int, max_evaluations? : Int, weight? : Double, crossover? : Double, rust_compatible_rng? : Bool, initial_population? : Array[Array[Double]], target_value? : Double, observer? : (SearchEvent) -> SearchControl raise) -> GlobalSolution raise

    Seeded synchronous DE/rand/1/bin on a finite box. The optional initial population must contain exactly population_size feasible points. Mutants outside the box are repaired halfway between the violated bound and the target. At least one crossover coordinate is forced.

    The returned best point includes partial initialization and generations; reaching a target does not certify a global optimum. The seed initializes MoonBit's ChaCha8 generator by default. Set rust_compatible_rng=true to use the Rust crate's seed expansion and sampling instead. The optional observer can stop after any successful evaluation; cancellation takes precedence over a target or budget reached at the same evaluation.

    test {
    let result = @search.differential_evolution(
    x => x[0] * x[0],
    [-1.0],
    [1.0],
    seed=42UL,
    max_evaluations=1000,
    target_value=1.0e-6,
    )
    assert_true(result.termination is TargetReached)
    }

    directional_difference

    fn directional_difference(context : EvaluationContext, x : ArrayView[Double], direction : ArrayView[Double], step : Double) -> Double raise

    Central directional difference using two value-only context evaluations. The direction is not normalized and trials are not projected onto bounds. A stop or error on the second probe leaves the first evaluation charged.

    test {
    let context = @search.EvaluationContext::new(x => x[0] * x[0], 1)
    let derivative = @search.directional_difference(context, [2.0], [1.0], 0.001)
    assert_true((derivative - 4.0).abs() < 1.0e-9)
    assert_eq(context.evaluations(), 2)
    }

    nelder_mead

    fn nelder_mead(objective : (ArrayView[Double]) -> Double raise, initial : ArrayView[Double], lower : ArrayView[Double], upper : ArrayView[Double], initial_step? : Double, step_tolerance? : Double, max_iterations? : Int, max_evaluations? : Int, target_value? : Double) -> SearchSolution raise

    Minimize a value-only objective over a finite box using a projected Nelder-Mead simplex. Each free coordinate contributes one simplex vertex; fixed coordinates are allowed. The inputs are copied and never modified.

    The returned best point includes trials evaluated during an incomplete simplex initialization or iteration. Small or collapsed simplices report Stagnated, not convergence or global optimality.

    test {
    let result = @search.nelder_mead(
    x => (x[0] - 0.25) * (x[0] - 0.25),
    [0.0],
    [-1.0],
    [1.0],
    target_value=1.0e-8,
    )
    assert_true(result.termination is TargetReached)
    }

    particle_swarm

    fn particle_swarm(objective : (ArrayView[Double]) -> Double raise, lower : ArrayView[Double], upper : ArrayView[Double], seed? : UInt64, population_size? : Int, max_generations? : Int, max_evaluations? : Int, inertia? : Double, cognitive? : Double, social? : Double, velocity_limit? : Double, rust_compatible_rng? : Bool, initial_population? : Array[Array[Double]], target_value? : Double, observer? : (SearchEvent) -> SearchControl raise) -> GlobalSolution raise

    Seeded synchronous global-best particle swarm on a finite box. Velocities start at zero and are limited to velocity_limit times the coordinate's box width. A particle that crosses a bound is clamped and its velocity in that coordinate is reset. All particles in a generation use the same best point from the previous generation.

    The returned best point includes partial initialization and generations; reaching a target does not certify a global optimum. Set rust_compatible_rng=true for Rust-compatible random sampling. The optional observer can stop after any successful evaluation; cancellation takes precedence over a target or budget reached at the same evaluation.

    test {
    let result = @search.particle_swarm(
    x => x[0] * x[0],
    [-1.0],
    [1.0],
    seed=42UL,
    max_evaluations=1000,
    target_value=1.0e-6,
    )
    assert_true(result.termination is TargetReached)
    }
    fn pattern_search(objective : (ArrayView[Double]) -> Double raise, initial : ArrayView[Double], lower : ArrayView[Double], upper : ArrayView[Double], initial_step? : Double, step_tolerance? : Double, max_iterations? : Int, max_evaluations? : Int, target_value? : Double) -> SearchSolution raise

    Minimize a value-only objective over a finite box by coordinate polling. initial_step and step_tolerance are fractions of each box width. Fixed coordinates are allowed. The inputs are copied and never modified.

    The returned best point may be from an unfinished poll. A target value is not a global-optimality certificate, and a small mesh is not convergence. Callback errors propagate and non-finite objective values raise an error.

    test {
    let result = @search.pattern_search(
    x => (x[0] - 0.25) * (x[0] - 0.25),
    [0.0],
    [-1.0],
    [1.0],
    target_value=1.0e-8,
    )
    assert_true(result.termination is TargetReached)
    assert_true(result.value <= 1.0e-8)
    }