File: DeepDecomposer.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (240 lines) | stat: -rw-r--r-- 5,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* 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
{

using namespace Goals;

DeepDecomposer::DeepDecomposer(const Nullkiller * ai)
	:ai(ai), depth(0)
{
}

void DeepDecomposer::reset()
{
	decompositionCache.clear();
	goals.clear();
}

void DeepDecomposer::decompose(TGoalVec & result, TSubgoal behavior, int depthLimit)
{
	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))
				{
					result.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();
			}
		}
	}
}

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(ai).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(ai) : 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(ai);
}

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;
			}
		}
	}
}

}