EvolvingObjects
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 
00039 template<class EOT> class eoSBXCrossover: public eoQuadOp<EOT>
00040 {
00041  public:
00042   /****
00043    * (Default) Constructor.
00044    * The bounds are initialized with the global object that says: no bounds.
00045    *
00046    *
00047    */
00048     eoSBXCrossover(const double& _eta = 1.0) :
00049     bounds(eoDummyVectorNoBounds), eta(_eta), range(1) {}
00050 
00051 
00053 
00065      eoSBXCrossover(eoRealVectorBounds & _bounds,
00066                      const double& _eta = 1.0) :
00067      bounds(_bounds), eta(_eta), range(1) {}
00068 
00070 
00072 
00079      eoSBXCrossover(eoParser & _parser) :
00080     // First, decide whether the objective variables are bounded
00081     // Warning, must be the same keywords than other possible objectBounds elsewhere
00082        bounds (_parser.getORcreateParam(eoDummyVectorNoBounds, "objectBounds", "Bounds for variables", 'B', "Variation Operators").value()) ,
00083     // then get eta value
00084        eta (_parser.getORcreateParam(1.0, "eta", "SBX eta parameter", '\0', "Variation Operators").value()) ,
00085        range(1) {}
00086 
00087 
00089   virtual std::string className() const { return "eoSBXCrossover"; }
00090 
00091   /*****************************************
00092    * SBX crossover - modifies both parents *
00093    * @param _eo1 The first parent          *
00094    * @param _eo2 The first parent          *
00095    *****************************************/
00096   bool operator()(EOT& _eo1, EOT& _eo2)
00097     {
00098       unsigned i;
00099       double r1, r2, beta;
00100 
00101         for (i=0; i<_eo1.size(); i++)
00102           {
00103       double u = rng.uniform(range) ;
00104 
00105       if ( u <= 0.5 )
00106         beta = exp( (1/(eta+1))*log(2*u));
00107       else
00108         beta = exp((1/(eta+1))*log(1/(2*(1-u))));
00109 
00110 
00111 
00112               r1=_eo1[i];
00113               r2=_eo2[i];
00114               _eo1[i] =0.5*((1+beta)*r1+(1-beta)*r2);
00115               _eo2[i] =0.5*((1-beta)*r1+(1+beta)*r2);
00116 
00117 
00118               if(!(bounds.isInBounds(i,_eo1[i])))
00119                 bounds.foldsInBounds(i,_eo1[i]);
00120               if(!(bounds.isInBounds(i,_eo2[i])))
00121                 bounds.foldsInBounds(i,_eo2[i]);
00122 
00123 
00124 
00125            }
00126         return true;
00127         }
00128 
00129 
00130 
00131 protected:
00132   eoRealVectorBounds & bounds;
00133   double eta;
00134   double range;                    // == 1
00135 };
 All Classes Namespaces Files Functions Variables Typedefs Friends