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

Base class for Controllers shared between multiple tasks. More...

#include <controller.h>

Inherits cherrypi::ControllerBase.

Inherited by cherrypi::GathererController.

Public Member Functions

virtual ~SharedController ()=default
 
- Public Member Functions inherited from cherrypi::ControllerBase
 ControllerBase (Module *module)
 
virtual ~ControllerBase ()=default
 
virtual void addUnit (State *state, Unit *unit, UpcId id)
 Add a unit to this controller. More...
 
virtual void removeUnit (State *state, Unit *unit, UpcId id)
 Remove a unit from this controller. More...
 
virtual bool keepUnit (State *state, Unit *unit) const
 Decide whether to keep a unit. More...
 
virtual void step (State *state)
 Advance controller state and produce UPCs. More...
 
bool isControllingUnitWith (Unit *unit, UpcId id) const
 Checks if the controller is controlling the given unit via the given UPC ID. More...
 
virtual const char * getName () const
 A name for this Controller, for debugging purposes. More...
 

Static Public Member Functions

template<typename T >
static std::shared_ptr< T > globalInstance (State *state, Module *module, std::string name=std::string())
 Retrieves the global instance of a shared controller. More...
 

Additional Inherited Members

- Protected Member Functions inherited from cherrypi::ControllerBase
void postUpcs (State *state)
 Posts scheduled UPCs to the Blackboard. More...
 
template<typename... Args>
void addUpc (Unit *unit, Args &&...args)
 Schedules an action (as a UPC) for the given unit which will be posted after doStep(). More...
 
- Protected Attributes inherited from cherrypi::ControllerBase
Modulemodule_
 
std::unordered_map< Unit *, UpcIdunits_
 
std::unordered_map< Unit *, std::pair< UpcId, std::shared_ptr< UPCTuple > > > upcs_
 

Detailed Description

Base class for Controllers shared between multiple tasks.

A common pattern is the control of multiple units in a centralized fashion. Since unit allocation is globally managed via Task objects which have a 1:1 relation to their respective UPCs, this requires handling multiple Task objects.

With SharedController and SharedControllerTask, this pattern can be implemented quite easily by inheriting from SharedController. Typically, the resulting code in Module::step() will look similar to this:

void MyModule::step(State* state) {
auto controller = SharedController::globalInstance<MyController>(state, this);
// For the current relevant UPCs on the Blackboard
for (auto& it : relevantUpcs()) {
auto upcId = it.first;
auto& upc = it.second;
board->consumeUPC(upcId, this);
// Select units from upc.unit
auto units = sampleUnits(upc);
// Create a new task and register it in the controller instance
auto task = std::make_shared<SharedControllerTask>(upcId, units,
controller);
board->postTask(task, this, true);
}
controller->step(state);
}

Constructor & Destructor Documentation

virtual cherrypi::SharedController::~SharedController ( )
virtualdefault

Member Function Documentation

template<typename T >
static std::shared_ptr<T> cherrypi::SharedController::globalInstance ( State state,
Module module,
std::string  name = std::string() 
)
inlinestatic

Retrieves the global instance of a shared controller.

Shared controllers can be stored in the Blackboard. This function will create the requested controller object if necessary (the Blackboard key is "controller_<module name>/<controller name>"


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