Listing 2: Access routines and example main program.
#include "FuncObject.h" CFuncObject::CFuncObject ( ) { } CFuncObject::CFuncObject ( const double& a, const double& b ) { m_vdDataArray.resize ( 2 ); m_iNVar = 2; m_vdDataArray[0] = a; m_vdDataArray[1] = b; } double CFuncObject::operator ( ) ( const valarray<double> v ) const { return (v[1]-1.0)*(v[1]-1.0) + (v[0]-2.0)*(v[0]-2.0) ; } valarray< double > CFuncObject::operator ( ) ( ) const { return ( m_vdDataArray ); } #if !defined( FUNCOBJECT_H_INCLUDED ) #define FUNCOBJECT_H_INCLUDED #include <valarray> using namespace std; class CFuncObject { public: CFuncObject ( ); CFuncObject ( const double& , const double& ); double operator ( ) ( const valarray<double> v ) const; valarray< double > operator( ) ( ) const; // NOTE THAT COPYING THE FUNCTION OBJECT INTO THE SIMPLEX OBJECT // IMPLIES THAT BOTH A COPY CONSTRUCTOR AND AN ASSIGNMENT // OPERATOR // WILL BE REQUIRED FOR FULL SAFETY // THE VALARRAY SEEMS TO BE COPIED CORRECTLY SINCE DELETING THE // ORIGINAL // FUNCOBJECT DOES NOT PERTURB OPERATION, BUT POINTERS IN THE // FUNCOBJECT // CLEARLY COULD GENERATE TROUBLE FROM A SHALLOW COPY private: valarray<double> m_vdDataArray; int m_iNVar; }; #endif // (FUNCOBJECT_H_INCLUDED) #include <stdlib.h> #include <cstdio> #include <iostream> #include <cmath> #include <valarray> // #include "publishedsimplexsource.h" #include "simplex.h" #include "FuncObject.h" using namespace std; typedef valarray<double> vd; void OutputVector ( const valarray<double>& dV ); void OutputVector ( const valarray<double>& dV ) { int i; cout << "{ "; int nVar = dV.size( ); for ( i=0; i<nVar; ++i ) { cout << dV[i]; if ( i < nVar - 1 ) cout << ", "; } cout << " }" << endl; } int main (void) { valarray<double> VOutput; CFuncObject f ( 11.0, -5.0 ); simplex<CFuncObject> s( f ); f.~CFuncObject( ); // s.SetMaximumIterations( 5000 ); // s.SetLimitingFractionalChange( 0.0 ); pair<double,vd> pReturn = s.fnMinimize( ); cout << endl << " final FOM " << pReturn.first << endl; // cout << endl << " final FOM " << s.GetFinalFOM( ) << endl; // cout << " total interations performed " << // s.GetTotalIterations( ) << endl; cout << "best point found = "; cout << pReturn.second[0] << " " << pReturn.second[1] << endl; // OutputVector( s.GetFinalVector( ) ); // cout << "best point found (again) = "; // OutputVector( pReturn.second ); // s.OutputSimplex( ); return ( EXIT_SUCCESS ); }