t-eoParetoFitness.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // t-eoParetoFitness.cpp
00005 // (c) Maarten Keijzer
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: mak@dhi.dk
00022 
00023 30/01/02 - MS - Added the eoVariableParetoTraits - and the compare Fn
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #include <cassert>
00028 #include <iostream>
00029 
00030 #include "eoParetoFitness.h"
00031 
00032 using namespace std;
00033 
00036 class MinimizingTraits : public eoParetoFitnessTraits
00037 {
00038 public :
00039 
00040   static bool maximizing(int) { return false; }
00041 };
00042 
00043 template <class F>
00044 void compare(F & _eo1, F & _eo2)
00045 {
00046   if (_eo1.dominates(_eo2))
00047     std::cout << _eo1 << " dominates " << _eo2 << std::endl;
00048   else if (_eo2.dominates(_eo1))
00049     std::cout << _eo2 << " dominates " << _eo1 << std::endl;
00050   else
00051     std::cout << "None of " << _eo1 << " and " << _eo2 << "dominates the other" << std::endl;
00052   return;
00053 }
00054 
00055 int main()
00056 {
00057   typedef eoParetoFitness<> MaxFitness;
00058   typedef eoParetoFitness<MinimizingTraits> MinFitness;
00059 
00060   typedef eoParetoFitness<eoVariableParetoTraits> VarFitness;
00061 
00062   try{
00063 
00064   MaxFitness f0;
00065   f0[0] = 0.0;
00066   f0[1] = 1.0;
00067 
00068   MaxFitness f1;
00069   f1[0] = 1.0;
00070   f1[1] = 0.0;
00071 
00072   MaxFitness f2;
00073   f2[0] = 0.0;
00074   f2[1] = 0.5;
00075 
00076   // now f0 should dominate f2;
00077 
00078   if (!f0.dominates(f2))
00079   {
00080     std::cout << f2 << " not dominated by " << f0;
00081     throw;
00082   }
00083 
00084   // f0 and f1 should not dominate each other
00085 
00086   if (f0.dominates(f1) || f1.dominates(f0))
00087   {
00088     std::cout << f0 << " and " << f1 << " dominate";
00089     throw;
00090   }
00091 
00092   if (! (f0 == f0))
00093   {
00094     std::cout << "f0 == f0 failed" << std::endl;
00095     throw;
00096   }
00097 
00098   // test ctors and such
00099   MaxFitness f3 = f0;
00100   f3[0] += 1e-9;
00101 
00102   // test tolerance
00103   assert(f3 == f0);
00104 
00105   MinFitness m0;
00106   MinFitness m1;
00107   MinFitness m2;
00108   MinFitness m3;
00109 
00110   m0[0] = 0.0;
00111   m0[1] = 1.0;
00112 
00113   m1[0] = 1.0;
00114   m1[1] = 0.0;
00115 
00116   m2[0] = 0.0;
00117   m2[1] = 0.5;
00118 
00119   m3[0] = 0.5;
00120   m3[1] = 0.5;
00121 
00122   //m2 should dominate m0
00123   assert(m2.dominates(m0));
00124 
00125   assert(!m1.dominates(m0));
00126   assert(!m0.dominates(m1));
00127   assert(!m0.dominates(m2)); // (m2 < m0));
00128   assert(m2.dominates(m3)); //m3 < m2);
00129   assert(!m3.dominates(m2)); // (m2 < m3));
00130   assert(m2.dominates(m3)); //m2 > m3);
00131 
00132 
00134   // now the run-time set-able number of objectives
00136 
00137   std::cout << "On y va" << std::endl;
00138 
00139 
00140   // setup fitness WARNING do not try to allocate any EO before that (runtime error)
00141   vector<bool> b(2, true);
00142   b[0]=true; 
00143   b[1]=false;
00144   VarFitness::setUp(2, b);
00145   std::cout << "\nMAXimizing on Obj 0 and MINimizing on Obj 1\n";
00146 
00147   VarFitness mv0;
00148   VarFitness mv1;
00149   VarFitness mv2;
00150   VarFitness mv3;
00151 
00152   mv0[0] = 0.0;
00153   mv0[1] = 1.0;
00154 
00155   mv1[0] = 1.0;
00156   mv1[1] = 0.0;
00157 
00158   mv2[0] = 0.0;
00159   mv2[1] = 0.5;
00160 
00161   mv3[0] = 0.5;
00162   mv3[1] = 0.5;
00163 
00164   compare <VarFitness>(mv0,mv1);
00165   compare <VarFitness>(mv0,mv2);
00166   compare <VarFitness>(mv0,mv3);
00167   compare <VarFitness>(mv1,mv2);
00168   compare <VarFitness>(mv1,mv3);
00169   compare <VarFitness>(mv2,mv3);
00170 
00171   std::cout << "\nChanging now the min <-> max\n";
00172   b[0]=false; 
00173   b[1]=true;
00174   VarFitness::setUp(2, b);
00175   std::cout << "\nMINimizing on Obj 0 and MAXimizing on Obj 1\n";
00176   compare <VarFitness>(mv0,mv1);
00177   compare <VarFitness>(mv0,mv2);
00178   compare <VarFitness>(mv0,mv3);
00179   compare <VarFitness>(mv1,mv2);
00180   compare <VarFitness>(mv1,mv3);
00181   compare <VarFitness>(mv2,mv3);
00182 
00183   std::cout << "\nTesting WARNING\n";
00184   b.resize(3);
00185   b[0]=false; 
00186   b[1]=true;
00187   b[2]=true;
00188   VarFitness::setUp(3, b);
00189 
00190   }
00191   catch(std::exception& e)
00192   {
00193     std::cout << e.what() << std::endl;
00194   }
00195 
00196 }

Generated on Thu Oct 19 05:06:43 2006 for EO by  doxygen 1.3.9.1