14 #include "surrogate.h" 46 mMaxPoints = maxPoints;
48 mX.resize(dim, maxPoints);
49 mfX.resize(maxPoints);
59 return mX.cols(0, mNumPoints-1);
65 return mfX.rows(0, mNumPoints-1);
67 double fX(
int i)
const {
79 if(mNumPoints >= mMaxPoints) {
80 throw std::logic_error(
"Capacity exceeded");
82 mX.col(mNumPoints) = point;
83 mfX(mNumPoints) = funVal;
95 int n = points.n_cols;
98 throw std::logic_error(
"Use add_point instead");
100 if(mNumPoints + n > mMaxPoints) {
101 throw std::logic_error(
"Capacity exceeded");
104 mX.cols(mNumPoints, mNumPoints + n - 1) = points;
105 mfX.rows(mNumPoints, mNumPoints + n - 1) = funVals;
111 uvec indices = sort_index(dists);
112 return arma::mean(
mfX(indices.rows(0, mk - 1)));
119 vec vals = arma::zeros<vec>(points.n_cols);
120 for(
int i=0; i < points.n_cols; i++) {
121 vals(i) =
eval(points.col(i));
126 return evals(points);
133 throw std::logic_error(
"No derivatives for kNN");
double eval(const vec &point, const vec &dists) const
Method for evaluating the surrogate at multiple points.
Definition: kNN.h:115
void addPoint(const vec &point, double funVal)
Method for adding a point with a known value.
Definition: kNN.h:78
int numPoints() const
Method for getting the current number of points.
Definition: kNN.h:55
arma::vec vec
Default (column) vector class.
Definition: common.h:17
vec fX() const
Method for getting the values of the current points.
Definition: kNN.h:64
arma::uvec uvec
Default unsigned (column) vector class.
Definition: common.h:22
void addPoints(const mat &points, const vec &funVals)
Method for adding multiple points with known values.
Definition: kNN.h:94
vec evals(const mat &points, const mat &dists) const
Method for evaluating the surrogate at multiple points.
Definition: kNN.h:125
vec mfX
Definition: kNN.h:35
int mMaxPoints
Definition: kNN.h:31
mat X() const
Method for getting the current points.
Definition: kNN.h:58
Abstract class for a SOT surrogate model.
Definition: surrogate.h:25
k-nearest neighbors
Definition: kNN.h:28
double eval(const vec &point) const
Method for evaluating the surrogate model at a point.
Definition: kNN.h:109
int mDim
Definition: kNN.h:30
void fit()
Fits kNN (does nothing)
Definition: kNN.h:139
double fX(int i) const
Method for getting the value of current point number i (0 is the first)
Definition: kNN.h:67
int mk
Definition: kNN.h:33
kNN(int maxPoints, int dim, int k)
Constructor.
Definition: kNN.h:43
int dim() const
Method for getting the number of dimensions.
Definition: kNN.h:52
void reset()
Method for resetting the surrogate model.
Definition: kNN.h:135
VecType squaredPointSetDistance(const VecType &x, const MatType &Y)
Fast level-2 distance computation between one point and a set of points.
Definition: utils.h:32
vec evals(const mat &points) const
Method for evaluating the surrogate at multiple points.
Definition: kNN.h:118
vec X(int i) const
Method for getting current point number i (0 is the first)
Definition: kNN.h:61
mat mX
Definition: kNN.h:34
int mNumPoints
Definition: kNN.h:32
SOT namespace.
Definition: sot.h:27
arma::mat mat
Default matrix class.
Definition: common.h:16
vec deriv(const vec &point) const
Method for evaluating the kNN derivative at one point (not implemented)
Definition: kNN.h:132