| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 | /** Nullkiller.cpp, part of VCMI engine** Authors: listed in file AUTHORS in main folder** License: GNU General Public License v2.0 or later* Full text of license available in license.txt file, in main folder**/#include "StdInc.h"#include "DeepDecomposer.h"#include "../AIGateway.h"#include "../Behaviors/CaptureObjectsBehavior.h"#include "../Behaviors/RecruitHeroBehavior.h"#include "../Behaviors/BuyArmyBehavior.h"#include "../Behaviors/StartupBehavior.h"#include "../Behaviors/DefenceBehavior.h"#include "../Behaviors/BuildingBehavior.h"#include "../Behaviors/GatherArmyBehavior.h"#include "../Behaviors/ClusterBehavior.h"#include "../Goals/Invalid.h"#include "../Goals/Composition.h"namespace NKAI{extern boost::thread_specific_ptr<CCallback> cb;extern boost::thread_specific_ptr<AIGateway> ai;using namespace Goals;void DeepDecomposer::reset(){	decompositionCache.clear();	goals.clear();}Goals::TGoalVec DeepDecomposer::decompose(TSubgoal behavior, int depthLimit){	TGoalVec tasks;	goals.clear();	goals.resize(depthLimit);	decompositionCache.resize(depthLimit);	depth = 0;	goals[0] = {behavior};	while(goals[0].size())	{		bool fromCache;		TSubgoal current = goals[depth].back();		TGoalVec subgoals = decomposeCached(unwrapComposition(current), fromCache);#if NKAI_TRACE_LEVEL >= 1		logAi->trace("Decomposition level %d returned %d goals", depth, subgoals.size());#endif		if(depth < depthLimit - 1)		{			goals[depth + 1].clear();		}		for(TSubgoal subgoal : subgoals)		{			if(subgoal->invalid())				continue;			if(subgoal->isElementar())			{				// need to get rid of priority control in behaviors like Startup to avoid this check.				// 0 - goals directly from behavior				Goals::TSubgoal task = depth >= 1 ? aggregateGoals(0, subgoal) : subgoal;#if NKAI_TRACE_LEVEL >= 1				logAi->trace("Found task %s", task->toString());#endif				if(!isCompositionLoop(subgoal))				{					tasks.push_back(task);					if(!fromCache)					{						addToCache(subgoal);					}				}			}			else if(depth < depthLimit - 1)			{#if NKAI_TRACE_LEVEL >= 1				logAi->trace("Found abstract goal %s", subgoal->toString());#endif				if(!isCompositionLoop(subgoal))				{					// depth 0 gives reward, deepest goal - task to execute, all the rest is not important					// so avoid details to reduce decomposition complexity but they can give some negative effect so					auto goalToAdd = depth >= 1 ? unwrapComposition(subgoal) : subgoal; 					if(!vstd::contains(goals[depth + 1], goalToAdd))					{						goals[depth + 1].push_back(subgoal);					}				}			}		}		if(depth < depthLimit - 1 && goals[depth + 1].size())		{			depth++;		}		else		{			goals[depth].pop_back();			while(depth > 0 && goals[depth].empty())			{				depth--;				goals[depth].pop_back();			}		}	}	return tasks;}Goals::TSubgoal DeepDecomposer::aggregateGoals(int startDepth, TSubgoal last){	Goals::Composition composition;	for(int i = 0; i <= depth; i++)	{		composition.addNext(goals[i].back());	}	composition.addNext(last);	return sptr(composition);}Goals::TSubgoal DeepDecomposer::unwrapComposition(Goals::TSubgoal goal){	return goal->goalType == Goals::COMPOSITION ? goal->decompose().back() : goal;}bool isEquivalentGoals(TSubgoal goal1, TSubgoal goal2){	if(goal1 == goal2) return true;	if(goal1->goalType == Goals::CAPTURE_OBJECT && goal2->goalType == Goals::CAPTURE_OBJECT)	{		auto o1 = cb->getObj(ObjectInstanceID(goal1->objid));		auto o2 = cb->getObj(ObjectInstanceID(goal2->objid));		return o1->ID == Obj::SHIPYARD && o1->ID == o2->ID;	}	return false;}bool DeepDecomposer::isCompositionLoop(TSubgoal goal){	auto goalsToTest = goal->goalType == Goals::COMPOSITION ? goal->decompose() : TGoalVec{goal};	for(auto goalToTest : goalsToTest)	{		for(int i = depth; i >= 0; i--)		{			auto parent = unwrapComposition(goals[i].back());			if(isEquivalentGoals(parent, goalToTest))			{				return true;			}		}	}	return false;}TGoalVec DeepDecomposer::decomposeCached(TSubgoal goal, bool & fromCache){#if NKAI_TRACE_LEVEL >= 1		logAi->trace("Decomposing %s, level %s", goal->toString(), depth);#endif	if(goal->hasHash())	{		for(int i = 0; i <= depth; i++)		{			auto cached = decompositionCache[i].find(goal);			if(cached != decompositionCache[i].end())			{#if NKAI_TRACE_LEVEL >= 1				logAi->trace("Use decomposition cache for %s, level: %d", goal->toString(), depth);#endif				fromCache = true;				return cached->second;			}		}		decompositionCache[depth][goal] = {}; // if goal decomposition yields no goals we still need it in cache to not decompose again	}#if NKAI_TRACE_LEVEL >= 2		logAi->trace("Calling decompose on %s, level %s", goal->toString(), depth);#endif	fromCache = false;	return goal->decompose();}void DeepDecomposer::addToCache(TSubgoal goal){	bool trusted = true;	for(int parentDepth = 1; parentDepth <= depth; parentDepth++)	{		TSubgoal parent = unwrapComposition(goals[parentDepth].back());		if(parent->hasHash())		{			auto solution = parentDepth < depth ? aggregateGoals(parentDepth + 1, goal) : goal;#if NKAI_TRACE_LEVEL >= 2			logAi->trace("Adding %s to decomosition cache of %s at level %d", solution->toString(), parent->toString(), parentDepth);#endif			decompositionCache[parentDepth][parent].push_back(solution);			if(trusted && parentDepth != 0)			{				decompositionCache[0][parent].push_back(solution);				trusted = false;			}		}	}}}
 |