Skip to content

Class cse491::WorldBase

ClassList > cse491 > WorldBase

Public Attributes

Type Name
netWorth::ClientManager * client_manager = = nullptr
Server manager for world if used.
netWorth::ServerManager * server_manager = = nullptr

Public Static Attributes

Type Name
constexpr size_t npos = = static_cast<size_t>(-1)

Public Functions

Type Name
AgentBase & AddAgent (std::string agent_name="None", PROPERTY_Ts... properties)
Build a new agent of the specified type.
AgentBase & AddConfiguredAgent (std::unique_ptr< AgentBase > agent_ptr)
Add an already-created agent.
ItemBase & AddItem (std::unique_ptr< ItemBase > item_ptr)
Add a new, already-built item.
ItemBase & AddItem (std::string item_name="None", PROPERTY_Ts... properties)
Build a new item and add it.
WorldBase & AddItemToGrid (size_t item_id, GridPosition pos, size_t grid_id=0)
void CollectData ()
virtual void ConfigAgent (AgentBase &) const
Client manager for world if used.
void Deserialize (std::istream & is, netWorth::ClientManager * manager)
Deserialize world, agents, and items from istream.
void DeserializeAgentSet (std::istream & is, netWorth::ClientManager * manager)
Add deserialized agents to world with a manager.
void DeserializeItemSet (std::istream & is)
Add deserialized items to world.
virtual int DoAction (AgentBase & agent, size_t action_id) = 0
Central function for an agent to take any action.
virtual std::vector< size_t > FindAgentsAt (GridPosition pos, size_t grid_id=0) const
Lookup IDs for all agents at a given grid position.
virtual std::vector< size_t > FindAgentsNear (GridPosition pos, double dist=1.0, size_t grid_id=0) const
Lookup IDs for all agents near a given grid position.
virtual std::vector< size_t > FindItemsAt (GridPosition pos, size_t grid_id=0) const
Lookup IDs for all items at a given grid position.
virtual std::vector< size_t > FindItemsNear (GridPosition pos, double dist=1.0, size_t grid_id=0) const
Lookup IDs for all items near a given grid position.
AgentBase & GetAgent (size_t id)
Return a reference to an agent with a given ID.
size_t GetAgentID (const std::string & name)
Return the ID of an agent with a given name.
size_t GetCellTypeID (const std::string & name) const
Return the ID associated with the cell type name.
const std::string & GetCellTypeName (size_t id) const
char GetCellTypeSymbol (size_t id) const
const type_options_t & GetCellTypes () const
virtual WorldGrid & GetGrid ()
Return an editable version of the current grid for this world (main_grid by default)
virtual WorldGrid & GetGrid (size_t grid_id)
virtual const WorldGrid & GetGrid () const
Return a const grid for this world (main_grid by default)
virtual const WorldGrid & GetGrid (size_t grid_id) const
ItemBase & GetItem (size_t id)
Return a reference to an agent with a given ID.
size_t GetItemID (const std::string & name)
Return the ID of an item with a given name.
size_t GetNumAgents () const
Get the total number of AGENT entities.
size_t GetNumItems () const
Get the total number of NON-agent entities.
double GetRandom ()
Return a uniform random value between 0.0 and 1.0.
double GetRandom (double max)
Return a uniform random value between 0.0 and max.
double GetRandom (double min, double max)
Return a uniform random value between min and max.
double GetRandomNormal ()
Return a gaussian random value with mean 0.0 and sd 1.0.
double GetRandomNormal (double mean, double sd=1.0)
Return a gaussian random value with provided mean and sd.
virtual bool GetRunOver () const
Determine if the run has ended.
unsigned int GetSeed () const
Get the seed used to initialize this RNG.
bool HasAgent (size_t id) const
Does an agent with the provided ID exist?
bool HasItem (size_t id) const
Does an item with the provided ID exist?
virtual bool IsTraversable (const AgentBase &, cse491::GridPosition) const
Determine if this tile can be walked on, defaults to every tile is walkable.
WorldBase & RemoveAgent (size_t agent_id)
Remove an agent from the agent map.
WorldBase & RemoveAgent (std::string agent_name="None")
Remove an agent from the agent map by name.
WorldBase & RemoveItem (size_t item_id)
Remove an item from the item map.
WorldBase & RemoveItem (std::string item_name)
Remove an item from the item map by name.
virtual void Reset ()
virtual void Run ()
Run all agents repeatedly until an end condition is met.
virtual void RunAgents ()
Step through each agent giving them a chance to take an action.
virtual void RunClient (netWorth::ClientManager * manager)
Run, but for client-side.
virtual void RunClientAgents ()
RunAgents, but with extra features for client-side.
virtual void RunServer (netWorth::ServerManager * manager)
Run, but for server-side.
virtual void RunServerAgents ()
RunAgents, but with extra features for server-side.
void Serialize (std::ostream & os)
Serialize world, agents, and items into ostream.
void SerializeAgentSet (std::ostream & os)
Serialize agent data into an ostream.
void SerializeItemSet (std::ostream & os)
Serialize item data into an ostream.
virtual void SetWorldRunning (bool running)
Set if world is running or not for concurrency purposes.
virtual void UpdateWorld ()
UpdateWorld() is run after every agent has a turn. Override this function to manage background events for a world.
WorldBase (unsigned int seed=0)
virtual ~WorldBase () = default

