DeepDecomposer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Nullkiller.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 "DeepDecomposer.h"
  12. #include "../AIGateway.h"
  13. #include "../Behaviors/CaptureObjectsBehavior.h"
  14. #include "../Behaviors/RecruitHeroBehavior.h"
  15. #include "../Behaviors/BuyArmyBehavior.h"
  16. #include "../Behaviors/StartupBehavior.h"
  17. #include "../Behaviors/DefenceBehavior.h"
  18. #include "../Behaviors/BuildingBehavior.h"
  19. #include "../Behaviors/GatherArmyBehavior.h"
  20. #include "../Behaviors/ClusterBehavior.h"
  21. #include "../Goals/Invalid.h"
  22. #include "../Goals/Composition.h"
  23. namespace NKAI
  24. {
  25. using namespace Goals;
  26. DeepDecomposer::DeepDecomposer(const Nullkiller * ai)
  27. :ai(ai), depth(0)
  28. {
  29. }
  30. void DeepDecomposer::reset()
  31. {
  32. decompositionCache.clear();
  33. goals.clear();
  34. }
  35. void DeepDecomposer::decompose(TGoalVec & result, TSubgoal behavior, int depthLimit)
  36. {
  37. goals.clear();
  38. goals.resize(depthLimit);
  39. decompositionCache.resize(depthLimit);
  40. depth = 0;
  41. goals[0] = {behavior};
  42. while(goals[0].size())
  43. {
  44. bool fromCache;
  45. TSubgoal current = goals[depth].back();
  46. TGoalVec subgoals = decomposeCached(unwrapComposition(current), fromCache);
  47. #if NKAI_TRACE_LEVEL >= 1
  48. logAi->trace("Decomposition level %d returned %d goals", depth, subgoals.size());
  49. #endif
  50. if(depth < depthLimit - 1)
  51. {
  52. goals[depth + 1].clear();
  53. }
  54. for(TSubgoal subgoal : subgoals)
  55. {
  56. if(subgoal->invalid())
  57. continue;
  58. if(subgoal->isElementar())
  59. {
  60. // need to get rid of priority control in behaviors like Startup to avoid this check.
  61. // 0 - goals directly from behavior
  62. Goals::TSubgoal task = depth >= 1 ? aggregateGoals(0, subgoal) : subgoal;
  63. #if NKAI_TRACE_LEVEL >= 1
  64. logAi->trace("Found task %s", task->toString());
  65. #endif
  66. if(!isCompositionLoop(subgoal))
  67. {
  68. result.push_back(task);
  69. if(!fromCache)
  70. {
  71. addToCache(subgoal);
  72. }
  73. }
  74. }
  75. else if(depth < depthLimit - 1)
  76. {
  77. #if NKAI_TRACE_LEVEL >= 1
  78. logAi->trace("Found abstract goal %s", subgoal->toString());
  79. #endif
  80. if(!isCompositionLoop(subgoal))
  81. {
  82. // depth 0 gives reward, deepest goal - task to execute, all the rest is not important
  83. // so avoid details to reduce decomposition complexity but they can give some negative effect so
  84. auto goalToAdd = depth >= 1 ? unwrapComposition(subgoal) : subgoal;
  85. if(!vstd::contains(goals[depth + 1], goalToAdd))
  86. {
  87. goals[depth + 1].push_back(subgoal);
  88. }
  89. }
  90. }
  91. }
  92. if(depth < depthLimit - 1 && goals[depth + 1].size())
  93. {
  94. depth++;
  95. }
  96. else
  97. {
  98. goals[depth].pop_back();
  99. while(depth > 0 && goals[depth].empty())
  100. {
  101. depth--;
  102. goals[depth].pop_back();
  103. }
  104. }
  105. }
  106. }
  107. Goals::TSubgoal DeepDecomposer::aggregateGoals(int startDepth, TSubgoal last)
  108. {
  109. Goals::Composition composition;
  110. for(int i = 0; i <= depth; i++)
  111. {
  112. composition.addNext(goals[i].back());
  113. }
  114. composition.addNext(last);
  115. return sptr(composition);
  116. }
  117. Goals::TSubgoal DeepDecomposer::unwrapComposition(Goals::TSubgoal goal)
  118. {
  119. return goal->goalType == Goals::COMPOSITION ? goal->decompose(ai).back() : goal;
  120. }
  121. bool isEquivalentGoals(TSubgoal goal1, TSubgoal goal2)
  122. {
  123. if(goal1 == goal2) return true;
  124. if(goal1->goalType == Goals::CAPTURE_OBJECT && goal2->goalType == Goals::CAPTURE_OBJECT)
  125. {
  126. auto o1 = cb->getObj(ObjectInstanceID(goal1->objid));
  127. auto o2 = cb->getObj(ObjectInstanceID(goal2->objid));
  128. return o1->ID == Obj::SHIPYARD && o1->ID == o2->ID;
  129. }
  130. return false;
  131. }
  132. bool DeepDecomposer::isCompositionLoop(TSubgoal goal)
  133. {
  134. auto goalsToTest = goal->goalType == Goals::COMPOSITION ? goal->decompose(ai) : TGoalVec{goal};
  135. for(auto goalToTest : goalsToTest)
  136. {
  137. for(int i = depth; i >= 0; i--)
  138. {
  139. auto parent = unwrapComposition(goals[i].back());
  140. if(isEquivalentGoals(parent, goalToTest))
  141. {
  142. return true;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. TGoalVec DeepDecomposer::decomposeCached(TSubgoal goal, bool & fromCache)
  149. {
  150. #if NKAI_TRACE_LEVEL >= 1
  151. logAi->trace("Decomposing %s, level %s", goal->toString(), depth);
  152. #endif
  153. if(goal->hasHash())
  154. {
  155. for(int i = 0; i <= depth; i++)
  156. {
  157. auto cached = decompositionCache[i].find(goal);
  158. if(cached != decompositionCache[i].end())
  159. {
  160. #if NKAI_TRACE_LEVEL >= 1
  161. logAi->trace("Use decomposition cache for %s, level: %d", goal->toString(), depth);
  162. #endif
  163. fromCache = true;
  164. return cached->second;
  165. }
  166. }
  167. decompositionCache[depth][goal] = {}; // if goal decomposition yields no goals we still need it in cache to not decompose again
  168. }
  169. #if NKAI_TRACE_LEVEL >= 2
  170. logAi->trace("Calling decompose on %s, level %s", goal->toString(), depth);
  171. #endif
  172. fromCache = false;
  173. return goal->decompose(ai);
  174. }
  175. void DeepDecomposer::addToCache(TSubgoal goal)
  176. {
  177. bool trusted = true;
  178. for(int parentDepth = 1; parentDepth <= depth; parentDepth++)
  179. {
  180. TSubgoal parent = unwrapComposition(goals[parentDepth].back());
  181. if(parent->hasHash())
  182. {
  183. auto solution = parentDepth < depth ? aggregateGoals(parentDepth + 1, goal) : goal;
  184. #if NKAI_TRACE_LEVEL >= 2
  185. logAi->trace("Adding %s to decomosition cache of %s at level %d", solution->toString(), parent->toString(), parentDepth);
  186. #endif
  187. decompositionCache[parentDepth][parent].push_back(solution);
  188. if(trusted && parentDepth != 0)
  189. {
  190. decompositionCache[0][parent].push_back(solution);
  191. trusted = false;
  192. }
  193. }
  194. }
  195. }
  196. }