From 094c8120503ea2bfc7d37617d62683b16e578602 Mon Sep 17 00:00:00 2001 From: jboisson Date: Fri, 7 Mar 2008 17:58:12 +0000 Subject: [PATCH] New test for each constructor of moHS, moTS, moILS. No memory leaks under Windows. No compilation errors or warning under Unix and Windows XP (visual studio 2005) git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1102 331e1502-861f-0410-8da2-ba01fb791d7f --- trunk/paradiseo-mo/src/moHC.h | 2 +- trunk/paradiseo-mo/src/moILS.h | 37 +++++++---- trunk/paradiseo-mo/src/moTS.h | 2 +- trunk/paradiseo-mo/test/CMakeLists.txt | 7 +- trunk/paradiseo-mo/test/t-moHC.cpp | 2 - trunk/paradiseo-mo/test/t-moILS.cpp | 90 +++++--------------------- trunk/paradiseo-mo/test/t-moSA.cpp | 4 +- 7 files changed, 51 insertions(+), 93 deletions(-) diff --git a/trunk/paradiseo-mo/src/moHC.h b/trunk/paradiseo-mo/src/moHC.h index a945c223f..d96c8c280 100755 --- a/trunk/paradiseo-mo/src/moHC.h +++ b/trunk/paradiseo-mo/src/moHC.h @@ -80,7 +80,7 @@ class moHC:public moAlgo < typename M::EOType > \param _full_evaluation a full evaluation function. */ moHC (moMoveExpl < M > & _move_explorer, eoEvalFunc < EOT > & _full_evaluation): - move_explorer (_move_explorer), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) + move_explorer (&_move_explorer), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) {} //! Destructor diff --git a/trunk/paradiseo-mo/src/moILS.h b/trunk/paradiseo-mo/src/moILS.h index 87ee02691..8fb4f343e 100644 --- a/trunk/paradiseo-mo/src/moILS.h +++ b/trunk/paradiseo-mo/src/moILS.h @@ -69,8 +69,8 @@ class moILS:public moAlgo < typename M::EOType > */ moILS (moAlgo & _algorithm, moSolContinue & _continue, moComparator & _acceptance_criterion, eoMonOp & _perturbation, eoEvalFunc & _full_evaluation): - algorithm(_algorithm), continu(_continue), acceptance_criterion(_acceptance_criterion), - perturbation(_perturbation), full_evaluation(_full_evaluation) + algorithm(& _algorithm), continu(_continue), acceptance_criterion(_acceptance_criterion), + perturbation(_perturbation), full_evaluation(_full_evaluation), algorithm_memory_allocation(false) {} //! Constructor for using a moHC for the moAlgo @@ -88,8 +88,9 @@ class moILS:public moAlgo < typename M::EOType > moMoveIncrEval < M > & _incremental_evaluation, moMoveSelect < M > & _move_selection, moSolContinue & _continue, moComparator & _acceptance_criterion, eoMonOp & _perturbation, eoEvalFunc & _full_evaluation): - algorithm( *new moHC(_move_initializer, _next_move_generator, _incremental_evaluation, _move_selection, _full_evaluation) ), - continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation) + algorithm(new moHC(_move_initializer, _next_move_generator, _incremental_evaluation, _move_selection, _full_evaluation) ), + continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation), + algorithm_memory_allocation(true) {} //! Constructor for using a moTS for the moAlgo @@ -110,9 +111,10 @@ class moILS:public moAlgo < typename M::EOType > moAspirCrit & _aspiration_criterion, moSolContinue & _moTS_continue, moSolContinue & _continue, moComparator & _acceptance_criterion, eoMonOp & _perturbation, eoEvalFunc & _full_evaluation): - algorithm( *new moTS(_move_initializer, _next_move_generator, _incremental_evaluation, _tabu_list, _aspiration_criterion, + algorithm(new moTS(_move_initializer, _next_move_generator, _incremental_evaluation, _tabu_list, _aspiration_criterion, _moTS_continue, _full_evaluation) ), - continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation) + continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation), + algorithm_memory_allocation(true) {} //! Constructor for using a moSA for the moAlgo @@ -130,11 +132,21 @@ class moILS:public moAlgo < typename M::EOType > moILS (moRandMove & _random_move_generator, moMoveIncrEval & _incremental_evaluation, moSolContinue & _moSA_continue, double _initial_temperature, moCoolingSchedule & _cooling_schedule, moSolContinue & _continue, moComparator & _acceptance_criterion, eoMonOp & _perturbation, eoEvalFunc & _full_evaluation): - algorithm( *new moSA(_random_move_generator, _incremental_evaluation, _moSA_continue, _initial_temperature, + algorithm(new moSA(_random_move_generator, _incremental_evaluation, _moSA_continue, _initial_temperature, _cooling_schedule, _full_evaluation) ), - continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation) + continu(_continue), acceptance_criterion(_acceptance_criterion), perturbation(_perturbation), full_evaluation(_full_evaluation), + algorithm_memory_allocation(true) {} + //! Destructor + ~moILS() + { + if(algorithm_memory_allocation) + { + delete(algorithm); + } + } + //! Function which launches the ILS /*! The ILS has to improve a current solution. @@ -152,7 +164,7 @@ class moILS:public moAlgo < typename M::EOType > // some code has been duplicated in order to avoid one perturbation and one evaluation without adding a test in the loop. // better than a do {} while; with a test in the loop. - algorithm(_solution); + (*algorithm)(_solution); if ( acceptance_criterion(_solution, _solution_saved) ) { @@ -169,7 +181,7 @@ class moILS:public moAlgo < typename M::EOType > perturbation(_solution); full_evaluation(_solution); - algorithm(_solution); + (*algorithm)(_solution); if ( acceptance_criterion(_solution, _solution_saved) ) { @@ -187,7 +199,7 @@ class moILS:public moAlgo < typename M::EOType > private: //! The solution based heuristic. - moAlgo & algorithm; + moAlgo * algorithm; //! The stopping criterion. moSolContinue & continu; @@ -200,6 +212,9 @@ class moILS:public moAlgo < typename M::EOType > //! The full evaluation function eoEvalFunc & full_evaluation; + + //! Indicate if the memory has been allocated for the algorithm. + bool algorithm_memory_allocation; }; #endif diff --git a/trunk/paradiseo-mo/src/moTS.h b/trunk/paradiseo-mo/src/moTS.h index 455c542e3..f17aadd3f 100755 --- a/trunk/paradiseo-mo/src/moTS.h +++ b/trunk/paradiseo-mo/src/moTS.h @@ -87,7 +87,7 @@ class moTS:public moAlgo < typename M::EOType > \param _full_evaluation A full evaluation function. */ moTS (moMoveExpl < M > & _move_explorer, moSolContinue < EOT > & _continue, eoEvalFunc < EOT > & _full_evaluation): - move_explorer (_move_explorer), continu (_continue), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) + move_explorer (&_move_explorer), continu (_continue), full_evaluation (_full_evaluation), move_explorer_memory_allocation(false) {} //! Destructor diff --git a/trunk/paradiseo-mo/test/CMakeLists.txt b/trunk/paradiseo-mo/test/CMakeLists.txt index 88645c710..d25c5f83a 100644 --- a/trunk/paradiseo-mo/test/CMakeLists.txt +++ b/trunk/paradiseo-mo/test/CMakeLists.txt @@ -37,8 +37,12 @@ SET (TEST_LIST t-moBestImprSelect t-moFitSolContinue t-moGenSolContinue t-moHC + t-moHC_2 t-moHCMoveLoopExpl - t-moILS + t-moILS + t-moILS_HC + t-moILS_TS + t-moILS_SA t-moImprBestFitAspirCrit t-moItRandNextMove t-moLinearCoolingSchedule @@ -51,6 +55,7 @@ SET (TEST_LIST t-moBestImprSelect t-moSimpleSolutionTabuList t-moSteadyFitSolContinue t-moTS + t-moTS_2 t-moTSMoveLoopExpl ) FOREACH (test ${TEST_LIST}) diff --git a/trunk/paradiseo-mo/test/t-moHC.cpp b/trunk/paradiseo-mo/test/t-moHC.cpp index 3e98c0d65..1fb6255a4 100644 --- a/trunk/paradiseo-mo/test/t-moHC.cpp +++ b/trunk/paradiseo-mo/test/t-moHC.cpp @@ -139,8 +139,6 @@ main() solution sol; - sol.fitness(0); - testMoveInit init; testMoveNext next; testMoveIncrEval incrEval; diff --git a/trunk/paradiseo-mo/test/t-moILS.cpp b/trunk/paradiseo-mo/test/t-moILS.cpp index 99e9375d7..d29221442 100644 --- a/trunk/paradiseo-mo/test/t-moILS.cpp +++ b/trunk/paradiseo-mo/test/t-moILS.cpp @@ -56,76 +56,13 @@ public : } } ; -class testMoveInit : public moMoveInit +class solutionAlgo : public moAlgo { public : - void operator () (testMove & _move, const solution & _solution) - { - testMove move=_move; - const solution sol(_solution); - } -} ; - -class testMoveNext : public moNextMove -{ -public : - bool operator () (testMove & _move, const solution & _solution) - { - testMove move=_move; - const solution sol(_solution); - - return false; - } -} ; - -class testMoveIncrEval : public moMoveIncrEval -{ -public : - unsigned int operator () (const testMove & _move, const solution & _solution) - { - const testMove move(_move); - const solution solution(_solution); - - return 2; - } -} ; - -class testMoveSelect : public moMoveSelect -{ -public : - void operator () (testMove & _move, unsigned int & _fitness) - { - testMove move; - - move=_move; - - _fitness=2; - } - - void init(const unsigned int & _fitness) - { - unsigned int fitness; - fitness=(unsigned int)_fitness; - } - - bool update(const testMove & _move, const unsigned int & _fitness) - { - testMove move; - unsigned int fitness; - - move=(testMove)_move; - fitness=(unsigned int)_fitness; - - return true; - } -} ; - -class solutionEval : public eoEvalFunc -{ -public : - void operator () (solution & _solution) + bool operator () (solution & _solution) { _solution.fitness(2); + return true; } } ; @@ -166,6 +103,15 @@ public : } } ; +class solutionEval : public eoEvalFunc +{ +public : + void operator () (solution & _solution) + { + _solution.fitness(0); + } +} ; + //----------------------------------------------------------------------------- int @@ -177,19 +123,13 @@ main() sol.fitness(0); - testMoveInit init; - testMoveNext next; - testMoveIncrEval incrEval; - testMoveSelect select; - solutionEval eval; - - moHC hc(init, next, incrEval, select, eval); - + solutionAlgo algorithm; solutionContinue continu; solutionComparator comparator; solutionPerturbation perturbation; + solutionEval eval; - moILS ils(hc, continu, comparator, perturbation, eval); + moILS ils(algorithm, continu, comparator, perturbation, eval); ils(sol); diff --git a/trunk/paradiseo-mo/test/t-moSA.cpp b/trunk/paradiseo-mo/test/t-moSA.cpp index b66128a51..008fb8bc8 100644 --- a/trunk/paradiseo-mo/test/t-moSA.cpp +++ b/trunk/paradiseo-mo/test/t-moSA.cpp @@ -45,7 +45,7 @@ using std::endl; //----------------------------------------------------------------------------- -typedef EO solution; +typedef EO solution; class testMove : public moMove { @@ -68,7 +68,7 @@ public : class testMoveIncrEval : public moMoveIncrEval { public : - unsigned int operator () (const testMove & _move, const solution & _solution) + int operator () (const testMove & _move, const solution & _solution) { const testMove move(_move); const solution solution(_solution);