# ParadisEO-MOMovingObjects: moSA.h Source File

# moSA.h

    00001 /*
    00002   <moSA.h>
    00003   Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
    00004   (C) OPAC Team, LIFL, 2002-2008
    00005
    00006   Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr)
    00007
    00008   This software is governed by the CeCILL license under French law and
    00009   abiding by the rules of distribution of free software.  You can  use,
    00010   modify and/ or redistribute the software under the terms of the CeCILL
    00011   license as circulated by CEA, CNRS and INRIA at the following URL
    00012   "http://www.cecill.info".
    00013
    00014   As a counterpart to the access to the source code and  rights to copy,
    00015   modify and redistribute granted by the license, users are provided only
    00016   with a limited warranty  and the software's author,  the holder of the
    00017   economic rights,  and the successive licensors  have only  limited liability.
    00018
    00019   In this respect, the user's attention is drawn to the risks associated
    00020   with loading,  using,  modifying and/or developing or reproducing the
    00021   software by the user in light of its specific status of free software,
    00022   that may mean  that it is complicated to manipulate,  and  that  also
    00023   therefore means  that it is reserved for developers  and  experienced
    00024   professionals having in-depth computer knowledge. Users are therefore
    00025   encouraged to load and test the software's suitability as regards their
    00026   requirements in conditions enabling the security of their systems and/or
    00027   data to be ensured and,  more generally, to use and operate it in the
    00028   same conditions as regards security.
    00029   The fact that you are presently reading this means that you have had
    00030   knowledge of the CeCILL license and that you accept its terms.
    00031
    00032   ParadisEO WebSite : http://paradiseo.gforge.inria.fr
    00033   Contact: paradiseo-help@lists.gforge.inria.fr
    00034 */
    00035
    00036 #ifndef _moSA_h
    00037 #define _moSA_h
    00038
    00039 #include <math.h>
    00040
    00041 #include <eoEvalFunc.h>
    00042 #include <moAlgo.h>
    00043 #include <moRandMove.h>
    00044 #include <moMoveIncrEval.h>
    00045 #include <moCoolingSchedule.h>
    00046 #include <moSolContinue.h>
    00047
    00049
    00052 template < class M >
    00053 class moSA:public moAlgo < typename M::EOType >
    00054 {
    00056   typedef typename M::EOType EOT;
    00057
    00059   typedef typename EOT::Fitness Fitness;
    00060
    00061  public:
    00062
    00064
    00074   moSA (moRandMove < M > & _random_move_generator, moMoveIncrEval < M > & _incremental_evaluation,
    00075         moSolContinue < EOT > & _continue, double _initial_temperature, moCoolingSchedule & _cooling_schedule,
    00076         eoEvalFunc < EOT > & _full_evaluation):
    00077   random_move_generator(_random_move_generator), incremental_evaluation(_incremental_evaluation),
    00078     continu(_continue), initial_temperature(_initial_temperature),
    00079     cooling_schedule(_cooling_schedule), full_evaluation(_full_evaluation)
    00080   {}
    00081
    00083
    00089   bool operator ()(EOT & _solution)
    00090   {
    00091     Fitness incremental_fitness, delta_fit;
    00092     EOT best_solution;
    00093     double temperature;
    00094     M move;
    00095
    00096     if (_solution.invalid())
    00097       {
    00098         full_evaluation (_solution);
    00099       }
    00100
    00101     temperature = initial_temperature;
    00102
    00103     best_solution = _solution;
    00104
    00105     do
    00106       {
    00107         continu.init ();
    00108
    00109         do
    00110           {
    00111             random_move_generator(move);
    00112
    00113             incremental_fitness = incremental_evaluation (move, _solution);
    00114
    00115             delta_fit = incremental_fitness - _solution.fitness ();
    00116
    00117             if( (_solution.fitness() > incremental_fitness ) && (exp (delta_fit / temperature) > 1.0) )
    00118               {
    00119                 delta_fit = -delta_fit;
    00120               }
    00121
    00122             if ( (incremental_fitness > _solution.fitness()) || (rng.uniform () < exp (delta_fit / temperature)) )
    00123               {
    00124                 move(_solution);
    00125                 _solution.fitness(incremental_fitness);
    00126
    00127                 // Updating the best solution found  until now ?
    00128                 if ( _solution.fitness() > best_solution.fitness() )
    00129                   {
    00130                     best_solution = _solution;
    00131                   }
    00132               }
    00133
    00134           }
    00135         while ( continu (_solution) );
    00136       }
    00137     while ( cooling_schedule (temperature) );
    00138
    00139     _solution = best_solution;
    00140
    00141     return true;
    00142   }
    00143
    00144  private:
    00145
    00147   moRandMove < M > & random_move_generator;
    00148
    00150   moMoveIncrEval < M > & incremental_evaluation;
    00151
    00153   moSolContinue < EOT > & continu;
    00154
    00156   double  initial_temperature;
    00157
    00159   moCoolingSchedule & cooling_schedule;
    00160
    00162   eoEvalFunc < EOT > & full_evaluation;
    00163 };
    00164
    00165 #endif