AbstractState.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <sstream>
00004 #include <cmath>
00005 #include "tfd_modules/opl/stringutil.h"
00006 // HACK
00007 #include "../../search/state.h"
00008 #include "tfd_modules/opl/AbstractState.h"
00009 
00010 namespace opl
00011 {
00012 
00013 namespace interface
00014 {
00015 
00016 inline bool double_equals(double a, double b)
00017 {
00018     return std::abs(a - b) < 0.0001;
00019 }
00020 
00021 AbstractState::AbstractState()
00022 {
00023     ObjectLookupTable::instance = this;
00024     currentState = NULL;
00025 }
00026 
00027 void AbstractState::updateFluentMappings()
00028 {
00029     initialize();
00030     for (ObjectMappingsType::iterator objectIterator = objects.begin(); objectIterator != objects.end(); objectIterator++)
00031     {
00032         objectIterator->second->initialize();
00033     }
00034 }
00035 
00036 std::string AbstractState::createKey(const std::string& fluentName, const std::vector<std::string>& arguments) const
00037 {
00038     std::string key = fluentName;
00039     for (std::vector<std::string>::const_iterator argumentIterator = arguments.begin(); argumentIterator != arguments.end(); argumentIterator++)
00040     {
00041         key += "%";
00042         key += *argumentIterator;
00043     }
00044 //    std::cout << "AbstractState::createKey: " << StringUtil::toLower(key) << std::endl;
00045     return StringUtil::toLower(key);
00046 }
00047 
00048 std::string AbstractState::createObjectKey(int index, double value) const
00049 {
00050     std::string key = StringUtil::createFromNumber(index);
00051     key += "|";
00052     key += StringUtil::createFromNumber((int)lrint(value));
00053     return key;
00054 }
00055 
00056 const FluentMapping* AbstractState::getVariable(const std::string& key) const
00057 {
00058     return fluentMappings.find(key)->second;
00059 }
00060 
00061 bool AbstractState::getPredicateValue(const FluentMapping* variable) const
00062 {
00063     if (variable == NULL)
00064     {
00065 //        std::cout << " NULL fluent " << std::endl;
00066         return false;
00067     }
00068     else if (variable->isConstant())
00069     {
00070 //        std::cout << " constant fluent " << (double_equals(0.0, variable->getValue())? "true":"false") << std::endl;
00071         return double_equals(0.0, variable->getValue());
00072     }
00073     else
00074     {
00075 //        std::cout << " variable fluent " << variable->getValue() << " " << currentState->operator[](variable->getIndex()) << std::endl;
00076         return double_equals(currentState->operator[](variable->getIndex()), variable->getValue());
00077     }
00078 }
00079 
00080 double AbstractState::getNumericValue(const FluentMapping* variable) const
00081 {
00082     if (variable == NULL)
00083     {
00084 //        dump();
00085 //        exit(1);
00086         return -0;
00087     }
00088     else if (variable->isConstant())
00089     {
00090         return variable->getValue();
00091     }
00092     else
00093     {
00094         return currentState->operator[](variable->getIndex());
00095     }
00096 }
00097 
00098 const Object* AbstractState::getObject(const FluentMapping* variable) const
00099 {
00100     if (variable == NULL)
00101     {
00102         std::cout << "!! FluentMapping is NULL !!" << std::endl;
00103         return NULL;
00104     }
00105     else if (variable->isConstant())
00106     {
00107 //        std::cout << " constant fluent " << std::endl;
00108         std::string objectKey = createObjectKey(variable->getIndex(), variable->getValue());
00109         return returnObjectMappings.find(objectKey)->second;
00110     }
00111     else
00112     {
00113 //        std::cout << " variable fluent " << std::endl;
00114         std::string objectKey = createObjectKey(variable->getIndex(), currentState->operator[](variable->getIndex()));
00115         return returnObjectMappings.find(objectKey)->second;
00116     }
00117 }
00118 
00119 void AbstractState::dump() const
00120 {
00121     std::cout << "OPL Callback Interface: State" << std::endl;
00122     std::cout << "\tObjects" << std::endl;
00123     for (ObjectMappingsType::const_iterator it = objects.begin(); it != objects.end(); it++)
00124     {
00125         std::cout << it->first << std::endl;
00126     }
00127     std::cout << "\tFluent mappings" << std::endl;
00128     for (FluentMappingsType::const_iterator it = fluentMappings.begin(); it != fluentMappings.end(); it++)
00129     {
00130         std::cout << it->first << " > " << it->second->dump();
00131         if (! it->second->isConstant())
00132         {
00133             std::cout << " in state: " << (*currentState)[it->second->getIndex()];
00134         }
00135         std::cout << std::endl;
00136     }
00137     std::cout << "\tObjects retrun mappings" << std::endl;
00138     for (ObjectMappingsType::const_iterator it = returnObjectMappings.begin(); it != returnObjectMappings.end(); it++)
00139     {
00140         std::cout << it->first << " > " << it->second->getObjectID() << std::endl;
00141     }
00142 
00143     std::cout << "\tTest" << std::endl;
00144 //    std::cout << test() << std::endl;
00145 
00146 }
00147 
00148 }
00149 
00150 }


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06