Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
HTN_Plan_Reuse
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Aleksey
HTN_Plan_Reuse
Commits
1227f1f6
Commit
1227f1f6
authored
Mar 02, 2016
by
DennisSoemers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleaned up code a little bit
parent
42a808bb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
8 additions
and
304 deletions
+8
-304
HTNPlan.h
HTN_Plugin/Source/HTN_Plugin/Classes/HTNPlanner/HTNPlan.h
+0
-5
HTNPlannerComponent.h
...ource/HTN_Plugin/Classes/HTNPlanner/HTNPlannerComponent.h
+0
-2
HTNPlan.cpp
HTN_Plugin/Source/HTN_Plugin/Private/HTNPlanner/HTNPlan.cpp
+0
-242
HTNPlannerComponent.cpp
...rce/HTN_Plugin/Private/HTNPlanner/HTNPlannerComponent.cpp
+6
-55
JSHOP2_ExperimenterSimpleFPS.cpp
...P2_Experiments/SimpleFPS/JSHOP2_ExperimenterSimpleFPS.cpp
+2
-0
No files found.
HTN_Plugin/Source/HTN_Plugin/Classes/HTNPlanner/HTNPlan.h
View file @
1227f1f6
...
...
@@ -23,15 +23,10 @@ public:
void
AppendTaskInstance
(
const
TSharedPtr
<
FHTNTaskInstance
>&
NewTaskInstance
);
void
AppendTaskInstances
(
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
NewTaskInstances
);
TSharedPtr
<
FHTNPlan
>
Copy
();
int32
GetLastStreakScore
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
;
int32
GetLongestMatchingStreak
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
;
int32
GetMatchingCompounds
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
;
int32
GetMatchingStreak
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
;
int32
GetPlanSize
()
const
;
int32
GetPotentialSimilarity
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
;
int32
GetRemainingPlanSize
()
const
;
int32
GetSimilarity
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
;
int32
GetStreakScore
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
;
TSharedPtr
<
FHTNTaskInstance
>
GetTaskInstanceToExecute
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
GetSearchHistory
()
const
;
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
GetTasks
()
const
;
...
...
HTN_Plugin/Source/HTN_Plugin/Classes/HTNPlanner/HTNPlannerComponent.h
View file @
1227f1f6
...
...
@@ -224,8 +224,6 @@ protected:
/** Flag used for probabilistic plan reuse */
uint8
bIgnoringPlanReuse
:
1
;
/** If true, plan reuse uses an adaptive minimum streak length instead of a constant */
uint8
bAdaptivePlanReuse
:
1
;
/** If true, we do a depth-first search. Otherwise, breadth-first/A* style search */
uint8
bDepthFirstSearch
:
1
;
/** if set, execution requests will be postponed */
...
...
HTN_Plugin/Source/HTN_Plugin/Private/HTNPlanner/HTNPlan.cpp
View file @
1227f1f6
...
...
@@ -39,61 +39,6 @@ TSharedPtr<FHTNPlan> FHTNPlan::Copy()
return
Copy
;
}
int32
FHTNPlan
::
GetLastStreakScore
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
{
if
(
!
OtherPlan
.
IsValid
())
{
return
0
;
}
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
MyTasks
=
GetSearchHistory
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
OtherTasks
=
OtherPlan
->
GetSearchHistory
();
int32
StreakLength
=
0
;
int32
FirstMatchFound
=
-
1
;
int32
LastMatchFound
=
OtherTasks
.
Num
();
for
(
int32
MyIdx
=
MyTasks
.
Num
()
-
1
;
MyIdx
>=
0
;
--
MyIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
MyTask
=
MyTasks
[
MyIdx
];
bool
bFoundMatch
=
false
;
for
(
int32
OtherIdx
=
LastMatchFound
-
1
;
OtherIdx
>=
0
;
--
OtherIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
OtherTask
=
OtherTasks
[
OtherIdx
];
if
(
MyTask
->
Task
==
OtherTask
->
Task
&&
MyTask
->
GetUint8Memory
()
==
OtherTask
->
GetUint8Memory
())
{
// found a match
++
StreakLength
;
LastMatchFound
=
OtherIdx
;
bFoundMatch
=
true
;
if
(
FirstMatchFound
==
-
1
)
{
// this is the first matching task we found
FirstMatchFound
=
OtherIdx
;
}
break
;
}
}
if
(
!
bFoundMatch
&&
FirstMatchFound
!=
-
1
)
{
// we have found a streak somewhere but it ended now
break
;
}
}
if
(
StreakLength
<
MinStreakLength
)
{
return
0
;
}
return
FMath
::
Max
(
0
,
StreakLength
-
(
OtherTasks
.
Num
()
-
1
-
FirstMatchFound
));
}
int32
FHTNPlan
::
GetLongestMatchingStreak
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
{
if
(
!
OtherPlan
.
IsValid
())
...
...
@@ -148,49 +93,6 @@ int32 FHTNPlan::GetLongestMatchingStreak(const TSharedPtr<FHTNPlan>& OtherPlan,
return
LongestStreakLength
;
}
int32
FHTNPlan
::
GetMatchingCompounds
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
{
if
(
!
OtherPlan
.
IsValid
())
{
return
0
;
}
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
MyTasks
=
GetSearchHistory
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
OtherTasks
=
OtherPlan
->
GetSearchHistory
();
// we'll append all tasks in TaskNetwork to MyTasks, since we know we'll be executing them in the future
UTaskNetwork
*
Network
=
Cast
<
UTaskNetwork
>
(
TaskNetwork
->
Task
);
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
NetworkInstances
=
Network
->
CollectAllTasks
(
TaskNetwork
->
GetMemory
());
MyTasks
.
Append
(
NetworkInstances
);
int32
MatchingCompounds
=
0
;
// the number of matching compound tasks that we have found so far
for
(
int32
MyIdx
=
0
;
MyIdx
<
MyTasks
.
Num
();
++
MyIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
MyTask
=
MyTasks
[
MyIdx
];
if
(
!
MyTask
->
Task
->
IsA
(
UCompoundTask
::
StaticClass
()))
{
continue
;
// only care about compounds
}
// try to find MyTask in OtherTasks
for
(
int32
OtherIdx
=
0
;
OtherIdx
<
OtherTasks
.
Num
();
++
OtherIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
OtherTask
=
OtherTasks
[
OtherIdx
];
if
(
MyTask
->
Task
==
OtherTask
->
Task
&&
MyTask
->
GetUint8Memory
()
==
OtherTask
->
GetUint8Memory
())
{
// found a match
++
MatchingCompounds
;
break
;
}
}
}
return
MatchingCompounds
;
}
int32
FHTNPlan
::
GetMatchingStreak
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
{
if
(
!
OtherPlan
.
IsValid
())
...
...
@@ -258,155 +160,11 @@ int32 FHTNPlan::GetPlanSize() const
return
TaskInstances
.
Num
();
}
// makes everything slow and finds bad solutions first
int32
FHTNPlan
::
GetPotentialSimilarity
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
{
if
(
!
OtherPlan
.
IsValid
())
{
return
0
;
}
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
MyTasks
=
GetSearchHistory
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
OtherTasks
=
OtherPlan
->
GetSearchHistory
();
// we'll append all tasks in TaskNetwork to MyTasks, since we know we'll be executing them in the future
UTaskNetwork
*
Network
=
Cast
<
UTaskNetwork
>
(
TaskNetwork
->
Task
);
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
NetworkInstances
=
Network
->
CollectAllTasks
(
TaskNetwork
->
GetMemory
());
MyTasks
.
Append
(
NetworkInstances
);
int32
MatchingTasks
=
0
;
// the number of matching tasks that we have found so far
int32
LastMatchFound
=
-
1
;
// the last index in the OtherTasks array in which we have found a match
for
(
int32
MyIdx
=
0
;
MyIdx
<
MyTasks
.
Num
();
++
MyIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
MyTask
=
MyTasks
[
MyIdx
];
// try to find MyTask in the remaining part of OtherTasks
for
(
int32
OtherIdx
=
LastMatchFound
+
1
;
OtherIdx
<
OtherTasks
.
Num
();
++
OtherIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
OtherTask
=
OtherTasks
[
OtherIdx
];
if
(
MyTask
->
Task
==
OtherTask
->
Task
&&
MyTask
->
GetUint8Memory
()
==
OtherTask
->
GetUint8Memory
())
{
// found a match
++
MatchingTasks
;
LastMatchFound
=
OtherIdx
;
break
;
}
}
}
// maximum potential similarity = number of matching tasks so far + number of tasks remaining at the end of OtherPlan
return
(
MatchingTasks
+
OtherTasks
.
Num
()
-
LastMatchFound
-
1
);
}
int32
FHTNPlan
::
GetRemainingPlanSize
()
const
{
return
TaskInstances
.
Num
()
-
ExecutionIndex
;
}
// Finds good plans first, but slows down everything (too expensive computationally?)
int32
FHTNPlan
::
GetSimilarity
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
const
TSharedPtr
<
FHTNTaskInstance
>&
TaskNetwork
)
const
{
if
(
!
OtherPlan
.
IsValid
())
{
return
0
;
}
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
MyTasks
=
GetSearchHistory
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
OtherTasks
=
OtherPlan
->
GetSearchHistory
();
// we'll append all tasks in TaskNetwork to MyTasks, since we know we'll be executing them in the future
UTaskNetwork
*
Network
=
Cast
<
UTaskNetwork
>
(
TaskNetwork
->
Task
);
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>
NetworkInstances
=
Network
->
CollectAllTasks
(
TaskNetwork
->
GetMemory
());
MyTasks
.
Append
(
NetworkInstances
);
int32
MatchingTasks
=
0
;
// the number of matching tasks that we have found so far
int32
LastMatchFound
=
-
1
;
// the last index in the OtherTasks array in which we have found a match
for
(
int32
MyIdx
=
0
;
MyIdx
<
MyTasks
.
Num
();
++
MyIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
MyTask
=
MyTasks
[
MyIdx
];
// try to find MyTask in the remaining part of OtherTasks
for
(
int32
OtherIdx
=
LastMatchFound
+
1
;
OtherIdx
<
OtherTasks
.
Num
();
++
OtherIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
OtherTask
=
OtherTasks
[
OtherIdx
];
if
(
MyTask
->
Task
==
OtherTask
->
Task
&&
MyTask
->
GetUint8Memory
()
==
OtherTask
->
GetUint8Memory
())
{
// found a match
++
MatchingTasks
;
LastMatchFound
=
OtherIdx
;
break
;
}
}
}
return
MatchingTasks
;
}
int32
FHTNPlan
::
GetStreakScore
(
const
TSharedPtr
<
FHTNPlan
>&
OtherPlan
,
int32
MinStreakLength
)
const
{
if
(
!
OtherPlan
.
IsValid
())
{
return
0
;
}
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
MyTasks
=
GetSearchHistory
();
const
TArray
<
TSharedPtr
<
FHTNTaskInstance
>>&
OtherTasks
=
OtherPlan
->
GetSearchHistory
();
int32
StreakLength
=
0
;
int32
LastMatchFound
=
OtherTasks
.
Num
();
bool
bImmediateStreak
=
true
;
bool
bStartedStreak
=
false
;
for
(
int32
MyIdx
=
MyTasks
.
Num
()
-
1
;
MyIdx
>=
0
;
--
MyIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
MyTask
=
MyTasks
[
MyIdx
];
bool
bFoundMatch
=
false
;
for
(
int32
OtherIdx
=
LastMatchFound
-
1
;
OtherIdx
>=
0
;
--
OtherIdx
)
{
const
TSharedPtr
<
FHTNTaskInstance
>&
OtherTask
=
OtherTasks
[
OtherIdx
];
if
(
MyTask
->
Task
==
OtherTask
->
Task
&&
MyTask
->
GetUint8Memory
()
==
OtherTask
->
GetUint8Memory
())
{
// found a match
++
StreakLength
;
LastMatchFound
=
OtherIdx
;
bFoundMatch
=
true
;
bStartedStreak
=
true
;
break
;
}
}
if
(
!
bFoundMatch
)
// end of the streak
{
if
(
bStartedStreak
)
{
break
;
}
else
{
bImmediateStreak
=
false
;
}
}
}
if
(
StreakLength
<
MinStreakLength
)
{
return
0
;
}
if
(
!
bImmediateStreak
)
{
StreakLength
=
(
int32
)(
0.5
f
*
StreakLength
);
}
return
StreakLength
;
}
TSharedPtr
<
FHTNTaskInstance
>
FHTNPlan
::
GetTaskInstanceToExecute
()
{
if
(
TaskInstances
.
IsValidIndex
(
ExecutionIndex
))
...
...
HTN_Plugin/Source/HTN_Plugin/Private/HTNPlanner/HTNPlannerComponent.cpp
View file @
1227f1f6
...
...
@@ -29,7 +29,6 @@ UHTNPlannerComponent::UHTNPlannerComponent(const FObjectInitializer& ObjectIniti
PreviousPlan
=
nullptr
;
NumStackElements
=
0
;
bAdaptivePlanReuse
=
false
;
DefaultMinMatchingStreakLength
=
10
;
MinMatchingStreakLength
=
DefaultMinMatchingStreakLength
;
...
...
@@ -67,18 +66,13 @@ void UHTNPlannerComponent::AddStackElement(const FHTNStackElement& StackElement)
// this node is continuing a streak
StreakStacks
[
CurrentMatchingStreakLength
].
Push
(
StackElement
);
MaxCurrentMatchingStreakLength
=
FMath
::
Max
(
MaxCurrentMatchingStreakLength
,
CurrentMatchingStreakLength
);
//MinMatchingStreakLength = MaxCurrentMatchingStreakLength;
}
else
if
(
MaxCurrentMatchingStreakLength
>
0
)
{
// this node is no longer continuing a streak, but it did previously have a streak
//int32 LongestOldStreak = StackElement.Plan->GetLongestMatchingStreak(PreviousPlan, MinMatchingStreakLength);
//StreakEndedQueues[LongestOldStreak].Enqueue(StackElement);
StreakEndedQueues
[
MaxCurrentMatchingStreakLength
].
Enqueue
(
StackElement
);
MaxEndedMatchingStreakLength
=
FMath
::
Max
(
MaxEndedMatchingStreakLength
,
MaxCurrentMatchingStreakLength
/*LongestOldStreak*/
);
//StreakEndedQueues[1].Enqueue(StackElement);
//MaxEndedMatchingStreakLength = 1;
MaxCurrentMatchingStreakLength
);
}
else
if
(
MaxPastMatchingStreakLength
>
0
)
{
...
...
@@ -128,8 +122,6 @@ FHTNStackElement UHTNPlannerComponent::PopStackElement()
}
else
if
(
MaxCurrentMatchingStreakLength
>
0
)
{
//UE_LOG(LogHTNPlanner, Warning, TEXT("Popping from StreakStacks with MaxCurrentMatchingStreakLength = %d"),
// MaxCurrentMatchingStreakLength);
// we can pop a node that's currently in a matching streak
return
StreakStacks
[
MaxCurrentMatchingStreakLength
].
Pop
(
false
);
}
...
...
@@ -293,8 +285,6 @@ void UHTNPlannerComponent::OnTaskFinished(TSharedPtr<FHTNTaskInstance> TaskInsta
if
(
TaskResult
!=
EHTNExecutionResult
::
InProgress
)
{
//GEngine->AddOnScreenDebugMessage(-1, 1.5f, FColor::Yellow, TEXT("UHTNPlannerComponent::OnTaskFinished()"));
// cleanup task observers
UnregisterMessageObserversFrom
(
TaskInstance
);
...
...
@@ -358,14 +348,6 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
return
;
}
/**
* Implementation of the following algorithm that searches for the plan based on:
* Menif, A., Jacopin, ., & Cazenave, T. (2014). SHPE: HTN Planning for Video Games.
* In Computer Games (pp. 119-132). Springer International Publishing.
*
* (link: http://www.lamsade.dauphine.fr/~cazenave/papers/MenifCGW2014.pdf)
*/
if
(
NumStackElements
==
0
)
{
//BestPlan = nullptr;
...
...
@@ -411,6 +393,11 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
++
NumNodesExpanded
;
#endif
if
(
PreviousPlan
.
IsValid
())
{
UE_LOG
(
LogHTNPlanner
,
Warning
,
TEXT
(
"%d nodes in data structure(s)"
),
NumStackElements
);
}
if
(
bProbabilisticPlanReuse
&&
bHitLeaf
)
{
// we've hit a leaf node, so it's time to re-evaluate whether we're ignoring plan reuse probabilistically
...
...
@@ -440,16 +427,6 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
if
(
PreviousPlan
.
IsValid
())
// we're doing plan reuse
{
//if(bAdaptivePlanReuse)
//{
// ++NumLeaves;
// TotalLeafStreakLengths += StackTop.Plan->GetLongestMatchingStreak(PreviousPlan, MinMatchingStreakLength);
//}
//if(bProbabilisticPlanReuse)
//{
// bHitLeaf = true;
//}
// verify that all of our values of maximum streak lengths among unprocessed nodes are still correct
UpdateMaxStreakLengths
();
}
...
...
@@ -494,11 +471,6 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
if
(
PreviousPlan
.
IsValid
())
// we're doing plan reuse
{
/*if(bAdaptivePlanReuse)
{
++NumLeaves;
TotalLeafStreakLengths += StackTop.Plan->GetLongestMatchingStreak(PreviousPlan, MinMatchingStreakLength);
}*/
if
(
bProbabilisticPlanReuse
)
{
bHitLeaf
=
true
;
...
...
@@ -547,11 +519,6 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
}
else
if
(
PreviousPlan
.
IsValid
())
{
/*if(bAdaptivePlanReuse)
{
++NumLeaves;
TotalLeafStreakLengths += StackTop.Plan->GetLongestMatchingStreak(PreviousPlan, MinMatchingStreakLength);
}*/
if
(
bProbabilisticPlanReuse
)
{
bHitLeaf
=
true
;
...
...
@@ -573,11 +540,6 @@ void UHTNPlannerComponent::ProcessExecutionRequest()
if
(
Decompositions
.
Num
()
==
0
)
// leaf node
{
/*if(bAdaptivePlanReuse)
{
++NumLeaves;
TotalLeafStreakLengths += StackTop.Plan->GetLongestMatchingStreak(PreviousPlan, MinMatchingStreakLength);
}*/
if
(
bProbabilisticPlanReuse
)
{
bHitLeaf
=
true
;
...
...
@@ -1120,15 +1082,4 @@ void UHTNPlannerComponent::UpdateMaxStreakLengths()
break
;
}
}
/*if(bAdaptivePlanReuse && NumLeaves > 0)
{
//int32 Previous = MinMatchingStreakLength;
MinMatchingStreakLength = FMath::Max(DefaultMinMatchingStreakLength, (int32)(1.1f * (TotalLeafStreakLengths / NumLeaves)));
//if(MinMatchingStreakLength != Previous)
//{
//UE_LOG(LogHTNPlanner, Warning, TEXT("Setting MinMatchingStreakLength to %d"), MinMatchingStreakLength);
//}
}*/
}
\ No newline at end of file
HTN_Plugin/Source/HTN_Plugin/Private/JSHOP2_Experiments/SimpleFPS/JSHOP2_ExperimenterSimpleFPS.cpp
View file @
1227f1f6
...
...
@@ -82,6 +82,8 @@ void AJSHOP2_ExperimenterSimpleFPS::InitializeWorldState(FHTNWorldState_SimpleFP
//InitializationSeed = 27207;
//InitializationSeed = 13123;
InitializationSeed
=
FMath
::
Rand
();
bInitializationSeedSet
=
true
;
HTNComp
->
DataCollector
->
SetWorldStateSeed
(
InitializationSeed
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment