DeepDecomposer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. 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 NKAI_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 NKAI_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 NKAI_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 isEquivalentGoals(TSubgoal goal1, TSubgoal goal2)
  120. {
  121. if(goal1 == goal2) return true;
  122. if(goal1->goalType == Goals::CAPTURE_OBJECT && goal2->goalType == Goals::CAPTURE_OBJECT)
  123. {
  124. auto o1 = cb->getObj(ObjectInstanceID(goal1->objid));
  125. auto o2 = cb->getObj(ObjectInstanceID(goal2->objid));
  126. return o1->ID == Obj::SHIPYARD && o1->ID == o2->ID;
  127. }
  128. return false;
  129. }
  130. bool DeepDecomposer::isCompositionLoop(TSubgoal goal)
  131. {
  132. auto goalsToTest = goal->goalType == Goals::COMPOSITION ? goal->decompose() : TGoalVec{goal};
  133. for(auto goalToTest : goalsToTest)
  134. {
  135. for(int i = depth; i >= 0; i--)
  136. {
  137. auto parent = unwrapComposition(goals[i].back());
  138. if(isEquivalentGoals(parent, goalToTest))
  139. {
  140. return true;
  141. }
  142. }
  143. }
  144. return false;
  145. }
  146. TGoalVec DeepDecomposer::decomposeCached(TSubgoal goal, bool & fromCache)
  147. {
  148. #if NKAI_TRACE_LEVEL >= 1
  149. logAi->trace("Decomposing %s, level %s", goal->toString(), depth);
  150. #endif
  151. if(goal->hasHash())
  152. {
  153. for(int i = 0; i <= depth; i++)
  154. {
  155. auto cached = decompositionCache[i].find(goal);
  156. if(cached != decompositionCache[i].end())
  157. {
  158. #if NKAI_TRACE_LEVEL >= 1
  159. logAi->trace("Use decomposition cache for %s, level: %d", goal->toString(), depth);
  160. #endif
  161. fromCache = true;
  162. return cached->second;
  163. }
  164. }
  165. decompositionCache[depth][goal] = {}; // if goal decomposition yields no goals we still need it in cache to not decompose again
  166. }
  167. #if NKAI_TRACE_LEVEL >= 2
  168. logAi->trace("Calling decompose on %s, level %s", goal->toString(), depth);
  169. #endif
  170. fromCache = false;
  171. return goal->decompose();
  172. }
  173. void DeepDecomposer::addToCache(TSubgoal goal)
  174. {
  175. bool trusted = true;
  176. for(int parentDepth = 1; parentDepth <= depth; parentDepth++)
  177. {
  178. TSubgoal parent = unwrapComposition(goals[parentDepth].back());
  179. if(parent->hasHash())
  180. {
  181. auto solution = parentDepth < depth ? aggregateGoals(parentDepth + 1, goal) : goal;
  182. #if NKAI_TRACE_LEVEL >= 2
  183. logAi->trace("Adding %s to decomosition cache of %s at level %d", solution->toString(), parent->toString(), parentDepth);
  184. #endif
  185. decompositionCache[parentDepth][parent].push_back(solution);
  186. if(trusted && parentDepth != 0)
  187. {
  188. decompositionCache[0][parent].push_back(solution);
  189. trusted = false;
  190. }
  191. }
  192. }
  193. }
  194. }