Protected Attributes

Type Name
std::string action
The action that the agent is currently performing.
agent_map_t agent_map
Map of IDs to pointers to agent entities.
std::shared_ptr< DataCollection::AgentReceiver > agent_receiver
std::unordered_map< size_t, WorldGrid > grids
Derived worlds may choose to have more than one grid.
item_map_t item_map
Map of IDs to pointers to non-agent entities.
size_t last_entity_id = = 0
The last Entity ID used; increment at each creation.
WorldGrid & main_grid
Main grid for this world; shortcut to grids["main"] __
std::normal_distribution norm_dist
Normal distribution; mean 0, std 1.
std::mt19937 random_gen
Random number generator.
bool run_over = = false
Should the run end?
unsigned int seed
Seed used for generator.
type_options_t type_options
Vector of types of cells in grids for this world.
std::uniform_real_distribution uni_dist
Uniform distribution of doubles, 0 to 1.
bool world_running = = true
Is the world currently running?

Protected Functions

Type Name
size_t AddCellType (const std::string & name, const std::string & desc="", char symbol='\0')
Add a new type of cell to this world.
virtual void ConfigAgent (AgentBase &)
size_t NextEntityID ()
Helper function to set the next entity id.

Public Attributes Documentation

variable client_manager

netWorth::ClientManager* client_manager;

variable server_manager

netWorth::ServerManager* server_manager;

Public Static Attributes Documentation

variable npos

constexpr size_t npos;

Public Functions Documentation

function AddAgent

Build a new agent of the specified type.

template<typename AGENT_T typename AGENT_T, typename... PROPERTY_Ts>
inline AgentBase & cse491::WorldBase::AddAgent (
    std::string agent_name="None",
    PROPERTY_Ts... properties
) 

Template parameters:

  • AGENT_T The type of agent to build
  • PROPERTY_Ts Types for any properties to set at creation (automatic)

Parameters:

  • agent_name The name of this agent
  • properties Name/value pairs for any properties set at creation

Returns:

A reference to the newly created agent

function AddConfiguredAgent

inline AgentBase & cse491::WorldBase::AddConfiguredAgent (
    std::unique_ptr< AgentBase > agent_ptr
) 

function AddItem [1/2]

Add a new, already-built item.

inline ItemBase & cse491::WorldBase::AddItem (
    std::unique_ptr< ItemBase > item_ptr
) 

Returns:

A reference to the newly created item

function AddItem [2/2]

Build a new item and add it.

template<typename ITEM_T typename ITEM_T, typename... PROPERTY_Ts>
inline ItemBase & cse491::WorldBase::AddItem (
    std::string item_name="None",
    PROPERTY_Ts... properties
) 

Template parameters:

  • PROPERTY_Ts Types for any properties to set at creation (automatic)

Parameters:

  • item_name The name of this item
  • properties Name/value pairs for any properties set at creation

Returns:

A reference to the newly created item

function AddItemToGrid

inline WorldBase & cse491::WorldBase::AddItemToGrid (
    size_t item_id,
    GridPosition pos,
    size_t grid_id=0
) 

function CollectData

inline void cse491::WorldBase::CollectData () 

function ConfigAgent [1/2]

Client manager for world if used.

inline virtual void cse491::WorldBase::ConfigAgent (
    AgentBase &
) const

Helper function that is run whenever a new agent is created.

Note:

Override this function to provide agents with actions or other setup.

function Deserialize

Deserialize world, agents, and items from istream.

inline void cse491::WorldBase::Deserialize (
    std::istream & is,
    netWorth::ClientManager * manager
) 

Parameters:

  • manager ClientManager for ControlledAgents

function DeserializeAgentSet

Add deserialized agents to world with a manager.

inline void cse491::WorldBase::DeserializeAgentSet (
    std::istream & is,
    netWorth::ClientManager * manager
) 

Parameters:

  • is istream
  • manager ClientManager which manages ControlledAgents

function DeserializeItemSet

