heuristic.cpp
Go to the documentation of this file.
00001 #include "heuristic.h"
00002 #include "operator.h"
00003 
00004 using namespace std;
00005 
00006 Heuristic::Heuristic()
00007 {
00008     heuristic = NOT_INITIALIZED;
00009     num_computations = 0;
00010 }
00011 
00012 Heuristic::~Heuristic()
00013 {
00014 }
00015 
00016 void Heuristic::set_preferred(const Operator *op)
00017 {
00018     if(op->get_name().compare("wait") == 0) {
00019         const ScheduledOperator *s_op = dynamic_cast<const ScheduledOperator*>(op);
00020         assert(op);
00021         set_waiting_time(min(waiting_time, s_op->time_increment));
00022         return;
00023     }
00024     for(int i = 0; i < preferred_operators.size(); i++) {
00025         if(preferred_operators[i] == op) {
00026             return;
00027         }
00028     }
00029     preferred_operators.push_back(op);
00030 }
00031 
00032 double Heuristic::evaluate(const TimeStampedState &state)
00033 {
00034 
00035     if(heuristic == NOT_INITIALIZED)
00036         initialize();
00037     preferred_operators.clear();
00038     heuristic = compute_heuristic(state);
00039     num_computations++;
00040     assert(heuristic == DEAD_END || heuristic >= 0);
00041 
00042     if(heuristic == DEAD_END) {
00043         // It is ok to have preferred operators in dead-end states.
00044         // This allows a heuristic to mark preferred operators on-the-fly,
00045         // selecting the first ones before it is clear that all goals
00046         // can be reached.
00047         preferred_operators.clear();
00048     }
00049 
00050 
00051 #ifndef NDEBUG
00052     if(heuristic != DEAD_END) {
00053         //      cout << "preferred operators:" << endl;
00054         //      for(int i = 0; i < preferred_operators.size(); i++) {
00055         //          //assert(preferred_operators[i]->is_applicable(state));
00056         //          cout << "  " << preferred_operators[i]->get_name() << endl;
00057         //      }
00058     }
00059 #endif
00060     return heuristic;
00061 }
00062 
00063 bool Heuristic::is_dead_end()
00064 {
00065     return heuristic == DEAD_END;
00066 }
00067 
00068 double Heuristic::get_heuristic()
00069 {
00070     // The -1 value for dead ends is an implementation detail which is
00071     // not supposed to leak. Thus, calling this for dead ends is an
00072     // error. Call "is_dead_end()" first.
00073     assert(heuristic >= 0);
00074     return heuristic;
00075 }
00076 
00077 void Heuristic::get_preferred_operators(std::vector<const Operator *> &result)
00078 {
00079     assert(heuristic >= 0);
00080     result.insert(result.end(), preferred_operators.begin(),
00081             preferred_operators.end());
00082 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03