74 s->
v = (1 - lambda) * vbar + lambda *
v;
87 int compare_aux(
const void * s1,
const void * s2)
94 void sort_simplex(std::vector<vnl_amoeba_SimplexCorner>& simplex)
96 std::qsort(&simplex[0], simplex.size(),
sizeof simplex[0], compare_aux);
103 for (
unsigned i = 0; i < a.
size(); ++i) {
112 double sorted_simplex_fdiameter(
const std::vector<vnl_amoeba_SimplexCorner>& simplex)
114 return simplex[simplex.size()-1].fv - simplex[0].fv;
118 double simplex_diameter(
const std::vector<vnl_amoeba_SimplexCorner>& simplex)
121 for (
unsigned i = 0; i < simplex.size() - 1; i++) {
122 double thismax = maxabsdiff(simplex[i].
v, simplex[i+1].
v);
132 s <<
'S' << simplex.
fv <<
' ';
136 std::ostream&
operator<<(std::ostream& s,
const std::vector<vnl_amoeba_SimplexCorner>& simplex)
138 for (
const auto & i : simplex)
156 simplex[0].fv =
f(x);
162 for (
int j = 0; j < n; ++j) {
168 s->
v[j] = (1 + usual_delta)*s->
v[j];
184 simplex[0].fv =
f(x);
186 for (
int j = 0; j < n; ++j) {
191 s->
v[j] = s->
v[j] + dx[j];
245 std::vector<vnl_amoeba_SimplexCorner>& simplex)
248 sort_simplex(simplex);
251 std::cerr <<
"initial\n" << simplex;
254 std::cerr <<
"initial: " << simplex <<
'\n';
271 for (
int k = 0; k < n; ++k) {
273 for (
int i = 0; i < n; ++i)
274 vbar[k] += simplex[i].
v[k];
281 const char *how =
"reflect ";
282 if (reflect.
fv < simplex[n-1].fv) {
284 if (reflect.
fv < simplex[0].fv) {
288 if (expand.
fv < simplex[0].fv) {
298 if (reflect.
fv < tmp->
fv)
304 if (contract.
fv < simplex[0].fv) {
311 for (
int j = 1; j < n; ++j)
322 sort_simplex(simplex);
327 std::sprintf(buf,
"iter %5d: %s ",
cnt, how);
330 std::cerr <<
"\nFirst corner: " << simplex[0].v;
333 std::streamsize a = std::cerr.width(10);
334 std::cerr <<
'\n' << simplex <<
'\n';
338 std::cerr << simplex <<
'\n';
401 ls_(&ls),
fx(ls.get_number_of_residuals()) {}
An object that represents a function from R^n -> R.
double f(const vnl_vector< double > &x)
vnl_amoeba_SimplexCorner & operator=(const vnl_amoeba_SimplexCorner &that)
static int compare(vnl_amoeba_SimplexCorner const &s1, vnl_amoeba_SimplexCorner const &s2)
int get_number_of_unknowns() const
Return the number of unknowns.
Nelder-Meade downhill simplex.
Abstract base for minimising functions.
double relative_diameter
Scaling used to select starting vertices relative to initial x0.
void set_corner(vnl_amoeba_SimplexCorner *s, const vnl_vector< double > &v)
vnl_amoeba_SimplexCorner(int=0)
void set_up_simplex_absolute(std::vector< vnl_amoeba_SimplexCorner > &simplex, const vnl_vector< double > &x, const vnl_vector< double > &dx)
Initialise the simplex given one corner, x and displacements of others.
size_t size() const
Return the length, number of elements, dimension of this vector.
double f(vnl_vector< double > const &x) override
Apply the function.
Namespace with standard math functions.
double f(vnl_vector< double > const &x) override
The main function. Given the parameter vector x, compute the value of f(x).
Nelder-Meade downhill simplex.
Abstract base for minimising functions.
abs_t squared_magnitude() const
Return sum of squares of elements.
void set_corner_a_plus_bl(vnl_amoeba_SimplexCorner *s, const vnl_vector< double > &vbar, const vnl_vector< double > &v, double lambda)
void minimize(vnl_vector< double > &x)
Modify x to minimise function supplied in constructor.
void amoeba(vnl_vector< double > &x)
Perform optimisation. Start simplex defined by scaling elements of x.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
bool operator==(int r1, vnl_finite_int< N > const &r2)
vnl_amoeba(vnl_cost_function &f)
Construct and supply function to be minimized.
static bool default_verbose
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
vnl_bignum abs(vnl_bignum const &x)
vnl_amoeba_LSCF(vnl_least_squares_function &ls)
~vnl_amoeba_LSCF() override=default
vnl_least_squares_function * ls_
vnl_amoebaFit(vnl_amoeba &a)
virtual void f(vnl_vector< double > const &x, vnl_vector< double > &fx)=0
The main function.
void set_up_simplex_relative(std::vector< vnl_amoeba_SimplexCorner > &simplex, const vnl_vector< double > &x)
Initialise the simplex given one corner, x (scale each element to get other corners).