Fuzzy.cpp 8.8 KB

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