TSPLIB
|
00001 00013 #include <iostream> 00014 #include <stdexcept> 00015 #include <limits> 00016 #include <algorithm> 00017 #include <fstream> 00018 #include <sstream> 00019 #include <iomanip> 00020 #include <string> 00021 #include <vector> 00022 00023 #include <xercesc/util/PlatformUtils.hpp> 00024 #include <xercesc/util/XMLString.hpp> 00025 #include <xercesc/dom/DOM.hpp> 00026 #include <xercesc/util/OutOfMemoryException.hpp> 00027 #include <xercesc/framework/LocalFileFormatTarget.hpp> 00028 00029 #include "TransformDOMErrorHandler.hpp" 00030 00031 #include "TransformConstantsClassesAndFunctions.hpp" 00032 00033 using namespace std; 00034 using namespace xercesc; 00035 00036 XERCES_CPP_NAMESPACE_USE 00037 00038 00039 void TransformInstance::init( 00040 const std::string &type, 00041 const std::string &name, 00042 const std::string &source, 00043 const std::string &description, 00044 const std::vector<std::vector<double> >::size_type n) { 00045 this->type = type; 00046 this->name = name; 00047 this->source = source; 00048 this->description = description; 00049 this->n = n; 00050 } 00051 00052 TransformInstance::TransformInstance( 00053 const std::string &type, 00054 const std::string &name, 00055 const std::string &source, 00056 const std::string &description, 00057 const std::vector<std::vector<double> >::size_type n) { 00058 init(type, name, source, description, n); 00059 00060 adjacencyMatrix.resize(n); 00061 if (type == VALUE_TYPE_TSP) { 00062 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00063 adjacencyMatrix.at(i).resize(i + 1); 00064 } 00065 } 00066 else { //if (type == VALUE_TYPE_ATSP) 00067 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00068 adjacencyMatrix.at(i).resize(n); 00069 } 00070 } 00071 } 00072 00073 TransformInstance::TransformInstance(const TransformInstance &transformInstance) { 00074 init( 00075 transformInstance.getType(), 00076 transformInstance.getName(), 00077 transformInstance.getSource(), 00078 transformInstance.getDescription(), 00079 transformInstance.getN()); 00080 00081 adjacencyMatrix.resize(n); 00082 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00083 adjacencyMatrix.at(i).resize(i + 1); 00084 for (vector<double>::size_type j = 0; j < n; j++) { 00085 setAdjacencyMatrixElement(i, j, getAdjacencyMatrixElement(i, j)); 00086 } 00087 } 00088 } 00089 00090 TransformInstance &TransformInstance::operator=(const TransformInstance &transformInstance) { 00091 if (this == &transformInstance) { 00092 return (*this); 00093 } 00094 else { 00095 init( 00096 transformInstance.getType(), 00097 transformInstance.getName(), 00098 transformInstance.getSource(), 00099 transformInstance.getDescription(), 00100 transformInstance.getN()); 00101 00102 adjacencyMatrix.resize(n); 00103 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00104 adjacencyMatrix.at(i).resize(i + 1); 00105 for (vector<double>::size_type j = 0; j < n; j++) { 00106 setAdjacencyMatrixElement(i, j, getAdjacencyMatrixElement(i, j)); 00107 } 00108 } 00109 00110 return (*this); 00111 } 00112 } 00113 00114 00118 struct Point { 00119 double x; 00120 double y; 00121 }; 00122 00123 TransformInstance *readInputFileTSPLIB(const string &inputFileName) { 00124 /* 00125 * Creating of the instance of the class TransformInstance. 00126 */ 00127 TransformInstance *transformInstance = 0; 00128 00129 /* 00130 * Indicator indicating if the file contains a symmetric travelling salesman problem. 00131 * <ul> 00132 * <li>true The file contains a symmetric travelling salesman problem.</li> 00133 * <li>false The file contains an asymmetric travelling salesman problem.</li> 00134 * </ul> 00135 */ 00136 bool symmetricTravellingSalesmanProblem; 00137 string inputFileFilenameExtensionTSP; 00138 if (inputFileName.size() >= INPUT_FILE_FILENAME_EXTENSION_TSP.size() + 1) { 00139 inputFileFilenameExtensionTSP = 00140 inputFileName.substr( 00141 inputFileName.size() - INPUT_FILE_FILENAME_EXTENSION_TSP.size(), 00142 INPUT_FILE_FILENAME_EXTENSION_TSP.size()); 00143 } 00144 else { 00145 inputFileFilenameExtensionTSP = ""; 00146 } 00147 00148 string inputFileFilenameExtensionATSP; 00149 if (inputFileName.size() >= INPUT_FILE_FILENAME_EXTENSION_ATSP.size() + 1) { 00150 inputFileFilenameExtensionATSP = 00151 inputFileName.substr( 00152 inputFileName.size() - INPUT_FILE_FILENAME_EXTENSION_ATSP.size(), 00153 INPUT_FILE_FILENAME_EXTENSION_ATSP.size()); 00154 } 00155 else { 00156 inputFileFilenameExtensionATSP = ""; 00157 } 00158 00159 transform( 00160 inputFileFilenameExtensionTSP.begin(), 00161 inputFileFilenameExtensionTSP.end(), 00162 inputFileFilenameExtensionTSP.begin(), ::tolower); 00163 transform( 00164 inputFileFilenameExtensionATSP.begin(), 00165 inputFileFilenameExtensionATSP.end(), 00166 inputFileFilenameExtensionATSP.begin(), ::tolower); 00167 00168 if (inputFileFilenameExtensionTSP == INPUT_FILE_FILENAME_EXTENSION_TSP) { 00169 symmetricTravellingSalesmanProblem = true; 00170 } 00171 if (inputFileFilenameExtensionATSP == INPUT_FILE_FILENAME_EXTENSION_ATSP) { 00172 symmetricTravellingSalesmanProblem = false; 00173 } 00174 00175 ifstream inputFile; 00176 inputFile.exceptions(ifstream::failbit | ifstream::badbit); 00177 try { 00178 inputFile.open(inputFileName.c_str()); 00179 00180 string inputLine = ""; 00181 00182 //NAME. 00183 if (!getline(inputFile, inputLine)) { 00184 throw ifstream::failure("Unexpected input!"); 00185 } 00186 trim(inputLine); 00187 if (inputLine.substr(0, TAG_NAME.length()) != TAG_NAME) { 00188 throw ifstream::failure("Unexpected input!"); 00189 } 00190 inputLine.erase(0, TAG_NAME.length()); 00191 trim(inputLine); 00192 if (inputLine == "") { 00193 throw ifstream::failure("Unexpected input!"); 00194 } 00195 const string name = inputLine; 00196 00197 //TYPE. 00198 if (!getline(inputFile, inputLine)) { 00199 throw ifstream::failure("Unexpected input!"); 00200 } 00201 trim(inputLine); 00202 if (inputLine.substr(0, TAG_TYPE.length()) != TAG_TYPE) { 00203 throw ifstream::failure("Unexpected input!"); 00204 } 00205 inputLine.erase(0, TAG_TYPE.length()); 00206 trim(inputLine); 00207 string type; 00208 if ((symmetricTravellingSalesmanProblem) && (inputLine == VALUE_TYPE_TSP)) { 00209 type = VALUE_TYPE_TSP; 00210 //COMMENT. 00211 if (!getline(inputFile, inputLine)) { 00212 throw ifstream::failure("Unexpected input!"); 00213 } 00214 trim(inputLine); 00215 if (inputLine.substr(0, TAG_COMMENT.length()) != TAG_COMMENT) { 00216 throw ifstream::failure("Unexpected input!"); 00217 } 00218 inputLine.erase(0, TAG_COMMENT.length()); 00219 trim(inputLine); 00220 const string comment = inputLine; 00221 00222 //DIMENSION. 00223 if (!getline(inputFile, inputLine)) { 00224 throw ifstream::failure("Unexpected input!"); 00225 } 00226 trim(inputLine); 00227 if (inputLine.substr(0, TAG_DIMENSION.length()) != TAG_DIMENSION) { 00228 throw ifstream::failure("Unexpected input!"); 00229 } 00230 inputLine.erase(0, TAG_DIMENSION.length()); 00231 trim(inputLine); 00232 vector<vector<double> >::size_type n; 00233 istringstream nIStringstream(inputLine); 00234 nIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00235 nIStringstream >> n; 00236 if (!nIStringstream.eof()) { 00237 throw ifstream::failure("Unexpected input!"); 00238 } 00239 00240 //EDGE_WEIGHT_TYPE. 00241 if (!getline(inputFile, inputLine)) { 00242 throw ifstream::failure("Unexpected input!"); 00243 } 00244 trim(inputLine); 00245 if (inputLine.substr(0, TAG_EDGE_WEIGHT_TYPE.length()) != TAG_EDGE_WEIGHT_TYPE) { 00246 throw ifstream::failure("Unexpected input!"); 00247 } 00248 inputLine.erase(0, TAG_EDGE_WEIGHT_TYPE.length()); 00249 trim(inputLine); 00250 const string edgeWeightType = inputLine; 00251 if (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_GEO) { 00252 //EDGE_WEIGHT_FORMAT. 00253 if (!getline(inputFile, inputLine)) { 00254 throw ifstream::failure("Unexpected input!"); 00255 } 00256 trim(inputLine); 00257 if (inputLine.substr(0, TAG_EDGE_WEIGHT_FORMAT.length()) == TAG_EDGE_WEIGHT_FORMAT) { 00258 inputLine.erase(0, TAG_EDGE_WEIGHT_FORMAT.length()); 00259 trim(inputLine); 00260 const string edgeWeightFormat = inputLine; 00261 if (edgeWeightFormat != VALUE_EDGE_WEIGHT_FORMAT_FUNCTION) { 00262 throw ifstream::failure("Unexpected input!"); 00263 } 00264 00265 if (!getline(inputFile, inputLine)) { 00266 throw ifstream::failure("Unexpected input!"); 00267 } 00268 trim(inputLine); 00269 } 00270 00271 //DISPLAY_DATA_TYPE. 00272 if (inputLine.substr(0, TAG_DISPLAY_DATA_TYPE.length()) != TAG_DISPLAY_DATA_TYPE) { 00273 throw ifstream::failure("Unexpected input!"); 00274 } 00275 inputLine.erase(0, TAG_DISPLAY_DATA_TYPE.length()); 00276 trim(inputLine); 00277 const string displayDataType = inputLine; 00278 if (displayDataType == VALUE_DISPLAY_DATA_TYPE_COORD_DISPLAY) { 00279 00280 //NODE_COORD_SECTION. 00281 if (!getline(inputFile, inputLine)) { 00282 throw ifstream::failure("Unexpected input!"); 00283 } 00284 trim(inputLine); 00285 if (inputLine != TAG_NODE_COORD_SECTION) { 00286 throw ifstream::failure("Unexpected input!"); 00287 } 00288 00289 vector<Point> points(n); 00290 for (vector<Point>::size_type i = 0; i < n; i++) { 00291 if (!getline(inputFile, inputLine)) { 00292 throw ifstream::failure("Unexpected input!"); 00293 } 00294 trim(inputLine); 00295 istringstream pointIStringstream(inputLine); 00296 pointIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00297 vector<Point>::size_type j; 00298 pointIStringstream >> j; 00299 if (j != i + 1) { 00300 throw ifstream::failure("Unexpected input!"); 00301 } 00302 Point point; 00303 pointIStringstream >> point.x; 00304 pointIStringstream >> point.y; 00305 if (!nIStringstream.eof()) { 00306 throw ifstream::failure("Unexpected input!"); 00307 } 00308 00309 points.at(i) = point; 00310 } 00311 transformInstance = new TransformInstance( 00312 type, 00313 name, 00314 XML_VALUE_SOURCE_TSPLIB, 00315 comment, 00316 n); 00317 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00318 for (vector<double>::size_type j = 0; j < i; j++) { 00319 double latitude1; 00320 { 00321 double degrees1 = floor( 00322 points.at(static_cast<vector<Point>::size_type>(i)).x); 00323 double minutes1 = 00324 points.at(static_cast<vector<Point>::size_type>(i)).x - 00325 degrees1; 00326 degrees1 += 5.0 * minutes1 / 3.0; 00327 latitude1 = M_PI * degrees1 / 180.0; 00328 } 00329 double longtitude1; 00330 { 00331 double degrees2 = floor( 00332 points.at(static_cast<vector<Point>::size_type>(i)).y); 00333 double minutes2 = 00334 points.at(static_cast<vector<Point>::size_type>(i)).y - 00335 degrees2; 00336 degrees2 += 5.0 * minutes2 / 3.0; 00337 longtitude1 = M_PI * degrees2 / 180.0; 00338 } 00339 00340 double latitude2; 00341 { 00342 double degrees1 = floor( 00343 points.at(static_cast<vector<Point>::size_type>(j)).x); 00344 double minutes1 = 00345 points.at(static_cast<vector<Point>::size_type>(j)).x - 00346 degrees1; 00347 degrees1 += 5.0 * minutes1 / 3.0; 00348 latitude2 = M_PI * degrees1 / 180.0; 00349 } 00350 double longtitude2; 00351 { 00352 double degrees2 = floor( 00353 points.at(static_cast<vector<Point>::size_type>(j)).y); 00354 double minutes2 = 00355 points.at(static_cast<vector<Point>::size_type>(j)).y - 00356 degrees2; 00357 degrees2 += 5.0 * minutes2 / 3.0; 00358 longtitude2 = M_PI * degrees2 / 180.0; 00359 } 00360 00361 double q1 = cos(longtitude1 - longtitude2); 00362 double q2 = cos(latitude1 - latitude2); 00363 double q3 = cos(latitude1 + latitude2); 00364 double distance = 00365 RRR * acos(0.5 *((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0; 00366 00367 transformInstance->setAdjacencyMatrixElement( 00368 i, 00369 j, 00370 distance); 00371 } 00372 } 00373 } 00374 else { 00375 throw ifstream::failure("Unexpected input!"); 00376 } 00377 00378 //EOF. 00379 if (!getline(inputFile, inputLine)) { 00380 throw ifstream::failure("Unexpected input!"); 00381 } 00382 trim(inputLine); 00383 if (inputLine != TAG_EOF) { 00384 throw ifstream::failure("Unexpected input!"); 00385 } 00386 } 00387 else if ( 00388 (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_EUC_2D) || 00389 (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_CEIL_2D)) { 00390 //NODE_COORD_SECTION. 00391 if (!getline(inputFile, inputLine)) { 00392 throw ifstream::failure("Unexpected input!"); 00393 } 00394 trim(inputLine); 00395 if (inputLine != TAG_NODE_COORD_SECTION) { 00396 throw ifstream::failure("Unexpected input!"); 00397 } 00398 00399 vector<Point> points(n); 00400 for (vector<Point>::size_type i = 0; i < n; i++) { 00401 if (!getline(inputFile, inputLine)) { 00402 throw ifstream::failure("Unexpected input!"); 00403 } 00404 trim(inputLine); 00405 istringstream pointIStringstream(inputLine); 00406 pointIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00407 vector<Point>::size_type j; 00408 pointIStringstream >> j; 00409 if (j != i + 1) { 00410 throw ifstream::failure("Unexpected input!"); 00411 } 00412 Point point; 00413 pointIStringstream >> point.x; 00414 pointIStringstream >> point.y; 00415 if (!nIStringstream.eof()) { 00416 throw ifstream::failure("Unexpected input!"); 00417 } 00418 00419 points.at(i) = point; 00420 } 00421 00422 transformInstance = new TransformInstance( 00423 type, 00424 name, 00425 XML_VALUE_SOURCE_TSPLIB, 00426 comment, 00427 n); 00428 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00429 for (vector<double>::size_type j = 0; j < i; j++) { 00430 double deltaX = points.at(i).x - points.at(j).x; 00431 double deltaY = points.at(i).y - points.at(j).y; 00432 double deltaSquare = deltaX * deltaX + deltaY * deltaY; 00433 if (deltaSquare == numeric_limits<double>::infinity()) { 00434 throw range_error("The range error occurs!"); 00435 } 00436 double delta = sqrt(deltaSquare); 00437 transformInstance->setAdjacencyMatrixElement(i, j, delta); 00438 } 00439 } 00440 00441 //EOF. 00442 if (!getline(inputFile, inputLine)) { 00443 throw ifstream::failure("Unexpected input!"); 00444 } 00445 trim(inputLine); 00446 if (inputLine != TAG_EOF) { 00447 throw ifstream::failure("Unexpected input!"); 00448 } 00449 } 00450 else if (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_ATT) { 00451 //NODE_COORD_SECTION. 00452 if (!getline(inputFile, inputLine)) { 00453 throw ifstream::failure("Unexpected input!"); 00454 } 00455 trim(inputLine); 00456 if (inputLine != TAG_NODE_COORD_SECTION) { 00457 throw ifstream::failure("Unexpected input!"); 00458 } 00459 00460 vector<Point> points(n); 00461 for (vector<Point>::size_type i = 0; i < n; i++) { 00462 if (!getline(inputFile, inputLine)) { 00463 throw ifstream::failure("Unexpected input!"); 00464 } 00465 trim(inputLine); 00466 istringstream pointIStringstream(inputLine); 00467 pointIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00468 vector<Point>::size_type j; 00469 pointIStringstream >> j; 00470 if (j != i + 1) { 00471 throw ifstream::failure("Unexpected input!"); 00472 } 00473 Point point; 00474 pointIStringstream >> point.x; 00475 pointIStringstream >> point.y; 00476 if (!nIStringstream.eof()) { 00477 throw ifstream::failure("Unexpected input!"); 00478 } 00479 00480 points.at(i) = point; 00481 } 00482 00483 transformInstance = new TransformInstance( 00484 type, 00485 name, 00486 XML_VALUE_SOURCE_TSPLIB, 00487 comment, 00488 n); 00489 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00490 for (vector<double>::size_type j = 0; j < i; j++) { 00491 double deltaX = points.at(i).x - points.at(j).x; 00492 double deltaY = points.at(i).y - points.at(j).y; 00493 double r = deltaX * deltaX + deltaY * deltaY; 00494 if (r == numeric_limits<double>::infinity()) { 00495 throw range_error("The range error occurs!"); 00496 } 00497 r /= 10.0; 00498 r = sqrt(r); 00499 double t = floor(r + 0.5); 00500 double distance; 00501 if (t < r) { 00502 distance = t + 1; 00503 } 00504 else { 00505 distance = t; 00506 } 00507 00508 transformInstance->setAdjacencyMatrixElement(i, j, distance); 00509 } 00510 } 00511 00512 //EOF. 00513 if (!getline(inputFile, inputLine)) { 00514 throw ifstream::failure("Unexpected input!"); 00515 } 00516 trim(inputLine); 00517 if (inputLine != TAG_EOF) { 00518 throw ifstream::failure("Unexpected input!"); 00519 } 00520 } 00521 else if (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_EXPLICIT) { 00522 //EDGE_WEIGHT_FORMAT. 00523 if (!getline(inputFile, inputLine)) { 00524 throw ifstream::failure("Unexpected input!"); 00525 } 00526 trim(inputLine); 00527 if (inputLine.substr(0, TAG_EDGE_WEIGHT_FORMAT.length()) != TAG_EDGE_WEIGHT_FORMAT) { 00528 throw ifstream::failure("Unexpected input!"); 00529 } 00530 inputLine.erase(0, TAG_EDGE_WEIGHT_FORMAT.length()); 00531 trim(inputLine); 00532 const string edgeWeightFormat = inputLine; 00533 if (edgeWeightFormat == VALUE_EDGE_WEIGHT_FORMAT_FULL_MATRIX) { 00534 if (!getline(inputFile, inputLine)) { 00535 throw ifstream::failure("Unexpected input!"); 00536 } 00537 trim(inputLine); 00538 if (inputLine == EDGE_WEIGHT_SECTION) { 00539 //EDGE_WEIGHT_SECTION. 00540 transformInstance = new TransformInstance( 00541 type, 00542 name, 00543 XML_VALUE_SOURCE_TSPLIB, 00544 comment, 00545 n); 00546 00547 inputLine = ""; 00548 while (true) { 00549 string partOfMatrixInputLine; 00550 if (!getline(inputFile, partOfMatrixInputLine)) { 00551 throw ifstream::failure("Unexpected input!"); 00552 } 00553 trim(partOfMatrixInputLine); 00554 if (partOfMatrixInputLine == TAG_EOF) { 00555 break; 00556 } 00557 inputLine.append(" "); 00558 inputLine.append(partOfMatrixInputLine); 00559 } 00560 trim(inputLine); 00561 istringstream rowIStringstream(inputLine); 00562 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00563 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00564 double cost; 00565 for (vector<double>::size_type j = 0; j < i; j++) { 00566 rowIStringstream >> cost; 00567 if ( 00568 abs(transformInstance->getAdjacencyMatrixElement(i, j) - cost) > 00569 TRANSFORM_DOUBLE_ZERO) { 00570 throw ifstream::failure("Unexpected input!"); 00571 } 00572 } 00573 rowIStringstream >> cost; 00574 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00575 throw ifstream::failure("Unexpected input!"); 00576 } 00577 for (vector<double>::size_type j = i + 1; j < n; j++) { 00578 rowIStringstream >> cost; 00579 transformInstance->setAdjacencyMatrixElement(j, i, cost); 00580 } 00581 } 00582 if (!rowIStringstream.eof()) { 00583 throw ifstream::failure("Unexpected input!"); 00584 } 00585 } 00586 else { 00587 //DISPLAY_DATA_TYPE. 00588 if (inputLine.substr(0, TAG_DISPLAY_DATA_TYPE.length()) != 00589 TAG_DISPLAY_DATA_TYPE) { 00590 throw ifstream::failure("Unexpected input!"); 00591 } 00592 inputLine.erase(0, TAG_DISPLAY_DATA_TYPE.length()); 00593 trim(inputLine); 00594 const string displayDataType = inputLine; 00595 if (displayDataType == VALUE_DISPLAY_DATA_TYPE_TWOD_DISPLAY) { 00596 //EDGE_WEIGHT_SECTION. 00597 if (!getline(inputFile, inputLine)) { 00598 throw ifstream::failure("Unexpected input!"); 00599 } 00600 trim(inputLine); 00601 if (inputLine != EDGE_WEIGHT_SECTION) { 00602 throw ifstream::failure("Unexpected input!"); 00603 } 00604 00605 transformInstance = new TransformInstance( 00606 type, 00607 name, 00608 XML_VALUE_SOURCE_TSPLIB, 00609 comment, 00610 n); 00611 00612 inputLine = ""; 00613 while (true) { 00614 string partOfMatrixInputLine; 00615 if (!getline(inputFile, partOfMatrixInputLine)) { 00616 throw ifstream::failure("Unexpected input!"); 00617 } 00618 trim(partOfMatrixInputLine); 00619 if (partOfMatrixInputLine == DISPLAY_DATA_SECTION) { 00620 break; 00621 } 00622 inputLine.append(" "); 00623 inputLine.append(partOfMatrixInputLine); 00624 } 00625 trim(inputLine); 00626 istringstream rowIStringstream(inputLine); 00627 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00628 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00629 double cost; 00630 for (vector<double>::size_type j = 0; j < i; j++) { 00631 rowIStringstream >> cost; 00632 if ( 00633 abs(transformInstance->getAdjacencyMatrixElement(i, j) - cost) > 00634 TRANSFORM_DOUBLE_ZERO) { 00635 throw ifstream::failure("Unexpected input!"); 00636 } 00637 } 00638 rowIStringstream >> cost; 00639 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00640 throw ifstream::failure("Unexpected input!"); 00641 } 00642 for (vector<double>::size_type j = i + 1; j < n; j++) { 00643 rowIStringstream >> cost; 00644 transformInstance->setAdjacencyMatrixElement(j, i, cost); 00645 } 00646 } 00647 if (!rowIStringstream.eof()) { 00648 throw ifstream::failure("Unexpected input!"); 00649 } 00650 00651 //DISPLAY_DATA_SECTION. 00652 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00653 if (!getline(inputFile, inputLine)) { 00654 throw ifstream::failure("Unexpected input!"); 00655 } 00656 } 00657 00658 //EOF. 00659 if (!getline(inputFile, inputLine)) { 00660 throw ifstream::failure("Unexpected input!"); 00661 } 00662 trim(inputLine); 00663 if (inputLine != TAG_EOF) { 00664 throw ifstream::failure("Unexpected input!"); 00665 } 00666 } 00667 else { 00668 throw ifstream::failure("Unexpected input!"); 00669 } 00670 } 00671 } 00672 else if (edgeWeightFormat == VALUE_EDGE_WEIGHT_FORMAT_LOWER_DIAG_ROW) { 00673 if (!getline(inputFile, inputLine)) { 00674 throw ifstream::failure("Unexpected input!"); 00675 } 00676 trim(inputLine); 00677 if (inputLine == EDGE_WEIGHT_SECTION) { 00678 //EDGE_WEIGHT_SECTION. 00679 transformInstance = new TransformInstance( 00680 type, 00681 name, 00682 XML_VALUE_SOURCE_TSPLIB, 00683 comment, 00684 n); 00685 00686 inputLine = ""; 00687 while (true) { 00688 string partOfMatrixInputLine; 00689 if (!getline(inputFile, partOfMatrixInputLine)) { 00690 throw ifstream::failure("Unexpected input!"); 00691 } 00692 trim(partOfMatrixInputLine); 00693 if (partOfMatrixInputLine == TAG_EOF) { 00694 break; 00695 } 00696 inputLine.append(" "); 00697 inputLine.append(partOfMatrixInputLine); 00698 } 00699 trim(inputLine); 00700 istringstream rowIStringstream(inputLine); 00701 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00702 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00703 double cost; 00704 for (vector<double>::size_type j = 0; j < i; j++) { 00705 rowIStringstream >> cost; 00706 transformInstance->setAdjacencyMatrixElement(i, j, cost); 00707 } 00708 rowIStringstream >> cost; 00709 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00710 throw ifstream::failure("Unexpected input!"); 00711 } 00712 } 00713 if (!rowIStringstream.eof()) { 00714 throw ifstream::failure("Unexpected input!"); 00715 } 00716 } 00717 else { 00718 //DISPLAY_DATA_TYPE. 00719 if (inputLine.substr(0, TAG_DISPLAY_DATA_TYPE.length()) != 00720 TAG_DISPLAY_DATA_TYPE) { 00721 throw ifstream::failure("Unexpected input!"); 00722 } 00723 inputLine.erase(0, TAG_DISPLAY_DATA_TYPE.length()); 00724 trim(inputLine); 00725 const string displayDataType = inputLine; 00726 if (displayDataType == VALUE_DISPLAY_DATA_TYPE_TWOD_DISPLAY) { 00727 //EDGE_WEIGHT_SECTION. 00728 if (!getline(inputFile, inputLine)) { 00729 throw ifstream::failure("Unexpected input!"); 00730 } 00731 trim(inputLine); 00732 if (inputLine != EDGE_WEIGHT_SECTION) { 00733 throw ifstream::failure("Unexpected input!"); 00734 } 00735 00736 transformInstance = new TransformInstance( 00737 type, 00738 name, 00739 XML_VALUE_SOURCE_TSPLIB, 00740 comment, 00741 n); 00742 00743 inputLine = ""; 00744 while (true) { 00745 string partOfMatrixInputLine; 00746 if (!getline(inputFile, partOfMatrixInputLine)) { 00747 throw ifstream::failure("Unexpected input!"); 00748 } 00749 trim(partOfMatrixInputLine); 00750 if (partOfMatrixInputLine == DISPLAY_DATA_SECTION) { 00751 break; 00752 } 00753 inputLine.append(" "); 00754 inputLine.append(partOfMatrixInputLine); 00755 } 00756 trim(inputLine); 00757 istringstream rowIStringstream(inputLine); 00758 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00759 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00760 double cost; 00761 for (vector<double>::size_type j = 0; j < i; j++) { 00762 rowIStringstream >> cost; 00763 transformInstance->setAdjacencyMatrixElement(i, j, cost); 00764 } 00765 rowIStringstream >> cost; 00766 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00767 throw ifstream::failure("Unexpected input!"); 00768 } 00769 } 00770 if (!rowIStringstream.eof()) { 00771 throw ifstream::failure("Unexpected input!"); 00772 } 00773 00774 //DISPLAY_DATA_SECTION. 00775 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00776 if (!getline(inputFile, inputLine)) { 00777 throw ifstream::failure("Unexpected input!"); 00778 } 00779 } 00780 00781 //EOF. 00782 if (!getline(inputFile, inputLine)) { 00783 throw ifstream::failure("Unexpected input!"); 00784 } 00785 trim(inputLine); 00786 if (inputLine != TAG_EOF) { 00787 throw ifstream::failure("Unexpected input!"); 00788 } 00789 } 00790 else { 00791 throw ifstream::failure("Unexpected input!"); 00792 } 00793 } 00794 } 00795 else if (edgeWeightFormat == VALUE_EDGE_WEIGHT_FORMAT_UPPER_DIAG_ROW) { 00796 if (!getline(inputFile, inputLine)) { 00797 throw ifstream::failure("Unexpected input!"); 00798 } 00799 trim(inputLine); 00800 if (inputLine == EDGE_WEIGHT_SECTION) { 00801 //EDGE_WEIGHT_SECTION. 00802 transformInstance = new TransformInstance( 00803 type, 00804 name, 00805 XML_VALUE_SOURCE_TSPLIB, 00806 comment, 00807 n); 00808 00809 inputLine = ""; 00810 while (true) { 00811 string partOfMatrixInputLine; 00812 if (!getline(inputFile, partOfMatrixInputLine)) { 00813 throw ifstream::failure("Unexpected input!"); 00814 } 00815 trim(partOfMatrixInputLine); 00816 if (partOfMatrixInputLine == TAG_EOF) { 00817 break; 00818 } 00819 inputLine.append(" "); 00820 inputLine.append(partOfMatrixInputLine); 00821 } 00822 trim(inputLine); 00823 istringstream rowIStringstream(inputLine); 00824 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00825 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00826 double cost; 00827 rowIStringstream >> cost; 00828 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00829 throw ifstream::failure("Unexpected input!"); 00830 } 00831 for (vector<double>::size_type j = i + 1; j < n; j++) { 00832 rowIStringstream >> cost; 00833 transformInstance->setAdjacencyMatrixElement(j, i, cost); 00834 } 00835 } 00836 if (!rowIStringstream.eof()) { 00837 throw ifstream::failure("Unexpected input!"); 00838 } 00839 } 00840 else { 00841 //DISPLAY_DATA_TYPE. 00842 if (inputLine.substr(0, TAG_DISPLAY_DATA_TYPE.length()) != 00843 TAG_DISPLAY_DATA_TYPE) { 00844 throw ifstream::failure("Unexpected input!"); 00845 } 00846 inputLine.erase(0, TAG_DISPLAY_DATA_TYPE.length()); 00847 trim(inputLine); 00848 const string displayDataType = inputLine; 00849 if (displayDataType == VALUE_DISPLAY_DATA_TYPE_TWOD_DISPLAY) { 00850 //EDGE_WEIGHT_SECTION. 00851 if (!getline(inputFile, inputLine)) { 00852 throw ifstream::failure("Unexpected input!"); 00853 } 00854 trim(inputLine); 00855 if (inputLine != EDGE_WEIGHT_SECTION) { 00856 throw ifstream::failure("Unexpected input!"); 00857 } 00858 00859 transformInstance = new TransformInstance( 00860 type, 00861 name, 00862 XML_VALUE_SOURCE_TSPLIB, 00863 comment, 00864 n); 00865 00866 inputLine = ""; 00867 while (true) { 00868 string partOfMatrixInputLine; 00869 if (!getline(inputFile, partOfMatrixInputLine)) { 00870 throw ifstream::failure("Unexpected input!"); 00871 } 00872 trim(partOfMatrixInputLine); 00873 if (partOfMatrixInputLine == DISPLAY_DATA_SECTION) { 00874 break; 00875 } 00876 inputLine.append(" "); 00877 inputLine.append(partOfMatrixInputLine); 00878 } 00879 trim(inputLine); 00880 istringstream rowIStringstream(inputLine); 00881 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00882 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00883 double cost; 00884 rowIStringstream >> cost; 00885 if (abs(cost) > TRANSFORM_DOUBLE_ZERO) { 00886 throw ifstream::failure("Unexpected input!"); 00887 } 00888 for (vector<double>::size_type j = i + 1; j < n; j++) { 00889 rowIStringstream >> cost; 00890 transformInstance->setAdjacencyMatrixElement(j, i, cost); 00891 } 00892 } 00893 if (!rowIStringstream.eof()) { 00894 throw ifstream::failure("Unexpected input!"); 00895 } 00896 00897 //DISPLAY_DATA_SECTION. 00898 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00899 if (!getline(inputFile, inputLine)) { 00900 throw ifstream::failure("Unexpected input!"); 00901 } 00902 } 00903 00904 //EOF. 00905 if (!getline(inputFile, inputLine)) { 00906 throw ifstream::failure("Unexpected input!"); 00907 } 00908 trim(inputLine); 00909 if (inputLine != TAG_EOF) { 00910 throw ifstream::failure("Unexpected input!"); 00911 } 00912 } 00913 else { 00914 throw ifstream::failure("Unexpected input!"); 00915 } 00916 } 00917 } 00918 else if (edgeWeightFormat == VALUE_EDGE_WEIGHT_FORMAT_UPPER_ROW) { 00919 if (!getline(inputFile, inputLine)) { 00920 throw ifstream::failure("Unexpected input!"); 00921 } 00922 trim(inputLine); 00923 if (inputLine == EDGE_WEIGHT_SECTION) { 00924 //EDGE_WEIGHT_SECTION. 00925 transformInstance = new TransformInstance( 00926 type, 00927 name, 00928 XML_VALUE_SOURCE_TSPLIB, 00929 comment, 00930 n); 00931 00932 inputLine = ""; 00933 while (true) { 00934 string partOfMatrixInputLine; 00935 if (!getline(inputFile, partOfMatrixInputLine)) { 00936 throw ifstream::failure("Unexpected input!"); 00937 } 00938 trim(partOfMatrixInputLine); 00939 if (partOfMatrixInputLine == TAG_EOF) { 00940 break; 00941 } 00942 inputLine.append(" "); 00943 inputLine.append(partOfMatrixInputLine); 00944 } 00945 trim(inputLine); 00946 istringstream rowIStringstream(inputLine); 00947 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00948 for (vector<vector<double> >::size_type i = 0; i < n - 1; i++) { 00949 double cost; 00950 for (vector<double>::size_type j = i + 1; j < n; j++) { 00951 rowIStringstream >> cost; 00952 transformInstance->setAdjacencyMatrixElement(j, i, cost); 00953 } 00954 } 00955 if (!rowIStringstream.eof()) { 00956 throw ifstream::failure("Unexpected input!"); 00957 } 00958 } 00959 else { 00960 //DISPLAY_DATA_TYPE. 00961 if (inputLine.substr(0, TAG_DISPLAY_DATA_TYPE.length()) != 00962 TAG_DISPLAY_DATA_TYPE) { 00963 throw ifstream::failure("Unexpected input!"); 00964 } 00965 inputLine.erase(0, TAG_DISPLAY_DATA_TYPE.length()); 00966 trim(inputLine); 00967 const string displayDataType = inputLine; 00968 if (displayDataType == VALUE_DISPLAY_DATA_TYPE_TWOD_DISPLAY) { 00969 //EDGE_WEIGHT_SECTION. 00970 if (!getline(inputFile, inputLine)) { 00971 throw ifstream::failure("Unexpected input!"); 00972 } 00973 trim(inputLine); 00974 if (inputLine != EDGE_WEIGHT_SECTION) { 00975 throw ifstream::failure("Unexpected input!"); 00976 } 00977 00978 transformInstance = new TransformInstance( 00979 type, 00980 name, 00981 XML_VALUE_SOURCE_TSPLIB, 00982 comment, 00983 n); 00984 00985 inputLine = ""; 00986 while (true) { 00987 string partOfMatrixInputLine; 00988 if (!getline(inputFile, partOfMatrixInputLine)) { 00989 throw ifstream::failure("Unexpected input!"); 00990 } 00991 trim(partOfMatrixInputLine); 00992 if (partOfMatrixInputLine == DISPLAY_DATA_SECTION) { 00993 break; 00994 } 00995 inputLine.append(" "); 00996 inputLine.append(partOfMatrixInputLine); 00997 } 00998 trim(inputLine); 00999 istringstream rowIStringstream(inputLine); 01000 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 01001 for (vector<vector<double> >::size_type i = 0; i < n - 1; i++) { 01002 double cost; 01003 for (vector<double>::size_type j = i + 1; j < n; j++) { 01004 rowIStringstream >> cost; 01005 transformInstance->setAdjacencyMatrixElement(j, i, cost); 01006 } 01007 } 01008 if (!rowIStringstream.eof()) { 01009 throw ifstream::failure("Unexpected input!"); 01010 } 01011 01012 //DISPLAY_DATA_SECTION. 01013 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 01014 if (!getline(inputFile, inputLine)) { 01015 throw ifstream::failure("Unexpected input!"); 01016 } 01017 } 01018 01019 //EOF. 01020 if (!getline(inputFile, inputLine)) { 01021 throw ifstream::failure("Unexpected input!"); 01022 } 01023 trim(inputLine); 01024 if (inputLine != TAG_EOF) { 01025 throw ifstream::failure("Unexpected input!"); 01026 } 01027 } 01028 else { 01029 throw ifstream::failure("Unexpected input!"); 01030 } 01031 } 01032 } 01033 else { 01034 throw ifstream::failure("Unexpected input!"); 01035 } 01036 } 01037 else { 01038 throw InputFileFormatNotSupported(); 01039 } 01040 01041 //Rest. 01042 inputFile.exceptions(ifstream::badbit); 01043 while (!inputFile.eof()) { 01044 getline(inputFile, inputLine); 01045 trim(inputLine); 01046 if (inputLine != "") { 01047 throw ifstream::failure("Unexpected input!"); 01048 } 01049 } 01050 01051 //The main diagonal must be set. 01052 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 01053 transformInstance->setAdjacencyMatrixElement( 01054 i, 01055 static_cast<vector<double>::size_type>(i), 01056 0); 01057 } 01058 } 01059 else if ((!symmetricTravellingSalesmanProblem) && (inputLine == VALUE_TYPE_ATSP)) { 01060 type = VALUE_TYPE_ATSP; 01061 01062 //COMMENT. 01063 if (!getline(inputFile, inputLine)) { 01064 throw ifstream::failure("Unexpected input!"); 01065 } 01066 trim(inputLine); 01067 if (inputLine.substr(0, TAG_COMMENT.length()) != TAG_COMMENT) { 01068 throw ifstream::failure("Unexpected input!"); 01069 } 01070 inputLine.erase(0, TAG_COMMENT.length()); 01071 trim(inputLine); 01072 const string comment = inputLine; 01073 01074 //DIMENSION. 01075 if (!getline(inputFile, inputLine)) { 01076 throw ifstream::failure("Unexpected input!"); 01077 } 01078 trim(inputLine); 01079 if (inputLine.substr(0, TAG_DIMENSION.length()) != TAG_DIMENSION) { 01080 throw ifstream::failure("Unexpected input!"); 01081 } 01082 inputLine.erase(0, TAG_DIMENSION.length()); 01083 trim(inputLine); 01084 vector<vector<double> >::size_type n; 01085 istringstream nIStringstream(inputLine); 01086 nIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 01087 nIStringstream >> n; 01088 if (!nIStringstream.eof()) { 01089 throw ifstream::failure("Unexpected input!"); 01090 } 01091 01092 //EDGE_WEIGHT_TYPE. 01093 if (!getline(inputFile, inputLine)) { 01094 throw ifstream::failure("Unexpected input!"); 01095 } 01096 trim(inputLine); 01097 if (inputLine.substr(0, TAG_EDGE_WEIGHT_TYPE.length()) != TAG_EDGE_WEIGHT_TYPE) { 01098 throw ifstream::failure("Unexpected input!"); 01099 } 01100 inputLine.erase(0, TAG_EDGE_WEIGHT_TYPE.length()); 01101 trim(inputLine); 01102 const string edgeWeightType = inputLine; 01103 if (edgeWeightType == VALUE_EDGE_WEIGHT_TYPE_EXPLICIT) { 01104 //EDGE_WEIGHT_FORMAT. 01105 if (!getline(inputFile, inputLine)) { 01106 throw ifstream::failure("Unexpected input!"); 01107 } 01108 trim(inputLine); 01109 if (inputLine.substr(0, TAG_EDGE_WEIGHT_FORMAT.length()) != TAG_EDGE_WEIGHT_FORMAT) { 01110 throw ifstream::failure("Unexpected input!"); 01111 } 01112 inputLine.erase(0, TAG_EDGE_WEIGHT_FORMAT.length()); 01113 trim(inputLine); 01114 const string edgeWeightFormat = inputLine; 01115 if (edgeWeightFormat == VALUE_EDGE_WEIGHT_FORMAT_FULL_MATRIX) { 01116 //EDGE_WEIGHT_SECTION. 01117 if (!getline(inputFile, inputLine)) { 01118 throw ifstream::failure("Unexpected input!"); 01119 } 01120 trim(inputLine); 01121 if (inputLine != EDGE_WEIGHT_SECTION) { 01122 throw ifstream::failure("Unexpected input!"); 01123 } 01124 01125 transformInstance = new TransformInstance( 01126 type, 01127 name, 01128 XML_VALUE_SOURCE_TSPLIB, 01129 comment, 01130 n); 01131 01132 inputLine = ""; 01133 while (true) { 01134 string partOfMatrixInputLine; 01135 if (!getline(inputFile, partOfMatrixInputLine)) { 01136 throw ifstream::failure("Unexpected input!"); 01137 } 01138 trim(partOfMatrixInputLine); 01139 if (partOfMatrixInputLine == TAG_EOF) { 01140 break; 01141 } 01142 inputLine.append(" "); 01143 inputLine.append(partOfMatrixInputLine); 01144 } 01145 trim(inputLine); 01146 istringstream rowIStringstream(inputLine); 01147 rowIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 01148 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 01149 double cost; 01150 for (vector<double>::size_type j = 0; j < n; j++) { 01151 rowIStringstream >> cost; 01152 transformInstance->setAdjacencyMatrixElement(i, j, cost); 01153 } 01154 } 01155 if (!rowIStringstream.eof()) { 01156 throw ifstream::failure("Unexpected input!"); 01157 } 01158 } 01159 else { 01160 throw ifstream::failure("Unexpected input!"); 01161 } 01162 } 01163 else { 01164 throw ifstream::failure("Unexpected input!"); 01165 } 01166 01167 //Rest. 01168 inputFile.exceptions(ifstream::badbit); 01169 while (!inputFile.eof()) { 01170 getline(inputFile, inputLine); 01171 trim(inputLine); 01172 if (inputLine != "") { 01173 throw ifstream::failure("Unexpected input!"); 01174 } 01175 } 01176 } 01177 else { 01178 throw ifstream::failure("Unexpected input!"); 01179 } 01180 01181 inputFile.close(); 01182 } 01183 catch (InputFileFormatNotSupported &e) { 01184 inputFile.close(); 01185 delete transformInstance; 01186 throw; 01187 } 01188 catch (ifstream::failure &e) { 01189 if (inputFile.is_open()) { 01190 inputFile.close(); 01191 } 01192 if (transformInstance != 0) { 01193 delete transformInstance; 01194 } 01195 throw; 01196 } 01197 catch (bad_alloc &e) { 01198 if (inputFile.is_open()) { 01199 inputFile.close(); 01200 } 01201 throw; 01202 } 01203 catch (range_error &e) { 01204 if (inputFile.is_open()) { 01205 inputFile.close(); 01206 } 01207 if (transformInstance != 0) { 01208 delete transformInstance; 01209 } 01210 throw; 01211 } 01212 catch (...) { 01213 if (inputFile.is_open()) { 01214 inputFile.close(); 01215 } 01216 if (transformInstance != 0) { 01217 delete transformInstance; 01218 } 01219 throw; 01220 } 01221 01222 return (transformInstance); 01223 } 01224 01228 class XMLStringTranscode { 01229 private: 01233 XMLCh *stringUnicodeForm; 01234 public: 01239 inline XMLStringTranscode(const string toTranscode) { 01240 // Call the private transcoding method 01241 stringUnicodeForm = XMLString::transcode(toTranscode.c_str()); 01242 } 01243 01247 inline ~XMLStringTranscode() { 01248 XMLString::release(&stringUnicodeForm); 01249 } 01250 01255 inline const XMLCh *unicodeForm() const { 01256 return (stringUnicodeForm); 01257 } 01258 }; 01259 #define unicodeForm(str) XMLStringTranscode(str).unicodeForm() 01260 01261 void writeOutputFile(const string &outputFileName, const TransformInstance *transformInstance) { 01262 try { 01263 XMLPlatformUtils::Initialize(); 01264 } 01265 catch (const XMLException &e) { 01266 throw; 01267 } 01268 01269 { 01270 DOMImplementation *dOMImplementation = 01271 DOMImplementationRegistry::getDOMImplementation(unicodeForm("Core")); 01272 if (dOMImplementation != 0) { 01273 try { 01274 /* 01275 * Creating the DOM structure. 01276 */ 01277 DOMDocument *document = dOMImplementation->createDocument( 01278 0, // root element namespace URI. 01279 unicodeForm(XML_DOCUMENT_NODE), // root element name 01280 0); // document type object (DTD). 01281 01282 DOMElement *documentElement = document->getDocumentElement(); 01283 01284 DOMElement *nameElement = document->createElement(unicodeForm(XML_NAME)); 01285 documentElement->appendChild(nameElement); 01286 DOMText *nameEntry = document-> 01287 createTextNode(unicodeForm(transformInstance->getName())); 01288 nameElement->appendChild(nameEntry); 01289 01290 DOMElement *sourceElement = document->createElement(unicodeForm(XML_SOURCE)); 01291 documentElement->appendChild(sourceElement); 01292 DOMText *sourceEntry = document-> 01293 createTextNode(unicodeForm(transformInstance->getSource())); 01294 sourceElement->appendChild(sourceEntry); 01295 01296 DOMElement *descriptionElement = document->createElement(unicodeForm(XML_DESCRIPTION)); 01297 documentElement->appendChild(descriptionElement); 01298 DOMText *descriptionEntry = document-> 01299 createTextNode(unicodeForm(transformInstance->getDescription())); 01300 descriptionElement->appendChild(descriptionEntry); 01301 01302 DOMElement *doublePrecisionElement = 01303 document->createElement(unicodeForm(XML_DOUBLE_PRECISION)); 01304 documentElement->appendChild(doublePrecisionElement); 01305 stringstream stringstreamDoublePrecision; 01306 stringstreamDoublePrecision.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01307 stringstreamDoublePrecision << 01308 setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01309 stringstreamDoublePrecision << TRANSFORM_DOUBLE_PRECISION; 01310 DOMText *doublePrecisionEntry = document-> 01311 createTextNode(unicodeForm(stringstreamDoublePrecision.str())); 01312 doublePrecisionElement->appendChild(doublePrecisionEntry); 01313 01314 DOMElement *ignoredDigitsElement = 01315 document->createElement(unicodeForm(XML_IGNORED_DIGITS)); 01316 documentElement->appendChild(ignoredDigitsElement); 01317 stringstream stringstreamIgnoredDigits; 01318 stringstreamIgnoredDigits.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01319 stringstreamIgnoredDigits << 01320 setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01321 stringstreamIgnoredDigits << TRANSFORM_IGNORED_DIGITS; 01322 DOMText *ignoredDigitsEntry = document-> 01323 createTextNode(unicodeForm(stringstreamIgnoredDigits.str())); 01324 ignoredDigitsElement->appendChild(ignoredDigitsEntry); 01325 01326 DOMElement *graphElement = document->createElement(unicodeForm(XML_GRAPH)); 01327 documentElement->appendChild(graphElement); 01328 for ( 01329 vector<vector<double> >::size_type i = 0; 01330 i < transformInstance->getN(); 01331 i++) { 01332 DOMElement *vertexElement = document->createElement(unicodeForm(XML_VERTEX)); 01333 graphElement->appendChild(vertexElement); 01334 for ( 01335 vector<double>::size_type j = 0; 01336 j < i; 01337 j++) { 01338 DOMElement *edgeElement = document->createElement(unicodeForm(XML_EDGE)); 01339 vertexElement->appendChild(edgeElement); 01340 stringstream stringstreamCost; 01341 stringstreamCost.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01342 stringstreamCost << setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01343 stringstreamCost << transformInstance->getAdjacencyMatrixElement(i, j); 01344 edgeElement->setAttribute( 01345 unicodeForm(XML_EDGE_ATTRIBUTE_COST), 01346 unicodeForm(stringstreamCost.str())); 01347 stringstream stringstreamElementEntry; 01348 stringstreamElementEntry << j; 01349 DOMText *edgeElementEntry = document-> 01350 createTextNode(unicodeForm(stringstreamElementEntry.str())); 01351 edgeElement->appendChild(edgeElementEntry); 01352 } 01353 if (transformInstance->getType() == VALUE_TYPE_ATSP) { 01354 DOMElement *edgeElement = document->createElement(unicodeForm(XML_EDGE)); 01355 vertexElement->appendChild(edgeElement); 01356 stringstream stringstreamCost; 01357 stringstreamCost.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01358 stringstreamCost << setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01359 stringstreamCost << transformInstance-> 01360 getAdjacencyMatrixElement( 01361 i, 01362 static_cast<vector<double>::size_type >(i)); 01363 edgeElement->setAttribute( 01364 unicodeForm(XML_EDGE_ATTRIBUTE_COST), 01365 unicodeForm(stringstreamCost.str())); 01366 stringstream stringstreamElementEntry; 01367 stringstreamElementEntry << i; 01368 DOMText *edgeElementEntry = document-> 01369 createTextNode(unicodeForm(stringstreamElementEntry.str())); 01370 edgeElement->appendChild(edgeElementEntry); 01371 } 01372 for ( 01373 vector<double>::size_type j = i + 1; 01374 j < transformInstance->getN(); 01375 j++) { 01376 DOMElement *edgeElement = document->createElement(unicodeForm(XML_EDGE)); 01377 vertexElement->appendChild(edgeElement); 01378 stringstream stringstreamCost; 01379 stringstreamCost.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01380 stringstreamCost << setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01381 stringstreamCost << transformInstance->getAdjacencyMatrixElement(i, j); 01382 edgeElement->setAttribute( 01383 unicodeForm(XML_EDGE_ATTRIBUTE_COST), 01384 unicodeForm(stringstreamCost.str())); 01385 stringstream stringstreamElementEntry; 01386 stringstreamElementEntry << j; 01387 DOMText *edgeElementEntry = document-> 01388 createTextNode(unicodeForm(stringstreamElementEntry.str())); 01389 edgeElement->appendChild(edgeElementEntry); 01390 } 01391 } 01392 01393 01394 /* 01395 * Writing to the output file. 01396 */ 01397 XMLCh *outputEncoding = XMLString::transcode(XML_ENCODING.c_str()); 01398 const bool splitCDataSections = true; 01399 const bool discardDefaultContent = true; 01400 const bool formatPrettyPrint = XML_FORMAT_PRETTY_PRINT; 01401 const bool writeBOM = false; 01402 01403 DOMErrorHandler *myErrorHandler = 0; 01404 XMLFormatTarget *myFormTarget = 0; 01405 try 01406 { 01407 //Gets a serializer, an instance of DOMLSSerializer. 01408 XMLCh tempStr[3] = {chLatin_L, chLatin_S, chNull}; 01409 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr); 01410 DOMLSSerializer *theSerializer = ((DOMImplementationLS *) impl)-> 01411 createLSSerializer(); 01412 DOMLSOutput *theOutputDesc = ((DOMImplementationLS *) impl)-> 01413 createLSOutput(); 01414 01415 //Sets the output encoding. 01416 theOutputDesc->setEncoding(outputEncoding); 01417 01418 myErrorHandler = new TransformDOMErrorHandler(); 01419 DOMConfiguration *serializerConfig=theSerializer->getDomConfig(); 01420 serializerConfig->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler); 01421 01422 if (serializerConfig-> 01423 canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, splitCDataSections)) { 01424 serializerConfig-> 01425 setParameter(XMLUni::fgDOMWRTSplitCdataSections, splitCDataSections); 01426 } 01427 if (serializerConfig-> 01428 canSetParameter( 01429 XMLUni::fgDOMWRTDiscardDefaultContent, discardDefaultContent)) { 01430 serializerConfig-> 01431 setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, discardDefaultContent); 01432 } 01433 if (serializerConfig-> 01434 canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, formatPrettyPrint)) { 01435 serializerConfig-> 01436 setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, formatPrettyPrint); 01437 } 01438 if (serializerConfig-> 01439 canSetParameter(XMLUni::fgDOMWRTBOM, writeBOM)) { 01440 serializerConfig-> 01441 setParameter(XMLUni::fgDOMWRTBOM, writeBOM); 01442 } 01443 01444 const char* outputFileNameCString = outputFileName.c_str(); 01445 myFormTarget = new LocalFileFormatTarget(outputFileNameCString); 01446 theOutputDesc->setByteStream(myFormTarget); 01447 01448 theSerializer->write(document, theOutputDesc); 01449 01450 theOutputDesc->release(); 01451 theSerializer->release(); 01452 01453 delete myFormTarget; 01454 delete myErrorHandler; 01455 01456 } 01457 catch (const OutOfMemoryException &e) { 01458 XMLString::release(&outputEncoding); 01459 if (myFormTarget != 0) { 01460 delete myFormTarget; 01461 } 01462 if (myErrorHandler != 0) { 01463 delete myErrorHandler; 01464 } 01465 document->release(); 01466 throw; 01467 } 01468 catch (XMLException &e) { 01469 XMLString::release(&outputEncoding); 01470 if (myFormTarget != 0) { 01471 delete myFormTarget; 01472 } 01473 if (myErrorHandler != 0) { 01474 delete myErrorHandler; 01475 } 01476 document->release(); 01477 throw; 01478 } 01479 catch ( ... ) { 01480 XMLString::release(&outputEncoding); 01481 if (myFormTarget != 0) { 01482 delete myFormTarget; 01483 } 01484 if (myErrorHandler != 0) { 01485 delete myErrorHandler; 01486 } 01487 document->release(); 01488 throw; 01489 } 01490 01491 XMLString::release(&outputEncoding); 01492 01493 document->release(); 01494 } 01495 catch (bad_alloc &e) { 01496 XMLPlatformUtils::Terminate(); 01497 throw; 01498 } 01499 catch (const OutOfMemoryException &e) { 01500 XMLPlatformUtils::Terminate(); 01501 throw; 01502 } 01503 catch (const DOMException &e) { 01504 XMLPlatformUtils::Terminate(); 01505 throw; 01506 } 01507 catch (XMLException &e) { 01508 XMLPlatformUtils::Terminate(); 01509 throw; 01510 } 01511 catch ( ... ) { 01512 XMLPlatformUtils::Terminate(); 01513 throw; 01514 } 01515 } 01516 else { 01517 throw DOMException(); 01518 } 01519 } 01520 01521 XMLPlatformUtils::Terminate(); 01522 } 01523 01524 01525 void writeOutputFileWithoutUsingAParser( 01526 const std::string &outputFileName, 01527 const TransformInstance *transformInstance) { 01528 ofstream outputFile; 01529 outputFile.exceptions(ifstream::failbit | ifstream::badbit); 01530 try { 01531 outputFile.open(outputFileName.c_str()); 01532 01533 outputFile.setf(DOUBLE_FLOATFIELD, ios::floatfield); 01534 outputFile << 01535 setprecision(static_cast<int>(TRANSFORM_DOUBLE_PRECISION)); 01536 01537 outputFile << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" << endl; 01538 outputFile << "<travellingSalesmanProblemInstance>" << endl; 01539 outputFile << endl; 01540 outputFile << " <name>" << transformInstance->getName() << "</name>" << endl; 01541 outputFile << endl; 01542 outputFile << " <source>TSPLIB</source>" << endl; 01543 outputFile << endl; 01544 outputFile << 01545 " <description>" << 01546 transformInstance->getDescription() << 01547 "</description>" << 01548 endl; 01549 outputFile << endl; 01550 outputFile << 01551 " <doublePrecision>" << 01552 TRANSFORM_DOUBLE_PRECISION << 01553 "</doublePrecision>" << 01554 endl; 01555 outputFile << endl; 01556 outputFile << " <ignoredDigits>" << TRANSFORM_IGNORED_DIGITS << "</ignoredDigits>" << endl; 01557 outputFile << endl; 01558 outputFile << " <graph>" << endl; 01559 01560 for ( 01561 vector<vector<double> >::size_type i = 0; 01562 i < transformInstance->getN(); 01563 i++) { 01564 outputFile << " <vertex>" << endl; 01565 for ( 01566 vector<double>::size_type j = 0; 01567 j < i; 01568 j++) { 01569 outputFile << 01570 " <edge cost=\"" << 01571 transformInstance->getAdjacencyMatrixElement(i, j) << 01572 "\">" << 01573 j << 01574 "</edge>"<< 01575 endl; 01576 } 01577 if (transformInstance->getType() == VALUE_TYPE_ATSP) { 01578 outputFile << 01579 " <edge cost=\"" << 01580 transformInstance->getAdjacencyMatrixElement( 01581 i, 01582 static_cast<vector<double>::size_type>(i)) << 01583 "\">" << 01584 static_cast<vector<double>::size_type>(i) << 01585 "</edge>"<< 01586 endl; 01587 } 01588 for ( 01589 std::vector<double>::size_type j = i + 1; 01590 j < transformInstance->getN(); 01591 j++) { 01592 outputFile << 01593 " <edge cost=\"" << 01594 transformInstance->getAdjacencyMatrixElement(i, j) << 01595 "\">" << 01596 j << 01597 "</edge>"<< 01598 endl; 01599 } 01600 outputFile << " </vertex>" << endl; 01601 } 01602 01603 outputFile << " </graph>" << endl; 01604 outputFile << endl; 01605 outputFile << "</travellingSalesmanProblemInstance>" << endl; 01606 01607 outputFile.close(); 01608 } 01609 catch (ofstream::failure &e) { 01610 if (outputFile.is_open()) { 01611 outputFile.close(); 01612 } 01613 throw; 01614 } 01615 catch (...) { 01616 if (outputFile.is_open()) { 01617 outputFile.close(); 01618 } 01619 throw; 01620 } 01621 }