AI Toolkit
Loading...
Searching...
No Matches
Utility AI

Classes

class  aitoolkit::utility::action< T >
 Base abstract class for all actions. More...
 
class  aitoolkit::utility::evaluator< T >
 Evaluate a set of actions and apply the best one. More...
 

Typedefs

template<typename T >
using aitoolkit::utility::action_ptr = std::unique_ptr<action<T>>
 Heap allocated pointer to an action.
 

Functions

template<typename T , action_trait< T > ... Actions>
std::vector< action_ptr< T > > aitoolkit::utility::action_list (Actions &&... actions)
 Helper function to create a list of actions.
 

Detailed Description

Introduction

Utility AI is a planning algorithm that can be used to find the best action to perform in a given situation. The algorithm works by assigning a score to each action based on how well it will achieve the goal. The algorithm is guaranteed to find a solution.

flowchart TD

  a1[Collect food\nscore: +50]
  a2[Collect wood\nscore: +150]
  a3[Collect stone\nscore: -10]
  a4[Collect gold\nscore: +75]

  style a1 color:darkred
  style a2 color:darkgreen
  style a3 color:darkred
  style a4 color:darkred

Usage

First, include the header file:

#include <aitoolkit/utility.hpp>

Then, create a blackboard type:

struct blackboard_type {
int food{0};
int wood{0};
int stone{0};
int gold{0};
};

Next, create a class for each action that you want to be able to perform:

using namespace aitoolkit::utility;
class collect_food final : public action<blackboard_type> {
public:
virtual float score(const blackboard_type& blackboard) const override {
return 50.0f;
}
virtual void apply(blackboard_type& blackboard) const override {
blackboard.food += 1;
}
};
class collect_wood final : public action<blackboard_type> {
public:
virtual float score(const blackboard_type& blackboard) const override {
return 150.0f;
}
virtual void apply(blackboard_type& blackboard) const override {
blackboard.wood += 1;
}
};
class collect_stone final : public action<blackboard_type> {
public:
virtual float score(const blackboard_type& blackboard) const override {
return -10.0f;
}
virtual void apply(blackboard_type& blackboard) const override {
blackboard.stone += 1;
}
};
class collect_gold final : public action<blackboard_type> {
public:
virtual float score(const blackboard_type& blackboard) const override {
return 75.0f;
}
virtual void apply(blackboard_type& blackboard) const override {
blackboard.gold += 1;
}
};
Base abstract class for all actions.
Definition utility.hpp:128

Finally, create an evaluator and run it:

action_list<blackboard_type>(
collect_food{},
collect_wood{},
collect_stone{},
collect_gol{}
)
};
auto blackboard = blackboard_type{};
evaluator.run(blackboard);
Evaluate a set of actions and apply the best one.
Definition utility.hpp:171
void run(T &blackboard) const
Find the best action and apply it to the blackboard.
Definition utility.hpp:181