DeepDecomposer.cpp 5.0 KB

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