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

Base class for single-task controllers. More...

#include <controller.h>

Inherits cherrypi::ControllerBase.

Inherited by cherrypi::BuilderControllerBase.

Public Member Functions

 Controller (Module *module)
 
virtual ~Controller ()=default
 
virtual bool didSucceed () const
 Implement this to return whether your custom Controller did succeed in its mission (if applicable) and can be disposed. More...
 
virtual bool didFail () const
 Implement this to return whether your custom Controller did fail in its mission (if applicable) and can be disposed. More...
 
void setUpcId (UpcId id)
 Set the UPC ID of the corresponding task. More...
 
- 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...
 

Protected Attributes

UpcId upcId_
 
- Protected Attributes inherited from cherrypi::ControllerBase
Modulemodule_
 
std::unordered_map< Unit *, UpcIdunits_
 
std::unordered_map< Unit *, std::pair< UpcId, std::shared_ptr< UPCTuple > > > upcs_
 

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...
 

Detailed Description

Base class for single-task controllers.

This class models a 1:1 relationship with an accompanying Task. Units are added to the controller when the respective task object (ControllerTask) is created.

Controller provides two additional virtual function that can be re-implemented by sub-classes: didSucceed() and didFail(). These will be used by the accompanying Task object to update its status. If your controller returns true for one of these functions, the accompanying Task will end and the controller is free to be disposed of and should not be stepped through any more.

A typical Module::step() function with Controller objects might look similar to this:

void MyModule::step(State* state) {
// 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 with a new controller instance
auto controller = std::make_shared<MyController>(this);
auto task = std::make_shared<ControllerTask>(upcId, units, controller);
board->postTask(task, this, true);
}
// Update active controllers
for (auto& task : state->board()->tasksOfModule(this)) {
auto ctask = std::static_pointer_cast<ControllerTask>(task);
auto controller = ctask->controller();
controller->step(state);
}
}

Constructor & Destructor Documentation

cherrypi::Controller::Controller ( Module module)
virtual cherrypi::Controller::~Controller ( )
virtualdefault

Member Function Documentation

bool cherrypi::Controller::didFail ( ) const
virtual

Implement this to return whether your custom Controller did fail in its mission (if applicable) and can be disposed.

By default, a Controller fails if it does not control any units.

Reimplemented in cherrypi::BuilderControllerBase.

virtual bool cherrypi::Controller::didSucceed ( ) const
inlinevirtual

Implement this to return whether your custom Controller did succeed in its mission (if applicable) and can be disposed.

By default, this returns false.

Reimplemented in cherrypi::BuilderControllerBase.

void cherrypi::Controller::setUpcId ( UpcId  id)

Set the UPC ID of the corresponding task.

Member Data Documentation

UpcId cherrypi::Controller::upcId_
protected

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