Add deserialized items to world.

inline void cse491::WorldBase::DeserializeItemSet (
    std::istream & is
) 

Parameters:

  • istream

function DoAction

Central function for an agent to take any action.

virtual int cse491::WorldBase::DoAction (
    AgentBase & agent,
    size_t action_id
) = 0

Parameters:

  • agent The specific agent taking the action
  • action The id of the action to take

Returns:

The result of this action (usually 0/1 to indicate success)

Note:

Thus function must be overridden in any derived world.

function FindAgentsAt

Lookup IDs for all agents at a given grid position.

inline virtual std::vector< size_t > cse491::WorldBase::FindAgentsAt (
    GridPosition pos,
    size_t grid_id=0
) const

Parameters:

  • pos Grid position to look up.

Returns:

A vector of agent IDs at the target position.

function FindAgentsNear

Lookup IDs for all agents near a given grid position.

inline virtual std::vector< size_t > cse491::WorldBase::FindAgentsNear (
    GridPosition pos,
    double dist=1.0,
    size_t grid_id=0
) const

Parameters:

  • pos Grid position to look up.
  • dist Maximum distance away from pos for an agent to be included.

Returns:

A vector of agent IDs within dist of the target position.

function FindItemsAt

Lookup IDs for all items at a given grid position.

inline virtual std::vector< size_t > cse491::WorldBase::FindItemsAt (
    GridPosition pos,
    size_t grid_id=0
) const

Parameters:

  • pos Grid position to look up.
  • grid_id ID of grid we are examining (default: main grid)

Returns:

A vector of item IDs at the target position.

function FindItemsNear

Lookup IDs for all items near a given grid position.

inline virtual std::vector< size_t > cse491::WorldBase::FindItemsNear (
    GridPosition pos,
    double dist=1.0,
    size_t grid_id=0
) const

Parameters:

  • pos Grid position to look up.
  • dist Maximum distance away from pos for an item to be included.

Returns:

A vector of item IDs within dist of the target position.

function GetAgent

inline AgentBase & cse491::WorldBase::GetAgent (
    size_t id
) 

function GetAgentID

inline size_t cse491::WorldBase::GetAgentID (
    const std::string & name
) 

function GetCellTypeID

Return the ID associated with the cell type name.

inline size_t cse491::WorldBase::GetCellTypeID (
    const std::string & name
) const

Parameters:

  • name The unique name of the cell type

Returns:

