TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
Public Types | Public Member Functions | Static Public Attributes | List of all members
cherrypi::Blackboard Class Reference

An access-aware blackboard. More...

#include <blackboard.h>

Public Types

using Data = mapbox::util::variant< bool, int, float, double, std::string, Position, std::shared_ptr< SharedController >, std::unordered_map< int, int >>
 A variant of types that are allowed in the Blackboard's key-value storage. More...
 
using UPCMap = std::map< UpcId, std::shared_ptr< UPCTuple >>
 
using TaskTimeStats = std::tuple< UpcId, std::string, std::chrono::milliseconds >
 

Public Member Functions

 Blackboard (State *state)
 
virtual ~Blackboard ()
 
void init ()
 
void clearCommands ()
 Clears the queue of commands. More...
 
void update ()
 Updates internal mappings after the torchcraft state has been updated. More...
 
void post (std::string const &key, Data const &data)
 
bool hasKey (std::string const &key)
 
Data const & get (std::string const &key) const
 
template<typename T >
T const & get (std::string const &key) const
 
template<typename T >
T const & get (std::string const &key, T const &defaultValue) const
 
void remove (std::string const &key)
 
template<typename T >
void iterValues (T f_do) const
 
bool isTracked (UnitId uid) const
 
void track (UnitId uid)
 
void untrack (UnitId uid)
 
UpcId postUPC (std::shared_ptr< UPCTuple > &&upc, UpcId sourceId, Module *origin, std::shared_ptr< UpcPostData > data=nullptr)
 Post a UPC tuple. More...
 
void consumeUPCs (std::vector< UpcId > const &ids, Module *consumer)
 
void consumeUPC (UpcId id, Module *consumer)
 
void removeUPCs (std::vector< UpcId > const &ids)
 
UPCMap upcs () const
 Returns all non-consumed UPCs. More...
 
UPCMap upcsFrom (Module *origin) const
 Returns all non-consumed UPCs from a given module. More...
 
UPCMap upcsFrom (std::shared_ptr< Module > origin) const
 Returns all non-consumed UPCs from a given module. More...
 
UPCMap upcsWithSharpCommand (Command cmd) const
 Returns all non-consumed UPCs with a Dirac command distribution on cmd. More...
 
UPCMap upcsWithCommand (Command cmd, float minProb) const
 Returns all non-consumed UPCs with a command distribution where cmd has at least a probability of minProb. More...
 
std::shared_ptr< UPCTupleupcWithId (UpcId id) const
 Returns the non-consumed UPC with the given ID. More...
 
UpcStorageupcStorage () const
 
void addUPCFilter (std::shared_ptr< UPCFilter > filter)
 
void removeUPCFilter (std::shared_ptr< UPCFilter > filter)
 
void postTask (std::shared_ptr< Task > task, Module *owner, bool autoRemove=false)
 
std::shared_ptr< TasktaskForId (UpcId id) const
 
std::vector< std::shared_ptr< Task > > tasksOfModule (Module *module) const
 
std::shared_ptr< TasktaskWithUnit (Unit *unit) const
 
TaskData taskDataWithUnit (Unit *unit) const
 
std::shared_ptr< TasktaskWithUnitOfModule (Unit *unit, Module *module) const
 
void markTaskForRemoval (UpcId upcId)
 
void markTaskForRemoval (std::shared_ptr< Task > task)
 
TaskStatus lastStatusOfTask (UpcId id) const
 This will return Unknown for tasks that were never registered. More...
 
void updateUnitAccessCounts (tc::Client::Command const &command)
 
void postCommand (tc::Client::Command const &command, UpcId sourceId)
 
std::vector< tc::Client::Command > commands (int stepsBack=0) const
 
size_t pastCommandsAvailable () const
 
void updateTasksByUnit (Task *task)
 Updates the taskByUnit mapping, should be called after setUnits on a task. More...
 
void checkPostStep ()
 UPC consistency checks Calling this only makes sense in the player's postStep function, once all the UPCs have been converted into commands to be posted to the game. More...
 
std::vector< TaskTimeStatsgetTaskTimeStats () const
 
void setCollectTimers (bool collect)
 
void setTraceDumper (std::shared_ptr< CherryVisDumperModule > tracer)
 
std::shared_ptr< CherryVisDumperModulegetTraceDumper ()
 

Static Public Attributes

static char const * kEnemyRaceKey = "enemy_race"
 
static char const * kEnemyNameKey = "enemy_name"
 
