00001 //###################################################################### 00002 // 00003 // GraspIt! 00004 // Copyright (C) 2002-2009 Columbia University in the City of New York. 00005 // All rights reserved. 00006 // 00007 // GraspIt! is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // GraspIt! is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with GraspIt!. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // Author(s): Matei T. Ciocarlie 00021 // 00022 // $Id: listPlanner.cpp,v 1.8 2009/07/02 21:05:12 cmatei Exp $ 00023 // 00024 //###################################################################### 00025 00026 #include "listPlanner.h" 00027 00028 #include "robot.h" 00029 #include "searchState.h" 00030 #include "searchEnergy.h" 00031 00032 //#define GRASPITDBG 00033 #include "debug.h" 00034 00035 //this should be a parameter of the EGPlanner 00036 //iros09 00037 #define BEST_LIST_SIZE 2000 00038 00039 ListPlanner::ListPlanner(Hand *h) 00040 { 00041 mHand = h; 00042 init(); 00043 mEnergyCalculator = new SearchEnergy(); 00044 mEnergyCalculator->disableRendering(false); 00045 } 00046 00050 ListPlanner::~ListPlanner() 00051 { 00052 while (!mInputList.empty()){ 00053 delete mInputList.back(); 00054 mInputList.pop_back(); 00055 } 00056 } 00057 00058 void 00059 ListPlanner::resetParameters(){ 00060 EGPlanner::resetParameters(); 00061 mPlanningIterator = mInputList.begin(); 00062 if(mCurrentState) delete mCurrentState; 00063 mCurrentState = new GraspPlanningState(*mPlanningIterator); 00064 } 00065 00066 void 00067 ListPlanner::setInput(std::list<GraspPlanningState*> input) 00068 { 00069 if (isActive()) { 00070 DBGA("Can not change input while planner is running"); 00071 return; 00072 } 00073 while (!mInputList.empty()){ 00074 delete mInputList.back(); 00075 mInputList.pop_back(); 00076 } 00077 mInputList = input; 00078 mMaxSteps = input.size(); 00079 invalidateReset(); 00080 } 00081 00082 GraspPlanningState* 00083 ListPlanner::getState(int index) 00084 { 00085 std::list<GraspPlanningState*>::iterator it = mInputList.begin(); 00086 int count = 0; 00087 while (it!=mInputList.end() && count < index) { 00088 count++; it++; 00089 } 00090 if (it==mInputList.end()) { 00091 DBGA("Requested grasp not in list"); 00092 return NULL; 00093 } 00094 return *it; 00095 } 00096 00097 void 00098 ListPlanner::testState(int index) 00099 { 00100 GraspPlanningState *state = getState(index); 00101 if (!state) return; 00102 bool legal; double energy; 00103 mEnergyCalculator->analyzeState(legal, energy, state, false); 00104 DBGA("Energy: " << energy); 00105 } 00106 00107 void 00108 ListPlanner::showState(int index) 00109 { 00110 GraspPlanningState *state = getState(index); 00111 if (!state) return; 00112 state->execute(); 00113 } 00114 00115 void 00116 ListPlanner::prepareState(int index) 00117 { 00118 showState(index); 00119 mHand->findInitialContact(200); 00120 } 00121 00125 void 00126 ListPlanner::mainLoop() 00127 { 00128 //check if we are done 00129 if (mPlanningIterator==mInputList.end()) { 00130 mCurrentStep = mMaxSteps+1; 00131 return; 00132 } 00133 00134 //analyze the current state 00135 //we don't allow it to leave the hand in the analysis posture 00136 //so that after dynamics object gets put back 00137 bool legal; double energy; 00138 PRINT_STAT(mOut, mCurrentStep); 00139 mEnergyCalculator->analyzeState(legal, energy, *mPlanningIterator, true); 00140 00141 //for rendering purposes; will see later if it's needed 00142 mCurrentState->copyFrom(*mPlanningIterator); 00143 mCurrentState->setLegal(legal); 00144 00145 //put a copy of the result in list if it's legal and there's room or it's 00146 //better than the worst solution so far 00147 //this whole thing could go into a higher level fctn in EGPlanner 00148 if (legal) { 00149 double worstEnergy; 00150 if ((int)mBestList.size() < BEST_LIST_SIZE) worstEnergy = 1.0e5; 00151 else worstEnergy = mBestList.back()->getEnergy(); 00152 if (energy < worstEnergy) { 00153 GraspPlanningState *insertState = new GraspPlanningState(*mPlanningIterator); 00154 insertState->setEnergy(energy); 00155 insertState->setItNumber(mCurrentStep); 00156 DBGP("Solution at step " << mCurrentStep); 00157 mBestList.push_back(insertState); 00158 mBestList.sort(GraspPlanningState::compareStates); 00159 while ((int)mBestList.size() > BEST_LIST_SIZE) { 00160 delete(mBestList.back()); 00161 mBestList.pop_back(); 00162 } 00163 } 00164 } 00165 00166 //advance the planning iterator 00167 mPlanningIterator++; 00168 mCurrentStep++; 00169 emit update(); 00170 PRINT_STAT(mOut, std::endl); 00171 } 00172 00173 void 00174 ListPlanner::showVisualMarkers(bool show) 00175 { 00176 std::list<GraspPlanningState*>::iterator it; 00177 for (it = mInputList.begin(); it!=mInputList.end(); it++) { 00178 if (show) { 00179 (*it)->showVisualMarker(); 00180 } else { 00181 (*it) ->hideVisualMarker(); 00182 } 00183 } 00184 }