The unique ID of the cell type (or 0 if it doesn't exist.)

function GetCellTypeName

inline const std::string & cse491::WorldBase::GetCellTypeName (
    size_t id
) const

function GetCellTypeSymbol

inline char cse491::WorldBase::GetCellTypeSymbol (
    size_t id
) const

function GetCellTypes

inline const type_options_t & cse491::WorldBase::GetCellTypes () const

function GetGrid [1/4]

inline virtual WorldGrid & cse491::WorldBase::GetGrid () 

function GetGrid [2/4]

inline virtual WorldGrid & cse491::WorldBase::GetGrid (
    size_t grid_id
) 

function GetGrid [3/4]

inline virtual const WorldGrid & cse491::WorldBase::GetGrid () const

function GetGrid [4/4]

inline virtual const WorldGrid & cse491::WorldBase::GetGrid (
    size_t grid_id
) const

function GetItem

inline ItemBase & cse491::WorldBase::GetItem (
    size_t id
) 

function GetItemID

inline size_t cse491::WorldBase::GetItemID (
    const std::string & name
) 

function GetNumAgents

inline size_t cse491::WorldBase::GetNumAgents () const

function GetNumItems

inline size_t cse491::WorldBase::GetNumItems () const

function GetRandom [1/3]

inline double cse491::WorldBase::GetRandom () 

function GetRandom [2/3]

inline double cse491::WorldBase::GetRandom (
    double max
) 

function GetRandom [3/3]

inline double cse491::WorldBase::GetRandom (
    double min,
    double max
) 

function GetRandomNormal [1/2]

inline double cse491::WorldBase::GetRandomNormal () 

function GetRandomNormal [2/2]

inline double cse491::WorldBase::GetRandomNormal (
    double mean,
    double sd=1.0
) 

function GetRunOver

inline virtual bool cse491::WorldBase::GetRunOver () const

function GetSeed

inline unsigned int cse491::WorldBase::GetSeed () const

function HasAgent

inline bool cse491::WorldBase::HasAgent (
    size_t id
) const

function HasItem

inline bool cse491::WorldBase::HasItem (
    size_t id
) const

function IsTraversable

Determine if this tile can be walked on, defaults to every tile is walkable.

inline virtual bool cse491::WorldBase::IsTraversable (
    const AgentBase &,
    cse491::GridPosition
) const

Author:

@mdkdoc15

Parameters:

  • pos The grid position we are checking

Returns:

If an agent should be allowed on this square

function RemoveAgent [1/2]

Remove an agent from the agent map.

inline WorldBase & cse491::WorldBase::RemoveAgent (
    size_t agent_id
) 

Parameters:

  • agent_id The unique ID this agent

Returns:

A reference to this world.

function RemoveAgent [2/2]

Remove an agent from the agent map by name.

inline WorldBase & cse491::WorldBase::RemoveAgent (
    std::string agent_name="None"
) 

Parameters:

  • agent_name The name of this agent

Returns:

This world

function RemoveItem [1/2]

Remove an item from the item map.

inline WorldBase & cse491::WorldBase::RemoveItem (
    size_t item_id
) 

Parameters:

  • item_id The unique ID this item

Returns:

A reference to this world.

function RemoveItem [2/2]

Remove an item from the item map by name.

inline WorldBase & cse491::WorldBase::RemoveItem (
    std::string item_name
) 

Parameters:

  • item_id The ID of this item

Returns:

This world

function Reset

inline virtual void cse491::WorldBase::Reset () 

function Run

inline virtual void cse491::WorldBase::Run () 

function RunAgents

Step through each agent giving them a chance to take an action.

inline virtual void cse491::WorldBase::RunAgents () 

Note:

Override this function if you want to control which grid the agents receive.

function RunClient

inline virtual void cse491::WorldBase::RunClient (
    netWorth::ClientManager * manager
) 

function RunClientAgents

RunAgents, but with extra features for client-side.

inline virtual void cse491::WorldBase::RunClientAgents () 

Note:

Override this function if you want to control which grid the agents receive.

function RunServer

inline virtual void cse491::WorldBase::RunServer (
    netWorth::ServerManager * manager
) 

function RunServerAgents

RunAgents, but with extra features for server-side.

inline virtual void cse491::WorldBase::RunServerAgents () 

Note:

Override this function if you want to control which grid the agents receive.

function Serialize

Serialize world, agents, and items into ostream.

inline void cse491::WorldBase::Serialize (
    std::ostream & os
) 

Parameters:

  • os ostream

function SerializeAgentSet

Serialize agent data into an ostream.

inline void cse491::WorldBase::SerializeAgentSet (
    std::ostream & os
) 

Parameters:

  • os ostream

function SerializeItemSet

Serialize item data into an ostream.

inline void cse491::WorldBase::SerializeItemSet (
    std::ostream & os
) 

Parameters:

  • os ostream

function SetWorldRunning

inline virtual void cse491::WorldBase::SetWorldRunning (
    bool running
) 

function UpdateWorld

inline virtual void cse491::WorldBase::UpdateWorld () 

function WorldBase

inline cse491::WorldBase::WorldBase (
    unsigned int seed=0
) 

Initializes world with cell types and random generator

Parameters:

  • seed Seed used for RNG. Use 0 for a non-deterministic result.

function ~WorldBase

virtual cse491::WorldBase::~WorldBase () = default

Protected Attributes Documentation

variable action

std::string action;

variable agent_map

agent_map_t agent_map;

variable agent_receiver

std::shared_ptr<DataCollection::AgentReceiver> agent_receiver;

variable grids

std::unordered_map<size_t, WorldGrid> grids;

variable item_map

item_map_t item_map;

variable last_entity_id

size_t last_entity_id;

variable main_grid

WorldGrid& main_grid;

variable norm_dist

std::normal_distribution norm_dist;

variable random_gen

std::mt19937 random_gen;

variable run_over

bool run_over;

variable seed

unsigned int seed;

variable type_options

type_options_t type_options;

variable uni_dist

std::uniform_real_distribution uni_dist;

variable world_running

bool world_running;

Protected Functions Documentation

function AddCellType

Add a new type of cell to this world.

inline size_t cse491::WorldBase::AddCellType (
    const std::string & name,
    const std::string & desc="",
    char symbol='\0'
) 

Parameters:

  • name A unique name for this cell type
  • desc A longer description of the cell type
  • symbol An (optional) unique symbol for text IO (files, command line)

Returns:

A unique ID associated with this cell type (position in type_options vector)

function ConfigAgent [2/2]

inline virtual void cse491::WorldBase::ConfigAgent (
    AgentBase &
) 

Helper function that is run whenever a new agent is created.

Note:

Override this function to provide agents with actions or other setup.

function NextEntityID

inline size_t cse491::WorldBase::NextEntityID () 

The documentation for this class was generated from the following file source/core/WorldBase.hpp