static char const * kBuildOrderKey = "buildorder"
 
static char const * kBuildOrderSwitchEnabledKey
 
static char const * kOpeningBuildOrderKey = "opening_buildorder"
 
static char const * kMinScoutFrameKey = "scout_min_frame"
 
static char const * kMaxScoutWorkersKey = "scout_max_workers"
 
static char const * kMaxScoutExplorersKey = "scout_max_explorers"
 
static char const * kGameUidKey = "game_uid"
 
static char const * kMineralsPerFramePerGatherer
 
static char const * kGasPerFramePerGatherer = "gas_per_frame_per_gatherer"
 
static char const * kGathererMinGasWorkers = "gatherer_min_gas_workers"
 
static char const * kGathererMaxGasWorkers = "gatherer_max_gas_workers"
 
static char const * kBanditRootKey = "bandit_root"
 

Detailed Description

An access-aware blackboard.

The blackboard provides a means for modules to exchange UPCTuples while keeping track of producers and consumers.

Furthermore, there is functionality for holding global state via a simple key-value store (post(), hasKey(), get() and remove()).

The blackboard itself will only store active UPCTuple objects, i.e. UPCs that have not been consumed and UPCs (as well as their sources) for which there are active tasks.

Member Typedef Documentation

using cherrypi::Blackboard::Data = mapbox::util::variant< bool, int, float, double, std::string, Position, std::shared_ptr<SharedController>, std::unordered_map<int, int>>

A variant of types that are allowed in the Blackboard's key-value storage.

using cherrypi::Blackboard::TaskTimeStats = std::tuple<UpcId, std::string, std::chrono::milliseconds>
using cherrypi::Blackboard::UPCMap = std::map<UpcId, std::shared_ptr<UPCTuple>>

Constructor & Destructor Documentation

cherrypi::Blackboard::Blackboard ( State state)
cherrypi::Blackboard::~Blackboard ( )
virtual

Member Function Documentation

void cherrypi::Blackboard::addUPCFilter ( std::shared_ptr< UPCFilter filter)
void cherrypi::Blackboard::checkPostStep ( )

UPC consistency checks Calling this only makes sense in the player's postStep function, once all the UPCs have been converted into commands to be posted to the game.

void cherrypi::Blackboard::clearCommands ( )

Clears the queue of commands.

std::vector< tc::Client::Command > cherrypi::Blackboard::commands ( int  stepsBack = 0) const
void cherrypi::Blackboard::consumeUPC ( UpcId  id,
Module consumer 
)
inline
void cherrypi::Blackboard::consumeUPCs ( std::vector< UpcId > const &  ids,
Module consumer 
)
Data const& cherrypi::Blackboard::get ( std::string const &  key) const
inline
template<typename T >
T const& cherrypi::Blackboard::get ( std::string const &  key) const
inline
template<typename T >
T const& cherrypi::Blackboard::get ( std::string const &  key,
T const &  defaultValue 
) const
inline
std::vector<TaskTimeStats> cherrypi::Blackboard::getTaskTimeStats ( ) const
inline
std::shared_ptr<CherryVisDumperModule> cherrypi::Blackboard::getTraceDumper ( )
inline
bool cherrypi::Blackboard::hasKey ( std::string const &  key)
inline
void cherrypi::Blackboard::init ( )
bool cherrypi::Blackboard::isTracked ( UnitId  uid) const
template<typename T >
void cherrypi::Blackboard::iterValues ( f_do) const
inline
TaskStatus cherrypi::Blackboard::lastStatusOfTask ( UpcId  id) const

This will return Unknown for tasks that were never registered.

void cherrypi::Blackboard::markTaskForRemoval ( UpcId  upcId)
void cherrypi::Blackboard::markTaskForRemoval ( std::shared_ptr< Task task)
inline
size_t cherrypi::Blackboard::pastCommandsAvailable ( ) const
inline
void cherrypi::Blackboard::post ( std::string const &  key,
Data const &  data 
)
inline
void cherrypi::Blackboard::postCommand ( tc::Client::Command const &  command,
UpcId  sourceId 
)
void cherrypi::Blackboard::postTask ( std::shared_ptr< Task task,
Module owner,
bool  autoRemove = false 
)
UpcId cherrypi::Blackboard::postUPC ( std::shared_ptr< UPCTuple > &&  upc,
UpcId  sourceId,
Module origin,
std::shared_ptr< UpcPostData data = nullptr 
)

Post a UPC tuple.

