Utopia  2
Framework for studying models of complex & adaptive systems.
agent.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_CORE_AGENT_HH
2 #define UTOPIA_CORE_AGENT_HH
3 
4 #include "state.hh"
5 #include "tags.hh"
6 #include "space.hh"
7 #include "types.hh"
8 #include "entity.hh"
9 
10 
11 namespace Utopia {
18 template<typename StateType,
19  Update update_mode,
20  bool use_def_state_constr=false,
21  typename AgentTags=EmptyTag,
22  template<class> class CustomLinkContainers=NoCustomLinks>
23 using AgentTraits = EntityTraits<StateType,
24  update_mode,
25  use_def_state_constr,
26  AgentTags,
27  CustomLinkContainers>;
28 
29 
31 
46 template<typename Traits, typename Space, typename enabled=void>
47 class Agent;
48 
49 
51 template<typename Traits, typename Space>
52 class Agent<Traits,
53  Space,
54  std::enable_if_t<Traits::mode != Update::sync>>
55 :
56  public Entity<Agent<Traits, Space>, Traits>
57 {
58 public:
61 
63  using State = typename Traits::State;
64 
66  using Position = typename Space::SpaceVec;
67 
69  template<class T, class M>
70  friend class AgentManager;
71 
72 private:
75 
76 public:
78 
82  Agent (const IndexType id,
83  const State initial_state,
84  const Position& initial_pos)
85  :
86  Entity<Self, Traits>(id, initial_state),
87  _pos(initial_pos)
88  {}
89 
91  const Position& position() const {
92  return _pos;
93  };
94 
95 
96 protected:
98 
107  void set_pos(const Position& pos) {
108  _pos = pos;
109  };
110 };
111 
112 
113 
115 template<typename Traits, typename Space>
116 class Agent<Traits,
117  Space,
118  std::enable_if_t<Traits::mode == Update::sync>>
119 :
120  public Entity<Agent<Traits, Space>, Traits>
121 {
122 public:
125 
127  using State = typename Traits::State;
128 
130  using Position = typename Space::SpaceVec;
131 
133  template<class T, class M>
134  friend class AgentManager;
135 
136 private:
139 
142 
143 public:
145 
149  Agent(const IndexType id,
150  const State& initial_state,
151  const Position& initial_pos)
152  :
153  Entity<Self, Traits>(id, initial_state),
154  _pos(initial_pos),
155  _pos_new(initial_pos)
156  {}
157 
159  const Position& position() const {
160  return _pos;
161  }
162 
164  const Position& position_new() const {
165  return _pos_new;
166  }
167 
169 
172  void update () {
173  // Update the state as defined in the Entity class
175 
176  // Update the position
177  _pos = _pos_new;
178  }
179 
180 protected:
182 
191  void set_pos(const Position& pos) {
192  _pos_new = pos;
193  }
194 };
195 
196 // end group AgentManager
201 } // namespace Utopia
202 
203 #endif // UTOPIA_CORE_AGENT_HH
void set_pos(const Position &pos)
Set the position of the agent.
Definition: agent.hh:107
Position _pos
The position of the agent.
Definition: agent.hh:74
const Position & position() const
Return a const reference of the current position of the agent.
Definition: agent.hh:91
typename Traits::State State
The type of the state.
Definition: agent.hh:63
Agent(const IndexType id, const State initial_state, const Position &initial_pos)
Construct an agent.
Definition: agent.hh:82
typename Space::SpaceVec Position
The type of the position vector.
Definition: agent.hh:66
const Position & position() const
Return the current position of the agent.
Definition: agent.hh:159
void set_pos(const Position &pos)
Set the position buffer of the (synchronously updated) agent.
Definition: agent.hh:191
const Position & position_new() const
Return the position buffer of the agent.
Definition: agent.hh:164
typename Traits::State State
The type of the state.
Definition: agent.hh:127
Position _pos
The current position of the agent.
Definition: agent.hh:138
Position _pos_new
The position buffer for the synchronous position update.
Definition: agent.hh:141
Agent(const IndexType id, const State &initial_state, const Position &initial_pos)
Construct an agent.
Definition: agent.hh:149
typename Space::SpaceVec Position
The type of the position vector.
Definition: agent.hh:130
void update()
Update the position and the state.
Definition: agent.hh:172
An agent is a slightly specialized state container.
Definition: agent.hh:47
The agent manager manages the agents living in a model.
Definition: agent_manager.hh:31
Definition: tags.hh:6
An entity is a slightly specialized state container.
Definition: entity.hh:83
Traits Traits
The traits of this entity.
Definition: entity.hh:90
Update
Update modes when applying rules.
Definition: state.hh:20
Definition: agent.hh:11
std::size_t IndexType
Type for indices, i.e. values used for container indexing, agent IDs, ...
Definition: types.hh:40
Definition: parallel.hh:235
The entity traits struct gathers types to be used for specializing an entity.
Definition: entity.hh:49
The Space bundles properties about the physical space a model resides in.
Definition: space.hh:27
SpaceVecType< dim > SpaceVec
The type for vectors relating to physical space.
Definition: space.hh:33