TSPSolver.cpp
Go to the documentation of this file.
00001 
00002 /*
00003  * TSPSolver.cpp
00004  *
00005  *  Created on: Feb 18, 2012
00006  *      Author: hess
00007  */
00008 
00009 #include <coverage_3d_planning/TSPSolver.h>
00010 #include <coverage_3d_planning/TSPSearch.h>
00011 #include <coverage_3d_tools/conversions.h>
00012 
00013 TSPSolver::TSPSolver() {
00014     dist_measure_="DIST";
00015     max_adjacency_=500;
00016 }
00017 TSPSolver::TSPSolver(double max_adjacency) {
00018     dist_measure_="DIST";
00019     max_adjacency_=max_adjacency;
00020 }
00021 
00022 void TSPSolver::getSolution(std::vector<std::vector<double> >& joint_path, std::vector<tf::Pose>& pose_path,
00023     std::vector<unsigned int>& action, std::vector<unsigned int>& res_traj) {
00024 
00025     joint_path.clear();
00026     pose_path.clear();
00027     action.clear();
00028 
00029     //DISTANCE VERSION
00030     std::map<int, int> index_map;
00031     Eigen::MatrixXf weights;
00032     computeWeightMatrix(weights);
00033 
00034     TSPSearch tsp;
00035     tsp.writeToTSPFile(weights);
00036     tsp.callTSPSolver();
00037     std::vector<unsigned int> traj;
00038     tsp.readResultTSPFile(traj); // TODO:make sure we read trajectory without start
00039 
00040     traj.erase(traj.begin());
00041     std::vector<SurfacePatch> res_patch;
00042     for (unsigned int i = 0; i < traj.size(); ++i) {
00043         traj[i] = traj[i] - 1; // minus one as we have to consider the start which is not a patch
00044         res_patch.push_back(patches_[traj[i]]);
00045     }
00046     std::vector<unsigned int> actions;
00047     getActions(traj, actions);
00048 
00049     std::vector<std::vector<std::vector<double> > > ik_solutions_to_optimize;
00050     //collect end-effector poses and iksolutions for the trajectory
00051     for (unsigned int i = 0; i < traj.size(); ++i) {
00052 
00053         int curr_node = traj[i];
00054         int next_node = traj[i + 1];
00055 
00056         if (i < traj.size() - 1) {
00057 
00058             if ((adjacency_(curr_node, next_node) == 1)) {
00059 
00060                 //get the number belonging to the edge we need the pose for
00061                 int local_edge_nb = -1; // minus one: e.g. edge 1 is indexed on index 0
00062                 for (int j = 0; j <= next_node; ++j) {
00063                     local_edge_nb += adjacency_(curr_node, j);
00064                 }
00065 
00066                 ik_solutions_to_optimize.push_back(coll_free_ik_[curr_node][local_edge_nb]);
00067 
00068                 pose_path.push_back(coll_free_poses_[curr_node][local_edge_nb]);
00069             } else {
00070                 ROS_WARN("TSPSolver::getSolution Path planning edge taken --> should usually not happen.");
00071                 std::vector<std::vector<std::vector<double> > > tmp = coll_free_ik_[traj[i]];
00072                 std::vector<std::vector<double> > tmp_joints;
00073                 for (unsigned int j = 0; j < tmp.size(); ++j) {
00074                     for (unsigned int k = 0; k < tmp[j].size(); ++k) {
00075                         tmp_joints.push_back(tmp[j][k]);
00076                     }
00077                     //                          tmp_joints[j][5] = ik_solutions_to_optimize[ik_solutions_to_optimize.size() - 1][0][5]; // get the last node -> gripper orientation should be the same for all poses
00078                 }
00079                 ik_solutions_to_optimize.push_back(tmp_joints);
00080                 pose_path.push_back(coll_free_poses_[curr_node][0]); // any edge just that we have a pose
00081             }
00082         } else {
00083             std::vector<std::vector<std::vector<double> > > tmp = coll_free_ik_[traj[i]];
00084             std::vector<std::vector<double> > tmp_joints;
00085             for (unsigned int j = 0; j < tmp.size(); ++j) {
00086                 for (unsigned int k = 0; k < tmp[j].size(); ++k) {
00087                     tmp_joints.push_back(tmp[j][k]);
00088                 }
00089                 //                              tmp_joints[j][5] = ik_solutions_to_optimize[ik_solutions_to_optimize.size() - 1][0][5]; // get the last node -> gripper orientation should be the same for all poses
00090             }
00091             ik_solutions_to_optimize.push_back(tmp_joints);
00092             pose_path.push_back(coll_free_poses_[curr_node][0]); // any edge just that we have a pose
00093         }
00094     }
00095     std::vector<std::vector<double> > tmp_joint_start;
00096     tmp_joint_start.push_back(start_joint_);
00097     ik_solutions_to_optimize.insert(ik_solutions_to_optimize.begin(), tmp_joint_start);
00098     ik_solutions_to_optimize.push_back(tmp_joint_start);
00099 
00100     std::vector<std::vector<double> > path;
00101     std::vector<bool> valid_poses(ik_solutions_to_optimize.size(), true);
00102     traj_planner_.computeDijkstraPath(ik_solutions_to_optimize, valid_poses, path,dist_measure_);
00103     joint_path = path;
00104 
00105     res_traj = traj;
00106     pose_path.push_back(start_pose_);
00107     pose_path.insert(pose_path.begin(), start_pose_);
00108     actions.push_back(1);
00109     actions.insert(actions.begin(), 1);
00110     action = actions;
00111 
00112 }
00113 
00114 void TSPSolver::computeWeightMatrix(Eigen::MatrixXf& weigths) {
00115     //add one node for start pose
00116 
00117     Eigen::Vector3f start = PoseTF2EigenVector3(start_pose_);
00118     weigths = Eigen::MatrixXf::Ones(adjacency_.rows() + 1, adjacency_.rows() + 1) * max_adjacency_;
00119     for (int i = 1; i < weigths.rows(); ++i) {
00120 
00121         weigths(i, 0) = (start - surface_points_[i - 1].getVector3fMap()).norm();
00122         weigths(0, i) = weigths(i, 0);
00123     }
00124 
00125     for (int i = 1; i < weigths.rows(); ++i) {
00126         for (int j = 1; j < weigths.cols(); ++j) {
00127             if (adjacency_(i - 1, j - 1) == 1) {
00128                 weigths(i, j)
00129                     = (surface_points_[i - 1].getVector3fMap() - surface_points_[j - 1].getVector3fMap()).norm();
00130             } else {
00131                 weigths(i, j)
00132                     += (surface_points_[i - 1].getVector3fMap() - surface_points_[j - 1].getVector3fMap()).norm();
00133 
00134             }
00135         }
00136     }
00137 }
00138 
00139 void TSPSolver::getActions(const std::vector<unsigned int>& traj, std::vector<unsigned int>& actions) {
00140 
00141     actions.clear();
00142 
00143     for (unsigned int i = 0; i < traj.size(); ++i) {
00144 
00145         if (i < (traj.size() - 1)) {
00146 
00147             if (adjacency_(traj[i], traj[i + 1]) == 1) {
00148                 actions.push_back(0);
00149             } else {
00150                 actions.push_back(1);
00151             }
00152         } else {
00153             actions.push_back(1); // we are at the last node - thus use planning to get to the final pose
00154         }
00155     }
00156 }
 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