The UPC tuple will be subject to filtering via UPCFilter objects. The filtered UPCTuple object will end up in the Blackboard, and the function will return a resulting UPC ID that is unique for this Blackboard instance. If the UPCTuple was rejected by a filter, kFilteredUpcId will be returned.

The signature of this function enforces posting UPC tuples via move() as UPC filters modify UPC tuples by creating a copy of them. Hence, the UPCTuple instance ending up in the Blackboard might be different from the one provided to this function.

void cherrypi::Blackboard::remove ( std::string const &  key)
inline
void cherrypi::Blackboard::removeUPCFilter ( std::shared_ptr< UPCFilter filter)
void cherrypi::Blackboard::removeUPCs ( std::vector< UpcId > const &  ids)
void cherrypi::Blackboard::setCollectTimers ( bool  collect)
void cherrypi::Blackboard::setTraceDumper ( std::shared_ptr< CherryVisDumperModule tracer)
inline
TaskData cherrypi::Blackboard::taskDataWithUnit ( Unit unit) const
std::shared_ptr< Task > cherrypi::Blackboard::taskForId ( UpcId  id) const
std::vector< std::shared_ptr< Task > > cherrypi::Blackboard::tasksOfModule ( Module module) const
std::shared_ptr< Task > cherrypi::Blackboard::taskWithUnit ( Unit unit) const
std::shared_ptr< Task > cherrypi::Blackboard::taskWithUnitOfModule ( Unit unit,
Module module 
) const
void cherrypi::Blackboard::track ( UnitId  uid)
void cherrypi::Blackboard::untrack ( UnitId  uid)
Blackboard::UPCMap cherrypi::Blackboard::upcs ( ) const

Returns all non-consumed UPCs.

Blackboard::UPCMap cherrypi::Blackboard::upcsFrom ( Module origin) const

Returns all non-consumed UPCs from a given module.

UPCMap cherrypi::Blackboard::upcsFrom ( std::shared_ptr< Module origin) const
inline

Returns all non-consumed UPCs from a given module.

UpcStorage * cherrypi::Blackboard::upcStorage ( ) const
Blackboard::UPCMap cherrypi::Blackboard::upcsWithCommand ( Command  cmd,
float  minProb 
) const

Returns all non-consumed UPCs with a command distribution where cmd has at least a probability of minProb.

Blackboard::UPCMap cherrypi::Blackboard::upcsWithSharpCommand ( Command  cmd) const

Returns all non-consumed UPCs with a Dirac command distribution on cmd.

std::shared_ptr< UPCTuple > cherrypi::Blackboard::upcWithId ( UpcId  id) const

Returns the non-consumed UPC with the given ID.

void cherrypi::Blackboard::update ( )

Updates internal mappings after the torchcraft state has been updated.

void cherrypi::Blackboard::updateTasksByUnit ( Task task)

Updates the taskByUnit mapping, should be called after setUnits on a task.

void cherrypi::Blackboard::updateUnitAccessCounts ( tc::Client::Command const &  command)

Member Data Documentation

char const * cherrypi::Blackboard::kBanditRootKey = "bandit_root"
static
char const * cherrypi::Blackboard::kBuildOrderKey = "buildorder"
static
char const * cherrypi::Blackboard::kBuildOrderSwitchEnabledKey
static
Initial value:
=
"build_order_switch_enabled"
char const * cherrypi::Blackboard::kEnemyNameKey = "enemy_name"
static
char const * cherrypi::Blackboard::kEnemyRaceKey = "enemy_race"
static
char const * cherrypi::Blackboard::kGameUidKey = "game_uid"
static
char const * cherrypi::Blackboard::kGasPerFramePerGatherer = "gas_per_frame_per_gatherer"
static
char const * cherrypi::Blackboard::kGathererMaxGasWorkers = "gatherer_max_gas_workers"
static
char const * cherrypi::Blackboard::kGathererMinGasWorkers = "gatherer_min_gas_workers"
static
char const * cherrypi::Blackboard::kMaxScoutExplorersKey = "scout_max_explorers"
static
char const * cherrypi::Blackboard::kMaxScoutWorkersKey = "scout_max_workers"
static
char const * cherrypi::Blackboard::kMineralsPerFramePerGatherer
static
Initial value:
=
"minerals_per_frame_per_gatherer"
char const * cherrypi::Blackboard::kMinScoutFrameKey = "scout_min_frame"
static
char const * cherrypi::Blackboard::kOpeningBuildOrderKey = "opening_buildorder"
static

The documentation for this class was generated from the following files: