TSPLIB
|
00001 00013 #ifndef _TransformConstantsClassesAndFunctions_HPP_ 00014 #define _TransformConstantsClassesAndFunctions_HPP_ 00015 00016 #include <cmath> 00017 #include <string> 00018 #include <vector> 00019 #include <iomanip> 00020 00021 #include <xercesc/util/PlatformUtils.hpp> 00022 00023 00027 const std::string INPUT_FILE_FILENAME_EXTENSION_TSP = ".tsp"; 00028 00032 const std::string INPUT_FILE_FILENAME_EXTENSION_ATSP = ".atsp"; 00033 00037 const std::string TAG_NAME = "NAME:"; 00038 00042 const std::string TAG_TYPE = "TYPE:"; 00043 00047 const std::string TAG_COMMENT = "COMMENT:"; 00048 00052 const std::string TAG_DIMENSION = "DIMENSION:"; 00053 00057 const std::string TAG_EDGE_WEIGHT_TYPE = "EDGE_WEIGHT_TYPE:"; 00058 00062 const std::string TAG_EDGE_WEIGHT_FORMAT = "EDGE_WEIGHT_FORMAT:"; 00063 00067 const std::string TAG_DISPLAY_DATA_TYPE = "DISPLAY_DATA_TYPE:"; 00068 00072 const std::string TAG_NODE_COORD_SECTION = "NODE_COORD_SECTION"; 00073 00077 const std::string EDGE_WEIGHT_SECTION = "EDGE_WEIGHT_SECTION"; 00078 00082 const std::string DISPLAY_DATA_SECTION = "DISPLAY_DATA_SECTION"; 00083 00087 const std::string TAG_EOF = "EOF"; 00088 00092 const std::string VALUE_TYPE_TSP = "TSP"; 00093 00097 const std::string VALUE_TYPE_ATSP = "ATSP"; 00098 00102 const double RRR = 6378.388; 00103 00107 const std::string VALUE_EDGE_WEIGHT_TYPE_GEO = "GEO"; 00108 00112 const std::string VALUE_EDGE_WEIGHT_TYPE_EUC_2D = "EUC_2D"; 00113 00117 const std::string VALUE_EDGE_WEIGHT_TYPE_CEIL_2D = "CEIL_2D"; 00118 00122 const std::string VALUE_EDGE_WEIGHT_TYPE_ATT = "ATT"; 00123 00127 const std::string VALUE_EDGE_WEIGHT_TYPE_EXPLICIT = "EXPLICIT"; 00128 00132 const std::string VALUE_EDGE_WEIGHT_FORMAT_FUNCTION = "FUNCTION"; 00133 00137 const std::string VALUE_EDGE_WEIGHT_FORMAT_FULL_MATRIX = "FULL_MATRIX"; 00138 00142 const std::string VALUE_EDGE_WEIGHT_FORMAT_LOWER_DIAG_ROW = "LOWER_DIAG_ROW"; 00143 00147 const std::string VALUE_EDGE_WEIGHT_FORMAT_UPPER_DIAG_ROW = "UPPER_DIAG_ROW"; 00148 00152 const std::string VALUE_EDGE_WEIGHT_FORMAT_UPPER_ROW = "UPPER_ROW"; 00153 00157 const std::string VALUE_DISPLAY_DATA_TYPE_COORD_DISPLAY = "COORD_DISPLAY"; 00158 00162 const std::string VALUE_DISPLAY_DATA_TYPE_TWOD_DISPLAY = "TWOD_DISPLAY"; 00163 00164 00168 const std::string OUTPUT_FILE_FILENAME_EXTENSION = ".xml"; 00169 00173 const bool XML_FORMAT_PRETTY_PRINT = true; 00174 00178 const std::string XML_DOCUMENT_NODE = "travellingSalesmanProblemInstance"; 00179 00183 const std::string XML_NAME = "name"; 00184 00188 const std::string XML_SOURCE = "source"; 00189 00193 const std::string XML_DESCRIPTION = "description"; 00194 00198 const std::string XML_DOUBLE_PRECISION = "doublePrecision"; 00199 00204 const std::string XML_IGNORED_DIGITS = "ignoredDigits"; 00205 00209 const std::string XML_GRAPH = "graph"; 00210 00214 const std::string XML_VERTEX = "vertex"; 00215 00219 const std::string XML_EDGE = "edge"; 00220 00224 const std::string XML_EDGE_ATTRIBUTE_COST = "cost"; 00225 00229 const std::string XML_ENCODING = "UTF-8"; 00230 00234 const std::string XML_VALUE_SOURCE_TSPLIB = "TSPLIB"; 00235 00239 const std::ios::fmtflags DOUBLE_FLOATFIELD = std::ios::scientific; 00240 00244 const std::streamsize TRANSFORM_DOUBLE_PRECISION = std::numeric_limits<double>::digits10; 00245 00250 const std::streamsize TRANSFORM_IGNORED_DIGITS = 5; 00251 00255 const double TRANSFORM_DOUBLE_ZERO = 00256 pow(10.0, -1.0 * static_cast<double>(TRANSFORM_DOUBLE_PRECISION - TRANSFORM_IGNORED_DIGITS)); 00257 00258 00264 inline void trimLeft (std::string &s, const std::string &t = " \t\r\n") { 00265 s.erase(0, s.find_first_not_of(t)); 00266 } 00267 00273 inline void trimRight (std::string &s, const std::string &t = " \t\r\n") { 00274 std::string::size_type i = s.find_last_not_of(t); 00275 if (i == std::string::npos) { 00276 s = ""; 00277 } 00278 else { 00279 s.erase(s.find_last_not_of(t) + 1); 00280 } 00281 } 00282 00288 inline void trim (std::string &s, const std::string &t = " \t\r\n") { 00289 trimLeft(s, t); 00290 trimRight(s, t); 00291 } 00292 00299 inline double roundToDoublePrecisionAndDoubleFloatField(double d) { 00300 std::stringstream dStringstream; 00301 dStringstream.setf(DOUBLE_FLOATFIELD, std::ios::floatfield); 00302 dStringstream << std::setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 00303 dStringstream << d; 00304 std::string dString; 00305 dString = dStringstream.str(); 00306 std::istringstream dIstringstream(dString); 00307 double result; 00308 dIstringstream >> result; 00309 return (result); 00310 } 00311 00312 00316 class InputFileFormatNotSupported : public std::exception { 00317 public: 00321 inline InputFileFormatNotSupported() { 00322 } 00323 }; 00324 00331 class TransformInstance { 00332 private: 00336 std::string type; 00337 00341 std::string name; 00342 00346 std::string source; 00347 00351 std::string description; 00352 00356 std::vector<std::vector<double> >::size_type n; 00357 00363 std::vector<std::vector<double> > adjacencyMatrix; 00364 00373 void init( 00374 const std::string &type, 00375 const std::string &name, 00376 const std::string &source, 00377 const std::string &description, 00378 const std::vector<std::vector<double> >::size_type n); 00379 00380 public: 00389 TransformInstance( 00390 const std::string &type, 00391 const std::string &name, 00392 const std::string &source, 00393 const std::string &description, 00394 const std::vector<std::vector<double> >::size_type n); 00395 00400 TransformInstance(const TransformInstance &transformInstance); 00401 00407 TransformInstance &operator=(const TransformInstance &transformInstance); 00408 00413 inline std::string getType() const { 00414 return (type); 00415 } 00416 00421 inline std::string getName() const { 00422 return (name); 00423 } 00424 00429 inline std::string getSource() const { 00430 return (source); 00431 } 00432 00437 inline std::string getDescription() const { 00438 return (description); 00439 } 00440 00445 inline std::vector<std::vector<double> >::size_type getN() const { 00446 return (n); 00447 } 00448 00457 inline void setAdjacencyMatrixElement( 00458 const std::vector<std::vector<double> >::size_type i, 00459 const std::vector<double>::size_type j, 00460 const double value) { 00461 double roundedValue = roundToDoublePrecisionAndDoubleFloatField(value); 00462 if (type == VALUE_TYPE_TSP) { 00463 if (i < j) { 00464 adjacencyMatrix.at( 00465 static_cast<std::vector<std::vector<double> >::size_type>(j)).at( 00466 static_cast<std::vector<double>::size_type>(i)) = roundedValue; 00467 } 00468 else { 00469 adjacencyMatrix.at(i).at(j) = roundedValue; 00470 } 00471 } 00472 else { //if (type == VALUE_TYPE_ATSP) 00473 adjacencyMatrix.at(i).at(j) = roundedValue; 00474 } 00475 } 00476 00483 inline double getAdjacencyMatrixElement( 00484 const std::vector<std::vector<double> >::size_type i, 00485 const std::vector<double>::size_type j) const { 00486 if (type == VALUE_TYPE_TSP) { 00487 if (i < j) { 00488 return (adjacencyMatrix.at( 00489 static_cast<std::vector<std::vector<double> >::size_type>(j)).at( 00490 static_cast<std::vector<double>::size_type>(i))); 00491 } 00492 else { 00493 return (adjacencyMatrix.at(i).at(j)); 00494 } 00495 } 00496 else { //if (type == VALUE_TYPE_ATSP) 00497 return (adjacencyMatrix.at(i).at(j)); 00498 } 00499 } 00500 }; 00501 00502 00550 TransformInstance *readInputFileTSPLIB(const std::string &inputFileName); 00551 00558 void writeOutputFile(const std::string &outputFileName, const TransformInstance *transformInstance); 00559 00567 void writeOutputFileWithoutUsingAParser( 00568 const std::string &outputFileName, 00569 const TransformInstance *transformInstance); 00570 00571 00572 #endif