Simple implementation of Decision Tree with C++
=================================
#include <iostream>
using namespace std;
bool val_visible = false;
bool val_evidence = true;
bool val_hungry = false;
float val_distance = 50;
class GameWorldEnv
{
public:
float GetPlayerDistance()
{
return rand() % 100;
}
};
class DecisionNode
{
public:
virtual DecisionNode* Decision() { return nullptr; };
virtual void Action() {};
};
class Decision : public DecisionNode
{
public:
};
class Boolean : public Decision
{
public:
Boolean(bool* val, DecisionNode* yes, DecisionNode* no)
: mTestValue(val)
, mYesNode(yes)
, mNoNode(no)
{
}
DecisionNode* Decision()
{
if (*mTestValue)
{
return mYesNode;
}
return mNoNode;
}
DecisionNode* mYesNode;
DecisionNode* mNoNode;
bool* mTestValue;
};
class Close : public Decision
{
public:
Close(float* distance, GameWorldEnv* env, DecisionNode* yes, DecisionNode* no)
: mTestDistance(distance)
, mEnv(env)
, mYesNode(yes)
, mNoNode(no)
{
}
DecisionNode* Decision()
{
if (mEnv->GetPlayerDistance() < *mTestDistance)
{
return mYesNode;
}
return mNoNode;
}
DecisionNode* mYesNode;
DecisionNode* mNoNode;
GameWorldEnv* mEnv;
float* mTestDistance;
};
class Action : public DecisionNode
{
public:
};
class Eat : public Action
{
public:
void Action()
{
cout << "Action Executed : Eat" << endl;
}
};
class Wander : public Action
{
public:
void Action()
{
cout << "Action Executed : Wander" << endl;
}
};
class Attack : public Action
{
public:
void Action()
{
cout << "Action Executed : Attack" << endl;
}
};
class Trace : public Action
{
public:
void Action()
{
cout << "Action Executed : Trace" << endl;
}
};
// Action은 Wander, Eat, Trace, Attack
// 총 4개가 있다.
void Process(DecisionNode* node)
{
DecisionNode* newNode = node->Decision();
if (newNode != nullptr)
{
Process(newNode);
}
else
{
// action
node->Action();
}
}
int main()
{
Eat eatNode;
Wander wanderNode;
Attack attackNode;
Trace traceNode;
GameWorldEnv env;
Close closeNode(&val_distance, &env, &attackNode, &traceNode);
Boolean hungryNode(&val_hungry, &eatNode, &wanderNode);
Boolean evidenceNode(&val_evidence, &closeNode, &hungryNode);
Boolean visibleNode(&val_visible, &closeNode, &evidenceNode);
DecisionNode* root = &visibleNode;
// Make a decision tree
for (int i = 0; i < 3; ++i)
{
Process(root);
Process(root);
}
}
Subscribe to:
Post Comments (Atom)
Task in UnrealEngine
https://www.youtube.com/watch?v=1lBadANnJaw
-
Unity released very good FPS example for people and I decided to analysis how they make this. Personally I wanted to show you how I analys...
-
When we use DrawDebugSphere function for debugging, it is working well but when you are trying to use it in anim node's function it will...
-
If you press a key 'L' in the jupyter notebook then you can see the line number in the editor.
No comments:
Post a Comment