2
0

DeepDecomposer.cpp 5.4 KB

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