FuzzyEngines.cpp 7.8 KB

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