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:
parent
c000ef9a05
commit
747be27d1a
3 changed files with 40 additions and 11 deletions
|
|
@ -65,6 +65,7 @@ class moRandImprSelect: public moMoveSelect < M >
|
||||||
initial_fitness = _fitness;
|
initial_fitness = _fitness;
|
||||||
better_fitnesses.clear();
|
better_fitnesses.clear();
|
||||||
better_moves.clear();
|
better_moves.clear();
|
||||||
|
firstTime=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Function that updates the fitness and move vectors
|
//! 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)
|
bool update (const M & _move, const Fitness & _fitness)
|
||||||
{
|
{
|
||||||
|
firstTime=false;
|
||||||
|
|
||||||
if (_fitness > initial_fitness)
|
if (_fitness > initial_fitness)
|
||||||
{
|
{
|
||||||
better_fitnesses.push_back(_fitness);
|
better_fitnesses.push_back(_fitness);
|
||||||
better_moves.push_back(_move);
|
better_moves.push_back(_move);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! The move selection
|
//! The move selection
|
||||||
|
|
@ -103,7 +103,11 @@ class moRandImprSelect: public moMoveSelect < M >
|
||||||
|
|
||||||
if( (better_fitnesses.size()==0) || (better_moves.size()==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 ());
|
index = rng.random (better_fitnesses.size ());
|
||||||
|
|
@ -122,6 +126,9 @@ class moRandImprSelect: public moMoveSelect < M >
|
||||||
|
|
||||||
//! Candidate move vector.
|
//! Candidate move vector.
|
||||||
std::vector < M > better_moves;
|
std::vector < M > better_moves;
|
||||||
|
|
||||||
|
//! Indicate if update has been called or not.
|
||||||
|
bool firstTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ void manage_configuration_file(eoParser & _parser);
|
||||||
int
|
int
|
||||||
main (int _argc, char* _argv [])
|
main (int _argc, char* _argv [])
|
||||||
{
|
{
|
||||||
std::string instancePath;
|
std::string instancePath, selectionType;
|
||||||
unsigned int seed;
|
unsigned int seed;
|
||||||
|
|
||||||
eoParser parser(_argc, _argv);
|
eoParser parser(_argc, _argv);
|
||||||
|
|
@ -51,6 +51,7 @@ main (int _argc, char* _argv [])
|
||||||
|
|
||||||
seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() );
|
seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() );
|
||||||
instancePath=parser.getParamWithLongName("instancePath")->getValue();
|
instancePath=parser.getParamWithLongName("instancePath")->getValue();
|
||||||
|
selectionType=parser.getParamWithLongName("selectionType")->getValue();
|
||||||
|
|
||||||
srand (seed);
|
srand (seed);
|
||||||
Graph::load(instancePath.c_str());
|
Graph::load(instancePath.c_str());
|
||||||
|
|
@ -74,16 +75,33 @@ main (int _argc, char* _argv [])
|
||||||
|
|
||||||
TwoOptIncrEval two_opt_incremental_evaluation;
|
TwoOptIncrEval two_opt_incremental_evaluation;
|
||||||
|
|
||||||
//moFirstImprSelect <TwoOpt> two_opt_selection;
|
moMoveSelect<TwoOpt>* two_opt_selection;
|
||||||
moBestImprSelect <TwoOpt> two_opt_selection;
|
|
||||||
//moRandImprSelect <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,
|
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) ;
|
hill_climbing (solution) ;
|
||||||
|
|
||||||
std :: cout << "[To] " << solution << std :: endl;
|
std :: cout << "[To] " << solution << std :: endl;
|
||||||
|
|
||||||
|
delete(two_opt_selection);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,6 +114,9 @@ manage_configuration_file(eoParser & _parser)
|
||||||
0, "Configuration", false);
|
0, "Configuration", false);
|
||||||
_parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 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())
|
if (_parser.userNeedsHelp())
|
||||||
{
|
{
|
||||||
_parser.printHelp(std::cout);
|
_parser.printHelp(std::cout);
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,6 @@
|
||||||
# --stopOnUnknownParam=1 # Stop if unkown param entered
|
# --stopOnUnknownParam=1 # Stop if unkown param entered
|
||||||
|
|
||||||
###### Configuration ######
|
###### Configuration ######
|
||||||
# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance
|
# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance.
|
||||||
# --seed=1202916317 # Seed for rand
|
# --seed=1203517190 # Seed for rand.
|
||||||
|
# --selectionType=Best # Type of the selection: 'Best', 'First' or 'Random'.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue