source/numeric/cycliccoordinate_test.cxx

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 
00029 #include "numeric/cycliccoordinate.hxx"
00030 #include "numeric/funcobj.hxx"
00031 #include "numeric/type.hxx"
00032 #include "numeric/nlpmodel.hxx"
00033 #include "numeric/exception.hxx"
00034 
00035 #include <cstdlib>
00036 #include <memory>
00037 
00038 using namespace ::scsolver::numeric;
00039 using ::scsolver::numeric::nlp::CyclicCoordinate;
00040 using ::std::string;
00041 using ::std::vector;
00042 using ::std::auto_ptr;
00043 
00044 void debugPrint(const vector<double>& vars, const char* msg)
00045 {
00046     FILE* fs = stdout;
00047     fprintf(fs, "%s: (", msg);
00048     size_t n = vars.size();
00049     for (size_t i = 0; i < n; ++i)
00050     {
00051         if (i > 0)
00052             fprintf(fs, ", ");
00053         fprintf(fs, "%g", vars[i]);
00054     }
00055     fprintf(fs, ")\n");
00056 }
00057 
00058 class TestFunc1 : public BaseFuncObj
00059 {
00060 public:
00061     TestFunc1() :
00062         BaseFuncObj(2)
00063     {
00064     }
00065 
00066     virtual ~TestFunc1()
00067     {
00068     }
00069 
00070     virtual double eval() const
00071     {
00072         const vector<double>& vars = getVars();
00073         double term1 = vars[0] - 2;
00074         term1 *= term1*term1*term1;
00075 
00076         double term2 = vars[0] - 2.0*vars[1];
00077         term2 *= term2;
00078 
00079         return term1 + term2;
00080     }
00081 
00085     virtual const string getFuncString() const
00086     {
00087         return string("(x1 - 2)^4 + (x1 - 2*x2)^2");
00088     }
00089 };
00090 
00091 class TestFunc2 : public BaseFuncObj
00092 {
00093 public:
00094     TestFunc2() :
00095         BaseFuncObj(3)
00096     {
00097         setVar(0, 0.0);
00098         setVar(1, 0.0);
00099         setVar(2, 0.0);
00100     }
00101 
00102     virtual ~TestFunc2()
00103     {
00104     }
00105 
00106     virtual double eval() const
00107     {
00108         const vector<double>& vars = getVars();
00109         double x1 = vars[0], x2 = vars[1], x3 = vars[2];
00110         return (x1*x1 - 2)*(x2 + 5) + (1 - x2)*(4 + x3);
00111     }
00112 
00113     virtual const string getFuncString() const
00114     {
00115         return string("(x1*x1 - 2)*(x2 + 5) + (1 - x2)*(4 + x3)");
00116     }
00117 };
00118 
00119 void runTest1()
00120 {
00121     fprintf(stdout, "runTest1: --begin\n");
00122     nlp::Model model;
00123     model.setGoal(GOAL_MINIMIZE);
00124 
00125     auto_ptr<BaseFuncObj> func1(new TestFunc1);
00126     model.setFuncObject(func1.get());
00127     model.pushVar(0.0);
00128     model.pushVar(3.0);
00129 
00130     auto_ptr<CyclicCoordinate> nlpSolver(new CyclicCoordinate);
00131     nlpSolver->setModel(&model);
00132     try
00133     {
00134         nlpSolver->solve();
00135     }
00136     catch (const ::std::exception& e)
00137     {
00138         fprintf(stdout, "runTest1:   standard exception: %s\n", e.what());
00139     }
00140     const vector<double>& sol = nlpSolver->getSolution();
00141     debugPrint(sol, "solution");
00142     fprintf(stdout, "runTest1: --end\n");
00143 }
00144 
00145 void runTest(BaseFuncObj* pFuncObj)
00146 {
00147     auto_ptr<BaseFuncObj> func(pFuncObj);
00148     nlp::Model model;
00149     model.setGoal(GOAL_MINIMIZE);
00150     model.setFuncObject(func.get());
00151 
00152     // Set initial variables to the mode instance.
00153     const vector<double>& vars = func->getVars();
00154     vector<double>::const_iterator itr = vars.begin(), itrEnd = vars.end();
00155     for (; itr != itrEnd; ++itr)
00156         model.pushVar(*itr);
00157 
00158     auto_ptr<CyclicCoordinate> nlpSolver(new CyclicCoordinate);
00159     nlpSolver->setModel(&model);
00160     try
00161     {
00162         nlpSolver->solve();
00163         const vector<double>& sol = nlpSolver->getSolution();
00164         debugPrint(sol, "solution");
00165     }
00166     catch (const ::std::exception& e)
00167     {
00168         fprintf(stdout, "  standard exception: %s\n", e.what());
00169     }
00170 }
00171 
00172 int main()
00173 {
00174     runTest1();
00175 //  runTest(new TestFunc2);
00176     return EXIT_SUCCESS;
00177 }

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