1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: TestAmoebaMinimizer.cxx,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkAmoebaMinimizer.h"
#include <math.h>
// the function to be minimized
static void vtkFunctionToMinimize(void *arg)
{
vtkAmoebaMinimizer *minimizer = (vtkAmoebaMinimizer *)arg;
double x = minimizer->GetParameterValue("x");
double y = minimizer->GetParameterValue("y");
double z = minimizer->GetParameterValue("z");
double r = (x-5)*(x-5) + (y+2)*(y+2) + (z)*(z);
minimizer->SetFunctionValue(r);
}
int TestAmoebaMinimizer(int argc, char*[])
{
vtkAmoebaMinimizer *minimizer = vtkAmoebaMinimizer::New();
minimizer->SetFunction(&vtkFunctionToMinimize, minimizer);
minimizer->SetParameterValue("x",0.0);
minimizer->SetParameterScale("x",2.0);
minimizer->SetParameterValue("y",0.0);
minimizer->SetParameterScale("y",2.0);
minimizer->SetParameterValue("z",0.0);
minimizer->SetParameterScale("z",2.0);
minimizer->Minimize();
double x = minimizer->GetParameterValue("x");
double y = minimizer->GetParameterValue("y");
double z = minimizer->GetParameterValue("z");
double r = minimizer->GetFunctionValue();
int iterations = minimizer->GetIterations();
int maxiterations = minimizer->GetMaxIterations();
int noconvergence = minimizer->Iterate();
minimizer->Delete();
// check parameters to make sure that they converged to the
// correct values
if (argc > 10 ||
fabs(x - 5.0) > 1e-4 ||
fabs(y + 2.0) > 1e-4 ||
fabs(z - 0.0) > 1e-4 ||
r > 1e-4 ||
iterations >= maxiterations ||
noconvergence)
{
return 1;
}
return 0;
}
|