NearestNeighbor#

Surrogate model based on the N-Dimensional Interpolation library by Stephen Marone.

Here is a simple example where the NearestNeighbor surrogate is used to approximate the output of a sinusoidal component.

import numpy as np

import openmdao.api as om

prob = om.Problem()

sin_mm = om.MetaModelUnStructuredComp()
sin_mm.add_input('x', 2.1)
sin_mm.add_output('f_x', 0., surrogate=om.NearestNeighbor(interpolant_type='linear'))

prob.model.add_subsystem('sin_mm', sin_mm)

prob.setup()

# train the surrogate and check predicted value
sin_mm.options['train_x'] = np.linspace(0,10,20)
sin_mm.options['train_f_x'] = .5*np.sin(sin_mm.options['train_x'])

prob.set_val('sin_mm.x', 2.1)

prob.run_model()

print(prob.get_val('sin_mm.f_x'))
[0.4309673]

NearestNeighbor Options#

All options can be passed in as arguments or set later by accessing the options dictionary.

OptionDefaultAcceptable ValuesAcceptable TypesDescription
interpolant_typerbf['linear', 'weighted', 'rbf']N/AType of interpolant, must be 'linear', 'weighted', or 'rbf'

Additional interpolant-specific options can be passed in as call arguments.

NearestNeighbor Constructor#

The call signature for the NearestNeighbor constructor is:

NearestNeighbor.__init__(**kwargs)[source]

Initialize all attributes.

Parameters:
**kwargsdict

options dictionary.

NearestNeighbor Option Examples#

interpolant_type

The NearestNeighbor surrogate allows you to choose from three different interpolant types.

Interpolant

Description

linear

Interpolates values by forming a hyperplane between the points closest to the prescribed inputs

weighted

Computes the weights based on the distance and distance effect.

rbf

Compactly Supported Radial Basis Function. (Default)

rbf interpolator arguments

When the “interpolant_type” option is set to “rbf”, there are some additional arguments that can be used to control the radial basis function interpolant.

For example, here we use the rbf interpolant for our simple sine model and set the number of neighbors (“num_neighbors”) to 3.

import numpy as np

import openmdao.api as om

prob = om.Problem()

sin_mm = om.MetaModelUnStructuredComp()
sin_mm.add_input('x', 2.1)
sin_mm.add_output('f_x', 0., surrogate=om.NearestNeighbor(interpolant_type='rbf', num_neighbors=3))

prob.model.add_subsystem('sin_mm', sin_mm)

prob.setup()

# train the surrogate and check predicted value
sin_mm.options['train_x'] = np.linspace(0,10,20)
sin_mm.options['train_f_x'] = .5*np.sin(sin_mm.options['train_x'])

prob.set_val('sin_mm.x', 2.1)

prob.run_model()

print(prob.get_val('sin_mm.f_x'))
[0.42963226]

The following parameters are available to be adjusted:

num_neighbors (int)

The number of neighbors to use for interpolation.

rbf_family (int)

Specifies the order of the radial basis function to be used.

  • -2 uses an 11th order

  • -1 uses a 9th order

  • any value from 0 to 4 uses an order equal to floor((dimensions-1)/2) + (3*comp) +1