mhics.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2003 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "nel/ai/nimat/mhics.h"
00027 #include "nel/misc/file.h"
00028 
00029 namespace NLAINIMAT
00030 {
00031         using namespace NLMISC;
00032 
00033         static const TTargetId NullTargetId = 0;
00034 
00036 // CMotivationEnergy
00038 CMotivationEnergy::CMotivationEnergy()
00039 {
00040         _SumValue = 0;
00041         _MHiCSagent = NULL;
00042 //      _WasPreviouslyActived = false;
00043 }
00044 
00045 CMotivationEnergy::~CMotivationEnergy()
00046 {
00047 }
00048 
00049 double CMotivationEnergy::getSumValue() const
00050 {
00051         return _SumValue;
00052 }
00053 
00054 void CMotivationEnergy::removeProvider(TMotivation providerName)
00055 {
00056         _MotivationProviders.erase(providerName);
00057         computeMotivationValue();
00058 }
00059 
00060 //void CMotivationEnergy::removeProvider(TAction providerName)
00061 //{
00062 //      _VirtualActionProviders.erase(providerName);
00063 //      computeMotivationValue();
00064 //}
00065 
00066 void CMotivationEnergy::addProvider(TMotivation providerName, TClassifierNumber classifierNumber)
00067 {
00068         _MotivationProviders[providerName].insert(classifierNumber);
00069         computeMotivationValue();
00070 }
00071 
00072 //void CMotivationEnergy::addProvider(TAction providerName, const CMotivationEnergy& providerMotivation)
00073 //{
00074 //      _VirtualActionProviders[providerName] = providerMotivation._EnergyByMotivation ;
00075 //      computeMotivationValue();
00076 //}
00077 
00078 //void CMotivationEnergy::updateProvider(TMotivation providerName, const CMotivationEnergy& providerMotivation)
00079 //{
00080 //      _MotivationProviders[providerName] = providerMotivation._EnergyByMotivation ;
00081 //      computeMotivationValue();
00082 //}
00083 
00084 //void CMotivationEnergy::updateProvider(TAction providerName, const CMotivationEnergy& providerMotivation)
00085 //{
00086 //      _VirtualActionProviders[providerName] = providerMotivation._EnergyByMotivation ;
00087 //      computeMotivationValue();
00088 //}
00089 //void CMotivationEnergy::setWasPreviouslyActived(bool yesOrNo)
00090 //{
00091 //      _WasPreviouslyActived = yesOrNo;
00092 //}
00093 
00094 
00095 void CMotivationEnergy::computeMotivationValue()
00096 {
00097         _EnergyByMotivation.clear();
00098 
00099         TMotivation lastMotivationName = Motivation_Unknown;
00100         // We look for motivation values comming directly from Motivations
00101         std::multimap<TMotivation, std::set<TClassifierNumber> >::iterator itMotivationProviders;
00102         for (itMotivationProviders = _MotivationProviders.begin();
00103                  itMotivationProviders != _MotivationProviders.end();
00104                  itMotivationProviders++)
00105         {
00106                 uint32 lastMaxMotiveValue = 0;
00107                 TMotivation motivationName = (*itMotivationProviders).first;
00108                 std::set<TClassifierNumber>::iterator itClassifierNumber;
00109                 for (itClassifierNumber = (*itMotivationProviders).second.begin(); itClassifierNumber != (*itMotivationProviders).second.end(); itClassifierNumber++)
00110                 {
00111                         TClassifierNumber classierNumber = (*itClassifierNumber);
00112                         uint32 classifierTimer = _MHiCSagent->getMHiCSbase()->getPriorityPart(motivationName, classierNumber).getClassifierTimer();;
00113                         bool wasPreviouslyActived = _MHiCSagent->wasClassifierPreviouslyActive(motivationName,classierNumber);
00114                         if (wasPreviouslyActived)
00115                         {
00116                                 uint32 temporaryClassifierPriorityTime = _MHiCSagent->getTemporaryClassifierPriorityTime(motivationName, classierNumber);
00117                                 // on donne une marge de 15 secondes à l'action activée.
00118                                 if (temporaryClassifierPriorityTime > classifierTimer + 15)
00119                                 {
00120                                         classifierTimer = temporaryClassifierPriorityTime;
00121                                 }
00122                                 else
00123                                 {
00124                                         classifierTimer -=1; // Pour lui donner un avantage en cas d'égalité
00125                                 }
00126                         }
00127                         double motiveValue = _MHiCSagent->getMotivationValue(motivationName);
00128                         double motivePP = _MHiCSagent->getMotivationPP(motivationName);
00129                         uint32 priorityTimer = _MHiCSagent->getMHiCSbase()->getPriorityPart(motivationName, classierNumber).getPriorityTimer();
00130                         priorityTimer = std::max(priorityTimer, classifierTimer);
00131 
00132 
00133                         uint32 combinedValue = (motiveValue * 10000.0) - priorityTimer;
00134                         if (combinedValue > lastMaxMotiveValue)
00135                         {
00136                                 lastMaxMotiveValue = combinedValue;
00137                                 _EnergyByMotivation[motivationName].Value = combinedValue;
00138                                 _EnergyByMotivation[motivationName].PP = motivePP;
00139                         }
00140                 }
00141         }
00142 
00143         TEnergyByMotivation::const_iterator itEnergyByMotivation;
00144         double sum = 0;
00145         for (itEnergyByMotivation = _EnergyByMotivation.begin(); itEnergyByMotivation != _EnergyByMotivation.end(); itEnergyByMotivation++)
00146         {
00147                 sum += (*itEnergyByMotivation).second.Value * (*itEnergyByMotivation).second.PP;
00148         }
00149         sum += _MyMotivationValue.PP * _MyMotivationValue.Value;
00150 
00151         _SumValue = sum;
00152         nlassert(_SumValue >= 0);
00153 }
00154 
00156 void CMotivationEnergy::setMotivationPP(TMotivation motivationName, double PP)
00157 {
00158         _SumValue -= _MyMotivationValue.Value * _MyMotivationValue.PP;
00159         _SumValue += _MyMotivationValue.Value * PP;
00160         _EnergyByMotivation[motivationName].PP = PP;
00161         _MyMotivationValue.PP = PP;
00162         nlassert(_SumValue >= 0);
00163 }
00164 
00166 double CMotivationEnergy::getMotivationPP(TMotivation motivationName) const
00167 {
00168         return _MyMotivationValue.PP;
00169 }
00170 
00172 void CMotivationEnergy::setMotivationValue(TMotivation motivationName, double value)
00173 {
00174         nlassert(value >= 0);
00175         _SumValue -= _MyMotivationValue.Value * _MyMotivationValue.PP;
00176         nlassert(_SumValue >= 0);
00177         _SumValue += value * _MyMotivationValue.PP;
00178         _EnergyByMotivation[motivationName].Value = value;
00179         _MyMotivationValue.Value = value;
00180 
00181         nlassert(_SumValue >= 0);
00182 }
00183 
00185 double  CMotivationEnergy::getMotivationValue(TMotivation motivationName) const
00186 {
00187         return _MyMotivationValue.Value;
00188 }
00189 
00190 const std::map<TMotivation, std::set<TClassifierNumber> >* CMotivationEnergy::getProviders() const
00191 {
00192         return &_MotivationProviders;
00193 }
00194 
00196 void CMotivationEnergy::getDebugString(std::string &t) const
00197 {
00198         std::string ret;
00199         TEnergyByMotivation::const_iterator itEnergyByMotivation;
00200         
00201         for (itEnergyByMotivation = _EnergyByMotivation.begin(); itEnergyByMotivation!= _EnergyByMotivation.end(); itEnergyByMotivation++)
00202         {
00203                 ret += " " + conversionMotivation.toString((*itEnergyByMotivation).first) + " (" + NLMISC::toString((*itEnergyByMotivation).second.Value * (*itEnergyByMotivation).second.PP) + ") ";
00204         }
00205         t+=ret;
00206 }
00207 
00209 // CMHiCSbase
00211 
00212 CMHiCSbase::CMHiCSbase()
00213 {
00214         pActionResources = new CActionResources();
00215 }
00216 
00217 CMHiCSbase::~CMHiCSbase()
00218 {
00219         delete pActionResources;
00220 }
00221 
00222 void CMHiCSbase::addVirtualActionCS(const CActionClassifiers &action)
00223 {
00224         const std::map<TMotivation, CClassifierSystem> *mapActionByMotivation = action.getClassifiersByMotivationMap();
00225         std::map<TMotivation, CClassifierSystem>::const_iterator ItMapActionByMotivation;
00226         for (ItMapActionByMotivation = mapActionByMotivation->begin(); ItMapActionByMotivation != mapActionByMotivation->end(); ItMapActionByMotivation++)
00227         {
00228                 CClassifierSystem* pCS;
00229                 TMotivation motivationName = (*ItMapActionByMotivation).first;
00230                 const CClassifierSystem* pOtherCS = &((*ItMapActionByMotivation).second);
00231 
00232                 pCS = &(_MotivationClassifierSystems[motivationName]);
00233                 pCS->addClassifierSystem(*pOtherCS);
00234         }
00235 
00236         const std::map<TAction, CClassifierSystem> *mapActionByVirtualAction = action.getClassifiersByVirtualActionMap();
00237         std::map<TAction, CClassifierSystem>::const_iterator ItMapActionByVirtualAction;
00238         for (ItMapActionByVirtualAction = mapActionByVirtualAction->begin(); ItMapActionByVirtualAction != mapActionByVirtualAction->end(); ItMapActionByVirtualAction++)
00239         {
00240                 CClassifierSystem* pCS;
00241                 TAction virtualActionName = (*ItMapActionByVirtualAction).first;
00242                 const CClassifierSystem* pOtherCS = &((*ItMapActionByVirtualAction).second);
00243 
00244                 pCS = &(_VirtualActionClassifierSystems[virtualActionName]);
00245                 pCS->addClassifierSystem(*pOtherCS);
00246         }
00247 }
00248 
00249 void CMHiCSbase::addActionCS(const CActionClassifiers& action)
00250 {
00251         nlassert ( action.getName() < Action_VIRTUAL_ACTIONS);
00252         addVirtualActionCS(action);
00253         _ActionSet.insert(action.getName());
00254 }
00255 
00256 void CMHiCSbase::selectBehavior(TMotivation motivationName,
00257                                                                 const CCSPerception* psensorMap,
00258                                                                 std::multimap<CClassifierPriority, std::pair<TClassifierNumber, TTargetId> > &mapActivableCS)
00259 {
00260         std::map<TMotivation, CClassifierSystem>::iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00261         if (itMotivationClassifierSystems != _MotivationClassifierSystems.end())
00262         {
00263                 (*itMotivationClassifierSystems).second.selectBehavior(psensorMap, mapActivableCS);
00264         }
00265 }
00266 
00267 //void CMHiCSbase::selectBehavior(TAction VirtualActionName,
00268 //                                                              const CCSPerception* psensorMap,
00269 //                                                              std::multimap<double, std::pair<TClassifierNumber, TTargetId> > &mapActivableCS,
00270 //                                                              TTargetId       &target)
00271 //{
00272 //      std::map<TAction, CClassifierSystem>::iterator itVirtualActionClassifierSystems = _VirtualActionClassifierSystems.find(VirtualActionName);
00273 //      nlassert(itVirtualActionClassifierSystems != _VirtualActionClassifierSystems.end());
00274 //      // When we select an high level action, we limit the perception to the target associated with this action.
00275 //      CCSPerception neoPerception;
00276 //      neoPerception.NoTargetSensors = psensorMap->NoTargetSensors;
00277 //      std::map<TTargetId, TSensorMap>::const_iterator itSensorMap = psensorMap->TargetSensors.find(target);
00278 //      if(itSensorMap != psensorMap->TargetSensors.end())
00279 //      {
00280 //              neoPerception.TargetSensors[target] = (*itSensorMap).second;
00281 //      }
00282 //      (*itVirtualActionClassifierSystems).second.selectBehavior(&neoPerception, mapActivableCS);
00283 //}
00284 
00285 TAction CMHiCSbase::getActionPart(TMotivation motivationName, TClassifierNumber classifierNumber) const
00286 {
00287         if (classifierNumber == -1) return Action_DoNothing;
00288 
00289         std::map<TMotivation, CClassifierSystem>::const_iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00290         nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00291         return (*itMotivationClassifierSystems).second.getActionPart(classifierNumber);
00292 }
00293 
00294 //TAction CMHiCSbase::getActionPart(TAction motivationName, TClassifierNumber classifierNumber)
00295 //{
00296 //      if (classifierNumber == -1) return Action_DoNothing;
00297 //      
00298 //      std::map<TAction, CClassifierSystem>::iterator itVirtualActionClassifierSystems = _VirtualActionClassifierSystems.find(motivationName);
00299 //      nlassert(itVirtualActionClassifierSystems != _VirtualActionClassifierSystems.end());
00300 //      return (*itVirtualActionClassifierSystems).second.getActionPart(classifierNumber);
00301 //}
00302 
00303 CClassifierPriority CMHiCSbase::getPriorityPart(TMotivation motivationName, TClassifierNumber classifierNumber) const
00304 {
00305         if (classifierNumber == -1) return CClassifierPriority();
00306         
00307         std::map<TMotivation, CClassifierSystem>::const_iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00308         nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00309         return (*itMotivationClassifierSystems).second.getPriorityPart(classifierNumber);
00310 }
00311 
00312 //void CMHiCSbase::dividePriorityByTheMinPriorityPartInAMotivation(TMotivation motivationName)
00313 //{
00314 //      std::map<TMotivation, CClassifierSystem>::iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00315 //      nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00316 //      (*itMotivationClassifierSystems).second.dividePriorityByTheMinPriorityPart();
00317 //}
00318 
00319 
00320 void CMHiCSbase::setPriorityValue(TMotivation motivationName, TClassifierNumber classifierNumber, CClassifierPriority priority)
00321 {
00322         std::map<TMotivation, CClassifierSystem>::iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00323         nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00324         (*itMotivationClassifierSystems).second.setPriorityPart(classifierNumber, priority);
00325 }
00326 
00327 bool CMHiCSbase::isAnAction(TAction behav) const
00328 {
00329         std::set<TAction>::const_iterator itActionSet = _ActionSet.find(behav);
00330         return (itActionSet != _ActionSet.end());
00331 }
00332 
00334 void CMHiCSbase::getDebugString(std::string &t) const
00335 {
00336         std::string ret = "\n---------------------------";
00337         std::map<TMotivation, CClassifierSystem>::const_iterator itMotivationClassifierSystems;
00338         for (itMotivationClassifierSystems = _MotivationClassifierSystems.begin(); itMotivationClassifierSystems != _MotivationClassifierSystems.end(); itMotivationClassifierSystems++)
00339         {
00340                 ret += "\nMotivation : " + conversionMotivation.toString((*itMotivationClassifierSystems).first);
00341                 (*itMotivationClassifierSystems).second.getDebugString(ret);
00342         }
00343         std::map<TAction, CClassifierSystem>::const_iterator itVirtualActionClassifierSystems;
00344         for (itVirtualActionClassifierSystems = _VirtualActionClassifierSystems.begin(); itVirtualActionClassifierSystems != _VirtualActionClassifierSystems.end(); itVirtualActionClassifierSystems++)
00345         {
00346                 ret += "\nVirtual Action : " + conversionAction.toString((*itVirtualActionClassifierSystems).first);
00347                 (*itVirtualActionClassifierSystems).second.getDebugString(ret);
00348         }
00349         ret += "\nACTIONS :\n";
00350         std::set<TAction>::const_iterator itActionSet;
00351         for (itActionSet = _ActionSet.begin(); itActionSet != _ActionSet.end(); itActionSet++)
00352         {
00353                 ret += conversionAction.toString((*itActionSet)) + "\n";
00354         }
00355         t+=ret;
00356 }
00357 
00358 
00359 void CMHiCSbase::printDebugString() const
00360 {
00361         std::string ret = "";
00362         nldebug("\n---------------------------");
00363         std::map<TMotivation, CClassifierSystem>::const_iterator itMotivationClassifierSystems;
00364         for (itMotivationClassifierSystems = _MotivationClassifierSystems.begin(); itMotivationClassifierSystems != _MotivationClassifierSystems.end(); itMotivationClassifierSystems++)
00365         {
00366                 ret += (("\nMotivation : " + conversionMotivation.toString((*itMotivationClassifierSystems).first)).c_str());
00367                 (*itMotivationClassifierSystems).second.getDebugString(ret);
00368                 nldebug(ret.c_str());
00369                 ret = "";
00370         }
00371         std::map<TAction, CClassifierSystem>::const_iterator itVirtualActionClassifierSystems;
00372         for (itVirtualActionClassifierSystems = _VirtualActionClassifierSystems.begin(); itVirtualActionClassifierSystems != _VirtualActionClassifierSystems.end(); itVirtualActionClassifierSystems++)
00373         {
00374                 ret += (("\nVirtual Action : " + conversionAction.toString((*itVirtualActionClassifierSystems).first)).c_str());
00375                 (*itVirtualActionClassifierSystems).second.getDebugString(ret);
00376                 nldebug(ret.c_str());
00377                 ret = "";
00378         }
00379         ret += ("\nACTIONS :\n");
00380         std::set<TAction>::const_iterator itActionSet;
00381         for (itActionSet = _ActionSet.begin(); itActionSet != _ActionSet.end(); itActionSet++)
00382         {
00383                 ret += ((conversionAction.toString((*itActionSet)) + "\n").c_str());
00384                 nldebug(ret.c_str());
00385                 ret = "";
00386         }
00387 }
00388 
00389 
00391 bool CMHiCSbase::loadClassifierFromFile(std::string fileName)
00392 {
00393         bool                    ret;
00394         const uint32    aboeufSize = 2048;
00395         char                    aboeuf[aboeufSize];
00396         std::string             laLigne, leMot;
00397         uint                    lastPos = 0;
00398         uint                    nextPos = 0;
00399         NLMISC::CIFile  melkior;
00400         CConditionMap   conditionsMap;
00401         std::map<TAction, CActionClassifiers >  actionsMap;
00402         std::vector<TSensor>                                    sensorVector;
00403         ret = melkior.open(fileName, true);
00404         if (!ret) return false;
00405         // 1ère ligne : titre conditions
00406         melkior.getline(aboeuf, aboeufSize);
00407         // 2ème ligne : Motivations; [Condition]*; Actions; priority; blabla
00408         melkior.getline(aboeuf, aboeufSize);
00409         laLigne = aboeuf;
00410         // on construit une map avec les conditions
00411         nextPos = laLigne.find_first_of(";",lastPos);
00412         leMot = laLigne.substr(lastPos,nextPos - lastPos);
00413         nlassert(leMot == "Motivations");
00414         lastPos = nextPos+1;
00415         nextPos = laLigne.find_first_of(";", lastPos);
00416         leMot = laLigne.substr(lastPos, nextPos-lastPos);
00417         while (leMot != "Actions")
00418         {
00419                 // on regarde le type du senseur :
00420                 TSensor titi = conversionSensor.fromString(leMot);
00421                 nlassert (titi != Sensor_Unknown);
00422                 sensorVector.push_back(titi);
00423 
00424                 lastPos = nextPos+1;
00425                 nextPos = laLigne.find_first_of(";", lastPos);
00426                 leMot = laLigne.substr(lastPos, nextPos-lastPos);
00427         }
00428         // on parse le reste
00429         melkior.getline(aboeuf, aboeufSize);
00430         laLigne = aboeuf;
00431         lastPos = 0;
00432         nextPos = 0;
00433         while (laLigne.size() > 0)
00434         {
00435                 TMotivation laMotive;
00436                 TAction         laVirtuelle;
00437                 // On récupère le nom de la motivation
00438                 nextPos = laLigne.find_first_of(";",lastPos);
00439                 leMot = laLigne.substr(lastPos,nextPos - lastPos);
00440                 if (leMot.size() == 0) break;
00441                 laMotive = conversionMotivation.fromString(leMot);
00442                 if (laMotive == Motivation_Unknown)
00443                 {
00444                         // Si c'est pas une motivation, c'est peut-être une action virtuelle.
00445                         laVirtuelle = conversionAction.fromString(leMot);
00446                         nlassert(laVirtuelle != Action_Unknown);
00447                 }
00448                 
00449                 lastPos = nextPos+1;
00450                 nextPos = laLigne.find_first_of(";",lastPos);
00451                 leMot = laLigne.substr(lastPos,nextPos - lastPos);
00452                 
00453                 // On récupère la liste des conditions
00454                 uint ii;
00455                 for (ii = 0; ii < sensorVector.size(); ii++)
00456                 {
00457                         if (leMot.size() >0)
00458                         {
00459                                 TSensor sensorName = sensorVector[ii];
00460                                 if (leMot[0] == '!')
00461                                 {
00462                                         conditionsMap.addIfNotSensorCondition(sensorName,leMot[1]);
00463                                 }
00464                                 else
00465                                 {
00466                                         conditionsMap.addIfSensorCondition(sensorName,leMot[0]);
00467                                 }
00468                         }
00469 
00470                         lastPos = nextPos+1;
00471                         nextPos = laLigne.find_first_of(";",lastPos);
00472                         leMot = laLigne.substr(lastPos,nextPos - lastPos);
00473                 }
00474                 // on récupère le nom de l'action
00475                 TAction actionName = conversionAction.fromString(leMot);
00476                 nlassert(actionName != Action_Unknown);
00477 
00478                 lastPos = nextPos+1;
00479                 nextPos = laLigne.find_first_of(";",lastPos);
00480                 leMot = laLigne.substr(lastPos,nextPos - lastPos);
00481 
00482                 // on récupère la force du classeur
00483                 CClassifierPriority laforce;
00484                 laforce.SetClassifierTimer( atof(leMot.c_str()) );
00485 
00486                 // on rajoute la règle dans les actions.
00487                 std::map<TAction, CActionClassifiers >::iterator itActionsMap = actionsMap.find(actionName);
00488                 if (itActionsMap == actionsMap.end())
00489                 {
00490                         CActionClassifiers bibu(actionName);
00491                         actionsMap.insert(std::make_pair(actionName, bibu));
00492                 }
00493                 CActionClassifiers mon_action(actionName);
00494                 // Si la motivation est inconnue, c'est que c'est une action virtuelle.
00495                 if (laMotive == Motivation_Unknown)
00496                 {
00497                         actionsMap[actionName].addVirtualActionRule(laVirtuelle,conditionsMap, laforce);
00498                 }
00499                 else
00500                 {
00501                         actionsMap[actionName].addMotivationRule(laMotive,conditionsMap, laforce);
00502                 }
00503 
00504                 conditionsMap.clear();
00505                 melkior.getline(aboeuf, aboeufSize);
00506                 laLigne = aboeuf;
00507                 lastPos = 0;
00508                 nextPos = 0;
00509         }
00510         melkior.close();
00511 
00512         std::map<TAction, CActionClassifiers >::iterator itActionsMap;
00513         for (itActionsMap = actionsMap.begin(); itActionsMap != actionsMap.end(); itActionsMap++)
00514         {
00515                 //***G*** ajouter aussi la gestion des actions virtuels
00516                 TAction testAction = (*itActionsMap).second.getName();
00517                 nlassert (testAction != Action_Unknown);
00518                 if (testAction > Action_VIRTUAL_ACTIONS)
00519                 {
00520                         addVirtualActionCS( (*itActionsMap).second );
00521                 }
00522                 else
00523                 {
00524                         addActionCS( (*itActionsMap).second );
00525                 }
00526         }
00527 
00528         return true;
00529 }
00530 
00531 void CMHiCSbase::dbgPrintClassifierPriorityInFile(std::string fileName) const
00532 {
00533         int i;
00534         std::map<TMotivation, CClassifierSystem>::const_iterator itMotivationClassifierSystems;
00535         std::string nomDuFichier = "D:\\jgab\\doc\\IA\\_These\\gnuplot\\TFC\\";
00536         nomDuFichier += fileName;
00537         
00538         NLMISC::CIFile baltazar;
00539         bool yaqqunauboutdufil = baltazar.open(nomDuFichier.c_str(), true);
00540         baltazar.close();
00541         
00542         NLMISC::COFile melkior;
00543         std::string ohlabellephrase = "";
00544         
00545         if (!yaqqunauboutdufil)
00546         {
00547                 melkior.open(nomDuFichier.c_str(), false, true);
00548                 
00549                 for(itMotivationClassifierSystems  = _MotivationClassifierSystems.begin();
00550                         itMotivationClassifierSystems != _MotivationClassifierSystems.end();
00551                         itMotivationClassifierSystems++)
00552                 {
00553                         TMotivation laMotive = (*itMotivationClassifierSystems).first;
00554                         for (i= 0; i< (*itMotivationClassifierSystems).second.getClassifierNumber(); i++)
00555                         {
00556                                 ohlabellephrase += "MAX ";
00557                                 ohlabellephrase += NLAINIMAT::conversionMotivation.toString(laMotive);
00558                                 ohlabellephrase += " ";
00559                                 (*itMotivationClassifierSystems).second.getDebugString(i,ohlabellephrase);
00560                                 ohlabellephrase += ";T2S ";
00561                                 ohlabellephrase += NLAINIMAT::conversionMotivation.toString(laMotive);
00562                                 ohlabellephrase += " ";
00563                                 (*itMotivationClassifierSystems).second.getDebugString(i,ohlabellephrase);
00564                                 ohlabellephrase += ";ExT ";
00565                                 ohlabellephrase += NLAINIMAT::conversionMotivation.toString(laMotive);
00566                                 ohlabellephrase += " ";
00567                                 (*itMotivationClassifierSystems).second.getDebugString(i,ohlabellephrase);
00568                                 //                              ohlabellephrase += NLMISC::toString(i);
00569                                 
00570                                 ohlabellephrase += ";";
00571                         }
00572                 }
00573                 ohlabellephrase += "\n";
00574         }
00575         else
00576         {
00577                 melkior.open(nomDuFichier.c_str(), true, true);
00578         }
00579 
00580         for(itMotivationClassifierSystems  = _MotivationClassifierSystems.begin();
00581                 itMotivationClassifierSystems != _MotivationClassifierSystems.end();
00582                 itMotivationClassifierSystems++)
00583         {
00584                 TMotivation laMotive = (*itMotivationClassifierSystems).first;
00585                 for (i= 0; i< (*itMotivationClassifierSystems).second.getClassifierNumber(); i++)
00586                 {
00587                         CClassifierPriority laSuperPrio = getPriorityPart(laMotive, i);
00588                         ohlabellephrase += NLMISC::toString(laSuperPrio.getPriority());
00589                         ohlabellephrase += ";";
00590                         ohlabellephrase += NLMISC::toString(laSuperPrio.getPriorityTimer());
00591                         ohlabellephrase += ";";
00592                         ohlabellephrase += NLMISC::toString(laSuperPrio.getClassifierTimer());
00593                         ohlabellephrase += ";";
00594                 }
00595         }
00596         ohlabellephrase += "\n";
00597         
00598         
00599         uint8 *buf = (uint8 *)ohlabellephrase.c_str();
00600         uint len = ohlabellephrase.length();
00601         melkior.serialBuffer(buf,len);
00602         melkior.close();
00603 }
00604 
00605 void CMHiCSbase::learningUpdatePriorityValueTimeToSuccess(TMotivation motivationName, TClassifierNumber classifierNumber, uint32 timeToSuccess)
00606 {
00607         // Le but est de faire une moyenne sur les valeurs de fitness. Pour ça on fait une moyenne sur les 4 pas de temps précédents.
00608         std::map<TMotivation, CClassifierSystem>::iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00609         nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00610         uint32 oldTime2Success = (*itMotivationClassifierSystems).second.getPriorityPart(classifierNumber).getPriorityTimer();
00611         uint32 newTime2Success;
00612         if (oldTime2Success == 0)
00613         {
00614                 newTime2Success = timeToSuccess;
00615         }
00616         else
00617         {
00618                 if (oldTime2Success > timeToSuccess)
00619                 {
00620                         newTime2Success = (timeToSuccess*3 + oldTime2Success)/4;
00621                 }
00622                 else
00623                 {
00624                         newTime2Success = (timeToSuccess + oldTime2Success*3)/4;
00625                 }
00626         }
00627 //      nlassert (newTime2Success != 0);
00628         CClassifierPriority newPrio;
00629         newPrio.setPriorityTimer(newTime2Success);
00630 //      newPrio.setPriorityTimer(timeToSuccess);
00631         newPrio.SetClassifierTimer((*itMotivationClassifierSystems).second.getPriorityPart(classifierNumber).getClassifierTimer());
00632         (*itMotivationClassifierSystems).second.setPriorityPart(classifierNumber, newPrio);
00633 }
00634 void CMHiCSbase::learningUpdatePriorityValueClassifierTime(TMotivation motivationName, TClassifierNumber classifierNumber, uint32 time)
00635 {
00636         // Le but est de faire une moyenne sur les valeurs de fitness. Pour ça on fait une moyenne sur les 10 pas de temps précédents.
00637         std::map<TMotivation, CClassifierSystem>::iterator itMotivationClassifierSystems = _MotivationClassifierSystems.find(motivationName);
00638         nlassert(itMotivationClassifierSystems != _MotivationClassifierSystems.end());
00639         uint32 oldTime = (*itMotivationClassifierSystems).second.getPriorityPart(classifierNumber).getClassifierTimer();
00640         uint32 newTime;
00641         if (oldTime == 0)
00642         {
00643                 newTime = time;
00644         }
00645         else
00646         {
00647                 if (oldTime > time)
00648                 {
00649                         newTime = (time + oldTime*3)/4;
00650                 }
00651                 else
00652                 {
00653                         newTime = (time*3 + oldTime)/4;
00654                 }
00655         }
00656         CClassifierPriority newPrio;
00657         newPrio.setPriorityTimer((*itMotivationClassifierSystems).second.getPriorityPart(classifierNumber).getPriorityTimer());
00658         newPrio.SetClassifierTimer(newTime);
00659 //      newPrio.SetClassifierTimer(time);
00660         (*itMotivationClassifierSystems).second.setPriorityPart(classifierNumber, newPrio);
00661 }
00662 
00663 
00665 // CMHiCSagent
00667 
00668 CMHiCSagent::CMHiCSagent(CMHiCSbase* pMHiCSbase)
00669 {
00670         nlassert (pMHiCSbase != NULL);
00671         _pMHiCSbase = pMHiCSbase;
00672         _pActionsExecutionIntensityByTarget = new std::map<TTargetId, std::map<TAction, CMotivationEnergy> >();
00673         _pOldActionsExecutionIntensityByTarget = new std::map<TTargetId, std::map<TAction, CMotivationEnergy> >();
00674         _Learning = true;
00675         _pInfoClassifierActivity = new std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> >();
00676         _pOldInfoClassifierActivity = new std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> >();
00677         //      _ActionsExecutionIntensity[Action_DoNothing] = CMotivationEnergy();
00678 //      _IdByActions[Action_DoNothing] = NullTargetId;
00679 //      _ItCurrentAction = _IdByActions.find(Action_DoNothing);
00680 }
00681 
00682 CMHiCSagent::~CMHiCSagent()
00683 {
00684         delete _pOldActionsExecutionIntensityByTarget;
00685         delete _pActionsExecutionIntensityByTarget;
00686         delete _pInfoClassifierActivity;
00687         delete _pOldInfoClassifierActivity;
00688 }
00689 
00691 std::string CMHiCSagent::targetId2String(TTargetId id) const
00692 {
00693 /*      // Le format est celui pour afficher en debug le Nb comme dans Ryzom.
00694         uint32 aiBoteId = id;
00695         uint32 managerID = (aiBoteId>>(8+12))&( (1<<10)-1 );
00696         uint32 groupeID = (aiBoteId>>8)&( (1<<12)-1 );
00697         uint32 boteID = aiBoteId&( (1<<8)-1 );
00698         char result[30];
00699         sprintf(result,"AI:%04x:BOT:%04x:%04x:%04x",aiBoteId,managerID,groupeID,boteID);
00700         return result;
00701 */
00702         std::string ret = NLMISC::toString(id);
00703         return ret;
00704 }
00705 
00707 void CMHiCSagent::getDebugString(std::string &t) const
00708 {
00709         std::string ret = "\n\n---------------------------\n";
00710         ret += "\nPerceptions :";
00711         ret += "\n Without target";
00712         NLAINIMAT::TSensorMap::const_iterator itNoTargetSensors;
00713         for (itNoTargetSensors  = _pSensorsValues->NoTargetSensors.begin();
00714                  itNoTargetSensors != _pSensorsValues->NoTargetSensors.end();
00715                  itNoTargetSensors++)
00716         {
00717                 ret += "\n  " + conversionSensor.toString((*itNoTargetSensors).first) + "(" + (*itNoTargetSensors).second + ")";
00718         }
00719         std::map<TTargetId, TSensorMap>::const_iterator itTargetSensors;
00720         for (itTargetSensors  = _pSensorsValues->TargetSensors.begin();
00721                  itTargetSensors != _pSensorsValues->TargetSensors.end();
00722                  itTargetSensors++)
00723         {
00724                 ret += "\n On target n#" + targetId2String((*itTargetSensors).first);
00725                 for (itNoTargetSensors  = (*itTargetSensors).second.begin();
00726                          itNoTargetSensors != (*itTargetSensors).second.end();
00727                          itNoTargetSensors++)
00728                 {
00729                          ret += "\n  " + conversionSensor.toString((*itNoTargetSensors).first) + "(" + (*itNoTargetSensors).second + ")";
00730                 }
00731         }
00732         ret += "\n\nMotivations :";
00733         std::map<TMotivation, CMotivationEnergy>::const_iterator itClassifiersAndMotivationIntensity;
00734         for (itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.begin();
00735                  itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end();
00736                  itClassifiersAndMotivationIntensity++)
00737         {
00738                 ret += "\n <" + conversionMotivation.toString((*itClassifiersAndMotivationIntensity).first) + "> ";
00739                 ret += "[MI=" + NLMISC::toString((*itClassifiersAndMotivationIntensity).second.getSumValue()) + "] :";
00740                 (*itClassifiersAndMotivationIntensity).second.getDebugString(ret);
00741 //              ret += "\n  -> Classifier number " + NLMISC::toString((*itClassifiersAndMotivationIntensity).second.ClassifierNumber); 
00742                 ret += "\n";
00743         }
00744 //      ret += "\nVirtual Actions :";
00745 //      std::map<TAction, CMotivationEnergy>::const_iterator itClassifiersAndVirtualActionIntensity;
00746 //      for (itClassifiersAndVirtualActionIntensity = _ClassifiersAndVirtualActionIntensity.begin();
00747 //               itClassifiersAndVirtualActionIntensity != _ClassifiersAndVirtualActionIntensity.end();
00748 //               itClassifiersAndVirtualActionIntensity++)
00749 //      {
00750 //              ret += "\n <" + conversionAction.toString((*itClassifiersAndVirtualActionIntensity).first) + "> ";
00751 //              ret += "[MI=" + NLMISC::toString((*itClassifiersAndVirtualActionIntensity).second.getSumValue()) + "] :";
00752 //              (*itClassifiersAndVirtualActionIntensity).second.getDebugString(ret);
00757 //              ret += "\n  -> Classifier number " + NLMISC::toString((*itClassifiersAndVirtualActionIntensity).second.ClassifierNumber); 
00758 //              ret += "\n";
00759 //      }
00760         ret += "\nACTIONS :";
00761         std::map<TAction, CMotivationEnergy>::const_iterator itActionsExecutionIntensity;
00762 //      for (itActionsExecutionIntensity = _ActionsExecutionIntensity.begin(); itActionsExecutionIntensity != _ActionsExecutionIntensity.end(); itActionsExecutionIntensity++)
00763 //      {
00764 //              ret += "\n <" + conversionAction.toString((* itActionsExecutionIntensity).first) + "> [EI=" + NLMISC::toString((*itActionsExecutionIntensity).second.getSumValue()) + "] : ";
00765 //              (*itActionsExecutionIntensity).second.getDebugString(ret);
00766 //              std::map<TAction, TTargetId>::const_iterator itIdByActions = _IdByActions.find((*itActionsExecutionIntensity).first);
00767 //              nlassert (itIdByActions != _IdByActions.end());
00769 //              ret += " on target n#" + targetId2String((*itIdByActions).second);
00770 //      }
00771 //      if (_ItCurrentAction != _IdByActions.end())
00772 //      {
00774 //              ret += "\nACTION ACTIVE : " + NLAINIMAT::conversionAction.toString((*_ItCurrentAction).first) + " on " + targetId2String((*_ItCurrentAction).second);
00775 //      }
00776         t+=ret;
00777 }
00778 
00780 void CMHiCSagent::setMotivationPP(TMotivation motivationName, double PP)
00781 {
00782         _ClassifiersAndMotivationIntensity[motivationName].setMHiCSagent(this);
00783         _ClassifiersAndMotivationIntensity[motivationName].setMotivationPP(motivationName, PP);
00784 //      spreadMotivationReckon(motivationName);
00785 }
00786 
00788 double CMHiCSagent::getMotivationPP(TMotivation motivationName) const
00789 {
00790         std::map<TMotivation, CMotivationEnergy>::const_iterator itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.find(motivationName);
00791         if (itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end()) 
00792         {
00793                 return (*itClassifiersAndMotivationIntensity).second.getMotivationPP(motivationName);
00794         }
00795         else
00796         {
00797                 return -1;
00798         }
00799 }
00800 
00802 void CMHiCSagent::setMotivationValue(TMotivation motivationName, double value)
00803 {
00804 //      if (_Learning)
00805 //      {
00806 //              double lastMotiveValue = _ClassifiersAndMotivationIntensity[motivationName].getMotivationValue(motivationName);
00807 //              // Si la valeur de motivation a diminuée, il est temps d'apprendre
00808 //              if (lastMotiveValue > value)
00809 //              {
00810 //                      learningComputationMotivationDecrease(motivationName);
00811 //              }
00812 //      }
00813 
00814         std::map<TMotivation, CMotivationEnergy>::iterator itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.find(motivationName);
00815         if (itClassifiersAndMotivationIntensity == _ClassifiersAndMotivationIntensity.end())
00816         {
00817                 itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.insert(std::make_pair(motivationName,CMotivationEnergy())).first;
00818 
00819                 // On en profite pour mettre à zero la date de la première réponse positive d'une motivation
00820 //              _TimeOfLastMotivationValueDecrease[motivationName] = NLMISC::CTime::getSecondsSince1970();
00821         }
00822         (*itClassifiersAndMotivationIntensity).second.setMHiCSagent(this);
00823         (*itClassifiersAndMotivationIntensity).second.setMotivationValue(motivationName, value);
00824         //      spreadMotivationReckon(motivationName);
00825 }
00826 
00827 double  CMHiCSagent::getMotivationValue(TMotivation motivationName) const
00828 {
00829         std::map<TMotivation, CMotivationEnergy>::const_iterator itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.find(motivationName);
00830         if (itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end()) 
00831         {
00832                 return (*itClassifiersAndMotivationIntensity).second.getMotivationValue(motivationName);
00833         }
00834         else
00835         {
00836                 return -1;
00837         }
00838 }
00839 
00841 //double CMHiCSagent::getMotivationIntensity(TAction virtualAction) const
00842 //{
00843 //      std::map<TAction, CMotivationEnergy>::const_iterator itClassifiersAndVirtualActionIntensity = _ClassifiersAndVirtualActionIntensity.find(virtualAction);
00844 //      if (itClassifiersAndVirtualActionIntensity != _ClassifiersAndVirtualActionIntensity.end()) 
00845 //      {
00846 //              return (*itClassifiersAndVirtualActionIntensity).second.getSumValue();
00847 //      }
00848 //      else
00849 //      {
00850 //              return -1;
00851 //      }
00852 //}
00853 
00855 double CMHiCSagent::getExecutionIntensity(TAction action, TTargetId target) const
00856 {
00857         std::map<TTargetId, std::map<TAction, CMotivationEnergy> >::const_iterator itActionsExecutionIntensityByTarget;
00858         itActionsExecutionIntensityByTarget = _pActionsExecutionIntensityByTarget->find(target);
00859         if (itActionsExecutionIntensityByTarget == _pActionsExecutionIntensityByTarget->end())
00860         {
00861                 return -1;
00862         }
00863         std::map<TAction, CMotivationEnergy>::const_iterator itActionsExecutionIntensity = (*itActionsExecutionIntensityByTarget).second.find(action);
00864         if (itActionsExecutionIntensity != (*itActionsExecutionIntensityByTarget).second.end()) 
00865         {
00866                 return (*itActionsExecutionIntensity).second.getSumValue();
00867         }
00868         else
00869         {
00870                 return -1;
00871         }
00872 }
00873 
00874 
00875 //void CMHiCSagent::spreadMotivationReckon(TMotivation CS)
00876 //{
00877 //      std::map<TMotivation, CMotivationEnergy>::iterator itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.find(CS);
00878 //      nlassert(itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end());
00879 //      TClassifierNumber lastClassifierNumber = (*itClassifiersAndMotivationIntensity).second.ClassifierNumber;
00880 //      if (lastClassifierNumber >=0 )
00881 //      {
00882 //              TAction lastActionName = _pMHiCSbase->getActionPart(CS, lastClassifierNumber);
00883 //              if (_pMHiCSbase->isAnAction(lastActionName))
00884 //              {
00885 //                      std::map<TAction, CMotivationEnergy>::iterator itActionsExecutionIntensity =
00886 //                                                                                                                      _ActionsExecutionIntensity.find(lastActionName);
00887 //                      // Test if the action selected hasn't been removed.
00888 //                      if (itActionsExecutionIntensity == _ActionsExecutionIntensity.end()) return;
00889 //
00890 //                      // Update the motivation provider for the action execution intensity.
00891 //                      (*itActionsExecutionIntensity).second.updateProvider(CS,
00892 //                                                                                                                               (*itClassifiersAndMotivationIntensity).second.MotivationIntensity);
00893 //                      // If the action doesn't receive motivation any more, we remove it.
00894 //                      double energy = (*itActionsExecutionIntensity).second.getSumValue();
00895 //                      if (energy <= 0)
00896 //                      {
00897 //                              _ActionsExecutionIntensity.erase(lastActionName);
00898 //                              _IdByActions.erase(lastActionName);
00899 //                              // we check if it was the current action
00900 //                              if ((*_ItCurrentAction).first == lastActionName)
00901 //                              {
00902 //                                      _ItCurrentAction = _IdByActions.find(Action_DoNothing);
00903 //                                      nlassert (_ItCurrentAction != _IdByActions.end());
00904 //                              }
00905 //                      }
00906 //              }
00907 //              else
00908 //              {
00909 //                      std::map<TAction, CMotivationEnergy>::iterator itClassifiersAndVirtualActionIntensity = 
00910 //                                                                                                              _ClassifiersAndVirtualActionIntensity.find(lastActionName);
00911 //                      // Test if the virtual action selected hasn't been removed.
00912 //                      if (itClassifiersAndVirtualActionIntensity == _ClassifiersAndVirtualActionIntensity.end()) return;
00913 //                      
00914 //                      // Update the motivation provider for the virtual action execution intensity.
00915 //                      (*itClassifiersAndVirtualActionIntensity).second.updateProvider(CS,
00916 //                                                                                                                              (*itClassifiersAndMotivationIntensity).second.MotivationIntensity);
00917 //                      spreadMotivationReckon(lastActionName);
00918 //                      // If the CS doesn't receive motivation any more, we remove it.
00919 //                      double energy = (*itClassifiersAndVirtualActionIntensity).second.getSumValue();
00920 //                      if (energy <= 0)
00921 //                      {
00922 //                              _ClassifiersAndVirtualActionIntensity.erase(lastActionName);
00923 //                      }
00924 //              }
00925 //      }
00926 //}
00927 //
00928 //void CMHiCSagent::spreadMotivationReckon(TAction CS)
00929 //{
00930 //      std::map<TAction, CMotivationEnergy>::iterator itClassifiersAndVirtualActionIntensityORIGIN = 
00931 //                                                                                                                                      _ClassifiersAndVirtualActionIntensity.find(CS);
00932 //      nlassert(itClassifiersAndVirtualActionIntensityORIGIN != _ClassifiersAndVirtualActionIntensity.end());
00933 //      TClassifierNumber lastClassifierNumber = (*itClassifiersAndVirtualActionIntensityORIGIN).second.ClassifierNumber;
00934 //      if (lastClassifierNumber >=0 )
00935 //      {
00936 //              TAction lastActionName = _pMHiCSbase->getActionPart(CS, lastClassifierNumber);
00937 //              if (_pMHiCSbase->isAnAction(lastActionName))
00938 //              {
00939 //                      std::map<TAction, CMotivationEnergy>::iterator itActionsExecutionIntensity =
00940 //                              _ActionsExecutionIntensity.find(lastActionName);
00941 //                      // Test if the action selected hasn't been removed.
00942 //                      if (itActionsExecutionIntensity == _ActionsExecutionIntensity.end()) return;
00943 //                      
00944 //                      // Update the motivation provider for the action execution intensity.
00945 //                      (*itActionsExecutionIntensity).second.updateProvider(CS,
00946 //                              (*itClassifiersAndVirtualActionIntensityORIGIN).second.MotivationIntensity);
00947 //                      // If the action doesn't receive motivation any more, we remove it.
00948 //                      double energy = (*itActionsExecutionIntensity).second.getSumValue();
00949 //                      if (energy <= 0)
00950 //                      {
00951 //                              _ActionsExecutionIntensity.erase(lastActionName);
00952 //                              _IdByActions.erase(lastActionName);
00953 //                              // we check if it was the current action
00954 //                              if ((*_ItCurrentAction).first == lastActionName)
00955 //                              {
00956 //                                      _ItCurrentAction = _IdByActions.find(Action_DoNothing);
00957 //                                      nlassert (_ItCurrentAction != _IdByActions.end());
00958 //                              }
00959 //                      }
00960 //              }
00961 //              else
00962 //              {
00963 //                      std::map<TAction, CMotivationEnergy>::iterator itClassifiersAndVirtualActionIntensity = 
00964 //                              _ClassifiersAndVirtualActionIntensity.find(lastActionName);
00965 //                      // Test if the virtual action selected hasn't been removed.
00966 //                      if (itClassifiersAndVirtualActionIntensity == _ClassifiersAndVirtualActionIntensity.end()) return;
00967 //                      
00968 //                      // Update the motivation provider for the virtual action execution intensity.
00969 //                      (*itClassifiersAndVirtualActionIntensity).second.updateProvider(CS,
00970 //                                                                                                              (*itClassifiersAndVirtualActionIntensityORIGIN).second.MotivationIntensity);
00971 //                      spreadMotivationReckon(lastActionName);
00972 //                      // If the CS doesn't receive motivation any more, we remove it.
00973 //                      double energy = (*itClassifiersAndVirtualActionIntensity).second.getSumValue();
00974 //                      if (energy <= 0)
00975 //                      {
00976 //                              _ClassifiersAndVirtualActionIntensity.erase(lastActionName);
00977 //                      }
00978 //              }
00979 //      }
00980 //}
00981 
00982 void CMHiCSagent::motivationCompute()
00983 {
00984         TAction behav;
00985 
00986         std::map<TMotivation, CMotivationEnergy>::iterator itClassifiersAndMotivationIntensity;
00987 
00988         // On parcour toutes les motivations.
00989         for (itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.begin();
00990                  itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end();
00991                  itClassifiersAndMotivationIntensity++)
00992         {
00993                 CMotivationEnergy* pCSselection = &((*itClassifiersAndMotivationIntensity).second);
00994                 TMotivation selectionName = (*itClassifiersAndMotivationIntensity).first;
00995                 double energy = pCSselection->getSumValue();
00996                 // Si une motivation est active (une energie >0 ) on actionne ses règles.
00997                 if (energy > 0)
00998                 {
00999                         // On fait calculer le CS
01000                         std::multimap<CClassifierPriority, std::pair<TClassifierNumber, TTargetId> > mapActivableCS;
01001                         std::multimap<CClassifierPriority, std::pair<TClassifierNumber, TTargetId> >::iterator itMapActivableCS;
01002 
01003                         // On fait la liste des classeurs activables.
01004                         _pMHiCSbase->selectBehavior(selectionName,_pSensorsValues, mapActivableCS);
01005 
01006                         // Pour chaque classeur activable, on transmet la valeur de motivation à l'action selectionnée.
01007                         for (itMapActivableCS = mapActivableCS.begin(); itMapActivableCS != mapActivableCS.end(); itMapActivableCS++)
01008                         {
01009                                 TClassifierNumber selectedClassifierNumber = (*itMapActivableCS).second.first;
01010                                 TTargetId currentTargetId = (*itMapActivableCS).second.second;
01011                                 behav = _pMHiCSbase->getActionPart(selectionName, selectedClassifierNumber);
01012 
01013                                 (*_pInfoClassifierActivity)[selectionName][selectedClassifierNumber].IsActivable = true;
01014                                 
01015                                 // We add the current motivation energy to the selected action.
01016                                 if (behav != Action_DoNothing)
01017                                 {
01018 //                                      std::map<TTargetId, std::map<TAction, CMotivationEnergy> >::const_iterator itOldActionsExecutionIntensityByTarget = (*_pOldActionsExecutionIntensityByTarget).find(currentTargetId);
01019 //                                      if (itOldActionsExecutionIntensityByTarget != (*_pOldActionsExecutionIntensityByTarget).end() )
01020 //                                      {
01021 //                                              std::map<TAction, CMotivationEnergy>::const_iterator itIntensityByTarget = (*itOldActionsExecutionIntensityByTarget).second.find(behav);
01022 //                                              if (itIntensityByTarget != (*itOldActionsExecutionIntensityByTarget).second.end())
01023 //                                              {
01024 //                                                      (*_pActionsExecutionIntensityByTarget)[currentTargetId][behav].setWasPreviouslyActived(true);
01025 //                                              }
01026 //                                      }
01027                                         (*_pActionsExecutionIntensityByTarget)[currentTargetId][behav].setMHiCSagent(this);
01028                                         (*_pActionsExecutionIntensityByTarget)[currentTargetId][behav].addProvider(selectionName, selectedClassifierNumber);
01029                                 }
01030                         }
01031                 }
01032         }
01033 }
01034 
01035 
01036 
01037 //void CMHiCSagent::virtualActionCompute()
01038 //{
01039 //      /*
01040 //      Je sélectionne par roulette weel l'action virtuel que je vais gérer
01041 //      Je met à jour l'énergie du vainqueur
01042 //      */
01043 //      double somme = 0;
01044 //      typedef std::map<TAction, CMotivationEnergy>::iterator TitNameAndVirtualAction;
01045 //      std::map<double, TitNameAndVirtualAction > mapCSweel;
01046 //      std::map<TAction, CMotivationEnergy>::iterator itClassifiersAndVirtualActionIntensity;
01047 //      // On calcule la somme
01048 //      for (itClassifiersAndVirtualActionIntensity = _ClassifiersAndVirtualActionIntensity.begin();
01049 //               itClassifiersAndVirtualActionIntensity != _ClassifiersAndVirtualActionIntensity.end();
01050 //               itClassifiersAndVirtualActionIntensity++)
01051 //      {
01052 //              CMotivationEnergy* pCMotivationEnergy = &((*itClassifiersAndVirtualActionIntensity).second);
01053 //              double energy = pCMotivationEnergy->getSumValue();
01054 //              if (energy > 0)
01055 //              {
01056 //                      somme += energy;
01057 //                      mapCSweel[somme] = itClassifiersAndVirtualActionIntensity;
01058 //              }
01059 //      }
01060 //      if (somme>0)
01061 //      {
01062 //              // on selectionne le classeur;
01063 //              double randomeNumber = (rand()%(int(somme*100)))/100.0;
01064 //              std::map<double, TitNameAndVirtualAction>::iterator itMapCSweel = mapCSweel.upper_bound(randomeNumber);
01065 //              CMotivationEnergy* pCSselection = &((*((*itMapCSweel).second)).second);
01066 //              TAction selectionName = (*((*itMapCSweel).second)).first;
01067 //
01068 //              // Get the target Id for this Virtual Action
01069 //              std::map<TAction, TTargetId>::const_iterator itIdByActions = _IdByActions.find(selectionName);
01070 //              nlassert (itIdByActions != _IdByActions.end());
01071 //              TTargetId myTarget = (*itIdByActions).second;
01072 //
01073 //              // On fait calculer le CS
01074 //              TClassifierNumber lastClassifierNumber = _ClassifiersAndVirtualActionIntensity[selectionName].ClassifierNumber;
01075 //              TClassifierNumber selectedClassifierNumber = lastClassifierNumber;
01076 //              TTargetId currentTargetId = myTarget;
01077 //              double lastSelectionMaxPriority = _ClassifiersAndVirtualActionIntensity[selectionName].LastSelectionMaxPriority;
01078 //              std::multimap<double, std::pair<TClassifierNumber, TTargetId> > mapActivableCS;
01079 //              
01080 //              _pMHiCSbase->selectBehavior(selectionName,_pSensorsValues, mapActivableCS, currentTargetId);
01081 //
01083 //              {
01084 //                      // ***G*** Ici on décide de rien faire si on sait pas quoi faire. En fait il faudrait créer un règle.
01085 //                      _ClassifiersAndVirtualActionIntensity[selectionName].ClassifierNumber = selectedClassifierNumber;
01086 //                      _ClassifiersAndVirtualActionIntensity[selectionName].TargetId = currentTargetId;
01087 //                      _ClassifiersAndVirtualActionIntensity[selectionName].LastSelectionMaxPriority = lastSelectionMaxPriority;
01088 //                      return; 
01089 //              }
01090 //*/            
01091 //              TAction behav = _pMHiCSbase->getActionPart(selectionName, selectedClassifierNumber);
01092 //
01093 //              // We check the last action selected by the current motivation to remove the motivation influence on this action.
01094 //              if (lastClassifierNumber >= 0)
01095 //              {
01096 //                      TAction lastActionName = _pMHiCSbase->getActionPart(selectionName, lastClassifierNumber);
01097 //
01098 //                      // We check if we have selected the same behavior.
01099 //                      if (lastActionName != behav)
01100 //                      {
01101 //                              if (_pMHiCSbase->isAnAction(lastActionName))
01102 //                              {
01103 //                                      _ActionsExecutionIntensity[lastActionName].removeProvider(selectionName);
01104 //                                      // If the action doesn't receive motivation any more, we remove it.
01105 //                                      double energy = _ActionsExecutionIntensity[lastActionName].getSumValue();
01106 //                                      if (energy <= 0)
01107 //                                      {
01108 //                                              _ActionsExecutionIntensity.erase(lastActionName);
01109 //                                              _IdByActions.erase(lastActionName);
01110 //                                              // we check if it was the current action
01111 //                                              if ((*_ItCurrentAction).first == lastActionName)
01112 //                                              {
01113 //                                                      _ItCurrentAction = _IdByActions.find(Action_DoNothing);
01114 //                                                      nlassert (_ItCurrentAction != _IdByActions.end());
01115 //                                              }
01116 //                                      }
01117 //                              }
01118 //                              else
01119 //                              {
01120 //                                      _ClassifiersAndVirtualActionIntensity[lastActionName].removeProvider(selectionName);
01121 //                                      spreadMotivationReckon(lastActionName);
01122 //                                      // If the CS doesn't receive motivation any more, we remove it.
01123 //                                      double energy = _ClassifiersAndVirtualActionIntensity[lastActionName].getSumValue();
01124 //                                      if (energy <= 0)
01125 //                                      {
01126 //                                              _ClassifiersAndVirtualActionIntensity.erase(lastActionName);
01127 //                                      }
01128 //                              }
01129 //                      }
01130 //              }
01131 //
01132 //              // We store the number of the new classifier actived by this motivation.
01133 //              _ClassifiersAndVirtualActionIntensity[selectionName].ClassifierNumber = selectedClassifierNumber;
01134 //              _ClassifiersAndVirtualActionIntensity[selectionName].TargetId = currentTargetId;
01135 //              _ClassifiersAndVirtualActionIntensity[selectionName].LastSelectionMaxPriority = lastSelectionMaxPriority;
01136 //              
01137 //              if (selectedClassifierNumber >= 0)
01138 //              {
01139 //                      // We add the current motivation energy to the selected action.
01140 //                      if (_pMHiCSbase->isAnAction(behav))
01141 //                      {
01142 //                              _ActionsExecutionIntensity[behav].addProvider(selectionName, pCSselection->MotivationIntensity);
01143 //                      }
01144 //                      else
01145 //                      {
01146 //                              // Else it must be a virtual action (common CS)
01147 //                              _ClassifiersAndVirtualActionIntensity[behav].addProvider(selectionName, pCSselection->MotivationIntensity);
01148 //                              spreadMotivationReckon(behav);
01149 //                      }
01150 //
01151 //                      // We set the Id of this action.
01152 //                      // For moment there's no test to see if it is the same target or not. In the futur it can be usefull to make this test
01153 //                      // to avoid unwilled target switch.
01154 //                      _IdByActions[behav] = currentTargetId;
01155 //              }
01156 //      }
01157 //}
01158 
01159 void CMHiCSagent::run()
01160 {
01161         std::map<TTargetId, std::map<TAction, CMotivationEnergy> >      *bibu = _pOldActionsExecutionIntensityByTarget;
01162         _pOldActionsExecutionIntensityByTarget = _pActionsExecutionIntensityByTarget;
01163         _pActionsExecutionIntensityByTarget = bibu;
01164         _pActionsExecutionIntensityByTarget->clear();
01165 
01166         std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> > *biba = _pOldInfoClassifierActivity;
01167         _pOldInfoClassifierActivity = _pInfoClassifierActivity;
01168         _pInfoClassifierActivity = biba;
01169         _pInfoClassifierActivity->clear();
01170         
01171         motivationCompute();
01172 //      virtualActionCompute();
01173 }
01174 
01175 
01176 void CMHiCSagent::setSensors(CCSPerception* psensorMap)
01177 {
01178         _pSensorsValues = psensorMap;
01179 }
01180 
01181 void CMHiCSagent::setLearning(bool active)
01182 {
01183         _Learning = active;
01184 }
01185 
01186 //void CMHiCSagent::learningComputationMotivationDecrease(TMotivation motivationName)
01187 //{
01188 //      // On calcule le temps en seconde depuis la dernière récompense
01189 //      uint32 previousLastTime = 0;
01190 //      uint32 newTime = NLMISC::CTime::getSecondsSince1970();
01191 //
01192 //      std::map<TMotivation, uint32>::const_iterator itTimeOfLastMotivationValueDecrease = _TimeOfLastMotivationValueDecrease.find(motivationName);
01193 //      if (itTimeOfLastMotivationValueDecrease != _TimeOfLastMotivationValueDecrease.end())
01194 //      {
01195 //              previousLastTime = (*itTimeOfLastMotivationValueDecrease).second;
01196 //      }
01197 //
01198 //      _TimeOfLastMotivationValueDecrease[motivationName] = newTime;
01199 //      TClassifierPriority diffTime = newTime - previousLastTime;
01200 //      nlassert (diffTime > 0);
01201 //
01202 //      // Pour tous les classeurs actifs de la motivation, je met à jour la fitness avec cette nouvelle valeur de temps (et je met à jour le temps).
01203 //      std::multimap<TMotivation, std::pair <TClassifierNumber, uint32> >::iterator itActiveClassifiersByMotivation = _ActiveClassifiersByMotivation.find(motivationName);
01204 //      if (itActiveClassifiersByMotivation != _ActiveClassifiersByMotivation.end() )
01205 //      {
01206 //              uint i;
01207 //              for (i = 0; i < _ActiveClassifiersByMotivation.count(motivationName); i++ )
01208 //              {
01209 //                      TClassifierNumber leNumeroDuClasseur = (*itActiveClassifiersByMotivation).second.first;
01210 //                      _ActiveClassifiersByMotivation.insert(std::make_pair(motivationName, std::make_pair(leNumeroDuClasseur, newTime)));
01211 //
01212 //                      //***G*** TODO : Faire qqchose avec leNumeroDuClasseur
01213 //                      _pMHiCSbase->learningUpdatePriorityValue(motivationName, leNumeroDuClasseur, diffTime);
01214 //              }
01215 //      }
01216 //}
01217 
01218 
01219 void CMHiCSagent::learningComputation()
01220 {
01221         /*
01222          *      Le but de l'apprentissage par ajustement des valeurs de priorité :
01223          *      La valeur de priorité est utilisé pour choisir un classeur lorsque plusieurs classeurs sont activable simultanement
01224          *      et partagent une même ressource d'action.
01225          *      On veut la faire évoluer afin que dans une situation donnée, c'est le classeur le plus apte à satisfaire la motivation 
01226          *      qui soit sélectionné.
01227          *      Celon le principe de bucket brigade, un classeur qui mène à satisfaire une motivation est un bon classeur.
01228          *      Ensuite un classeur qui a défaut de satisfaire la motivation mène à un classeur pouvant la satisfaire est bon mais un peut moins, etc.
01229          */
01230 
01231         
01232 //      std::map<TMotivation, std::set<TClassifierNumber> > oldClassifierByMotivation; // Liste des classeurs actifs au pas précédent.
01233 //      std::map<TMotivation, std::set<TClassifierNumber> > classifierByMotivation; // Liste des classeurs activés
01234         
01235         //      On note les descentes de motivations
01236         uint32 newTime = NLMISC::CTime::getSecondsSince1970();
01237         std::set<TMotivation> decreasingMotivations;
01238         std::map<TMotivation, CMotivationEnergy>::iterator      itOldClassifiersAndMotivationIntensity, itClassifiersAndMotivationIntensity;
01239         for(itOldClassifiersAndMotivationIntensity = _OldClassifiersAndMotivationIntensity.begin();
01240                 itOldClassifiersAndMotivationIntensity != _OldClassifiersAndMotivationIntensity.end();
01241                 itOldClassifiersAndMotivationIntensity++)
01242         {
01243                 TMotivation motivationName = (*itOldClassifiersAndMotivationIntensity).first;
01244                 double oldMV = (*itOldClassifiersAndMotivationIntensity).second.getMotivationValue(motivationName);
01245                 itClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity.find(motivationName);
01246                 nlassert(itClassifiersAndMotivationIntensity != _ClassifiersAndMotivationIntensity.end());
01247                 double newMV = (*itClassifiersAndMotivationIntensity).second.getMotivationValue(motivationName);
01248                 if (newMV < oldMV)
01249                 {
01250                         decreasingMotivations.insert(motivationName);
01251                 }
01252         }
01253 
01254 //      // On établit la liste des classeurs utilisés précédemment
01255 //      std::map<TMotivation, std::set<TClassifierNumber> >::iterator itOldClassifierByMotivation;
01256 //      std::map<TTargetId, std::map<TAction, CMotivationEnergy> >::iterator itOldActionsExecutionIntensityByTarget;
01257 //      std::map<TAction, CMotivationEnergy>::iterator itOldActionsExecutionIntensity;
01258 //      for(itOldActionsExecutionIntensityByTarget  = _pOldActionsExecutionIntensityByTarget->begin();
01259 //              itOldActionsExecutionIntensityByTarget != _pOldActionsExecutionIntensityByTarget->end();
01260 //              itOldActionsExecutionIntensityByTarget++)
01261 //      {
01262 //              for(itOldActionsExecutionIntensity  = (*itOldActionsExecutionIntensityByTarget).second.begin();
01263 //                      itOldActionsExecutionIntensity != (*itOldActionsExecutionIntensityByTarget).second.end();
01264 //                      itOldActionsExecutionIntensity++)
01265 //              {
01266 //                      const std::map<TMotivation, std::set<TClassifierNumber> >* provounet = (*itOldActionsExecutionIntensity).second.getProviders();
01267 //                      std::map<TMotivation, std::set<TClassifierNumber> >::const_iterator itProvounet;
01268 //                      for(itProvounet  = provounet->begin();
01269 //                              itProvounet != provounet->end();
01270 //                              itProvounet++)
01271 //                      {
01272 //                              TMotivation motivationName = (*itProvounet).first;
01273 //                              std::set<TClassifierNumber>::const_iterator itClassifierNumber;
01274 //                              for (itClassifierNumber = (*itProvounet).second.begin(); itClassifierNumber != (*itProvounet).second.end(); itClassifierNumber++)
01275 //                              {
01276 //                                      TClassifierNumber classifierNumber = (*itClassifierNumber);
01277 //                                      // On établit une liste des classeurs ayant été actifs.
01278 //                                      oldClassifierByMotivation[motivationName].insert(classifierNumber);
01279 //                              }
01280 //                      }
01281 //              }
01282 //      }
01283 
01284         // On remet à zero les compteurs de temps pour les actions qui on satisfait une motivation
01285         std::set<TMotivation>::iterator itDecreasingMotivations;
01286         for (itDecreasingMotivations = decreasingMotivations.begin(); itDecreasingMotivations != decreasingMotivations.end(); itDecreasingMotivations++)
01287         {
01288                 TMotivation motivationName = (*itDecreasingMotivations);
01289                 std::map<TMotivation, std::map<TClassifierNumber, CTemporaryPriority> >::iterator itTemporaryPriority = _TemporaryPriority.find(motivationName);
01290                 if (itTemporaryPriority != _TemporaryPriority.end())
01291                 {
01292                         std::map<TClassifierNumber, CTemporaryPriority>::iterator itPrioByCl;
01293                         for (itPrioByCl = (*itTemporaryPriority).second.begin(); itPrioByCl != (*itTemporaryPriority).second.end(); itPrioByCl++ )
01294                         {
01295                                 // Si le temps a été fixé
01296 //                              bool aUnFixedStartTime = (*itPrioByCl).second.FixedStartTime;
01297 //                              if (aUnFixedStartTime)
01298 //                              {
01299                                         uint32 startTime = (*itPrioByCl).second.StartTime;
01300                                         if (startTime > newTime)
01301                                         {
01302                                                 newTime = startTime;
01303                                         }
01304                                         uint32 timeToSatisfaction = newTime - startTime;
01305                                         TClassifierNumber classifierNumber = (*itPrioByCl).first;
01306                                         // On met à jour la fitness des classeurs ayant été activés.
01307                                         _pMHiCSbase->learningUpdatePriorityValueTimeToSuccess(motivationName, classifierNumber, timeToSatisfaction);
01308 //                              }
01309                         }
01310                 }
01311                 _TemporaryPriority.erase(motivationName);
01312         }
01313                 
01314         // On établit la liste des classeurs nouveaux utilisés à ce pas ci
01315         std::map<TTargetId, std::map<TAction, CMotivationEnergy> >::iterator itActionsExecutionIntensityByTarget;
01316         std::map<TAction, CMotivationEnergy>::iterator itActionsExecutionIntensity;
01317         for(itActionsExecutionIntensityByTarget  = _pActionsExecutionIntensityByTarget->begin();
01318                 itActionsExecutionIntensityByTarget != _pActionsExecutionIntensityByTarget->end();
01319                 itActionsExecutionIntensityByTarget++)
01320         {
01321                 for(itActionsExecutionIntensity  = (*itActionsExecutionIntensityByTarget).second.begin();
01322                         itActionsExecutionIntensity != (*itActionsExecutionIntensityByTarget).second.end();
01323                         itActionsExecutionIntensity++)
01324                 {
01325                         const std::map<TMotivation, std::set<TClassifierNumber> >* provounet = (*itActionsExecutionIntensity).second.getProviders();
01326                         std::map<TMotivation, std::set<TClassifierNumber> >::const_iterator itProvounet;
01327                         for(itProvounet  = provounet->begin();
01328                                 itProvounet != provounet->end();
01329                                 itProvounet++)
01330                         {
01331                                 TMotivation motivationName = (*itProvounet).first;
01332                                 std::set<TClassifierNumber>::const_iterator itClassifierNumber;
01333                                 for (itClassifierNumber = (*itProvounet).second.begin(); itClassifierNumber != (*itProvounet).second.end(); itClassifierNumber++)
01334                                 {
01335                                         TClassifierNumber classifierNumber = (*itClassifierNumber);
01336 //                                      itOldClassifierByMotivation = oldClassifierByMotivation.find(motivationName);
01337 //                                      if (itOldClassifierByMotivation != oldClassifierByMotivation.end() )
01338 //                                      {
01339                                                 // ajout du classeur dans la liste des classeurs activés
01340                                                 (*_pInfoClassifierActivity)[motivationName][classifierNumber].IsActive = true;
01341 //                                              classifierByMotivation[motivationName].insert(classifierNumber);
01342 //                                      }
01343                                 }
01344                         }
01345                 }
01346         }
01347 
01348         // on établit la liste des classeurs qui étaient activables et qui ne le sont plus
01349         std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> >::iterator itOldInfoClassifierActivity;
01350         std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> >::iterator itInfoClassifierActivity;
01351         for(itOldInfoClassifierActivity = (*_pOldInfoClassifierActivity).begin();
01352                 itOldInfoClassifierActivity != (*_pOldInfoClassifierActivity).end();
01353                 itOldInfoClassifierActivity++ )
01354         {
01355                 TMotivation motivationName = (*itOldInfoClassifierActivity).first;
01356                 std::map<TClassifierNumber, CClassifierActivityInfo>::iterator itClassifiers, itClassifiersBis;
01357                 for (itClassifiers = (*itOldInfoClassifierActivity).second.begin(); itClassifiers != (*itOldInfoClassifierActivity).second.end(); itClassifiers++)
01358                 {
01359 //                      std::set<TClassifierNumber>::iterator itClassifiersBisBis;
01360 //                      bool isStillActivable = false;
01361                         TClassifierNumber classifierNumber = (*itClassifiers).first;
01362                         bool wasActive = (*itClassifiers).second.IsActive;
01363                         bool isActive = (*_pInfoClassifierActivity)[motivationName][classifierNumber].IsActive;
01364                         if (wasActive && !isActive)
01365                         {
01366                                 // Le classeur vient de finir son activité, je calcule sa nouvelle priorité.
01367                                 uint32 diffTime = _TemporaryPriority[motivationName][classifierNumber].TemporaryClassifierPriorityTime;
01368                                 if (diffTime >0)
01369                                 {
01370                                         _pMHiCSbase->learningUpdatePriorityValueClassifierTime(motivationName, classifierNumber, diffTime);
01371 //                                      _TemporaryPriority[motivationName][classifierNumber].FixedStartTime = false;
01372                                 }
01373                         }
01374 
01375 //                      bool wasActivable = (*itClassifiers).second.IsActivable;
01376 //                      if (wasActivable)
01377 //                      {
01378 //                              itInfoClassifierActivity = (*_pInfoClassifierActivity).find(motivationName);
01379 //                              if (itInfoClassifierActivity != (*_pInfoClassifierActivity).end())
01380 //                              {
01381 //                                      itClassifiersBis = (*itInfoClassifierActivity).second.find(classifierNumber);
01382 //                                      if ( itClassifiersBis != (*itInfoClassifierActivity).second.end() )
01383 //                                      {
01384 //                                              isStillActivable = true;
01385 //                                      }
01386 //                              }
01387 //                              if (isStillActivable)
01388 //                              {
01389 //                                      // s'il est activable mais plus activé alors on met à jour son compteur de temps d'execution
01390 //                                      bool estActif = (*_pInfoClassifierActivity)[motivationName][classifierNumber].IsActive;
01400 //                                      if (!estActif)
01401 //                                      {
01402 //                                              std::map<TMotivation, std::set<TClassifierNumber> >::iterator itOldClassifierByMotivation = oldClassifierByMotivation.find(motivationName);
01403 //                                              if (itOldClassifierByMotivation != oldClassifierByMotivation.end())
01404 //                                              {
01405 //                                                      itClassifiersBisBis = (*itOldClassifierByMotivation).second.find(classifierNumber);
01406 //                                                      if ( itClassifiersBisBis != (*itOldClassifierByMotivation).second.end() )
01407 //                                                      {
01408 //                                                              // Le classeur vient de finir son activité, je calcule sa nouvelle priorité.
01409 //                                                              uint32 diffTime = _TemporaryPriority[motivationName][classifierNumber].TemporaryClassifierPriorityTime;
01410 //                                                              if (diffTime >0)
01411 //                                                              {
01412 //                                                                      _pMHiCSbase->learningUpdatePriorityValueClassifierTime(motivationName, classifierNumber, diffTime);
01413 //                                                                      _TemporaryPriority[motivationName][classifierNumber].FixedStartTime = false;
01414 //                                                              }
01415 //                                                      }
01416 //                                              }
01417 //                                      }
01418 //                              }
01419 //                              else
01420 //                              {
01421 //                                      // Dans tous les cas on met à jour son temps d'activation s'il a été activé par le passé.
01422 //                                      std::map<TMotivation, std::map<TClassifierNumber, CTemporaryPriority> >::iterator itTemporaryPriority = _TemporaryPriority.find(motivationName);
01423 //                                      if (itTemporaryPriority != _TemporaryPriority.end())
01424 //                                      {
01425 //                                              std::map<TClassifierNumber, CTemporaryPriority>::iterator itClassifiersTerce = (*itTemporaryPriority).second.find(classifierNumber);
01426 //                                              if (itClassifiersTerce != (*itTemporaryPriority).second.end() )
01427 //                                              {
01428 //                                                      // Le classeur vient de finir son activité, je calcule sa nouvelle priorité.
01429 //                                                      uint32 diffTime = _TemporaryPriority[motivationName][classifierNumber].TemporaryClassifierPriorityTime;
01430 //                                                      if (diffTime >0)
01431 //                                                      {
01432 //                                                              _pMHiCSbase->learningUpdatePriorityValueClassifierTime(motivationName, classifierNumber, diffTime);
01433 //                                                              _TemporaryPriority[motivationName][classifierNumber].FixedStartTime = false;
01434 //                                                      }
01435 //                                              }
01436 //                                      }
01437 //                                      
01438 //                                      // S'il n'était pas activé (avant de n'être plus activable), on le retire de la liste des éligibles au T2S
01451 //                                              _TemporaryPriority[motivationName][classifierNumber].FixedStartTime = false;
01453 //                              }
01454 //                      }
01455                 }
01456         }
01457 
01458         std::map<TClassifierNumber, CClassifierActivityInfo>::iterator itUnChtitClassifierNumberInfo;
01459         // On regarde quelles sont les nouveaux classeurs activés
01460         for(itInfoClassifierActivity  = (*_pInfoClassifierActivity).begin();
01461                 itInfoClassifierActivity != (*_pInfoClassifierActivity).end();
01462                 itInfoClassifierActivity++)
01463         {
01464                 TMotivation motivationName = (*itInfoClassifierActivity).first;
01465                 
01466                 for(itUnChtitClassifierNumberInfo  = (*itInfoClassifierActivity).second.begin();
01467                         itUnChtitClassifierNumberInfo != (*itInfoClassifierActivity).second.end();
01468                         itUnChtitClassifierNumberInfo++)
01469                 {
01470                         TClassifierNumber leNumberDuClasseur = (*itUnChtitClassifierNumberInfo).first;
01471                         bool wasActive = (*_pOldInfoClassifierActivity)[motivationName][leNumberDuClasseur].IsActive;
01472                         bool isActive = (*itUnChtitClassifierNumberInfo).second.IsActive;
01473                         if (isActive)
01474                         {
01475                                 if (!wasActive)
01476                                 {
01477                                         _TemporaryPriority[motivationName][leNumberDuClasseur].StartTime = newTime;
01478                                         _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime = newTime;
01479                                         _TemporaryPriority[motivationName][leNumberDuClasseur].FixedStartTime = true;
01480                                         _TemporaryPriority[motivationName][leNumberDuClasseur].TemporaryClassifierPriorityTime = 0;
01481                                 }
01482                                 // Je fais progresser le timer
01483                                 uint32 lastTime = _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime;
01484                                 if (lastTime > newTime)
01485                                 {
01486                                         newTime = lastTime;
01487                                 }
01488                                 uint32 diffTime = newTime - lastTime;
01489                                 _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime = newTime;
01490                                 _TemporaryPriority[motivationName][leNumberDuClasseur].TemporaryClassifierPriorityTime += diffTime;
01491                         }
01492                         
01493 //                      if (isActive)
01494 //                      {
01495 //                              // Si l'action n'avait pas de startTime fixé, on lui en donne un.
01496 //                              bool avaitUnFixedStartTime = false;
01497 //                              std::map<TMotivation, std::map<TClassifierNumber, CTemporaryPriority> >::iterator itTemporaryPriority = _TemporaryPriority.find(motivationName);
01498 //                              if (itTemporaryPriority != _TemporaryPriority.end() )
01499 //                              {
01500 //                                      std::map<TClassifierNumber, CTemporaryPriority>::iterator itClassifierAndPrio = (*itTemporaryPriority).second.find(leNumberDuClasseur);
01501 //                                      if (itClassifierAndPrio != (*itTemporaryPriority).second.end() )
01502 //                                      {
01503 //                                              avaitUnFixedStartTime = (*itClassifierAndPrio).second.FixedStartTime;
01504 //                                      }
01505 //                              }
01506 //                              if (!avaitUnFixedStartTime)
01507 //                              {
01508 //                                      _TemporaryPriority[motivationName][leNumberDuClasseur].StartTime = newTime;
01509 //                                      _TemporaryPriority[motivationName][leNumberDuClasseur].FixedStartTime = true;
01510 //                                      _TemporaryPriority[motivationName][leNumberDuClasseur].TemporaryClassifierPriorityTime = 0;
01511 //                              }
01512 //                              else
01513 //                              {
01514 //                                      // Si elle avait un startime, on regarde si elle était active au tour précédent.
01515 //                                      bool etaitActifJusteAvant = false;
01516 //                                      std::map<TMotivation, std::set<TClassifierNumber> >::iterator itOldClassifierByMotivation = oldClassifierByMotivation.find(motivationName);
01517 //                                      if (itOldClassifierByMotivation != oldClassifierByMotivation.end())
01518 //                                      {
01519 //                                              std::set<TClassifierNumber>::iterator itClassifier = (*itOldClassifierByMotivation).second.find(leNumberDuClasseur);
01520 //                                              if (itClassifier != (*itOldClassifierByMotivation).second.end() )
01521 //                                              {
01522 //                                                      etaitActifJusteAvant = true;
01523 //                                              }
01524 //                                      }
01525 //                                      if (!etaitActifJusteAvant)
01526 //                                      {
01527 //                                              _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime = newTime;
01529 //                                      }
01530 //                              }
01531 //                              
01532 //                              // Je fais progresser le timer
01533 //                              uint32 lastTime = _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime;
01534 //                              if (lastTime > newTime)
01535 //                              {
01536 //                                      newTime = lastTime;
01537 //                              }
01538 //                              uint32 diffTime = newTime - lastTime;
01539 //                              _TemporaryPriority[motivationName][leNumberDuClasseur].LastTime = newTime;
01540 //                              _TemporaryPriority[motivationName][leNumberDuClasseur].TemporaryClassifierPriorityTime += diffTime;
01541 //                      }
01542 
01543                 }
01544         }
01545 }
01546 
01547 
01548 const std::map<TTargetId, std::map<TAction, CMotivationEnergy> >* CMHiCSagent::selectBehavior()
01549 {
01550         //We sort actions by priority
01551         double priority;
01552         TAction action;
01553         TTargetId target;
01554         std::multimap<double, std::pair<TTargetId,TAction> > actionsToRemove;
01555 
01556         std::map<TTargetId, std::map<TAction, CMotivationEnergy> >::iterator itActionsExecutionIntensityByTarget;
01557         std::map<TAction, CMotivationEnergy>::const_iterator itMotiveByAction;
01558         for (itActionsExecutionIntensityByTarget  = _pActionsExecutionIntensityByTarget->begin();
01559                  itActionsExecutionIntensityByTarget != _pActionsExecutionIntensityByTarget->end();
01560                  itActionsExecutionIntensityByTarget++)
01561         {
01562                 for (itMotiveByAction  = (*itActionsExecutionIntensityByTarget).second.begin();
01563                          itMotiveByAction != (*itActionsExecutionIntensityByTarget).second.end();
01564                          itMotiveByAction++)
01565                 {
01566                         priority = (*itMotiveByAction).second.getSumValue();
01567                         action = (*itMotiveByAction).first;
01568                         target = (*itActionsExecutionIntensityByTarget).first;
01569 
01570                         // on rajoute du bruit sur les priorité afin d'avoir une diversité si des priorités sont proches
01571 //                      double randomeNumber = ((rand()%5)*priority)/100;
01572 //                      priority += randomeNumber;
01573                         
01574                         actionsToRemove.insert(std::make_pair(priority, std::make_pair(target,action)));
01575                 }
01576         }
01577 
01578         _pMHiCSbase->pActionResources->filterMyActions(actionsToRemove);
01579 
01580         std::multimap<double, std::pair<TTargetId,TAction> >::iterator itActionsToRemove;
01581         for (itActionsToRemove = actionsToRemove.begin();
01582                  itActionsToRemove != actionsToRemove.end();
01583                  itActionsToRemove++)
01584         {
01585                  target = (*itActionsToRemove).second.first;
01586                  action = (*itActionsToRemove).second.second;
01587                  itActionsExecutionIntensityByTarget = _pActionsExecutionIntensityByTarget->find(target);
01588                  nlassert (itActionsExecutionIntensityByTarget != _pActionsExecutionIntensityByTarget->end());
01589                  itMotiveByAction = (*itActionsExecutionIntensityByTarget).second.find(action);
01590                  nlassert (itMotiveByAction != (*itActionsExecutionIntensityByTarget).second.end());
01591                  (*itActionsExecutionIntensityByTarget).second.erase(action);
01592                  if ( (*itActionsExecutionIntensityByTarget).second.begin() == (*itActionsExecutionIntensityByTarget).second.end() )
01593                  {
01594                          _pActionsExecutionIntensityByTarget->erase(target);
01595                  }
01596         }
01597 
01598         if (_Learning)
01599         {
01600                 learningComputation();
01601 
01602                 _OldClassifiersAndMotivationIntensity = _ClassifiersAndMotivationIntensity;
01603         }
01604 
01605         return _pActionsExecutionIntensityByTarget;
01606 }
01607 
01608 uint32 CMHiCSagent::getTemporaryClassifierPriorityTime(TMotivation motivationName, TClassifierNumber classifierNumber) const
01609 {
01610         std::map<TMotivation, std::map<TClassifierNumber, CTemporaryPriority> >::const_iterator itTemporaryPriority = _TemporaryPriority.find(motivationName);
01611         if (itTemporaryPriority == _TemporaryPriority.end())
01612         {
01613                 return 0;
01614         }
01615         std::map<TClassifierNumber, CTemporaryPriority>::const_iterator itIteratorBis = (*itTemporaryPriority).second.find(classifierNumber);
01616         if (itIteratorBis == (*itTemporaryPriority).second.end())
01617         {
01618                 return 0;
01619         }
01620         uint32 bibureturn = (*itIteratorBis).second.TemporaryClassifierPriorityTime;
01621         return bibureturn;
01622 }
01623 
01624 bool CMHiCSagent::wasClassifierPreviouslyActive (TMotivation motivationName, TClassifierNumber classifierNumber) const
01625 {
01626         bool wasPreviouslyActived = false;
01627 
01628         std::map<TMotivation, std::map<TClassifierNumber, CClassifierActivityInfo> >::const_iterator itOldInfoClassifierActivity;
01629         itOldInfoClassifierActivity = (*_pOldInfoClassifierActivity).find(motivationName);
01630         if (itOldInfoClassifierActivity != _pOldInfoClassifierActivity->end())
01631         {
01632                 std::map<TClassifierNumber, CClassifierActivityInfo>::const_iterator itClassifierInfo = (*itOldInfoClassifierActivity).second.find(classifierNumber);
01633                 if (itClassifierInfo != (*itOldInfoClassifierActivity).second.end() )
01634                 {
01635                         wasPreviouslyActived = (*itClassifierInfo).second.IsActive;
01636                 }
01637         }
01638         return wasPreviouslyActived;    
01639 }
01640 
01641 
01643 //void CMHiCSagent::behaviorTerminate(TAction action, TTargetId target, TBehaviorTerminate how_does_it_terminate)
01644 //{
01645 //      (*_pActionsExecutionIntensityByTarget)[target].erase(action);
01646 //}
01647 
01648 } // NLAINIMAT
01649 

Generated on Tue Mar 16 06:28:45 2004 for NeL by doxygen 1.3.6