00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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