TSPSearch.cpp
Go to the documentation of this file.
00001 
00002 /*
00003  * MDPSearch.cpp
00004  *
00005  *  Created on: 20.01.2011
00006  *      Author: hess
00007  */
00008 #include <coverage_3d_planning/TSPSearch.h>
00009 #include <ros/ros.h>
00010 #include <visualization_msgs/Marker.h>
00011 #include <sys/syscall.h>
00012 #include <sys/sysctl.h>
00013 //c++
00014 #include <cmath>
00015 #include <limits>
00016 #include <vector>
00017 #include <algorithm>
00018 #include <ostream>
00019 #include <fstream>
00020 
00021 #include <limits>
00022 
00023 #include <Eigen/Core>
00024 
00025 //TSPSearch::TSPSearch() :
00026 //        file_tsp_result_("/home/hess/coverage.result"), file_tsp_problem_("/home/hess/coverage.tsp"), file_tsp_param_(
00027 //                "/home/hess/coverage.par") {
00028 //
00029 //}
00030 
00031 TSPSearch::TSPSearch() :
00032         file_tsp_result_("/tmp/coverage.result"), file_tsp_problem_("/tmp/coverage.tsp"), file_tsp_param_(
00033                 "/tmp/coverage.par") {
00034 
00035     std::time_t seconds;
00036     std::time(&seconds);
00037     std::srand((unsigned int) seconds);
00038 
00039     std::stringstream sstream;
00040     sstream << "/tmp/coverage_3d_" << std::rand() << std::rand();
00041     file_tsp_result_ = sstream.str();
00042     file_tsp_result_+=".result";
00043     file_tsp_problem_ = sstream.str();
00044     file_tsp_problem_ += ".tsp";
00045     file_tsp_param_ = sstream.str();
00046     file_tsp_param_ += ".par";
00047 }
00048 
00049 void TSPSearch::writeToTSPFile(const Eigen::MatrixXf& weights) {
00050 
00051     //write parameter file: .par
00052     //PROBLEM_FILE = tsp1.tsp
00053     //OUTPUT_TOUR_FILE = output.tsp
00054     //MOVE_TYPE = 5
00055     //PATCHING_C = 3
00056     //PATCHING_A = 2
00057     //RUNS = 1
00058     ofstream file;
00059     ROS_DEBUG_STREAM("TSPSearch::writeToTSPFile: TSP file" << file_tsp_param_);
00060     file.open(file_tsp_param_.c_str(), ios::out);
00061     file.precision(5);
00062     file << "PROBLEM_FILE = " << file_tsp_problem_ << std::endl;
00063     file << "OUTPUT_TOUR_FILE = " << file_tsp_result_ << std::endl;
00064     file << "MOVE_TYPE = 5" << std::endl;
00065     file << "PATCHING_C = 3" << std::endl;
00066     file << "PATCHING_A = 2" << std::endl;
00067     file << "PRECISION = 1" << std::endl;
00068     file << "RUNS = 1" << std::endl;
00069     file << "SEED = "<< rand() << std::endl;
00070     file.close();
00071 
00072     //write problem file: .tsp
00073     //NAME : tsp1
00074     //COMMENT : test problem
00075     //TYPE : ATSP
00076     //DIMENSION : 4
00077     //EDGE_WEIGHT_TYPE : EXPLICIT
00078     //EDGE_WEIGHT_FORMAT : FULL_MATRIX
00079     //EDGE_WEIGHT_SECTION :
00080     //0 9 8 5 1 0 8 6 9 1 0 4 1 2 3 0
00081     //EOF
00082 
00083 //      ROS_INFO_STREAM("filename out2 " << tsp_file.str().c_str());
00084     file.open(file_tsp_problem_.c_str(), ios::out);
00085     file.precision(5);
00086     file << "NAME : WIPE_TSP" << std::endl;
00087     file << "TYPE : ATSP" << std::endl;
00088     file << "DIMENSION : " << weights.rows() << std::endl;
00089     file << "EDGE_WEIGHT_TYPE : EXPLICIT" << std::endl;
00090     file << "EDGE_WEIGHT_FORMAT : FULL_MATRIX" << std::endl;
00091 //    file << "EDGE_DATA_FORMAT : ADJ_LIST" << std::endl;
00092 //
00093 //    file << "EDGE_DATA_SECTION : " << std::endl;
00094 //    //one more node for current tool position
00095 //    for (unsigned int i = 0; i < weights.rows(); i++) {
00096 //        file << i+1 << " ";
00097 //        for (unsigned int j = 0; j < weights.cols(); j++) {
00098 //            if (weights(i,j) > 0.00001){
00100 //                file << j+1 << " ";
00101 //            }
00102 //
00104 //        }
00105 //        if(i ==weights.rows()-1){
00106 //            file << "-1 -1" << endl;
00107 //        } else {
00108 //            file << "-1" << endl;
00109 //        }
00110 //
00111 //    }
00112 //    file << "-1" << endl;
00113 
00114     file << "EDGE_WEIGHT_SECTION : " << std::endl;
00115     //one more node for current tool position
00116     for (unsigned int i = 0; i < weights.rows(); i++) {
00117         for (unsigned int j = 0; j < weights.cols(); j++) {
00118 //            if (weights(i,j) < 0.000000001){
00119 //                file << 100 << " ";
00120 //            } else {
00121                         int tmp =static_cast<int>(weights(i, j)*100);
00122                 file << tmp << " ";
00123                 if (tmp==-2147483648){
00124                         std::cout<< "overflow "<<  weights(i, j) << " i " <<i << " j " <<j << std::endl;
00125                 }
00126 //            }
00127 
00128 //                      std::cout <<  static_cast<int>(edge_weights_[i][j] * 100) <<  " "  << edge_weights_[i][j] << std::endl;
00129         }
00130     }
00131     file << std::endl;
00132     file << "EOF";
00133     file.close();
00134 
00135 }
00136 bool TSPSearch::callTSPSolver() {
00137 
00138     stringstream command;
00139 //      command << lkh_bin_dir_ <<"LKH " <<  file_tsp_param_;
00140     command << "rosrun lkh_solver LKH " << file_tsp_param_;
00141     int result = system(command.str().c_str());
00142     ROS_INFO_STREAM("result " << result);
00143     return true;
00144 }
00145 
00146 void TSPSearch::tokenize(const string& str, vector<string>& tokens, const string& delimiters) {
00147     string::size_type last_pos = str.find_first_not_of(delimiters, 0);
00148     string::size_type pos = str.find_first_of(delimiters, last_pos);
00149 
00150     while (string::npos != pos || string::npos != last_pos) {
00151         tokens.push_back(str.substr(last_pos, pos - last_pos));
00152 
00153         last_pos = str.find_first_not_of(delimiters, pos);
00154         pos = str.find_first_of(delimiters, last_pos);
00155     }
00156 }
00157 
00158 void TSPSearch::readResultTSPFile(std::vector<unsigned int>& traj) {
00159 
00160     //NAME : tsp1.10.tour
00161     //COMMENT : Length = 10
00162     //COMMENT : Found by LKH [Keld Helsgaun]
00163     //TYPE : TOUR
00164     //DIMENSION : 4
00165     //TOUR_SECTION
00166     //1
00167     //4
00168     //3
00169     //2
00170     //-1
00171     //EOF
00172 //TODO:read in the file
00173 
00174     traj.clear();
00175     ifstream file(file_tsp_result_.c_str(), ios::in);
00176 
00177 //      ROS_INFO_STREAM("filename in " << filename.str().c_str());
00178     bool ready = false;
00179     std::string line;
00180     std::vector<std::string> tokens;
00181     if (file.good()) {
00182 
00183         while (!file.eof()) {
00184             std::getline(file, line);
00185 
00186 //                      ROS_INFO_STREAM("line " << line);
00187             if (ready) {
00188                 int node = boost::lexical_cast<int>(line);
00189                 if (node == -1) {
00190                     ROS_INFO_STREAM("[TSPSearch] Read end of tour.");
00191                     break;
00192                 }
00193                 node = node - 1; //TODO: -2 if arm position inserted
00194                 if (node >= 0) { //do not consider first node as we are there already
00195                     unsigned int tmp = static_cast<unsigned int>(node);
00196                     traj.push_back(tmp);
00197 //                                      ROS_INFO_STREAM("node " << tmp);
00198                 }
00199             }
00200 //              ROS_INFO_STREAM("compare " << strcmp(line.c_str(),"TOUR_SECTION"));
00201             std::string tour = "TOUR_SECTION";
00202             if (line == tour)
00203                 ready = true;
00204 //              ROS_INFO_STREAM("ready " << ready);
00205         }
00206     } else {
00207         ROS_WARN_STREAM("[TSPSearch] Could not read TSP result.");
00208     }
00209 
00210 //    traj.pop_back();// delete last element - as it goes to an artificial node
00211 }
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


coverage_3d_planning
Author(s): Juergen Hess
autogenerated on Wed Dec 26 2012 15:26:11