main.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "main_ga.cpp"
00004 
00005 // (c) OPAC Team, LIFL, January 2006
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: cahon@lifl.fr
00022 */
00023 
00024 #include "param.h"
00025 #include "route_init.h"
00026 #include "route_eval.h"
00027 
00028 #include "order_xover.h"
00029 #include "edge_xover.h"
00030 #include "partial_mapped_xover.h"
00031 #include "city_swap.h"
00032 #include "part_route_eval.h"
00033 #include "merge_route_eval.h"
00034 #include "two_opt_init.h"
00035 #include "two_opt_next.h"
00036 #include "two_opt_incr_eval.h"
00037 
00038 #include <paradiseo>
00039 
00040 #define POP_SIZE 10
00041 #define NUM_GEN 100
00042 #define CROSS_RATE 1.0
00043 #define MUT_RATE 0.01
00044 #define NUM_PART_EVALS 2
00045 
00046 #define MIG_FREQ 10
00047 #define MIG_SIZE 10
00048 #define HYBRID_SIZE 3
00049 
00050 int main (int __argc, char * * __argv) {
00051 
00052   peo :: init (__argc, __argv);
00053 
00054   loadParameters (__argc, __argv); /* Processing some parameters relative to the tackled
00055                                       problem (TSP) */
00056 
00057   RouteInit route_init; /* Its builds random routes */  
00058   RouteEval full_eval; /* Full route evaluator */
00059 
00060   MergeRouteEval merge_eval; 
00061   
00062   std :: vector <eoEvalFunc <Route> *> part_eval;
00063   for (unsigned i = 1 ; i <= NUM_PART_EVALS ; i ++)
00064     part_eval.push_back (new PartRouteEval ((float) (i - 1) / NUM_PART_EVALS, (float) i / NUM_PART_EVALS));
00065   
00066   OrderXover order_cross; /* Recombination */
00067   PartialMappedXover pm_cross;
00068   EdgeXover edge_cross;
00069   CitySwap city_swap_mut;  /* Mutation */
00070 
00071   RingTopology topo;
00072  
00075   eoPop <Route> ox_pop (POP_SIZE, route_init);  /* Population */
00076   
00077   eoGenContinue <Route> ox_cont (NUM_GEN); /* A fixed number of iterations */  
00078   eoCheckPoint <Route> ox_checkpoint (ox_cont); /* Checkpoint */
00079   peoParaPopEval <Route> ox_pop_eval (part_eval, merge_eval);  
00080   eoStochTournamentSelect <Route> ox_select_one;
00081   eoSelectNumber <Route> ox_select (ox_select_one, POP_SIZE);
00082   eoSGATransform <Route> ox_transform (order_cross, CROSS_RATE, city_swap_mut, MUT_RATE);
00083   peoSeqTransform <Route> ox_para_transform (ox_transform);    
00084   eoEPReplacement <Route> ox_replace (2);
00085   
00086   /* The migration policy */
00087   eoPeriodicContinue <Route> ox_mig_cont (MIG_FREQ); /* Migration occurs periodically */
00088   eoRandomSelect <Route> ox_mig_select_one; /* Emigrants are randomly selected */
00089   eoSelectNumber <Route> ox_mig_select (ox_mig_select_one, MIG_SIZE);
00090   eoPlusReplacement <Route> ox_mig_replace; /* Immigrants replace the worse individuals */
00091   
00092   peoAsyncIslandMig <Route> ox_mig (ox_mig_cont, ox_mig_select, ox_mig_replace, topo, ox_pop, ox_pop);
00093   //peoSyncIslandMig <Route> ox_mig (MIG_FREQ, ox_mig_select, ox_mig_replace, topo, ox_pop, ox_pop);
00094   
00095   ox_checkpoint.add (ox_mig);
00096   
00097   peoEA <Route> ox_ea (ox_checkpoint, ox_pop_eval, ox_select, ox_para_transform, ox_replace);
00098   ox_mig.setOwner (ox_ea);
00099   
00100   ox_ea (ox_pop);   /* Application to the given population */    
00101 
00104   eoPop <Route> pmx_pop (POP_SIZE, route_init);  /* Population */
00105 
00106   eoGenContinue <Route> pmx_cont (NUM_GEN); /* A fixed number of iterations */  
00107   eoCheckPoint <Route> pmx_checkpoint (pmx_cont); /* Checkpoint */
00108   peoSeqPopEval <Route> pmx_pop_eval (full_eval);  
00109   eoRankingSelect <Route> pmx_select_one;
00110   eoSelectNumber <Route> pmx_select (pmx_select_one, POP_SIZE);
00111   eoSGATransform <Route> pmx_transform (pm_cross, CROSS_RATE, city_swap_mut, MUT_RATE);
00112   peoSeqTransform <Route> pmx_para_transform (pmx_transform);    
00113   eoPlusReplacement <Route> pmx_replace;
00114 
00115   /* The migration policy */
00116   eoPeriodicContinue <Route> pmx_mig_cont (MIG_FREQ); /* Migration occurs periodically */
00117   eoRandomSelect <Route> pmx_mig_select_one; /* Emigrants are randomly selected */
00118   eoSelectNumber <Route> pmx_mig_select (pmx_mig_select_one, MIG_SIZE);
00119   eoPlusReplacement <Route> pmx_mig_replace; /* Immigrants replace the worse individuals */
00120   peoAsyncIslandMig <Route> pmx_mig (pmx_mig_cont, pmx_mig_select, pmx_mig_replace, topo, pmx_pop, pmx_pop);
00121   //peoSyncIslandMig <Route> pmx_mig (MIG_FREQ, pmx_mig_select, pmx_mig_replace, topo, pmx_pop, pmx_pop);
00122   pmx_checkpoint.add (pmx_mig);
00123   
00124   /* Hybridization with a Local Search */
00125   TwoOptInit pmx_two_opt_init;
00126   TwoOptNext pmx_two_opt_next;
00127   TwoOptIncrEval pmx_two_opt_incr_eval;
00128   moBestImprSelect <TwoOpt> pmx_two_opt_move_select;
00129   moHC <TwoOpt> hc (pmx_two_opt_init, pmx_two_opt_next, pmx_two_opt_incr_eval, pmx_two_opt_move_select, full_eval);
00130 
00131   eoPeriodicContinue <Route> pmx_ls_cont (MIG_FREQ); /* Hybridization occurs periodically */
00132   eoRandomSelect <Route> pmx_ls_select_one; /* ? */
00133   eoSelectNumber <Route> pmx_ls_select (pmx_ls_select_one, HYBRID_SIZE); 
00134   eoPlusReplacement <Route> pmx_ls_replace;
00135 
00136   peoSyncMultiStart <Route> pmx_ls (pmx_ls_cont, pmx_ls_select, pmx_ls_replace, hc, pmx_pop);
00137   pmx_checkpoint.add (pmx_ls);
00138 
00139   peoEA <Route> pmx_ea (pmx_checkpoint, pmx_pop_eval, pmx_select, pmx_para_transform, pmx_replace);
00140   pmx_mig.setOwner (pmx_ea);
00141   pmx_ls.setOwner (pmx_ea);
00142 
00143   pmx_ea (pmx_pop);   /* Application to the given population */    
00144 
00147   eoPop <Route> edge_pop (POP_SIZE, route_init);  /* Population */
00148 
00149   eoGenContinue <Route> edge_cont (NUM_GEN); /* A fixed number of iterations */  
00150   eoCheckPoint <Route> edge_checkpoint (edge_cont); /* Checkpoint */
00151   peoSeqPopEval <Route> edge_pop_eval (full_eval);  
00152   eoRankingSelect <Route> edge_select_one;
00153   eoSelectNumber <Route> edge_select (edge_select_one, POP_SIZE);        
00154   peoParaSGATransform <Route> edge_para_transform (edge_cross, CROSS_RATE, city_swap_mut, MUT_RATE);
00155   eoPlusReplacement <Route> edge_replace;
00156 
00157   /* The migration policy */
00158   eoPeriodicContinue <Route> edge_mig_cont (MIG_FREQ); /* Migration occurs periodically */
00159   eoRandomSelect <Route> edge_mig_select_one; /* Emigrants are randomly selected */
00160   eoSelectNumber <Route> edge_mig_select (edge_mig_select_one, MIG_SIZE);
00161   eoPlusReplacement <Route> edge_mig_replace; /* Immigrants replace the worse individuals */
00162   peoAsyncIslandMig <Route> edge_mig (edge_mig_cont, edge_mig_select, edge_mig_replace, topo, edge_pop, edge_pop);
00163   //peoSyncIslandMig <Route> edge_mig (MIG_FREQ, edge_mig_select, edge_mig_replace, topo, edge_pop, edge_pop);
00164   edge_checkpoint.add (edge_mig);
00165 
00166   peoEA <Route> edge_ea (edge_checkpoint, edge_pop_eval, edge_select, edge_para_transform, edge_replace);
00167 
00168   edge_mig.setOwner (edge_ea);
00169 
00170   edge_ea (edge_pop);   /* Application to the given population */    
00171   
00172   peo :: run ();
00173 
00174   peo :: finalize (); /* Termination */
00175 
00176   return 0;
00177 }

Generated on Fri Dec 22 16:54:58 2006 for ParadisEO by  doxygen 1.4.7