00001
00002
00003
00004
00005
00006
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
00025
00026 facOfNumOfPlayerNumbers = 1;
00027 int i;
00028 for (i = 2; i <= Player::numOfPlayerNumbers; ++i)
00029 facOfNumOfPlayerNumbers *= i;
00030
00031
00032 for (i = 0; i < Player::numOfPlayerNumbers; ++i)
00033 dogPermutation[i] = new int[facOfNumOfPlayerNumbers];
00034
00035
00036 int divisors[Player::numOfPlayerNumbers-2];
00037 int tmp = facOfNumOfPlayerNumbers;
00038
00039
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
00048 int dogs[Player::numOfPlayerNumbers];
00049 int j,k,l;
00050 for (i = 0; i < facOfNumOfPlayerNumbers; ++i) {
00051
00052 for (j = 0; j < Player::numOfPlayerNumbers; ++j)
00053 dogs[j] = j;
00054
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
00062 dogPermutation[j][i] = dogs[k];
00063 for (l = k; l < Player::numOfPlayerNumbers-1; ++l)
00064 dogs[l] = dogs[l+1];
00065 }
00066
00067
00068 dogPermutation[j][i] = dogs[tmp];
00069 if (Player::numOfPlayerNumbers > 1)
00070 dogPermutation[j+1][i] = dogs[1-tmp];
00071 }
00072
00073
00074
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
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
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
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
00152 for (int te=0; te < tacticEntryArray.entryCount; ++te)
00153 {
00154
00155 double score = 0;
00156
00157 TacticEntry& aktTE = tacticEntryArray.entrys[te];
00158
00159
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
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
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 }
00198 }
00199 }
00200 }
00201
00202
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
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 }
00219
00220 INFO(sendOptionRatings,idChoosenOption,text,RateableOptions::getOptionName(optToChoose));
00221 return optToChoose;
00222
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236