-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearningAgentsManager.cpp
More file actions
453 lines (358 loc) · 14.2 KB
/
Copy pathLearningAgentsManager.cpp
File metadata and controls
453 lines (358 loc) · 14.2 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// // If adding more than one agent gives an error despite the correct "MaxAgentNum"-varibale replace the file in "..\UE_5.4\Engine\Plugins\Experimental\LearningAgents\Source\LearningAgents\Private" with this one, it forces LA to generate 12 agent ids.
// // Copyright Epic Games, Inc. All Rights Reserved.
// #include "LearningAgentsManager.h"
// #include "LearningAgentsManagerListener.h"
// #include "LearningArray.h"
// #include "LearningLog.h"
// #include "UObject/ScriptInterface.h"
// #include "EngineDefines.h"
// ULearningAgentsManager::ULearningAgentsManager() : Super(FObjectInitializer::Get()) { PrimaryComponentTick.bCanEverTick = true; }
// ULearningAgentsManager::ULearningAgentsManager(FVTableHelper& Helper) : Super(Helper) {}
// ULearningAgentsManager::~ULearningAgentsManager() = default;
// void ULearningAgentsManager::PostInitProperties()
// {
// Super::PostInitProperties();
// if (HasAnyFlags(RF_ClassDefaultObject))
// {
// return;
// }
// // Pre-populate the vacant ids
// OnEventAgentIds.Reserve(MaxAgentNum);
// OccupiedAgentIds.Reserve(MaxAgentNum);
// VacantAgentIds.Reserve(MaxAgentNum);
// Agents.Reserve(MaxAgentNum);
// // UE_LOG(LogLearning, Error, TEXT("Please dont call me Shirly"));
// for (int32 AgentId = 12 - 1; AgentId >= 0; AgentId--)
// {
// // UE_LOG(LogLearning, Error, TEXT("MaxAgentNum: (%d)"), MaxAgentNum);
// VacantAgentIds.Push(AgentId);
// Agents.Add(nullptr);
// }
// UpdateAgentSets();
// }
// void ULearningAgentsManager::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
// {
// Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// // We only want to give agent tick events when the actual level is ticking - not when paused or just the viewport is ticking.
// if (TickType == ELevelTick::LEVELTICK_All)
// {
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsManagerTick(OccupiedAgentIds, DeltaTime);
// }
// }
// }
// int32 ULearningAgentsManager::GetMaxAgentNum() const
// {
// return MaxAgentNum;
// }
// int32 ULearningAgentsManager::AddAgent(UObject* Agent)
// {
// if (Agent == nullptr)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempted to add an agent but agent is nullptr."), *GetName());
// return INDEX_NONE;
// }
// if (VacantAgentIds.IsEmpty())
// {
// UE_LOG(LogLearning, Error, TEXT("%s: OOOAttempting to add an agent but we have no more vacant ids. Increase MaxAgentNum (%d) or remove unused agents."), *GetName(), MaxAgentNum);
// return INDEX_NONE;
// }
// // Add Agent
// // UE_LOG(LogLearning, Error, TEXT("First Agent enters adding process"));
// const int32 AgentId = VacantAgentIds.Pop();
// Agents[AgentId] = Agent;
// OccupiedAgentIds.Add(AgentId);
// UpdateAgentSets();
// OnEventAgentIds.Reset();
// OnEventAgentIds.Add(AgentId);
// // Call OnAgentsAdded Event
// UE_LOG(LogLearning, Display, TEXT("%s: OOOAdding Agent %s with id %i."), *GetName(), *Agent->GetName(), AgentId);
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsAdded(OnEventAgentIds);
// }
// // Return new id
// return AgentId;
// }
// void ULearningAgentsManager::AddAgents(TArray<int32>& OutAgentIds, const TArray<UObject*>& InAgents)
// {
// OnEventAgentIds.Reset();
// OutAgentIds.SetNumUninitialized(InAgents.Num());
// for (int32 AgentIdx = 0; AgentIdx < InAgents.Num(); AgentIdx++)
// {
// if (InAgents[AgentIdx] == nullptr)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempted to add an agent but agent is nullptr."), *GetName());
// OutAgentIds[AgentIdx] = INDEX_NONE;
// continue;
// }
// if (VacantAgentIds.IsEmpty())
// {
// UE_LOG(LogLearning, Error, TEXT("%s: AAAAttempting to add an agent but we have no more vacant ids. Increase MaxAgentNum (%d) or remove unused agents."), *GetName(), MaxAgentNum);
// OutAgentIds[AgentIdx] = INDEX_NONE;
// continue;
// }
// // Add Agent
// OutAgentIds[AgentIdx] = VacantAgentIds.Pop();
// Agents[OutAgentIds[AgentIdx]] = InAgents[AgentIdx];
// OccupiedAgentIds.Add(OutAgentIds[AgentIdx]);
// OnEventAgentIds.Add(OutAgentIds[AgentIdx]);
// }
// UpdateAgentSets();
// // Call OnAgentsAdded Event
// UE_LOG(LogLearning, Display, TEXT("%s: Adding Agents %s."), *GetName(), *UE::Learning::Array::FormatInt32(OnEventAgentIds));
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsAdded(OnEventAgentIds);
// }
// }
// void ULearningAgentsManager::RemoveAgent(const int32 AgentId)
// {
// if (AgentId == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempting to remove an agent with id of -1."), *GetName());
// return;
// }
// const int32 RemovedCount = OccupiedAgentIds.RemoveSingleSwap(AgentId, EAllowShrinking::No);
// if (RemovedCount == 0)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Trying to remove an agent with id of %i but it was not found."), *GetName(), AgentId);
// return;
// }
// UE_LEARNING_CHECKF(RemovedCount == 1, TEXT("Somehow agent id was included multiple times in set."));
// // Remove Agent
// VacantAgentIds.Push(AgentId);
// Agents[AgentId] = nullptr;
// UpdateAgentSets();
// OnEventAgentIds.Reset();
// OnEventAgentIds.Add(AgentId);
// // Call OnAgentsRemoved Event
// UE_LOG(LogLearning, Display, TEXT("%s: Removing Agent %i."), *GetName(), AgentId);
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsRemoved(OnEventAgentIds);
// }
// }
// void ULearningAgentsManager::RemoveAgents(const TArray<int32>& AgentIds)
// {
// OnEventAgentIds.Reset();
// for (int32 AgentIdx = 0; AgentIdx < AgentIds.Num(); AgentIdx++)
// {
// if (AgentIds[AgentIdx] == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempting to remove an agent with id of -1."), *GetName());
// continue;
// }
// const int32 RemovedCount = OccupiedAgentIds.RemoveSingleSwap(AgentIds[AgentIdx], EAllowShrinking::No);
// if (RemovedCount == 0)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Trying to remove an agent with id of %i but it was not found."), *GetName(), AgentIds[AgentIdx]);
// continue;
// }
// UE_LEARNING_CHECKF(RemovedCount == 1, TEXT("Somehow agent id was included multiple times in set."));
// // Remove Agent
// VacantAgentIds.Push(AgentIds[AgentIdx]);
// Agents[AgentIds[AgentIdx]] = nullptr;
// OnEventAgentIds.Add(AgentIds[AgentIdx]);
// }
// UpdateAgentSets();
// // Call OnAgentsRemoved Event
// UE_LOG(LogLearning, Display, TEXT("%s: Removing Agents %s."), *GetName(), *UE::Learning::Array::FormatInt32(OnEventAgentIds));
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsRemoved(OnEventAgentIds);
// }
// }
// void ULearningAgentsManager::RemoveAllAgents()
// {
// // We need to make a copy as OccupiedAgentIds will be modified during removal.
// const TArray<int32> RemoveAgentIds = OccupiedAgentIds;
// RemoveAgents(RemoveAgentIds);
// }
// void ULearningAgentsManager::ResetAgent(const int32 AgentId)
// {
// if (AgentId == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempting to reset an agent with id of -1."), *GetName());
// return;
// }
// if (!HasAgent(AgentId))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Trying to reset an agent with id of %i but it was not found."), *GetName(), AgentId);
// return;
// }
// OnEventAgentIds.Reset();
// OnEventAgentIds.Add(AgentId);
// // Call OnAgentsReset Event
// UE_LOG(LogLearning, Display, TEXT("%s: Resetting Agent %i."), *GetName(), AgentId);
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsReset(OnEventAgentIds);
// }
// }
// void ULearningAgentsManager::ResetAgents(const TArray<int32>& AgentIds)
// {
// OnEventAgentIds.Reset();
// for (int32 AgentIdx = 0; AgentIdx < AgentIds.Num(); AgentIdx++)
// {
// if (AgentIds[AgentIdx] == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Attempting to reset an agent with id of -1."), *GetName());
// continue;
// }
// if (!HasAgent(AgentIds[AgentIdx]))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Trying to reset an agent with id of %i but it was not found."), *GetName(), AgentIds[AgentIdx]);
// continue;
// }
// OnEventAgentIds.Add(AgentIds[AgentIdx]);
// }
// // Call OnAgentsReset Event
// UE_LOG(LogLearning, Display, TEXT("%s: Resetting Agents %s."), *GetName(), *UE::Learning::Array::FormatInt32(OnEventAgentIds));
// for (ULearningAgentsManagerListener* Listener : Listeners)
// {
// Listener->OnAgentsReset(OnEventAgentIds);
// }
// }
// void ULearningAgentsManager::ResetAllAgents()
// {
// ResetAgents(OccupiedAgentIds);
// }
// UObject* ULearningAgentsManager::GetAgent(const int32 AgentId, const TSubclassOf<UObject> AgentClass) const
// {
// if (!HasAgent(AgentId))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: AgentId %d not found. Be sure to only use AgentIds returned by AddAgent and check that the agent has not be removed."), *GetName(), AgentId);
// return nullptr;
// }
// if (Agents[AgentId] == nullptr)
// {
// UE_LOG(LogLearning, Warning, TEXT("%s: AgentId %d object is nullptr."), *GetName(), AgentId);
// return nullptr;
// }
// if (!Agents[AgentId].IsA(AgentClass))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: AgentId %d cast invalid. Agent was of class '%s', not '%s'."), *GetName(), AgentId, *Agents[AgentId]->GetClass()->GetName(), *AgentClass->GetName());
// return nullptr;
// }
// return Agents[AgentId];
// }
// void ULearningAgentsManager::GetAgents(TArray<UObject*>& OutAgents, const TArray<int32>& AgentIds, const TSubclassOf<UObject> AgentClass) const
// {
// OutAgents.SetNumUninitialized(AgentIds.Num());
// for (int32 AgentIdIdx = 0; AgentIdIdx < AgentIds.Num(); AgentIdIdx++)
// {
// if (!HasAgent(AgentIds[AgentIdIdx]))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: AgentId %d not found. Be sure to only use AgentIds returned by AddAgent and check that the agent has not be removed."), *GetName(), AgentIds[AgentIdIdx]);
// OutAgents[AgentIdIdx] = nullptr;
// continue;
// }
// if (Agents[AgentIds[AgentIdIdx]] == nullptr)
// {
// UE_LOG(LogLearning, Warning, TEXT("%s: AgentId %d object is nullptr."), *GetName(), AgentIds[AgentIdIdx]);
// OutAgents[AgentIdIdx] = nullptr;
// continue;
// }
// if (!Agents[AgentIds[AgentIdIdx]].IsA(AgentClass))
// {
// UE_LOG(LogLearning, Error, TEXT("%s: AgentId %d cast invalid. Agent was of class '%s', not '%s'."), *GetName(), AgentIds[AgentIdIdx], *Agents[AgentIds[AgentIdIdx]]->GetClass()->GetName(), *AgentClass->GetName());
// OutAgents[AgentIdIdx] = nullptr;
// continue;
// }
// OutAgents[AgentIdIdx] = Agents[AgentIds[AgentIdIdx]];
// }
// }
// void ULearningAgentsManager::GetAllAgents(TArray<UObject*>& OutAgents, TArray<int32>& OutAgentIds, const TSubclassOf<UObject> AgentClass) const
// {
// OutAgentIds = OccupiedAgentIds;
// GetAgents(OutAgents, OutAgentIds, AgentClass);
// }
// int32 ULearningAgentsManager::GetAgentId(UObject* Agent) const
// {
// const int32 AgentId = Agents.Find(Agent);
// if (AgentId == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Cannot get id for Agent %s, as it was not found in added agent set."), *GetName(), *Agent->GetName());
// return INDEX_NONE;
// }
// else
// {
// return AgentId;
// }
// }
// void ULearningAgentsManager::GetAgentIds(TArray<int32>& OutAgentIds, const TArray<UObject*>& InAgents) const
// {
// OutAgentIds.SetNumUninitialized(InAgents.Num());
// for (int32 AgentIdx = 0; AgentIdx < InAgents.Num(); AgentIdx++)
// {
// const int32 AgentId = Agents.Find(InAgents[AgentIdx]);
// if (AgentId == INDEX_NONE)
// {
// UE_LOG(LogLearning, Error, TEXT("%s: Cannot get id for Agent %s, as it was not found in added agent set."), *GetName(), *InAgents[AgentIdx]->GetName());
// OutAgentIds[AgentIdx] = INDEX_NONE;
// }
// else
// {
// OutAgentIds[AgentIdx] = AgentId;
// }
// }
// }
// int32 ULearningAgentsManager::GetAgentNum() const
// {
// return OccupiedAgentIds.Num();
// }
// bool ULearningAgentsManager::HasAgentObject(UObject* Agent) const
// {
// return Agents.Contains(Agent);
// }
// bool ULearningAgentsManager::HasAgent(const int32 AgentId) const
// {
// return OccupiedAgentSet.Contains(AgentId);
// }
// const UObject* ULearningAgentsManager::GetAgent(const int32 AgentId) const
// {
// UE_LEARNING_CHECK(HasAgent(AgentId));
// return Agents[AgentId];
// }
// UObject* ULearningAgentsManager::GetAgent(const int32 AgentId)
// {
// UE_LEARNING_CHECK(HasAgent(AgentId));
// return Agents[AgentId];
// }
// const TArray<int32>& ULearningAgentsManager::GetAllAgentIds() const
// {
// return OccupiedAgentIds;
// }
// UE::Learning::FIndexSet ULearningAgentsManager::GetAllAgentSet() const
// {
// return OccupiedAgentSet;
// }
// TConstArrayView<TObjectPtr<UObject>> ULearningAgentsManager::GetAgents() const
// {
// return Agents;
// }
// void ULearningAgentsManager::AddListener(ULearningAgentsManagerListener* Listener)
// {
// Listeners.Add(Listener);
// OnEventAgentIds = OccupiedAgentIds;
// Listener->OnAgentsAdded(OnEventAgentIds);
// }
// void ULearningAgentsManager::RemoveListener(ULearningAgentsManagerListener* Listener)
// {
// OnEventAgentIds = OccupiedAgentIds;
// Listener->OnAgentsRemoved(OnEventAgentIds);
// Listeners.Remove(Listener);
// }
// void ULearningAgentsManager::UpdateAgentSets()
// {
// // Keep OccupiedAgentIds sorted in ascending order for better memory access patterns.
// OccupiedAgentIds.Sort();
// OccupiedAgentSet = OccupiedAgentIds;
// OccupiedAgentSet.TryMakeSlice();
// // Keep VacantAgentIds sorted in descending order so that lowest ids are popped first.
// VacantAgentIds.Sort([](const int32& Lhs, const int32& Rhs) { return Lhs > Rhs; });
// VacantAgentSet = VacantAgentIds;
// VacantAgentSet.TryMakeSlice();
// }