source/inc/numeric/testtool.hxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  The Contents of this file are made available subject to
00004  *  the terms of GNU Lesser General Public License Version 2.1.
00005  *
00006  *
00007  *    GNU Lesser General Public License Version 2.1
00008  *    =============================================
00009  *    Copyright 2008 by Kohei Yoshida.
00010  *    1039 Kingsway Dr., Apex, NC 27502, USA
00011  *
00012  *    This library is free software; you can redistribute it and/or
00013  *    modify it under the terms of the GNU Lesser General Public
00014  *    License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *    This library is distributed in the hope that it will be useful,
00017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *    Lesser General Public License for more details.
00020  *
00021  *    You should have received a copy of the GNU Lesser General Public
00022  *    License along with this library; if not, write to the Free Software
00023  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *    MA  02111-1307  USA
00025  *
00026  ************************************************************************/
00027 
00028 #ifndef _SCSOLVER_NUMERIC_TESTTOOL_HXX_
00029 #define _SCSOLVER_NUMERIC_TESTTOOL_HXX_
00030 
00031 #ifndef SCSOLVER_UNITTEST
00032 #warning "this header should only be used in unit-testing code."
00033 #endif
00034 
00035 #include "numeric/funcobj.hxx"
00036 #include "numeric/baselinesearch.hxx"
00037 #include "numeric/nlpbase.hxx"
00038 #include "numeric/nlpmodel.hxx"
00039 #include "numeric/type.hxx"
00040 #include <stdio.h>
00041 #include <string>
00042 #include <memory>
00043 #include <vector>
00044 
00045 class SingleVarTestFuncBase : public ::scsolver::numeric::SingleVarFuncObj
00046 {
00047 public:
00048     virtual double getVar() const
00049     {
00050         return mVar;
00051     }
00052 
00053     virtual void setVar(double var)
00054     {
00055         mVar = var;
00056     }
00057 
00058 private:
00059     double mVar;
00060 };
00061 
00062 template<typename DoubleContainerType>
00063 void printContainer(typename DoubleContainerType::const_iterator begin, 
00064                     typename DoubleContainerType::const_iterator end, 
00065                     const char* msg)
00066 {
00067     FILE* fs = stdout;
00068     fprintf(fs, "%s: (", msg);
00069 
00070     for (typename DoubleContainerType::const_iterator itr = begin; itr != end; ++itr)
00071     {
00072         if (itr != begin)
00073             fprintf(fs, ", ");
00074         fprintf(fs, "%g", *itr);
00075     }
00076     fprintf(fs, ")\n");
00077 }
00078 
00079 void runSingleVarTestFunc(::scsolver::numeric::BaseLineSearch* _lineSearch, 
00080                           ::scsolver::numeric::SingleVarFuncObj* _funcObj,
00081                           ::scsolver::numeric::GoalType _goal = ::scsolver::numeric::GOAL_MINIMIZE)
00082 {
00083     using namespace ::scsolver::numeric;
00084     ::std::auto_ptr<BaseLineSearch> pLineSearch(_lineSearch);
00085     ::std::auto_ptr<SingleVarFuncObj> pFunc(_funcObj);
00086     pLineSearch->setFuncObj(pFunc.get());
00087 
00088     fprintf(stdout, "--------------------------------------------------------------------\n");
00089     fprintf(stdout, "now testing the function [ %s ]\n", pFunc->getFuncString().c_str());
00090     pLineSearch->setGoal(_goal);
00091     pLineSearch->setDebug(true);
00092     double x = pLineSearch->solve();
00093 
00094     switch (_goal)
00095     {
00096         case ::scsolver::numeric::GOAL_MAXIMIZE:
00097             fprintf(stdout, "maximization point (%g)\n", x);
00098         break;
00099         case ::scsolver::numeric::GOAL_MINIMIZE:
00100             fprintf(stdout, "minimization point (%g)\n", x);
00101         break;
00102         case ::scsolver::numeric::GOAL_UNKNOWN:
00103         default:
00104             // not handled
00105         break;
00106     }
00107 }
00108 
00119 void runNonLinearTest(::scsolver::numeric::nlp::BaseAlgorithm* _nlpSolver, 
00120                       ::scsolver::numeric::BaseFuncObj* _funcObj)
00121 {
00122     using namespace ::scsolver::numeric;
00123 
00124     ::std::auto_ptr<nlp::BaseAlgorithm> nlpSolver(_nlpSolver);
00125     ::std::auto_ptr<BaseFuncObj> func(_funcObj);
00126     nlp::Model model;
00127     model.setGoal(GOAL_MINIMIZE);
00128     model.setFuncObject(func.get());
00129 
00130     // Transfer initial variables from the function object to the mode instance.
00131     const ::std::vector<double>& vars = func->getVars();
00132     ::std::vector<double>::const_iterator itr = vars.begin(), itrEnd = vars.end();
00133     for (; itr != itrEnd; ++itr)
00134         model.pushVar(*itr);
00135 
00136     nlpSolver->setModel(&model);
00137     nlpSolver->setDebug(true);
00138     try
00139     {
00140         nlpSolver->solve();
00141         const ::std::vector<double>& sol = nlpSolver->getSolution();
00142         printContainer< ::std::vector<double> >(sol.begin(), sol.end(), "solution");
00143     }
00144     catch (const ::std::exception& e)
00145     {
00146         fprintf(stdout, "  standard exception: %s\n", e.what());
00147     }
00148 }
00149 
00150 #endif

Generated on Mon Jul 28 09:13:20 2008 for scsolver by  doxygen 1.5.3