20 #ifndef EMP_EVO_STATE_GRID_H 21 #define EMP_EVO_STATE_GRID_H 26 #include "../base/assert.h" 27 #include "../base/Ptr.h" 28 #include "../base/vector.h" 30 #include "../tools/BitVector.h" 31 #include "../tools/File.h" 32 #include "../tools/map_utils.h" 33 #include "../tools/math.h" 34 #include "../tools/Random.h" 51 const std::string & _name,
const std::string & _desc)
52 : state_id(_id), symbol(_sym), score_change(_change), name(_name), desc(_desc) { ; }
69 size_t GetKey(
const std::string &
name)
const {
return Find(name_map, name, 0); }
90 return states[
GetKey(symbol) ].state_id;
95 size_t key_id = states.
size();
97 state_map[id] = key_id;
98 symbol_map[
symbol] = key_id;
99 name_map[
name] = key_id;
113 StateGrid() : width(0), height(0), states(0), info() { ; }
115 : width(_w), height(_h), states(_w*_h,init_val), info(_i) { ; }
117 : width(1), height(1), states(), info(_i) { Load(filename); }
134 return states[y*width+x];
139 return states[y*width+x];
144 return states[y*width+x];
146 int GetState(
size_t id)
const {
return states[id]; }
150 states[y*width+x] = in;
163 const std::string &
GetName(
size_t x,
size_t y)
const {
172 for (
size_t i = 0; i < states.
size(); i++) sites[i] = (states[i] == target_state);
177 template <
typename... Ts>
181 template <
typename... Ts>
186 File file(std::forward<Ts>(args)...);
192 width = file[0].
size();
196 size_t size = width * height;
200 for (
size_t row = 0; row < height; row++) {
202 for (
size_t col = 0; col < width; col++) {
203 states[row*width+col] = info.
GetState(file[row][col]);
211 template <
typename... Ts>
213 std::string out(width*2-1,
' ');
214 for (
size_t i = 0; i < height; i++) {
215 out[0] = info.
GetSymbol( states[i*width] );
216 for (
size_t j = 1; j < width; j++) {
217 out[j*2] = info.
GetSymbol( states[i*width+j] );
225 template <
typename... Ts>
229 for (
size_t i = 0; i < height; i++) {
231 out += info.
GetSymbol( states[i*width] );
232 for (
size_t j = 1; j < width; j++) {
234 out +=info.
GetSymbol( states[i*width+j] );
238 file.
Write(std::forward<Ts>(args)...);
251 State(
size_t _x=0,
size_t _y=0,
size_t _f=1) : x(_x), y(_y), facing((int)_f) { ; }
252 bool IsAt(
size_t _x,
size_t _y)
const {
return x == _x && y == _y; }
267 cur_state.
x = (size_t)
Mod(steps + (
int) cur_state.
x, (int) grid.
GetWidth());
273 cur_state.
y = (size_t)
Mod(steps + (
int) cur_state.
y, (int) grid.
GetHeight());
284 size_t GetX()
const {
return cur_state.
x; }
285 size_t GetY()
const {
return cur_state.
y; }
288 return (
size_t) cur_state.
facing;
291 bool IsAt(
size_t x,
size_t y)
const {
return cur_state.
IsAt(x,y); }
292 bool WasAt(
size_t x,
size_t y)
const {
293 for (
const State & state : history)
if (state.IsAt(x,y))
return true;
300 for (
const State & state : history) {
301 size_t pos = state.x + grid.
GetWidth() * state.y;
308 bool prev = track_moves;
310 if (!prev && track_moves) history.
push_back(cur_state);
318 cur_state.
facing = (int) _f;
337 switch (cur_state.
facing) {
338 case 0: MoveX(grid, -steps); MoveY(grid, -steps);
break;
339 case 1: MoveY(grid, -steps);
break;
340 case 2: MoveX(grid, +steps); MoveY(grid, -steps);
break;
341 case 3: MoveX(grid, +steps);
break;
342 case 4: MoveX(grid, +steps); MoveY(grid, +steps);
break;
343 case 5: MoveY(grid, +steps);
break;
344 case 6: MoveX(grid, -steps); MoveY(grid, +steps);
break;
345 case 7: MoveX(grid, -steps);
break;
366 return grid(cur_state.
x, cur_state.
y);
372 grid.
SetState(cur_state.
x, cur_state.
y, new_state);
377 emp_assert(history.
size(),
"You can only print history of a StateGrid if you track it!");
378 const size_t width = grid.
GetWidth();
380 std::string out(width*2-1,
' ');
381 for (
size_t i = 0; i < height; i++) {
382 for (
size_t j = 1; j < width; j++) {
384 if (WasAt(j,i)) out[j*2] =
'*';
StateGridInfo()
Definition: StateGrid.h:71
void Randomize(const StateGrid &grid, Random &random)
Move the current status to a random position and orientation.
Definition: StateGrid.h:360
void Move(const StateGrid &grid, int steps=1)
Move in the direction currently faced.
Definition: StateGrid.h:333
size_t x
X-coordinate of this agent.
Definition: StateGrid.h:247
int Scan(const StateGrid &grid)
Examine state of current position.
Definition: StateGrid.h:365
void PrintHistory(StateGrid &grid, std::ostream &os=std::cout) const
Print the history of an organim moving around a state grid.
Definition: StateGrid.h:376
size_t height
Height of the overall grid.
Definition: StateGrid.h:108
emp::vector< int > states
Specific states at each position in the grid.
Definition: StateGrid.h:109
Information about a particular agent on a state grid.
Definition: StateGrid.h:244
emp::BitVector GetVisited(const StateGrid &grid) const
Get a BitVector indicating the full history of which positions this organism has traversed.
Definition: StateGrid.h:298
StateGridStatus & Set(size_t _x, size_t _y, size_t _f)
Definition: StateGrid.h:315
Information about what a particular state type means in a state grid.
Definition: StateGrid.h:43
size_t GetKey(const std::string &name) const
Definition: StateGrid.h:69
int GetState(const std::string &name) const
Definition: StateGrid.h:92
emp::vector< State > history
All previous positions and facings in this path.
Definition: StateGrid.h:257
char GetSymbol(size_t x, size_t y) const
Definition: StateGrid.h:153
StateGridInfo info
Information about the set of states used in this grid.
Definition: StateGrid.h:110
int GetState(size_t x, size_t y) const
Definition: StateGrid.h:141
const std::string & GetName(size_t x, size_t y) const
Definition: StateGrid.h:163
void UpdateHistory()
If we are tracking moves, store the current position in the history.
Definition: StateGrid.h:262
A class to maintin files for loading, writing, storing, and easy access to components.
Definition: File.h:32
A versatile and non-patterned pseudo-random-number generator (Mersenne Twister).
Definition: ce_random.h:52
const StateGrid & Print(std::ostream &os=std::cout) const
Print the current status of the StateGrid to an output stream.
Definition: StateGrid.h:212
double GetScoreChange(int state_id) const
Definition: StateGrid.h:83
A drop-in replacement for std::vector<bool>, but with extra bitwise logic features.
Definition: BitVector.h:39
StateGrid()
Definition: StateGrid.h:113
void push_back(PB_Ts &&...args)
Definition: vector.h:189
void SetState(StateGrid &grid, int new_state)
Set the current position in the state grid.
Definition: StateGrid.h:371
~StateGrid()
Definition: StateGrid.h:120
A StateGrid describes a map of grid positions to the current state of each position.
Definition: StateGrid.h:105
char GetSymbol(int state_id) const
Definition: StateGrid.h:82
StateGrid & SetState(size_t x, size_t y, int in)
Definition: StateGrid.h:147
size_t width
Width of the overall grid.
Definition: StateGrid.h:107
double GetScoreChange(size_t x, size_t y) const
Definition: StateGrid.h:158
StateInfo & operator=(const StateInfo &)=default
File & Append(const std::string &line)
Append a new line to the end of the file.
Definition: File.h:84
bool IsAt(size_t _x, size_t _y) const
Definition: StateGrid.h:252
size_t size() const
Definition: vector.h:151
void emplace_back(ARGS &&...args)
Definition: vector.h:219
Definition: StateGrid.h:246
int GetState(size_t id) const
Definition: StateGrid.h:146
constexpr uint32_t GetUInt(const uint32_t max)
Definition: ce_random.h:191
size_t GetKey(char symbol) const
Definition: StateGrid.h:68
emp::vector< StateInfo > states
All available states. Position is key ID.
Definition: StateGrid.h:61
size_t GetKey(int state_id) const
Definition: StateGrid.h:67
std::map< int, size_t > state_map
Map of state_id to key ID (state_id can be < 0)
Definition: StateGrid.h:63
bool track_moves
Should we record every move made by this organism?
Definition: StateGrid.h:256
File & Write(std::ostream &output)
Write this file to a provided output stream.
Definition: File.h:153
void AddState(int id, char symbol, double mult=1.0, std::string name="", std::string desc="")
Definition: StateGrid.h:94
const StateGridInfo & GetInfo() const
Definition: StateGrid.h:129
void MoveX(const StateGrid &grid, int steps=1)
Move explicitly in the x direction (regardless of facing).
Definition: StateGrid.h:265
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
StateGrid(StateGridInfo &_i, const std::string &filename)
Definition: StateGrid.h:116
void AddState(Ts &&...args)
Setup the StateGridInfo with possible states.
Definition: StateGrid.h:178
size_t GetNumLines() const
How many lines are in this file?
Definition: File.h:60
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
constexpr int Mod(int in_val, int mod_val)
% is actually remainder; Mod is a proper modulus command that handles negative #'s correctly ...
Definition: math.h:26
double score_change
Change ammount for organism score by stepping on this square.
Definition: StateGrid.h:46
std::string name
Name of this state.
Definition: StateGrid.h:47
int GetState(char symbol) const
Definition: StateGrid.h:88
StateGrid & Load(Ts &&...args)
Load in the contents of a StateGrid using the file information provided.
Definition: StateGrid.h:182
size_t GetSize() const
Definition: StateGrid.h:127
int & operator()(size_t x, size_t y)
Definition: StateGrid.h:131
const std::string & GetName(int state_id) const
Definition: StateGrid.h:84
int facing
0=UL, 1=Up, 2=UR, 3=Right, 4=DR, 5=Down, 6=DL, 7=Left (+=Clockwise)
Definition: StateGrid.h:249
void resize(size_t new_size)
Definition: vector.h:161
Full information about the states available in a state grid and meanings of each state.
Definition: StateGrid.h:39
StateGridStatus & SetPos(size_t _x, size_t _y)
Definition: StateGrid.h:324
StateGrid(StateGridInfo &_i, size_t _w=1, size_t _h=1, int init_val=0)
Definition: StateGrid.h:114
std::map< char, size_t > symbol_map
Map of symbols to associated key ID.
Definition: StateGrid.h:64
void Rotate(int turns=1)
Rotate starting from current facing.
Definition: StateGrid.h:354
char symbol
Symbol for printing this state.
Definition: StateGrid.h:45
size_t size() const
Compatibility with size()
Definition: File.h:63
size_t GetX() const
Definition: StateGrid.h:284
std::map< std::string, size_t > name_map
Map of names to associated key ID.
Definition: StateGrid.h:65
If we are in emscripten, make sure to include the header.
Definition: array.h:37
size_t GetY() const
Definition: StateGrid.h:285
State(size_t _x=0, size_t _y=0, size_t _f=1)
Definition: StateGrid.h:251
State cur_state
Position and facing currently used.
Definition: StateGrid.h:255
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
int operator()(size_t x, size_t y) const
Definition: StateGrid.h:136
StateGridStatus & TrackMoves(bool track=true)
Definition: StateGrid.h:307
size_t GetNumStates() const
Definition: StateGrid.h:79
#define emp_assert(...)
Definition: assert.h:199
bool WasAt(size_t x, size_t y) const
Definition: StateGrid.h:292
~StateInfo()
Definition: StateGrid.h:55
StateGridStatus & SetY(size_t _y)
Definition: StateGrid.h:323
size_t GetHeight() const
Definition: StateGrid.h:126
size_t GetFacing() const
Definition: StateGrid.h:286
const StateGrid & Write(Ts &&...args) const
Store the current status of the StateGrid to a file.
Definition: StateGrid.h:226
size_t y
Y-coordinate of this agent.
Definition: StateGrid.h:248
const std::string & GetDesc(int state_id) const
Definition: StateGrid.h:85
StateGridStatus()
Definition: StateGrid.h:277
StateInfo(int _id, char _sym, double _change, const std::string &_name, const std::string &_desc)
Definition: StateGrid.h:50
~StateGridInfo()
Definition: StateGrid.h:74
const emp::vector< int > GetStates() const
Definition: StateGrid.h:128
File & RemoveWhitespace(bool keep_newlines=true)
Delete all whitespace; by default keep newlines.
Definition: File.h:209
void MoveY(const StateGrid &grid, int steps=1)
Move explicitly in the y direction (regardless of facing).
Definition: StateGrid.h:271
StateGridStatus & SetX(size_t _x)
Definition: StateGrid.h:322
StateGridStatus & SetFacing(size_t _f)
Definition: StateGrid.h:330
std::string desc
Explanation of this state.
Definition: StateGrid.h:48
size_t GetWidth() const
Definition: StateGrid.h:125
int state_id
Ordinal id for this state.
Definition: StateGrid.h:44
emp::BitVector IsState(int target_state)
Return a BitVector indicating which positions in the state grid have a particular state...
Definition: StateGrid.h:170
bool IsAt(size_t x, size_t y) const
Definition: StateGrid.h:291