StateGrid.hpp

StateGrid maintains a rectilinear grid that agents can traverse.

State grids are a matrix of values, representing states of a 2D environment that an organism can traverse.

Todo:

Functions such as Load() should throw exceptions (or equilv.), not use asserts.

Need to figure out a default mapping for how outputs translate to moves around a state grid. -1 = Back up ; 0 = Turn left ; 1 = Move fast-forwards ; 2 = Turn right

Allow StateGridInfo to be built inside of StateGrid (change reference to pointer and possible ownership)

class StateGridInfo
#include <StateGrid.hpp>

Full information about the states available in a state grid and meanings of each state.

Public Functions

inline StateGridInfo()
StateGridInfo(const StateGridInfo&) = default
StateGridInfo(StateGridInfo&&) = default
inline ~StateGridInfo()
StateGridInfo &operator=(const StateGridInfo&) = default
StateGridInfo &operator=(StateGridInfo&&) = default
inline size_t GetNumStates() const
inline char GetSymbol(int state_id) const
inline double GetScoreChange(int state_id) const
inline const std::string &GetName(int state_id) const
inline const std::string &GetDesc(int state_id) const
inline int GetState(char symbol) const
inline int GetState(const std::string &name) const
inline void AddState(int id, char symbol, double mult = 1.0, std::string name = "", std::string desc = "")

Protected Functions

inline size_t GetKey(int state_id) const
inline size_t GetKey(char symbol) const
inline size_t GetKey(const std::string &name) const

Protected Attributes

vector<StateInfo> states

All available states. Position is key ID.

std::map<int, size_t> state_map

Map of state_id to key ID (state_id can be < 0)

std::map<char, size_t> symbol_map

Map of symbols to associated key ID.

std::map<std::string, size_t> name_map

Map of names to associated key ID.

struct StateInfo
#include <StateGrid.hpp>

Information about what a particular state type means in a state grid.

Public Functions

inline StateInfo(int _id, char _sym, double _change, const std::string &_name, const std::string &_desc)
StateInfo(const StateInfo&) = default
StateInfo(StateInfo&&) = default
inline ~StateInfo()
StateInfo &operator=(const StateInfo&) = default
StateInfo &operator=(StateInfo&&) = default

Public Members

int state_id

Ordinal id for this state.

char symbol

Symbol for printing this state.

double score_change

Change ammount for organism score by stepping on this square.

std::string name

Name of this state.

std::string desc

Explanation of this state.

class StateGrid
#include <StateGrid.hpp>

A StateGrid describes a map of grid positions to the current state of each position.

Public Functions

inline StateGrid()
inline StateGrid(StateGridInfo &_i, size_t _w = 1, size_t _h = 1, int init_val = 0)
inline StateGrid(StateGridInfo &_i, const std::string &filename)
StateGrid(const StateGrid&) = default
StateGrid(StateGrid &&in) = default
inline ~StateGrid()
StateGrid &operator=(const StateGrid&) = default
StateGrid &operator=(StateGrid&&) = default
inline size_t GetWidth() const
inline size_t GetHeight() const
inline size_t GetSize() const
inline const vector<int> GetStates() const
inline const StateGridInfo &GetInfo() const
inline int &operator()(size_t x, size_t y)
inline int operator()(size_t x, size_t y) const
inline int GetState(size_t x, size_t y) const
inline int GetState(size_t id) const
inline StateGrid &SetState(size_t x, size_t y, int in)
inline char GetSymbol(size_t x, size_t y) const
inline double GetScoreChange(size_t x, size_t y) const
inline const std::string &GetName(size_t x, size_t y) const
inline BitVector IsState(int target_state)

Return a BitVector indicating which positions in the state grid have a particular state.

template<typename ...Ts>
inline void AddState(Ts&&... args)

Setup the StateGridInfo with possible states.

template<typename ...Ts>
inline StateGrid &Load(Ts&&... args)

Load in the contents of a StateGrid using the file information provided.

template<typename ...Ts>
inline const StateGrid &Print(std::ostream &os = std::cout) const

Print the current status of the StateGrid to an output stream.

template<typename ...Ts>
inline const StateGrid &Write(Ts&&... args) const

Store the current status of the StateGrid to a file.

Protected Attributes

size_t width

Width of the overall grid.

size_t height

Height of the overall grid.

vector<int> states

Specific states at each position in the grid.

StateGridInfo info

Information about the set of states used in this grid.

class StateGridStatus
#include <StateGrid.hpp>

Information about a particular agent on a state grid.

Public Functions

inline StateGridStatus()
StateGridStatus(const StateGridStatus&) = default
StateGridStatus(StateGridStatus&&) = default
StateGridStatus &operator=(const StateGridStatus&) = default
StateGridStatus &operator=(StateGridStatus&&) = default
inline size_t GetX() const
inline size_t GetY() const
inline size_t GetFacing() const
inline bool IsAt(size_t x, size_t y) const
inline bool WasAt(size_t x, size_t y) const
inline BitVector GetVisited(const StateGrid &grid) const

Get a BitVector indicating the full history of which positions this organism has traversed.

inline StateGridStatus &TrackMoves(bool track = true)
inline StateGridStatus &Set(size_t _x, size_t _y, size_t _f)
inline StateGridStatus &SetX(size_t _x)
inline StateGridStatus &SetY(size_t _y)
inline StateGridStatus &SetPos(size_t _x, size_t _y)
inline StateGridStatus &SetFacing(size_t _f)
inline void Move(const StateGrid &grid, int steps = 1)

Move in the direction currently faced.

inline void Rotate(int turns = 1)

Rotate starting from current facing.

inline void Randomize(const StateGrid &grid, Random &random)

Move the current status to a random position and orientation.

inline int Scan(const StateGrid &grid)

Examine state of current position.

inline void SetState(StateGrid &grid, int new_state)

Set the current position in the state grid.

inline void PrintHistory(StateGrid &grid, std::ostream &os = std::cout) const

Print the history of an organim moving around a state grid.

Protected Functions

inline void UpdateHistory()

If we are tracking moves, store the current position in the history.

inline void MoveX(const StateGrid &grid, int steps = 1)

Move explicitly in the x direction (regardless of facing).

inline void MoveY(const StateGrid &grid, int steps = 1)

Move explicitly in the y direction (regardless of facing).

Protected Attributes

State cur_state

Position and facing currently used.

bool track_moves

Should we record every move made by this organism?

vector<State> history

All previous positions and facings in this path.

struct State
#include <StateGrid.hpp>

Public Functions

inline State(size_t _x = 0, size_t _y = 0, size_t _f = 1)
inline bool IsAt(size_t _x, size_t _y) const

Public Members

size_t x

X-coordinate of this agent.

size_t y

Y-coordinate of this agent.

int facing

0=UL, 1=Up, 2=UR, 3=Right, 4=DR, 5=Down, 6=DL, 7=Left (+=Clockwise)