AdaptiveSampling

Documentation for AdaptiveSampling.

AdaptiveSampling.AbstractCostType
AbstractCost{N}

An abstract type representing a cost function with N neighbors. This type serves as a base for defining various cost functions that compute the cost between points in a vector space.

To define a custom cost function, a subtype of AbstractCost must implement a method call for the type that takes two vectors as input and returns the cost between them.

Example

```julia struct MyCustomCost <: AbstractCost{2} norm::Float64 end

function (c::MyCustomCost)(x, y) # Custom cost computation return abs(last(x) - first(x)) / c.norm end

cost = MyCustomCost(1.0) cost([1, 3], [4, 6]) # returns 2.0

source
AdaptiveSampling.CompositeCostType
CompositeCost{N, M} <: AbstractCost{N}

A cost function that combines multiple cost functions into a composite cost. The composite cost is a weighted sum of the individual costs:

\[cost(x, y) = \sum_{k=1}^{M} w_k c_k(x, y)\]

where c_k is the k-th individual cost function, and w_k is the weight for the k-th cost function. The weights are normalized to sum to 1.

If a weight is set to Inf, the composite cost is the maximum of the individual costs. Otherwise, the composite cost is the weighted average of the individual costs.

Type Parameters

  • N: The maximum number of neighbors considered by the composite cost function.
  • M: The number of individual cost functions combined in the composite cost.

Fields

  • costs::NTuple{M, AbstractCost}: A tuple of individual cost functions.
  • weights::NTuple{M, Float64}: A tuple of weights for the individual cost functions.

Constructors

  • CompositeCost(costs::NTuple{M, AbstractCost}, weights::NTuple{M, Real}) where {M}: Creates a CompositeCost with the specified cost functions and weights. The weights are normalized to sum to 1.
  • CompositeCost(costs::Vararg{AbstractCost, M}) where {M}: Creates a CompositeCost with the specified cost functions and default weights of Inf.
source
AdaptiveSampling.EuclideanCostType
EuclideanCost <: AbstractCost{2}

A cost function that computes the cost as the Euclidean distance between last and first elements of the input vectors, normalized by a specified norm:

\[cost(x, y) = \frac{\sqrt{(x[end] - x[1])^2 + (y[end] - y[1])^2}}{\text{norm}}\]

Fields

  • norm::Float64: The normalization factor for the cost.

Constructors

  • EuclideanCost(): Creates a EuclideanCost with a default norm of 1.0.
  • EuclideanCost(norm): Creates a EuclideanCost with the specified norm.
  • EuclideanCost(a, b, fa, fb): Creates a EuclideanCost with the norm set to the Euclidean distance between points (a, fa) and (b, fb).
source
AdaptiveSampling.UniformCostType
UniformCost <: AbstractCost{2}

A cost function that computes the cost as the absolute difference between the last and first elements of the input vectors, normalized by a specified norm:

\[cost(x, y) = \frac{|x[end] - x[1]|}{\text{norm}}\]

Fields

  • norm::Float64: The normalization factor for the cost.

Constructors

  • UniformCost(): Creates a UniformCost with a default norm of 1.0.
  • UniformCost(norm): Creates a UniformCost with the specified norm.
  • UniformCost(a, b): Creates a UniformCost with the norm set to the absolute difference between a and b.
source
AdaptiveSampling.VisvalingamCostType
VisvalingamCost <: AbstractCost{3}

A cost function that computes the Visvalingam-Whyatt area-based cost between two points. The cost is defined as the square root of the area formed by the points, normalized by a specified norm:

\[cost(x, y) = \sqrt{\frac{\text{area}(x, y)}{\text{norm}}}\]

where area(x, y) is the area of the triangle formed by the points x and y, each of length 3 identifying three points in the x-y plane.

Fields

  • norm::Float64: The normalization factor for the cost.

Constructors

  • VisvalingamCost(): Creates a VisvalingamCost with a default norm of 1.0.
  • VisvalingamCost(norm): Creates a VisvalingamCost with the specified norm.
  • VisvalingamCost(a, b, fa, fb): Creates a VisvalingamCost with the norm set to the area of the triangle formed by points (a, fa) and (b, fb).
source
AdaptiveSampling.sampleFunction
sample(f, a, b; cost::AbstractCost=CompositeCost((UniformCost(a, b), VisvalingamCost(a, b, f(a), f(b))), (1, 100)), tol=1e-3, maxsamples=10000)

Samples the function f over the interval [a, b] using the specified cost function. The sampling process continues until the maximum cost is below the tolerance tol or the maximum number of samples maxsamples is reached.

Arguments

  • f: The function to be sampled.
  • a: The start of the interval.
  • b: The end of the interval.
  • cost::AbstractCost: The cost function to be used. Defaults to a composite cost combining UniformCost and VisvalingamCost.
  • tol: The tolerance for the maximum cost. Defaults to 1e-3.
  • maxsamples: The maximum number of samples. Defaults to 10000.

Returns

  • x: A vector of sample points.
  • y: A vector of function values at the sample points.
source
AdaptiveSampling.sample_costsMethod
sample_costs(f, a, b; cost::AbstractCost=CompositeCost((UniformCost(a, b), VisvalingamCost(a, b, f(a), f(b))), (1, 100)), tol=1e-3, maxsamples=10000)

Samples the function f over the interval [a, b] and computes the associated costs using the specified cost function. The sampling process continues until the maximum cost is below the tolerance tol or the maximum number of samples maxsamples is reached.

Arguments

  • f: The function to be sampled.
  • a: The start of the interval.
  • b: The end of the interval.
  • cost::AbstractCost: The cost function to be used. Defaults to a composite cost combining UniformCost and VisvalingamCost.
  • tol: The tolerance for the maximum cost. Defaults to 1e-3.
  • maxsamples: The maximum number of samples. Defaults to 10000.

Returns

  • x: A vector of sample points.
  • y: A vector of function values at the sample points.
  • c: A vector of costs associated with the sample points.
source