Go to the documentation of this file.00001
00002
00003
00004
00005
00006
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
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
00026
00027
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
00052
00053
00054
00055
00056
00057
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
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
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
00092
00093
00094
00095
00096
00097
00098
00100
00101
00102
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 file << "EDGE_WEIGHT_SECTION : " << std::endl;
00115
00116 for (unsigned int i = 0; i < weights.rows(); i++) {
00117 for (unsigned int j = 0; j < weights.cols(); j++) {
00118
00119
00120
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
00129 }
00130 }
00131 file << std::endl;
00132 file << "EOF";
00133 file.close();
00134
00135 }
00136 bool TSPSearch::callTSPSolver() {
00137
00138 stringstream command;
00139
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
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 traj.clear();
00175 ifstream file(file_tsp_result_.c_str(), ios::in);
00176
00177
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
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;
00194 if (node >= 0) {
00195 unsigned int tmp = static_cast<unsigned int>(node);
00196 traj.push_back(tmp);
00197
00198 }
00199 }
00200
00201 std::string tour = "TOUR_SECTION";
00202 if (line == tour)
00203 ready = true;
00204
00205 }
00206 } else {
00207 ROS_WARN_STREAM("[TSPSearch] Could not read TSP result.");
00208 }
00209
00210
00211 }
00212