// "main_ga.cpp" // (c) OPAC Team, LIFL, January 2006 /* Contact: paradiseo-help@lists.gforge.inria.fr */ #include "param.h" #include "route_init.h" #include "route_eval.h" #include "order_xover.h" #include "edge_xover.h" #include "partial_mapped_xover.h" #include "city_swap.h" #include "part_route_eval.h" #include "merge_route_eval.h" #include "two_opt_init.h" #include "two_opt_next.h" #include "two_opt_incr_eval.h" #include #define POP_SIZE 10 #define NUM_GEN 100 #define CROSS_RATE 1.0 #define MUT_RATE 0.01 #define NUM_PART_EVALS 2 #define MIG_FREQ 10 #define MIG_SIZE 10 #define HYBRID_SIZE 3 int main (int __argc, char * * __argv) { peo :: init (__argc, __argv); loadParameters (__argc, __argv); /* Processing some parameters relative to the tackled problem (TSP) */ RouteInit route_init; /* Its builds random routes */ RouteEval full_eval; /* Full route evaluator */ MergeRouteEval merge_eval; std :: vector *> part_eval; for (unsigned i = 1 ; i <= NUM_PART_EVALS ; i ++) part_eval.push_back (new PartRouteEval ((float) (i - 1) / NUM_PART_EVALS, (float) i / NUM_PART_EVALS)); OrderXover order_cross; /* Recombination */ PartialMappedXover pm_cross; EdgeXover edge_cross; CitySwap city_swap_mut; /* Mutation */ RingTopology topo; /** The first EA **/ eoPop ox_pop (POP_SIZE, route_init); /* Population */ eoGenContinue ox_cont (NUM_GEN); /* A fixed number of iterations */ eoCheckPoint ox_checkpoint (ox_cont); /* Checkpoint */ peoParaPopEval ox_pop_eval (part_eval, merge_eval); eoStochTournamentSelect ox_select_one; eoSelectNumber ox_select (ox_select_one, POP_SIZE); eoSGATransform ox_transform (order_cross, CROSS_RATE, city_swap_mut, MUT_RATE); peoSeqTransform ox_para_transform (ox_transform); eoEPReplacement ox_replace (2); /* The migration policy */ eoPeriodicContinue ox_mig_cont (MIG_FREQ); /* Migration occurs periodically */ eoRandomSelect ox_mig_select_one; /* Emigrants are randomly selected */ eoSelectNumber ox_mig_select (ox_mig_select_one, MIG_SIZE); eoPlusReplacement ox_mig_replace; /* Immigrants replace the worse individuals */ peoAsyncIslandMig ox_mig (ox_mig_cont, ox_mig_select, ox_mig_replace, topo, ox_pop, ox_pop); //peoSyncIslandMig ox_mig (MIG_FREQ, ox_mig_select, ox_mig_replace, topo, ox_pop, ox_pop); ox_checkpoint.add (ox_mig); peoEA ox_ea (ox_checkpoint, ox_pop_eval, ox_select, ox_para_transform, ox_replace); ox_mig.setOwner (ox_ea); ox_ea (ox_pop); /* Application to the given population */ /** The second EA **/ eoPop pmx_pop (POP_SIZE, route_init); /* Population */ eoGenContinue pmx_cont (NUM_GEN); /* A fixed number of iterations */ eoCheckPoint pmx_checkpoint (pmx_cont); /* Checkpoint */ peoSeqPopEval pmx_pop_eval (full_eval); eoRankingSelect pmx_select_one; eoSelectNumber pmx_select (pmx_select_one, POP_SIZE); eoSGATransform pmx_transform (pm_cross, CROSS_RATE, city_swap_mut, MUT_RATE); peoSeqTransform pmx_para_transform (pmx_transform); eoPlusReplacement pmx_replace; /* The migration policy */ eoPeriodicContinue pmx_mig_cont (MIG_FREQ); /* Migration occurs periodically */ eoRandomSelect pmx_mig_select_one; /* Emigrants are randomly selected */ eoSelectNumber pmx_mig_select (pmx_mig_select_one, MIG_SIZE); eoPlusReplacement pmx_mig_replace; /* Immigrants replace the worse individuals */ peoAsyncIslandMig pmx_mig (pmx_mig_cont, pmx_mig_select, pmx_mig_replace, topo, pmx_pop, pmx_pop); //peoSyncIslandMig pmx_mig (MIG_FREQ, pmx_mig_select, pmx_mig_replace, topo, pmx_pop, pmx_pop); pmx_checkpoint.add (pmx_mig); /* Hybridization with a Local Search */ TwoOptInit pmx_two_opt_init; TwoOptNext pmx_two_opt_next; TwoOptIncrEval pmx_two_opt_incr_eval; moBestImprSelect pmx_two_opt_move_select; moHC hc (pmx_two_opt_init, pmx_two_opt_next, pmx_two_opt_incr_eval, pmx_two_opt_move_select, full_eval); eoPeriodicContinue pmx_ls_cont (MIG_FREQ); /* Hybridization occurs periodically */ eoRandomSelect pmx_ls_select_one; /* ? */ eoSelectNumber pmx_ls_select (pmx_ls_select_one, HYBRID_SIZE); eoPlusReplacement pmx_ls_replace; peoSyncMultiStart pmx_ls (pmx_ls_cont, pmx_ls_select, pmx_ls_replace, hc, pmx_pop); pmx_checkpoint.add (pmx_ls); peoEA pmx_ea (pmx_checkpoint, pmx_pop_eval, pmx_select, pmx_para_transform, pmx_replace); pmx_mig.setOwner (pmx_ea); pmx_ls.setOwner (pmx_ea); pmx_ea (pmx_pop); /* Application to the given population */ /** The third EA **/ eoPop edge_pop (POP_SIZE, route_init); /* Population */ eoGenContinue edge_cont (NUM_GEN); /* A fixed number of iterations */ eoCheckPoint edge_checkpoint (edge_cont); /* Checkpoint */ peoSeqPopEval edge_pop_eval (full_eval); eoRankingSelect edge_select_one; eoSelectNumber edge_select (edge_select_one, POP_SIZE); peoParaSGATransform edge_para_transform (edge_cross, CROSS_RATE, city_swap_mut, MUT_RATE); eoPlusReplacement edge_replace; /* The migration policy */ eoPeriodicContinue edge_mig_cont (MIG_FREQ); /* Migration occurs periodically */ eoRandomSelect edge_mig_select_one; /* Emigrants are randomly selected */ eoSelectNumber edge_mig_select (edge_mig_select_one, MIG_SIZE); eoPlusReplacement edge_mig_replace; /* Immigrants replace the worse individuals */ peoAsyncIslandMig edge_mig (edge_mig_cont, edge_mig_select, edge_mig_replace, topo, edge_pop, edge_pop); //peoSyncIslandMig edge_mig (MIG_FREQ, edge_mig_select, edge_mig_replace, topo, edge_pop, edge_pop); edge_checkpoint.add (edge_mig); peoEA edge_ea (edge_checkpoint, edge_pop_eval, edge_select, edge_para_transform, edge_replace); edge_mig.setOwner (edge_ea); edge_ea (edge_pop); /* Application to the given population */ peo :: run (); peo :: finalize (); /* Termination */ return 0; }