hill_climbing.cpp and moRandImprSelect.h have been updated

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@977 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jboisson 2008-02-20 14:41:23 +00:00
commit 747be27d1a
3 changed files with 40 additions and 11 deletions

View file

@ -65,6 +65,7 @@ class moRandImprSelect: public moMoveSelect < M >
initial_fitness = _fitness;
better_fitnesses.clear();
better_moves.clear();
firstTime=true;
}
//! Function that updates the fitness and move vectors
@ -78,14 +79,13 @@ class moRandImprSelect: public moMoveSelect < M >
*/
bool update (const M & _move, const Fitness & _fitness)
{
firstTime=false;
if (_fitness > initial_fitness)
{
better_fitnesses.push_back(_fitness);
better_moves.push_back(_move);
}
return true;
}
//! The move selection
@ -100,10 +100,14 @@ class moRandImprSelect: public moMoveSelect < M >
unsigned int index;
index=0;
if( (better_fitnesses.size()==0) || (better_moves.size()==0) )
{
throw std::runtime_error("[moRandImprSelect.h]: no move or/and no fitness already saved, update has to be called first.");
if(firstTime)
{
throw std::runtime_error("[moRandImprSelect.h]: no move or/and no fitness already saved, update has to be called first.");
}
return;
}
index = rng.random (better_fitnesses.size ());
@ -122,6 +126,9 @@ class moRandImprSelect: public moMoveSelect < M >
//! Candidate move vector.
std::vector < M > better_moves;
//! Indicate if update has been called or not.
bool firstTime;
};
#endif

View file

@ -42,7 +42,7 @@ void manage_configuration_file(eoParser & _parser);
int
main (int _argc, char* _argv [])
{
std::string instancePath;
std::string instancePath, selectionType;
unsigned int seed;
eoParser parser(_argc, _argv);
@ -51,6 +51,7 @@ main (int _argc, char* _argv [])
seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() );
instancePath=parser.getParamWithLongName("instancePath")->getValue();
selectionType=parser.getParamWithLongName("selectionType")->getValue();
srand (seed);
Graph::load(instancePath.c_str());
@ -74,16 +75,33 @@ main (int _argc, char* _argv [])
TwoOptIncrEval two_opt_incremental_evaluation;
//moFirstImprSelect <TwoOpt> two_opt_selection;
moBestImprSelect <TwoOpt> two_opt_selection;
//moRandImprSelect <TwoOpt> two_opt_selection;
moMoveSelect<TwoOpt>* two_opt_selection;
if(selectionType.compare("Best")==0)
{
two_opt_selection= new moBestImprSelect<TwoOpt>();
}
else if (selectionType.compare("First")==0)
{
two_opt_selection= new moFirstImprSelect<TwoOpt>();
}
else if (selectionType.compare("Random")==0)
{
two_opt_selection= new moRandImprSelect<TwoOpt>();
}
else
{
throw std::runtime_error("[hill_climbing.cpp]: the type of selection '"+selectionType+"' is not correct.");
}
moHC <TwoOpt> hill_climbing (two_opt_initializer, two_opt_next_move_generator, two_opt_incremental_evaluation,
two_opt_selection, full_evaluation);
*two_opt_selection, full_evaluation);
hill_climbing (solution) ;
std :: cout << "[To] " << solution << std :: endl;
delete(two_opt_selection);
return EXIT_SUCCESS;
}
@ -96,6 +114,9 @@ manage_configuration_file(eoParser & _parser)
0, "Configuration", false);
_parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false);
_parser.getORcreateParam(std::string("Best"), "selectionType", "Type of the selection: 'Best', 'First' or 'Random'.",
0, "Configuration", false);
if (_parser.userNeedsHelp())
{
_parser.printHelp(std::cout);

View file

@ -4,5 +4,6 @@
# --stopOnUnknownParam=1 # Stop if unkown param entered
###### Configuration ######
# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance
# --seed=1202916317 # Seed for rand
# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance.
# --seed=1203517190 # Seed for rand.
# --selectionType=Best # Type of the selection: 'Best', 'First' or 'Random'.