tsp sources have updated including the configuration file use.
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@961 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
557133c8d2
commit
6ed60013eb
3 changed files with 151 additions and 60 deletions
16
contribution/trunk/combinatorial/routing/tsp/param
Normal file
16
contribution/trunk/combinatorial/routing/tsp/param
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
###### General ######
|
||||
# --help=0 # -h : Prints this message
|
||||
# --stopOnUnknownParam=1 # Stop if unkown param entered
|
||||
|
||||
###### Configuration ######
|
||||
# --instancePath=benchs/berlin52.tsp # Path to the instance.
|
||||
# --seed=1203082112 # Seed for rand.
|
||||
# --popSize=100 # Size of the population.
|
||||
# --maxIter=1000 # Maximum number of iterations.
|
||||
# --crossRate=1 # Probability of crossover.
|
||||
# --mutRate=0.01 # Probability of mutation.
|
||||
# --nbSelPar=100 # Number of selected parents.
|
||||
# --elitismRate=1 # Percentage of the best individuals kept.
|
||||
# --tournRate=0.7 # Percentage of the individuals used during the tournament.
|
||||
# --crossType=Partial # Crossover to use, it can be 'Partial', 'Order' or 'Edge'.
|
||||
|
|
@ -40,6 +40,9 @@
|
|||
|
||||
#include "graph.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
namespace Graph
|
||||
{
|
||||
|
||||
|
|
@ -70,103 +73,103 @@ namespace Graph
|
|||
{
|
||||
double distX = (double)(vectCoord [i].first - vectCoord [j].first) ;
|
||||
double distY = (double)(vectCoord [i].second - vectCoord [j].second) ;
|
||||
dist [i] [j] = dist [j] [i] = (unsigned) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ;
|
||||
dist [i] [j] = dist [j] [i] = (unsigned int) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void load (const char * __fileName)
|
||||
void load (const char * _fileName)
|
||||
{
|
||||
unsigned int i, dimension;
|
||||
|
||||
std::string string_read, buffer;
|
||||
|
||||
std :: ifstream file (__fileName) ;
|
||||
std :: ifstream file (_fileName) ;
|
||||
|
||||
std :: cout << ">> Loading [" << __fileName << "]" << std :: endl ;
|
||||
cout << endl << "\tLoading [" << _fileName << "]" << endl << endl;
|
||||
|
||||
if (file)
|
||||
if( file.is_open() )
|
||||
{
|
||||
// Read NAME:
|
||||
file >> string_read;
|
||||
if (string_read.compare("NAME:")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'NAME:\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'NAME:\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Read instance name
|
||||
file >> string_read;
|
||||
std::cout << "\t Instance Name = " << string_read << std::endl;
|
||||
cout << "\t\tInstance Name = " << string_read << endl;
|
||||
// Read TYPE:
|
||||
file >> string_read;
|
||||
if (string_read.compare("TYPE:")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'TYPE:\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'TYPE:\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Read instance type;
|
||||
file >> string_read;
|
||||
std::cout << "\t Instance type = " << string_read << std::endl;
|
||||
cout << "\t\tInstance type = " << string_read << endl;
|
||||
if (string_read.compare("TSP")!=0)
|
||||
{
|
||||
std::cout << "ERROR: only TSP type instance can be loaded" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: only TSP type instance can be loaded" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Read COMMENT:
|
||||
file >> string_read;
|
||||
if (string_read.compare("COMMENT:")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'COMMENT:\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'COMMENT:\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Read comments
|
||||
std::cout << "\t Instance comments = ";
|
||||
cout << "\t\tInstance comments = ";
|
||||
file >> string_read;
|
||||
buffer = string_read+"_first";
|
||||
while((string_read.compare("DIMENSION:")!=0) && (string_read.compare(buffer)!=0))
|
||||
{
|
||||
if(string_read.compare("COMMENT:")!=0)
|
||||
{
|
||||
std::cout << string_read << " ";
|
||||
cout << string_read << " ";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << std::endl << "\t ";
|
||||
cout << endl << "\t ";
|
||||
}
|
||||
buffer = string_read;
|
||||
file >> string_read;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
cout << endl;
|
||||
|
||||
// Read dimension;
|
||||
file >> dimension ;
|
||||
std::cout << "\t Instance dimension = " << dimension << std::endl;
|
||||
cout << "\t\tInstance dimension = " << dimension << endl;
|
||||
vectCoord.resize (dimension) ;
|
||||
|
||||
// Read EDGE_WEIGHT_TYPE
|
||||
file >> string_read;
|
||||
if (string_read.compare("EDGE_WEIGHT_TYPE:")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'EDGE_WEIGHT_TYPE:\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'EDGE_WEIGHT_TYPE:\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Read edge weight type
|
||||
file >> string_read;
|
||||
std::cout << "\t Instance edge weight type = " << string_read << std::endl;
|
||||
cout << "\t\tInstance edge weight type = " << string_read << endl;
|
||||
if (string_read.compare("EUC_2D")!=0)
|
||||
{
|
||||
std::cout << "ERROR: only EUC_2D edge weight type instance can be loaded" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: only EUC_2D edge weight type instance can be loaded" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Read NODE_COORD_SECTION
|
||||
file >> string_read;
|
||||
if (string_read.compare("NODE_COORD_SECTION")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'NODE_COORD_SECTION\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'NODE_COORD_SECTION\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Read coordonates.
|
||||
|
|
@ -182,11 +185,11 @@ namespace Graph
|
|||
file >> string_read;
|
||||
if(string_read.compare("EOF")!=0)
|
||||
{
|
||||
std::cout << "ERROR: \'EOF\' espected, \'" << string_read << "\' found" << std::endl;
|
||||
exit(1);
|
||||
cout << "ERROR: \'EOF\' espected, \'" << string_read << "\' found" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
cout << endl;
|
||||
|
||||
file.close () ;
|
||||
|
||||
|
|
@ -194,16 +197,14 @@ namespace Graph
|
|||
}
|
||||
else
|
||||
{
|
||||
|
||||
std :: cout << __fileName << " does not exist !!!" << std :: endl ;
|
||||
// Bye !!!
|
||||
exit (1) ;
|
||||
cout << _fileName << " does not exist !!!" << endl ;
|
||||
exit(EXIT_FAILURE) ;
|
||||
}
|
||||
}
|
||||
|
||||
float distance (unsigned int __from, unsigned int __to)
|
||||
float distance (unsigned int _from, unsigned int _to)
|
||||
{
|
||||
return (float)(dist [__from] [__to]) ;
|
||||
return (float)(dist [_from] [_to]) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,50 +37,124 @@
|
|||
#include <eo>
|
||||
#include <tsp>
|
||||
|
||||
int main (int __argc, char * __argv []) {
|
||||
void manage_configuration_file(eoParser & _parser);
|
||||
|
||||
int
|
||||
main (int _argc, char* _argv [])
|
||||
{
|
||||
std::string instancePath, crossoverType;
|
||||
unsigned int seed, populationSize, maxIterations, selectedParentNumber;
|
||||
double crossoverRate, mutationRate, elitismRate, tournamentRate;
|
||||
|
||||
eoParser parser(_argc, _argv);
|
||||
|
||||
manage_configuration_file(parser);
|
||||
|
||||
seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() );
|
||||
instancePath=parser.getParamWithLongName("instancePath")->getValue();
|
||||
populationSize=atoi( (parser.getParamWithLongName("popSize")->getValue()).c_str() );
|
||||
maxIterations=atoi( (parser.getParamWithLongName("maxIter")->getValue()).c_str() );
|
||||
crossoverRate=atof( (parser.getParamWithLongName("crossRate")->getValue()).c_str() );
|
||||
mutationRate=atof( (parser.getParamWithLongName("mutRate")->getValue()).c_str() );
|
||||
selectedParentNumber=atoi( (parser.getParamWithLongName("nbSelPar")->getValue()).c_str() );
|
||||
elitismRate=atof( (parser.getParamWithLongName("elitismRate")->getValue()).c_str() );
|
||||
tournamentRate=atof( (parser.getParamWithLongName("tournRate")->getValue()).c_str() );
|
||||
crossoverType=parser.getParamWithLongName("crossType")->getValue();
|
||||
|
||||
srand (seed);
|
||||
Graph::load(instancePath.c_str());
|
||||
|
||||
RouteInit init ;
|
||||
|
||||
if (__argc != 2) {
|
||||
|
||||
std :: cerr << "Usage : ./tsp [instance]" << std :: endl ;
|
||||
std :: cerr << "=> info: You can copy the benchs in the current directory by using 'make install'." << std :: endl ;
|
||||
return 1 ;
|
||||
}
|
||||
|
||||
Graph :: load (__argv [1]) ; // Instance
|
||||
|
||||
RouteInit init ; // Sol. Random Init.
|
||||
|
||||
RouteEval full_eval ; // Full Evaluator
|
||||
RouteEval full_eval ;
|
||||
|
||||
eoPop <Route> pop (100, init) ; // Population
|
||||
eoPop <Route> pop (populationSize, init) ;
|
||||
apply <Route> (full_eval, pop) ;
|
||||
|
||||
std :: cout << "[From] " << pop.best_element () << std :: endl ;
|
||||
|
||||
eoGenContinue <Route> cont (1000) ; /* Continuator (A fixed number of
|
||||
1000 iterations */
|
||||
eoGenContinue <Route> continu (maxIterations) ;
|
||||
|
||||
eoStochTournamentSelect <Route> select_one ; // Selector
|
||||
eoStochTournamentSelect <Route> select_one ;
|
||||
|
||||
eoSelectNumber <Route> select (select_one, 100) ;
|
||||
eoSelectNumber <Route> select (select_one, selectedParentNumber) ;
|
||||
|
||||
// OrderXover cross ; // Order Crossover
|
||||
PartialMappedXover cross ;
|
||||
eoQuadOp <Route>*crossover;
|
||||
|
||||
CitySwap mut ; // City Swap Mutator
|
||||
if(crossoverType.compare("Partial")==0)
|
||||
{
|
||||
crossover=new PartialMappedXover();
|
||||
}
|
||||
else if (crossoverType.compare("Order")==0)
|
||||
{
|
||||
crossover=new OrderXover();
|
||||
}
|
||||
else if (crossoverType.compare("Edge")==0)
|
||||
{
|
||||
crossover=new EdgeXover();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("[tsp.cpp]: the crossover type '"+crossoverType+"' is not correct.");
|
||||
}
|
||||
|
||||
eoSGATransform <Route> transform (cross, 1, mut, 0.01) ;
|
||||
CitySwap mutation ;
|
||||
|
||||
eoElitism <Route> merge (1) ; // Use of Elistism
|
||||
eoSGATransform <Route> transform (*crossover, crossoverRate, mutation, mutationRate) ;
|
||||
|
||||
eoStochTournamentTruncate <Route> reduce (0.7) ; // Stoch. Replacement
|
||||
eoElitism <Route> merge (elitismRate) ;
|
||||
|
||||
eoEasyEA <Route> ea (cont, full_eval, select, transform, merge, reduce) ;
|
||||
eoStochTournamentTruncate <Route> reduce (tournamentRate) ;
|
||||
|
||||
eoEasyEA <Route> ea (continu, full_eval, select, transform, merge, reduce) ;
|
||||
|
||||
ea (pop) ;
|
||||
|
||||
std :: cout << "[To] " << pop.best_element () << std :: endl ;
|
||||
|
||||
return 0 ;
|
||||
delete(crossover);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void
|
||||
manage_configuration_file(eoParser & _parser)
|
||||
{
|
||||
std::ofstream os;
|
||||
|
||||
_parser.getORcreateParam(std::string("benchs/berlin52.tsp"), "instancePath", "Path to the instance.",
|
||||
0, "Configuration", false);
|
||||
_parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((unsigned int)100, "popSize", "Size of the population.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((unsigned int)1000, "maxIter", "Maximum number of iterations.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((double)1.0, "crossRate", "Probability of crossover.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((double)0.01, "mutRate", "Probability of mutation.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((unsigned int)100, "nbSelPar", "Number of selected parents.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((double)1.0, "elitismRate", "Percentage of the best individuals kept.", 0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam((double)0.7, "tournRate", "Percentage of the individuals used during the tournament.",
|
||||
0, "Configuration", false);
|
||||
|
||||
_parser.getORcreateParam(std::string("Partial"), "crossType", "Crossover to use, it can be 'Partial', 'Order' or 'Edge'.",
|
||||
0, "Configuration", false);
|
||||
|
||||
if (_parser.userNeedsHelp())
|
||||
{
|
||||
_parser.printHelp(std::cout);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
os.open("current_param");
|
||||
if(!os.is_open())
|
||||
{
|
||||
throw std::runtime_error("[tsp.cpp]: the file current_param cannot be created.");
|
||||
}
|
||||
os <<_parser;
|
||||
os.close();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue