Pyrogenesis  trunk
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
RL::Interface Class Reference

Implements an interface providing fundamental capabilities required for reinforcement learning (over HTTP). More...

#include <RLInterface.h>

Collaboration diagram for RL::Interface:
Collaboration graph
[legend]

Public Member Functions

 Interface (const char *server_address)
 
void TryApplyMessage ()
 Non-blocking call to process any pending messages from the RL client. More...
 

Private Member Functions

 NONCOPYABLE (Interface)
 
std::string Step (std::vector< GameCommand > &&commands)
 Process commands, update the simulation by one turn. More...
 
std::string Reset (ScenarioConfig &&scenario)
 Reset the game state according to scenario, cleaning up existing games if required. More...
 
std::string Evaluate (std::string &&code)
 Evaluate JS code in the engine such as applying arbitrary modifiers. More...
 
std::vector< std::string > GetTemplates (const std::vector< std::string > &names) const
 
bool IsGameRunning () const
 
std::string SendGameMessage (GameMessage &&msg)
 Internal helper. More...
 
bool TryGetGameMessage (GameMessage &msg)
 Internal helper. More...
 
void ApplyMessage (const GameMessage &msg)
 Process any pending messages from the RL client. More...
 
std::string GetGameState () const
 

Static Private Member Functions

static void * MgCallback (mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info)
 
static std::string GetRequestContent (struct mg_connection *conn)
 

Private Attributes

GameMessage m_GameMessage
 
ScenarioConfig m_ScenarioConfig
 
std::string m_ReturnValue
 
bool m_NeedsGameState = false
 
std::mutex m_Lock
 
std::mutex m_MsgLock
 
std::condition_variable m_MsgApplied
 
std::string m_Code
 

Detailed Description

Implements an interface providing fundamental capabilities required for reinforcement learning (over HTTP).

This consists of enabling an external script to configure the scenario (via Reset) and then step the game engine manually and apply player actions (via Step). The interface also supports querying unit templates to provide information about max health and other potentially relevant game state information.

See source/tools/rlclient/ for the external client code.

The HTTP server is threaded. Flow of data (with the interface active): 0. The game/main thread calls TryApplyMessage()

TryApplyMessage locks m_MsgLock, pulls the message, processes it, advances the simulation, and sets m_ReturnValue.

  1. TryApplyMessage notifies the RL thread that it can carry on and unlocks m_MsgLock. The main thread carries on frame rendering and goes back to 0.
  2. The RL thread locks m_MsgLock, reads m_ReturnValue, unlocks m_MsgLock, and sends the gamestate as HTTP Response to the RL client.
  3. The client processes the response and ultimately sends a new HTTP message to the RL Interface.
  4. The RL thread locks m_MsgLock, pushes the message, and starts waiting on the game/main thread to notify it (step 2).
    • GOTO 0.

Constructor & Destructor Documentation

◆ Interface()

RL::Interface::Interface ( const char *  server_address)

Member Function Documentation

◆ ApplyMessage()

void RL::Interface::ApplyMessage ( const GameMessage msg)
private

Process any pending messages from the RL client.

Updates m_ReturnValue to the gamestate after messages have been processed.

◆ Evaluate()

std::string RL::Interface::Evaluate ( std::string &&  code)
private

Evaluate JS code in the engine such as applying arbitrary modifiers.

Returns
the gamestate after script evaluation.

◆ GetGameState()

std::string RL::Interface::GetGameState ( ) const
private
Returns
the full gamestate as a JSON strong. This uses the AI representation since it is readily available in the JS Engine.

◆ GetRequestContent()

std::string RL::Interface::GetRequestContent ( struct mg_connection conn)
staticprivate

◆ GetTemplates()

std::vector< std::string > RL::Interface::GetTemplates ( const std::vector< std::string > &  names) const
private
Returns
template data for all templates of
Parameters
names.

◆ IsGameRunning()

bool RL::Interface::IsGameRunning ( ) const
private
Returns
true if a game is currently running.

◆ MgCallback()

void * RL::Interface::MgCallback ( mg_event  event,
struct mg_connection conn,
const struct mg_request_info request_info 
)
staticprivate

◆ NONCOPYABLE()

RL::Interface::NONCOPYABLE ( Interface  )
private

◆ Reset()

std::string RL::Interface::Reset ( ScenarioConfig &&  scenario)
private

Reset the game state according to scenario, cleaning up existing games if required.

Returns
the gamestate after resetting.

◆ SendGameMessage()

std::string RL::Interface::SendGameMessage ( GameMessage &&  msg)
private

Internal helper.

Move

Parameters
msginto m_GameMessage, wait until it has been processed by the main thread, and
Returns
the gamestate after that message is processed. It is invalid to call this if m_GameMessage is not currently empty.

◆ Step()

std::string RL::Interface::Step ( std::vector< GameCommand > &&  commands)
private

Process commands, update the simulation by one turn.

Returns
the gamestate after processing commands.

◆ TryApplyMessage()

void RL::Interface::TryApplyMessage ( )

Non-blocking call to process any pending messages from the RL client.

Updates m_ReturnValue to the gamestate after messages have been processed.

◆ TryGetGameMessage()

bool RL::Interface::TryGetGameMessage ( GameMessage msg)
private

Internal helper.

Returns
true if m_GameMessage is not empty, and updates
Parameters
msg,falseotherwise (msg is then unchanged).

Member Data Documentation

◆ m_Code

std::string RL::Interface::m_Code
private

◆ m_GameMessage

GameMessage RL::Interface::m_GameMessage
private

◆ m_Lock

std::mutex RL::Interface::m_Lock
mutableprivate

◆ m_MsgApplied

std::condition_variable RL::Interface::m_MsgApplied
private

◆ m_MsgLock

std::mutex RL::Interface::m_MsgLock
private

◆ m_NeedsGameState

bool RL::Interface::m_NeedsGameState = false
private

◆ m_ReturnValue

std::string RL::Interface::m_ReturnValue
private

◆ m_ScenarioConfig

ScenarioConfig RL::Interface::m_ScenarioConfig
private

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