Fuzzy.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "StdInc.h"
  2. #include "Fuzzy.h"
  3. #include "../../lib/CObjectHandler.h"
  4. #include "AreaCentroidAlgorithm.cpp"
  5. #include "CompoundTerm.cpp"
  6. #include "DescriptiveAntecedent.cpp"
  7. #include "FuzzyEngine.cpp"
  8. #include "FuzzyAnd.cpp"
  9. #include "FuzzyOr.cpp"
  10. #include "InputLVar.cpp"
  11. #include "OutputLVar.cpp"
  12. #include "FuzzyAntecedent.cpp"
  13. #include "FuzzyConsequent.cpp"
  14. #include "FuzzyDefuzzifier.cpp"
  15. #include "FuzzyModulation.cpp"
  16. #include "FuzzyOperator.cpp"
  17. #include "FuzzyOperation.cpp"
  18. #include "FuzzyException.cpp"
  19. #include "FuzzyExceptions.cpp"
  20. #include "FuzzyRule.cpp"
  21. #include "HedgeSet.cpp"
  22. #include "Hedge.cpp"
  23. #include "SingletonTerm.cpp"
  24. #include "TrapezoidalTerm.cpp"
  25. #include "TriangularTerm.cpp"
  26. #include "LinguisticTerm.cpp"
  27. #include "LinguisticVariable.cpp"
  28. #include "RuleBlock.cpp"
  29. #include "ShoulderTerm.cpp"
  30. #include "StrOp.cpp"
  31. #include "MamdaniRule.cpp"
  32. #include "MamdaniConsequent.cpp"
  33. /*
  34. * Fuzzy.cpp, part of VCMI engine
  35. *
  36. * Authors: listed in file AUTHORS in main folder
  37. *
  38. * License: GNU General Public License v2.0 or later
  39. * Full text of license available in license.txt file, in main folder
  40. *
  41. */
  42. #define MIN_AI_STRENGHT (0.5f) //lower when combat AI gets smarter
  43. class BankConfig;
  44. class FuzzyEngine;
  45. class InputLVar;
  46. class CGTownInstance;
  47. using namespace boost::assign;
  48. using namespace vstd;
  49. FuzzyHelper fh;
  50. struct armyStructure
  51. {
  52. float walkers, shooters, flyers;
  53. ui32 maxSpeed;
  54. };
  55. ui64 evaluateBankConfig (BankConfig * bc)
  56. {
  57. ui64 danger = 0;
  58. BOOST_FOREACH (auto opt, bc->guards)
  59. {
  60. danger += VLC->creh->creatures[opt.first]->fightValue * opt.second;
  61. }
  62. return danger;
  63. }
  64. armyStructure evaluateArmyStructure (const CArmedInstance * army)
  65. {
  66. ui64 totalStrenght = army->getArmyStrength();
  67. double walkersStrenght = 0;
  68. double flyersStrenght = 0;
  69. double shootersStrenght = 0;
  70. ui32 maxSpeed = 0;
  71. BOOST_FOREACH(auto s, army->Slots())
  72. {
  73. bool walker = true;
  74. if (s.second->type->hasBonusOfType(Bonus::SHOOTER))
  75. {
  76. shootersStrenght += s.second->getPower();
  77. walker = false;
  78. }
  79. if (s.second->type->hasBonusOfType(Bonus::FLYING))
  80. {
  81. flyersStrenght += s.second->getPower();
  82. walker = false;
  83. }
  84. if (walker)
  85. walkersStrenght += s.second->getPower();
  86. amax (maxSpeed, s.second->type->speed);
  87. }
  88. armyStructure as;
  89. as.walkers = walkersStrenght / totalStrenght;
  90. as.shooters = shootersStrenght / totalStrenght;
  91. as.flyers = flyersStrenght / totalStrenght;
  92. as.maxSpeed = maxSpeed;
  93. return as;
  94. }
  95. FuzzyHelper::FuzzyHelper()
  96. {
  97. initBank();
  98. initTacticalAdvantage();
  99. }
  100. void FuzzyHelper::initBank()
  101. {
  102. try
  103. {
  104. //Trivial bank estimation
  105. bankInput = new fl::InputLVar("BankInput");
  106. bankDanger = new fl::OutputLVar("BankDanger");
  107. bankInput->addTerm(new fl::SingletonTerm ("SET"));
  108. engine.addRuleBlock (&bankBlock); //have to be added before the rules are parsed
  109. engine.addInputLVar (bankInput);
  110. engine.addOutputLVar (bankDanger);
  111. for (int i = 0; i < 4; ++i)
  112. {
  113. bankDanger->addTerm(new fl::TriangularTerm ("Bank" + boost::lexical_cast<std::string>(i), 0, 1));
  114. bankBlock.addRule(new fl::MamdaniRule("if BankInput is SET then BankDanger is Bank" + boost::lexical_cast<std::string>(i), engine));
  115. }
  116. }
  117. catch (fl::FuzzyException fe)
  118. {
  119. tlog1 << "initBank " << fe.name() << ": " << fe.message() << '\n';
  120. }
  121. }
  122. void FuzzyHelper::initTacticalAdvantage()
  123. {
  124. try
  125. {
  126. //Tactical advantage calculation
  127. std::vector<fl::InputLVar*> helper;
  128. ourShooters = new fl::InputLVar("OurShooters");
  129. ourWalkers = new fl::InputLVar("OurWalkers");
  130. ourFlyers = new fl::InputLVar("OurFlyers");
  131. enemyShooters = new fl::InputLVar("EnemyShooters");
  132. enemyWalkers = new fl::InputLVar("EnemyWalkers");
  133. enemyFlyers = new fl::InputLVar("EnemyFlyers");
  134. helper += ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers;
  135. BOOST_FOREACH (auto val, helper)
  136. {
  137. val->addTerm (new fl::ShoulderTerm("FEW", 0, 0.75, true));
  138. val->addTerm (new fl::ShoulderTerm("MANY", 0.25, 1, false));
  139. engine.addInputLVar(val);
  140. }
  141. helper.clear();
  142. ourSpeed = new fl::InputLVar("OurSpeed");
  143. enemySpeed = new fl::InputLVar("EnemySpeed");
  144. helper += ourSpeed, enemySpeed;
  145. BOOST_FOREACH (auto val, helper)
  146. {
  147. val->addTerm (new fl::ShoulderTerm("LOW", 3, 9, true));
  148. val->addTerm (new fl::TriangularTerm("MEDIUM", 7, 13));
  149. val->addTerm (new fl::ShoulderTerm("HIGH", 11, 16, false));
  150. engine.addInputLVar(val);
  151. }
  152. castleWalls = new fl::InputLVar("CastleWalls");
  153. castleWalls->addTerm(new fl::SingletonTerm("NONE", CGTownInstance::NONE));
  154. castleWalls->addTerm(new fl::TrapezoidalTerm("MEDIUM", CGTownInstance::FORT, 2.5));
  155. castleWalls->addTerm(new fl::ShoulderTerm("HIGH", CGTownInstance::CITADEL, CGTownInstance::CASTLE));
  156. engine.addInputLVar(castleWalls);
  157. bankPresent = new fl::InputLVar("Bank");
  158. bankPresent->addTerm(new fl::SingletonTerm("FALSE", 0));
  159. bankPresent->addTerm(new fl::SingletonTerm("TRUE", 1));
  160. engine.addInputLVar(bankPresent);
  161. threat = new fl::OutputLVar("Threat");
  162. threat->addTerm (new fl::TriangularTerm("LOW", MIN_AI_STRENGHT, 1));
  163. threat->addTerm (new fl::TriangularTerm("MEDIUM", 0.8, 1.2));
  164. threat->addTerm (new fl::ShoulderTerm("HIGH", 1, 1.5, false));
  165. engine.addOutputLVar(threat);
  166. engine.hedgeSet().add(new fl::HedgeSomewhat());
  167. engine.hedgeSet().add(new fl::HedgeVery());
  168. tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is very LOW", engine));
  169. tacticalAdvantage.addRule(new fl::MamdaniRule("if OurSpeed is LOW and OurShooters is FEW and EnemyShooters is MANY then Threat is very HIGH", engine));
  170. tacticalAdvantage.addRule(new fl::MamdaniRule("if (OurShooters is MANY or OurFlyers is MANY) and EnemyShooters is MANY then Threat is LOW", engine));
  171. tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is somewhat HIGH", engine));
  172. //tacticalAdvantage.addRule(new fl::MamdaniRule("if OurShooters is MANY and EnemyShooters is MANY then Threat is MEDIUM", engine));
  173. tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and OurShooters is MANY then Threat is somewhat HIGH", engine));
  174. tacticalAdvantage.addRule(new fl::MamdaniRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW", engine));
  175. tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is very HIGH", engine));
  176. tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM", engine));
  177. tacticalAdvantage.addRule(new fl::MamdaniRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW", engine));
  178. engine.addRuleBlock (&tacticalAdvantage);
  179. }
  180. catch(fl::ParsingException pe)
  181. {
  182. tlog1 << "initTacticalAdvantage " << pe.name() << ": " << pe.message() << '\n';
  183. }
  184. catch (fl::FuzzyException fe)
  185. {
  186. tlog1 << "initTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
  187. }
  188. }
  189. ui64 FuzzyHelper::estimateBankDanger (int ID)
  190. {
  191. std::vector <ConstTransitivePtr<BankConfig>> & configs = VLC->objh->banksInfo[ID];
  192. ui64 val = INFINITY;
  193. try
  194. {
  195. switch (configs.size())
  196. {
  197. case 4:
  198. try
  199. {
  200. int bankVal;
  201. for (int i = 0; i < 4; ++i)
  202. {
  203. bankVal = evaluateBankConfig (VLC->objh->banksInfo[ID][i]);
  204. bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMinimum(bankVal * 0.5f);
  205. bankDanger->term("Bank" + boost::lexical_cast<std::string>(i))->setMaximum(bankVal * 1.5f);
  206. }
  207. //comparison purposes
  208. //int averageValue = (evaluateBankConfig (VLC->objh->banksInfo[ID][0]) + evaluateBankConfig (VLC->objh->banksInfo[ID][3])) * 0.5;
  209. dynamic_cast<fl::SingletonTerm*>(bankInput->term("SET"))->setValue(0.5);
  210. bankInput->setInput (0.5);
  211. engine.process (BANK_DANGER);
  212. val = bankDanger->output().defuzzify(); //some expected value of this bank
  213. }
  214. catch (fl::FuzzyException fe)
  215. {
  216. tlog1 << fe.name() << ": " << fe.message() << '\n';
  217. }
  218. break;
  219. case 1: //rare case - Pyramid
  220. val = evaluateBankConfig (VLC->objh->banksInfo[ID][0]);
  221. break;
  222. default:
  223. tlog3 << ("Uhnandled bank config!\n");
  224. }
  225. }
  226. catch (fl::FuzzyException fe)
  227. {
  228. tlog1 << "estimateBankDanger " << fe.name() << ": " << fe.message() << '\n';
  229. }
  230. return val;
  231. }
  232. float FuzzyHelper::getTacticalAdvantage (const CArmedInstance *we, const CArmedInstance *enemy)
  233. {
  234. float output = 1;
  235. try
  236. {
  237. armyStructure ourStructure = evaluateArmyStructure(we);
  238. armyStructure enemyStructure = evaluateArmyStructure(enemy);
  239. ourWalkers->setInput(ourStructure.walkers);
  240. ourShooters->setInput(ourStructure.shooters);
  241. ourFlyers->setInput(ourStructure.flyers);
  242. ourSpeed->setInput(ourStructure.maxSpeed);
  243. enemyWalkers->setInput(enemyStructure.walkers);
  244. enemyShooters->setInput(enemyStructure.shooters);
  245. enemyFlyers->setInput(enemyStructure.flyers);
  246. enemySpeed->setInput(enemyStructure.maxSpeed);
  247. bool bank = dynamic_cast<const CBank*>(enemy);
  248. if (bank)
  249. bankPresent->setInput(1);
  250. else
  251. bankPresent->setInput(0);
  252. const CGTownInstance * fort = dynamic_cast<const CGTownInstance*>(enemy);
  253. if (fort)
  254. {
  255. castleWalls->setInput (fort->fortLevel());
  256. }
  257. else
  258. castleWalls->setInput(0);
  259. engine.process (TACTICAL_ADVANTAGE);
  260. output = threat->output().defuzzify();
  261. }
  262. catch (fl::FuzzyException fe)
  263. {
  264. tlog1 << "getTacticalAdvantage " << fe.name() << ": " << fe.message() << '\n';
  265. }
  266. return output;
  267. }