00001 #ifndef HEURISTIC_CHAIN_CPP 00002 #define HEURISTIC_CHAIN_CPP 00003 00004 //-------------------------------------------------------------------- 00005 // 00006 // This file is part of PEACE. 00007 // 00008 // PEACE is free software: you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation, either version 3 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // PEACE is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with PEACE. If not, see <http://www.gnu.org/licenses/>. 00020 // 00021 // Miami University makes no representations or warranties about the 00022 // suitability of the software, either express or implied, including 00023 // but not limited to the implied warranties of merchantability, 00024 // fitness for a particular purpose, or non-infringement. Miami 00025 // University shall not be liable for any damages suffered by licensee 00026 // as a result of using, result of using, modifying or distributing 00027 // this software or its derivatives. 00028 // 00029 // By using or copying this Software, Licensee agrees to abide by the 00030 // intellectual property laws, and all other applicable laws of the 00031 // U.S., and the terms of GNU General Public License (version 3). 00032 // 00033 // Authors: Dhananjai M. Rao raodm@muohio.edu 00034 // 00035 //--------------------------------------------------------------------- 00036 00037 #include "HeuristicChain.h" 00038 #include "HeuristicFactory.h" 00039 #include "Utilities.h" 00040 00041 // Get rid of magic numbers 00042 #define NO_ERROR 0 00043 00044 // The static pointer to the singleton heuristic chain instance. 00045 HeuristicChain* HeuristicChain::ptrInstance = NULL; 00046 00047 void 00048 HeuristicChain::showArguments(std::ostream& os) { 00049 for (size_t i = 0; (i < chain.size()); i++) { 00050 chain[i]->showArguments(os); 00051 } 00052 } 00053 00054 bool 00055 HeuristicChain::parseArguments(int& argc, char **argv) { 00056 bool flag = true; 00057 for (size_t i = 0; (i < chain.size()); i++) { 00058 if (!chain[i]->parseArguments(argc, argv)) { 00059 flag = false; 00060 } 00061 } 00062 return flag; 00063 } 00064 00065 bool 00066 HeuristicChain::addHeuristic(Heuristic* h) { 00067 ASSERT( h != NULL ); 00068 chain.push_back(h); 00069 // Everything went well 00070 return true; 00071 } 00072 00073 int 00074 HeuristicChain::initialize() { 00075 int result; 00076 for (size_t i = 0; (i < chain.size()); i++) { 00077 if ((result = chain[i]->initialize()) != NO_ERROR) { 00078 // Error occured during initialization. Bail out. 00079 std::cerr << "Error initializing heuristic " 00080 << chain[i]->getName() << std::endl; 00081 return result; 00082 } 00083 } 00084 return 0; // Everything went well 00085 } 00086 00087 int 00088 HeuristicChain::setReferenceEST(const int estIdx) { 00089 int result; 00090 for (size_t i = 0; (i < chain.size()); i++) { 00091 if ((result = chain[i]->setReferenceEST(estIdx)) != NO_ERROR) { 00092 // Error occured during initialization. Bail out. 00093 return result; 00094 } 00095 } 00096 return 0; // Everything went well 00097 } 00098 00099 Heuristic* 00100 HeuristicChain::getHeuristic(const std::string& name) const { 00101 for(size_t i = 0; (i < chain.size()); i++) { 00102 if (chain[i]->getName() == name) { 00103 // Found a match! 00104 return chain[i]; 00105 } 00106 } 00107 // A matching heuristic was not found. 00108 return NULL; 00109 } 00110 00111 HeuristicChain::~HeuristicChain() { 00112 for(size_t i = 0; (i < chain.size()); i++) { 00113 delete chain[i]; 00114 } 00115 chain.clear(); 00116 } 00117 00118 HeuristicChain::HeuristicChain() { 00119 // Nothing to be done for now 00120 } 00121 00122 void 00123 HeuristicChain::printStats(std::ostream& os, const int rank) const { 00124 os << "Heuristics on process with Rank " << rank << "\n" 00125 << "-------------------------------------------\n"; 00126 for (size_t i = 0; (i < chain.size()); i++) { 00127 chain[i]->printStats(os); 00128 } 00129 } 00130 00131 00132 HeuristicChain* 00133 HeuristicChain::setupChain(const char* heuristicStr, const int refESTidx, 00134 const std::string& outputFile) { 00135 // First validate and create a blank heuristic chain. 00136 ASSERT ( ptrInstance == NULL ); 00137 ptrInstance = new HeuristicChain; 00138 // Check if there is anything to be created. 00139 if (heuristicStr == NULL) { 00140 return NULL; 00141 } 00142 // Process the non-empty heuristic string. 00143 std::string hStr(heuristicStr); 00144 HeuristicChain* heuristicChain = ptrInstance; 00145 ASSERT ( heuristicChain != NULL ); 00146 // Process one word at a time. Words are separated by a "hyphen" 00147 // character. 00148 while (!hStr.empty()) { 00149 // Locate the next hyphen character and get name of heuristic 00150 const std::string::size_type hyphenLoc = hStr.find('-'); 00151 const std::string name = hStr.substr(0, hyphenLoc); 00152 // Create heuristic and add it to the chain 00153 Heuristic *heuristic = 00154 HeuristicFactory::create(name.c_str(), refESTidx, outputFile); 00155 if (heuristic == NULL) { 00156 // Break out and return null, which will cause main to show usage 00157 return NULL; 00158 } 00159 // This assert caused errors if invalid heuristics were entered 00160 //ASSERT( heuristic != NULL ); 00161 heuristicChain->addHeuristic(heuristic); 00162 // Remove already created heuristic to process next one in chain. 00163 if (hyphenLoc == std::string::npos) { 00164 // No hyphen, so this was the last heuristic in the chain 00165 hStr.clear(); 00166 } else { 00167 // hStr becomes the remaining heuristics in the chain 00168 hStr = hStr.substr(hyphenLoc + 1); 00169 } 00170 } 00171 // Return the newly populated heuristic chain for easy reference 00172 return heuristicChain; 00173 } 00174 00175 #endif