source/numeric/quadfitlinesearch_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 #include "numeric/quadfitlinesearch.hxx"
00029 #include "numeric/funcobj.hxx"
00030 #include "numeric/testtool.hxx"
00031 #include <memory>
00032 #include <string>
00033 #include <cmath>
00034 
00035 using namespace ::scsolver::numeric;
00036 using namespace ::std;
00037 
00038 class TestFunc1 : public SingleVarTestFuncBase
00039 {
00040 public:
00041     virtual double eval() const
00042     {
00043         double var = getVar();
00044         return (var - 2.0)*(var*2.0 + 5.0) + 10.0;
00045     }
00046 
00047     virtual const string getFuncString() const
00048     {
00049         // 2*x^2 + x
00050         return string("(x - 2) * (2x + 5) + 10");
00051     }
00052 };
00053 
00054 class TestFunc2 : public SingleVarTestFuncBase
00055 {
00056 public:
00057     virtual double eval() const
00058     {
00059         double var = getVar();
00060         return ::std::sin(var) * var;
00061     }
00062 
00066     virtual const string getFuncString() const
00067     {
00068         return string("sin(x) * x");
00069     }
00070 };
00071 
00072 class TestFunc3 : public SingleVarTestFuncBase
00073 {
00074 public:
00075     virtual double eval() const
00076     {
00077         double x = getVar();
00078         return x*(2*x-20)*(x*x+5*x+10);
00079     }
00080 
00084     virtual const string getFuncString() const
00085     {
00086         return string("x*(2*x-20)*(x*x+5*x+10)");
00087     }
00088 };
00089 
00090 class TestFunc4 : public SingleVarTestFuncBase
00091 {
00092 public:
00093     virtual double eval() const
00094     {
00095         using namespace std;
00096         double x = getVar();
00097         return ((x-2)*(x*2+8) + 45)*(2*x+3)*cos(x/2);
00098     }
00099 
00103     virtual const string getFuncString() const
00104     {
00105         return string("((x-2)*(x*2+8) + 45)*(2*x+3)*cos(x/2)");
00106     }
00107 };
00108 
00109 class TestFunc5 : public SingleVarTestFuncBase
00110 {
00111 public:
00112     virtual double eval() const
00113     {
00114         using namespace std;
00115         double x = getVar();
00116         return (x-2)*(x-2)*(x-2)*(x-2) + (x-6)*(x-6);
00117     }
00118 
00122     virtual const::std::string getFuncString() const
00123     {
00124         return string("(x - 2)^4 + (x - 6)^2");
00125     }
00126 };
00127 
00128 class TestFunc6 : public SingleVarTestFuncBase
00129 {
00130 public:
00131     virtual double eval() const
00132     {
00133         double x = getVar();
00134         return x*x;
00135     }
00136 
00140     virtual const::std::string getFuncString() const
00141     {
00142         return string("x^2");
00143     }
00144 };
00145 
00146 class TestFunc7 : public SingleVarTestFuncBase
00147 {
00148 public:
00149     virtual double eval() const
00150     {
00151         double x = getVar();
00152         return (x-1)*(x-1);
00153     }
00154 
00158     virtual const::std::string getFuncString() const
00159     {
00160         return string("(x-1)^2");
00161     }
00162 };
00163 
00164 class TestFunc8 : public SingleVarTestFuncBase
00165 {
00166 public:
00167     virtual double eval() const
00168     {
00169         double x = getVar();
00170         return (x-2)*(x-2)*(x-2)*(x-2) + 0.006715617*x*x;
00171     }
00172 
00176     virtual const::std::string getFuncString() const
00177     {
00178         return string("(x-2)^4 + (0.081948871*x)^2");
00179     }
00180 };
00181 
00182 class TestFuncMax1 : public SingleVarTestFuncBase
00183 {
00184 public:
00185     virtual double eval() const
00186     {
00187         double x = getVar();
00188         return x*x*-1.0;
00189     }
00190 
00194     virtual const::std::string getFuncString() const
00195     {
00196         return string("-x^2");
00197     }
00198 };
00199 
00200 class TestFuncMax2 : public SingleVarTestFuncBase
00201 {
00202 public:
00203     virtual double eval() const
00204     {
00205         double x = getVar();
00206         return 2.0*x - x*x + 15;
00207     }
00208 
00212     virtual const::std::string getFuncString() const
00213     {
00214         return string("2x - x^2 + 15");
00215     }
00216 };
00217 
00218 void run()
00219 {
00220     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc1);
00221     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc2);
00222     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc3);
00223     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc4);
00224     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc5);
00225     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc6);
00226     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc7);
00227     runSingleVarTestFunc(new QuadFitLineSearch, new TestFunc8);
00228     runSingleVarTestFunc(new QuadFitLineSearch, new TestFuncMax1, GOAL_MAXIMIZE);
00229     runSingleVarTestFunc(new QuadFitLineSearch, new TestFuncMax2, GOAL_MAXIMIZE);
00230 }
00231 
00232 int main()
00233 {
00234     try
00235     {
00236         run();
00237         fprintf(stdout, "Test success!\n");
00238     }
00239     catch (const exception& e)
00240     {
00241         fprintf(stdout, "Test failed (reason: %s)\n", e.what());
00242     }
00243 }

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