FuzzyEngines.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * FuzzyEngines.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "../StdInc.h"
  11. #include "FuzzyEngines.h"
  12. #include "../Goals/Goals.h"
  13. #include "../../../lib/mapObjects/MapObjects.h"
  14. #include "../AIGateway.h"
  15. namespace NKAI
  16. {
  17. constexpr float MIN_AI_STRENGTH = 0.5f; //lower when combat AI gets smarter
  18. engineBase::engineBase()
  19. {
  20. rules = new fl::RuleBlock();
  21. engine.addRuleBlock(rules);
  22. }
  23. void engineBase::configure()
  24. {
  25. engine.configure("Minimum", "Maximum", "Minimum", "AlgebraicSum", "Centroid", "Proportional");
  26. logAi->trace(engine.toString());
  27. }
  28. void engineBase::addRule(const std::string & txt)
  29. {
  30. rules->addRule(fl::Rule::parse(txt, &engine));
  31. }
  32. struct armyStructure
  33. {
  34. float walkers;
  35. float shooters;
  36. float flyers;
  37. ui32 maxSpeed;
  38. };
  39. armyStructure evaluateArmyStructure(const CArmedInstance * army)
  40. {
  41. ui64 totalStrength = army->getArmyStrength();
  42. double walkersStrength = 0;
  43. double flyersStrength = 0;
  44. double shootersStrength = 0;
  45. ui32 maxSpeed = 0;
  46. static const CSelector selectorSHOOTER = Selector::type()(BonusType::SHOOTER);
  47. static const std::string keySHOOTER = "type_"+std::to_string((int32_t)BonusType::SHOOTER);
  48. static const CSelector selectorFLYING = Selector::type()(BonusType::FLYING);
  49. static const std::string keyFLYING = "type_"+std::to_string((int32_t)BonusType::FLYING);
  50. static const CSelector selectorSTACKS_SPEED = Selector::type()(BonusType::STACKS_SPEED);
  51. static const std::string keySTACKS_SPEED = "type_"+std::to_string((int32_t)BonusType::STACKS_SPEED);
  52. for(auto s : army->Slots())
  53. {
  54. bool walker = true;
  55. auto bearer = s.second->getType()->getBonusBearer();
  56. if(bearer->hasBonus(selectorSHOOTER, keySHOOTER))
  57. {
  58. shootersStrength += s.second->getPower();
  59. walker = false;
  60. }
  61. if(bearer->hasBonus(selectorFLYING, keyFLYING))
  62. {
  63. flyersStrength += s.second->getPower();
  64. walker = false;
  65. }
  66. if(walker)
  67. walkersStrength += s.second->getPower();
  68. vstd::amax(maxSpeed, bearer->valOfBonuses(selectorSTACKS_SPEED, keySTACKS_SPEED));
  69. }
  70. armyStructure as;
  71. as.walkers = static_cast<float>(walkersStrength / totalStrength);
  72. as.shooters = static_cast<float>(shootersStrength / totalStrength);
  73. as.flyers = static_cast<float>(flyersStrength / totalStrength);
  74. as.maxSpeed = maxSpeed;
  75. assert(as.walkers || as.flyers || as.shooters);
  76. return as;
  77. }
  78. TacticalAdvantageEngine::TacticalAdvantageEngine()
  79. {
  80. try
  81. {
  82. ourShooters = new fl::InputVariable("OurShooters");
  83. ourWalkers = new fl::InputVariable("OurWalkers");
  84. ourFlyers = new fl::InputVariable("OurFlyers");
  85. enemyShooters = new fl::InputVariable("EnemyShooters");
  86. enemyWalkers = new fl::InputVariable("EnemyWalkers");
  87. enemyFlyers = new fl::InputVariable("EnemyFlyers");
  88. //Tactical advantage calculation
  89. std::vector<fl::InputVariable *> helper =
  90. {
  91. ourShooters, ourWalkers, ourFlyers, enemyShooters, enemyWalkers, enemyFlyers
  92. };
  93. for(auto val : helper)
  94. {
  95. engine.addInputVariable(val);
  96. val->addTerm(new fl::Ramp("FEW", 0.6, 0.0));
  97. val->addTerm(new fl::Ramp("MANY", 0.4, 1));
  98. val->setRange(0.0, 1.0);
  99. }
  100. ourSpeed = new fl::InputVariable("OurSpeed");
  101. enemySpeed = new fl::InputVariable("EnemySpeed");
  102. helper = { ourSpeed, enemySpeed };
  103. for(auto val : helper)
  104. {
  105. engine.addInputVariable(val);
  106. val->addTerm(new fl::Ramp("LOW", 6.5, 3));
  107. val->addTerm(new fl::Triangle("MEDIUM", 5.5, 10.5));
  108. val->addTerm(new fl::Ramp("HIGH", 8.5, 16));
  109. val->setRange(0, 25);
  110. }
  111. castleWalls = new fl::InputVariable("CastleWalls");
  112. engine.addInputVariable(castleWalls);
  113. {
  114. int wallsNone = CGTownInstance::NONE;
  115. int wallsFort = CGTownInstance::FORT;
  116. int wallsCitadel = CGTownInstance::CITADEL;
  117. int wallsCastle = CGTownInstance::CASTLE;
  118. fl::Rectangle * none = new fl::Rectangle("NONE", wallsNone, wallsNone + (wallsFort - wallsNone) * 0.5f);
  119. castleWalls->addTerm(none);
  120. fl::Trapezoid * medium = new fl::Trapezoid("MEDIUM", (wallsFort - wallsNone) * 0.5f, wallsFort, wallsCitadel, wallsCitadel + (wallsCastle - wallsCitadel) * 0.5f);
  121. castleWalls->addTerm(medium);
  122. fl::Ramp * high = new fl::Ramp("HIGH", wallsCitadel - 0.1, wallsCastle);
  123. castleWalls->addTerm(high);
  124. castleWalls->setRange(wallsNone, wallsCastle);
  125. }
  126. bankPresent = new fl::InputVariable("Bank");
  127. engine.addInputVariable(bankPresent);
  128. {
  129. fl::Rectangle * termFalse = new fl::Rectangle("FALSE", 0.0, 0.5f);
  130. bankPresent->addTerm(termFalse);
  131. fl::Rectangle * termTrue = new fl::Rectangle("TRUE", 0.5f, 1);
  132. bankPresent->addTerm(termTrue);
  133. bankPresent->setRange(0, 1);
  134. }
  135. threat = new fl::OutputVariable("Threat");
  136. engine.addOutputVariable(threat);
  137. threat->addTerm(new fl::Ramp("LOW", 1, MIN_AI_STRENGTH));
  138. threat->addTerm(new fl::Triangle("MEDIUM", 0.8, 1.2));
  139. threat->addTerm(new fl::Ramp("HIGH", 1, 1.5));
  140. threat->setRange(MIN_AI_STRENGTH, 1.5);
  141. addRule("if OurShooters is MANY and EnemySpeed is LOW then Threat is LOW");
  142. addRule("if OurShooters is MANY and EnemyShooters is FEW then Threat is LOW");
  143. addRule("if OurSpeed is LOW and EnemyShooters is MANY then Threat is HIGH");
  144. addRule("if OurSpeed is HIGH and EnemyShooters is MANY then Threat is LOW");
  145. addRule("if OurWalkers is FEW and EnemyShooters is MANY then Threat is LOW");
  146. addRule("if OurShooters is MANY and EnemySpeed is HIGH then Threat is HIGH");
  147. //just to cover all cases
  148. addRule("if OurShooters is FEW and EnemySpeed is HIGH then Threat is MEDIUM");
  149. addRule("if EnemySpeed is MEDIUM then Threat is MEDIUM");
  150. addRule("if EnemySpeed is LOW and OurShooters is FEW then Threat is MEDIUM");
  151. addRule("if Bank is TRUE and OurShooters is MANY then Threat is HIGH");
  152. addRule("if Bank is TRUE and EnemyShooters is MANY then Threat is LOW");
  153. addRule("if CastleWalls is HIGH and OurWalkers is MANY then Threat is HIGH");
  154. addRule("if CastleWalls is HIGH and OurFlyers is MANY and OurShooters is MANY then Threat is MEDIUM");
  155. addRule("if CastleWalls is MEDIUM and OurShooters is MANY and EnemyWalkers is MANY then Threat is LOW");
  156. }
  157. catch(fl::Exception & pe)
  158. {
  159. logAi->error("initTacticalAdvantage: %s", pe.getWhat());
  160. }
  161. configure();
  162. }
  163. float TacticalAdvantageEngine::getTacticalAdvantage(const CArmedInstance * we, const CArmedInstance * enemy)
  164. {
  165. float output = 1;
  166. /*try //TODO: rework this engine, it tends to produce nonsense output
  167. {
  168. armyStructure ourStructure = evaluateArmyStructure(we);
  169. armyStructure enemyStructure = evaluateArmyStructure(enemy);
  170. ourWalkers->setValue(ourStructure.walkers);
  171. ourShooters->setValue(ourStructure.shooters);
  172. ourFlyers->setValue(ourStructure.flyers);
  173. ourSpeed->setValue(ourStructure.maxSpeed);
  174. enemyWalkers->setValue(enemyStructure.walkers);
  175. enemyShooters->setValue(enemyStructure.shooters);
  176. enemyFlyers->setValue(enemyStructure.flyers);
  177. enemySpeed->setValue(enemyStructure.maxSpeed);
  178. const CGTownInstance * fort = dynamic_cast<const CGTownInstance *>(enemy);
  179. if(fort)
  180. castleWalls->setValue(fort->fortLevel());
  181. else
  182. castleWalls->setValue(0);
  183. engine.process();
  184. output = threat->getValue();
  185. }
  186. catch(fl::Exception & fe)
  187. {
  188. logAi->error("getTacticalAdvantage: %s ", fe.getWhat());
  189. }
  190. if(output < 0 || (output != output))
  191. {
  192. fl::InputVariable * tab[] = { bankPresent, castleWalls, ourWalkers, ourShooters, ourFlyers, ourSpeed, enemyWalkers, enemyShooters, enemyFlyers, enemySpeed };
  193. std::string names[] = { "bankPresent", "castleWalls", "ourWalkers", "ourShooters", "ourFlyers", "ourSpeed", "enemyWalkers", "enemyShooters", "enemyFlyers", "enemySpeed" };
  194. std::stringstream log("Warning! Fuzzy engine doesn't cover this set of parameters: ");
  195. for(int i = 0; i < boost::size(tab); i++)
  196. log << names[i] << ": " << tab[i]->getValue() << " ";
  197. logAi->error(log.str());
  198. assert(false);
  199. }*/
  200. return output;
  201. }
  202. }