eoSBXcross.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoSBXcross.h
00005 // (c) Maarten Keijzer 2000 - Marc Schoenauer 2001
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: Marc.Schoenauer@polytechnique.fr
00022              mak@dhi.dk
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #include <algorithm>    // swap_ranges
00027 #include <utils/eoParser.h>
00028 #include <utils/eoRNG.h>
00029 #include <es/eoReal.h>
00030 #include <utils/eoRealBounds.h>
00031 #include <utils/eoRealVectorBounds.h>
00032 
00033 
00034 
00035 template<class EOT> class eoSBXCrossover: public eoQuadOp<EOT>
00036 {
00037  public:
00038   /****
00039    * (Default) Constructor.
00040    * The bounds are initialized with the global object that says: no bounds.
00041    *
00042    * 
00043    */
00044     eoSBXCrossover(const double& _eta = 1.0) :
00045     bounds(eoDummyVectorNoBounds), eta(_eta), range(1) {}
00046 
00047 
00049 
00061      eoSBXCrossover(eoRealVectorBounds & _bounds, 
00062                      const double& _eta = 1.0) : 
00063      bounds(_bounds), eta(_eta), range(1) {}
00064 
00066 
00068 
00075      eoSBXCrossover(eoParser & _parser) : 
00076     // First, decide whether the objective variables are bounded
00077     // Warning, must be the same keywords than other possible objectBounds elsewhere
00078        bounds (_parser.getORcreateParam(eoDummyVectorNoBounds, "objectBounds", "Bounds for variables", 'B', "Variation Operators").value()) ,
00079     // then get eta value
00080        eta (_parser.getORcreateParam(1.0, "eta", "SBX eta parameter", '\0', "Variation Operators").value()) ,
00081        range(1) {}
00082     
00083 
00085   virtual std::string className() const { return "eoSBXCrossover"; }
00086 
00087   /*****************************************
00088    * SBX crossover - modifies both parents *
00089    * @param _eo1 The first parent          *
00090    * @param _eo2 The first parent          *    
00091    *****************************************/
00092   bool operator()(EOT& _eo1, EOT& _eo2)
00093     {
00094       unsigned i;
00095       double r1, r2, beta;
00096 
00097         for (i=0; i<_eo1.size(); i++)
00098           {
00099       double u = rng.uniform(range) ;
00100 
00101       if ( u <= 0.5 ) 
00102         beta = exp( (1/(eta+1))*log(2*u));
00103       else
00104         beta = exp((1/(eta+1))*log(1/(2*(1-u))));
00105 
00106 
00107             
00108               r1=_eo1[i];
00109               r2=_eo2[i];   
00110               _eo1[i] =0.5*((1+beta)*r1+(1-beta)*r2);
00111               _eo2[i] =0.5*((1-beta)*r1+(1+beta)*r2);
00112 
00113         
00114               if(!(bounds.isInBounds(i,_eo1[i])))
00115                 bounds.foldsInBounds(i,_eo1[i]);
00116               if(!(bounds.isInBounds(i,_eo2[i])))
00117                 bounds.foldsInBounds(i,_eo2[i]);
00118 
00119 
00120 
00121            }
00122         return true;
00123         }
00124                            
00125     
00126 
00127 protected:
00128   eoRealVectorBounds & bounds;
00129   double eta;
00130   double range;                    // == 1
00131 };
00132 
00133 

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