Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Modules/BehaviorControl/GT2004BehaviorControl/GT2004DTT/DefaultTacticChooser.cpp

Go to the documentation of this file.
00001 /**
00002 * @file DefaultTacticChooser.cpp
00003 * 
00004 * Implementation of class DefaultTacticChooser
00005 *  
00006 *  @author <A href=mailto:b@tchman.de>Mathias Hülsbusch</A>
00007 */
00008 
00009 #include "DefaultTacticChooser.h"
00010 #include "Tools/DynamicTeamTactic/RateableOptions.h"
00011 #include "Tools/Location.h"
00012 #include "Tools/Streams/InStreams.h"
00013 
00014 static const int FADING_START_TIME = 1000;
00015 static const int FADING_END_TIME = 6000;
00016 
00017 
00018 DefaultTacticChooser::DefaultTacticChooser(const BehaviorControlInterfaces& interfaces,
00019                                            CollectedBeliefs& collectedBeliefs,
00020                                            ChooserRaterInterfaces& chooserRaterInterfaces)
00021 : TacticChooser(interfaces, collectedBeliefs, chooserRaterInterfaces)
00022 {
00023   freeze = false;
00024   // precalculation of all possible Dog permutaions...
00025   // it's not nice, but does it for all kinds of numOfPlayerNumbers > 0
00026   facOfNumOfPlayerNumbers = 1;
00027   int i;
00028   for (i = 2; i <= Player::numOfPlayerNumbers; ++i)
00029     facOfNumOfPlayerNumbers *= i;
00030 
00031   // create some space to store permutations...
00032   for (i = 0; i < Player::numOfPlayerNumbers; ++i)
00033     dogPermutation[i] = new int[facOfNumOfPlayerNumbers];
00034 
00035   // calculating some ranges..
00036   int divisors[Player::numOfPlayerNumbers-2];
00037   int tmp = facOfNumOfPlayerNumbers;
00038   
00039   // to be sure ...
00040   if (Player::numOfPlayerNumbers > 2) {
00041     for (i = Player::numOfPlayerNumbers; i > 2; --i) {
00042       divisors[Player::numOfPlayerNumbers - i] = tmp / i;
00043       tmp = divisors[Player::numOfPlayerNumbers - i];
00044     }
00045   }
00046 
00047   // generate the permutations...
00048   int dogs[Player::numOfPlayerNumbers];
00049   int j,k,l;
00050   for (i = 0; i < facOfNumOfPlayerNumbers; ++i) {
00051     // initialising some fresh dogs...
00052     for (j = 0; j < Player::numOfPlayerNumbers; ++j)
00053       dogs[j] = j;
00054     // go, create...
00055     tmp = i;
00056     j = 0;
00057     if (Player::numOfPlayerNumbers > 2) 
00058       for (j = 0; j < Player::numOfPlayerNumbers-2; ++j) {
00059         k = tmp / divisors[j];
00060         tmp %= divisors[j];
00061         // adding dog to permutation...
00062         dogPermutation[j][i] = dogs[k];
00063         for (l = k; l < Player::numOfPlayerNumbers-1; ++l)
00064           dogs[l] = dogs[l+1];
00065       }
00066 
00067     // adding the last two dogs...
00068     dogPermutation[j][i]   = dogs[tmp];
00069     if (Player::numOfPlayerNumbers > 1) 
00070       dogPermutation[j+1][i] = dogs[1-tmp];
00071   }
00072 
00073 
00074   // Loading the TacticEntryArray
00075   InBinaryFile stream(getLocation().getFilename("default.dtt"));
00076   if (stream.exists())
00077   {
00078     stream >> tacticEntryArray;
00079   }
00080   else
00081   {
00082     OUTPUT(idText,text,"DefaultTacticChoose : couldn't load TacticEntryArray "<<getLocation().getFilename("default.dtt"));
00083   }
00084 
00085 
00086 }
00087 
00088 DefaultTacticChooser::~DefaultTacticChooser()
00089 {
00090   // release the space for the permutations...
00091   for (int i = 0; i < Player::numOfPlayerNumbers; ++i)
00092     delete[] dogPermutation[i];
00093 }
00094 
00095 bool DefaultTacticChooser::handleMessage(InMessage& message)
00096 {
00097   if (message.getMessageID() == idTacticEntryArray)
00098   {
00099     message.bin >> tacticEntryArray;
00100     return true;
00101   }
00102   if (message.getMessageID() == idFreezeRequest)
00103   {
00104     message.bin >> freeze;
00105     return true;
00106   }
00107   return false;
00108 }
00109 
00110 
00111 
00112 RateableOptions::OptionID DefaultTacticChooser::chooseOption()
00113 {
00114   if (tacticEntryArray.entryCount == 0)
00115   {
00116     INFO(sendOptionRatings,idChoosenTacticEntry,text,"empty Taktic Entry Array");
00117     return RateableOptions::noOption;
00118   }
00119 
00120   RateableOptions::OptionID optToChoose = RateableOptions::noOption;
00121   if (freeze)
00122     optToChoose = RateableOptions::stand;
00123   else {
00124     RateableOptions::OptionID usedOptions[Player::numOfPlayerNumbers];
00125     double maxScore = 0;
00126     int choosenTacticEntry = 0;
00127     unsigned long actTime = SystemCall::getCurrentSystemTime();
00128     double timeWeight[Player::numOfPlayerNumbers];
00129     int k;
00130     int timediff;
00131     // doing some Aging...
00132     for (k = 0; k < Player::numOfPlayerNumbers; ++k) {
00133       timediff = actTime - collectedBeliefs.singleBeliefs[k].timeStamp;
00134       if (timediff < FADING_START_TIME)
00135         timediff = 0;
00136       else if (timediff > FADING_END_TIME)
00137         timediff = FADING_END_TIME - FADING_START_TIME;
00138       else timediff = timediff - FADING_START_TIME;
00139       timeWeight[k] = (double)((FADING_END_TIME - FADING_START_TIME)-timediff) /
00140                       (double)(FADING_END_TIME - FADING_START_TIME);
00141     }
00142 
00143       
00144     // preparing the global Analysers...
00145     for (int n = 0; n < RateableOptions::numOfGlobalAnalysers; ++n)
00146       globalAnalyser[n]->update();
00147 
00148     for (int i = 0; i < facOfNumOfPlayerNumbers; ++i) 
00149     {
00150 
00151       // walk through all TacticEntrys with that permutation..
00152       for (int te=0; te < tacticEntryArray.entryCount; ++te)
00153       {
00154         // get score for permutation i
00155         double score = 0;
00156 
00157         TacticEntry& aktTE = tacticEntryArray.entrys[te];
00158 
00159         // is this permutation a valid permutation for this tacticEntry ?
00160         int validPermutation = 1;
00161         for (int j = 0; j < Player::numOfPlayerNumbers; ++j)
00162           validPermutation *= aktTE.allowedDogs[dogPermutation[j][i]][j];
00163 
00164         if (validPermutation)
00165         {
00166           // get the options for this entry ...
00167           for (k = 0; k < Player::numOfPlayerNumbers; ++k)
00168             if (aktTE.isOptionClass[k])
00169               usedOptions[k] = 
00170     collectedBeliefs.singleBeliefs[dogPermutation[k][i]].getBestOptionInOptionClass(
00171                            (RateableOptions::OptionClassID)(aktTE.neededOptions[k])
00172                                                                                    );
00173             else usedOptions[k] = (RateableOptions::OptionID)(aktTE.neededOptions[k]);
00174 
00175           for (k = 0; k < Player::numOfPlayerNumbers; ++k)
00176             score += aktTE.optionWeights[k] * timeWeight[dogPermutation[k][i]] *
00177     collectedBeliefs.singleBeliefs[dogPermutation[k][i]].ratedOptions[usedOptions[k]];
00178 
00179           score *= aktTE.weight;
00180 
00181           // applying global analysers
00182           for (k = 0; k < RateableOptions::numOfGlobalAnalysers; ++k)
00183             score *= globalAnalyser[k]->getWeight(aktTE.globalAnalysersInfo[k]);
00184 
00185           if (score >= maxScore)
00186           {
00187             maxScore = score;
00188             choosenTacticEntry = te;
00189             for (int l = 0; l < Player::numOfPlayerNumbers; ++l)
00190               if (dogPermutation[l][i] == getPlayer().getPlayerNumber())
00191               {
00192                 optToChoose = usedOptions[l];
00193                 break;
00194               }
00195             for (int j = 0; j < Player::numOfPlayerNumbers; ++j)
00196               ocplayerrole[dogPermutation[j][i]] = usedOptions[j];
00197           }// if (maxScore)
00198         }// if (validPermutation)
00199       }// for (TE)
00200     }
00201 
00202   //  OUTPUT(idText, text, RateableOptions::getOptionName(optToChoose));
00203     INFO(sendOptionRatings,idChoosenTacticEntry,text,tacticEntryArray.entrys[choosenTacticEntry].name);
00204     char buffer[10];
00205     char buffer2[50] = "|";
00206     for (k=0; k < Player::numOfPlayerNumbers; ++k)
00207     {
00208       //_gcvt(timeWeight[k],2,buffer);
00209       sprintf( buffer, "%4.2f", timeWeight[k]);
00210       strcat(buffer,"|");
00211       strcat(buffer2,buffer);
00212     }
00213     INFO(sendOptionRatings,idTimeWeights,text,buffer2);
00214 
00215     if (optToChoose == RateableOptions::keepOption)
00216       optToChoose = chooserRaterInterfaces.lastChoosenOption;
00217 
00218   } // end else part of "if (freeze)"
00219  
00220   INFO(sendOptionRatings,idChoosenOption,text,RateableOptions::getOptionName(optToChoose));
00221   return optToChoose;
00222 
00223 }
00224 
00225 
00226 /*
00227  * Change log :
00228  * $Log: DefaultTacticChooser.cpp,v $
00229  * Revision 1.2  2004/08/09 13:51:48  hamerla
00230  * import OpenChallen Behavior to WM
00231  *
00232  * Revision 1.1  2004/07/22 22:38:31  kerdels
00233  * added DTT used by Open Challenge, RIP and Xabsl-Options will follow
00234  *
00235   *
00236  */

Generated on Thu Sep 23 19:57:26 2004 for GT2004 by doxygen 1.3.6