FuzzyEngines.cpp 8.0 KB

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