uatlib
agent.hpp
Go to the documentation of this file.
1 
4 #ifndef UAT_AGENT_HPP
5 #define UAT_AGENT_HPP
6 
7 #include <uat/permit.hpp>
8 
9 #include <span>
10 #include <variant>
11 
12 #include <type_safe/reference.hpp>
13 
14 namespace uat
15 {
16 
19 {
22 };
23 
24 namespace permit_public_status
25 {
26 
29 {};
30 
32 struct available
33 {
35 
38  std::span<const trade_value_t> trades;
39 };
40 
42 struct owned
43 {};
44 
45 } // namespace permit_public_status
46 
49  std::variant<permit_public_status::unavailable, permit_public_status::available, permit_public_status::owned>;
50 
52 using bid_fn = type_safe::function_ref<bool(region_view, uint_t, value_t)>;
53 
55 using ask_fn = type_safe::function_ref<bool(region_view, uint_t, value_t)>;
56 
59 
67 template <region_compatible R> struct agent
68 {
69  using region_type = R;
70 
86  virtual auto bid_phase([[maybe_unused]] uint_t time, [[maybe_unused]] bid_fn bid,
87  [[maybe_unused]] permit_public_status_fn status, [[maybe_unused]] int seed) -> void
88  {}
89 
105  virtual auto ask_phase([[maybe_unused]] uint_t time, [[maybe_unused]] ask_fn ask,
106  [[maybe_unused]] permit_public_status_fn status, [[maybe_unused]] int seed) -> void
107  {}
108 
117  virtual auto on_bought([[maybe_unused]] const R& region, [[maybe_unused]] uint_t time, [[maybe_unused]] value_t value) -> void
118  {}
119 
128  virtual auto on_sold([[maybe_unused]] const R& region, [[maybe_unused]] uint_t time, [[maybe_unused]] value_t value) -> void {}
129 
138  virtual auto stop(uint_t time, int seed) -> bool = 0;
139 };
140 
142 template <typename T>
143 concept agent_compatible = std::movable<T> && std::derived_from<T, agent<typename T::region_type>>;
144 
149 {
151  class agent_interface
152  {
153  public:
154  virtual ~agent_interface() = default;
155 
156  virtual auto bid_phase(uint_t, bid_fn, permit_public_status_fn, int) -> void = 0;
157  virtual auto ask_phase(uint_t, ask_fn, permit_public_status_fn, int) -> void = 0;
158 
159  virtual auto on_bought(region_view, uint_t, value_t) -> void = 0;
160  virtual auto on_sold(region_view, uint_t, value_t) -> void = 0;
161 
162  virtual auto stop(uint_t, int) -> bool = 0;
163  };
164 
166  template <agent_compatible Agent> class agent_model : public agent_interface
167  {
168  public:
169  agent_model(Agent any_agent) : agent_(std::move(any_agent)) {}
170  virtual ~agent_model() = default;
171 
172  auto bid_phase(uint_t t, bid_fn b, permit_public_status_fn i, int seed) -> void override
173  {
174  agent_.bid_phase(t, std::move(b), std::move(i), seed);
175  }
176 
177  auto ask_phase(uint_t t, ask_fn a, permit_public_status_fn i, int seed) -> void override
178  {
179  agent_.ask_phase(t, std::move(a), std::move(i), seed);
180  }
181 
182  auto on_bought(region_view s, uint_t t, value_t v) -> void override
183  {
184  agent_.on_bought(s.downcast<typename Agent::region_type>(), t, v);
185  }
186 
187  auto on_sold(region_view s, uint_t t, value_t v) -> void override
188  {
189  agent_.on_sold(s.downcast<typename Agent::region_type>(), t, v);
190  }
191 
192  auto stop(uint_t t, int seed) -> bool override { return agent_.stop(t, seed); }
193 
194  private:
195  Agent agent_;
196  };
197 
198 public:
201  template <agent_compatible Agent> any_agent(Agent a) : interface_(new agent_model<Agent>(std::move(a))) {}
202 
203  any_agent() = delete;
204 
205  any_agent(const any_agent&) = delete;
206  any_agent(any_agent&&) noexcept = default;
207 
208  auto operator=(const any_agent&) -> any_agent& = delete;
209  auto operator=(any_agent&&) noexcept -> any_agent& = default;
210 
211  auto bid_phase(uint_t, bid_fn, permit_public_status_fn, int) -> void;
212  auto ask_phase(uint_t, ask_fn, permit_public_status_fn, int) -> void;
213 
214  auto on_bought(region_view, uint_t, value_t) -> void;
215  auto on_sold(region_view, uint_t, value_t) -> void;
216 
217  auto stop(uint_t time, int seed) -> bool;
218 
219 private:
220  std::unique_ptr<agent_interface> interface_;
221 };
222 
223 } // namespace uat
224 
225 #endif // UAT_AGENT_HPP
type_safe::function_ref< bool(region_view, uint_t, value_t)> bid_fn
Function reference that allows the agent to bid for a permit.
Definition: agent.hpp:52
type_safe::function_ref< bool(region_view, uint_t, value_t)> ask_fn
Function reference that allows the agent to ask for a permit.
Definition: agent.hpp:55
type_safe::function_ref< permit_public_status_t(region_view, uint_t)> permit_public_status_fn
Function reference that returns the public status of a permit.
Definition: agent.hpp:58
std::variant< permit_public_status::unavailable, permit_public_status::available, permit_public_status::owned > permit_public_status_t
Variant that represents the possible public status of a permit.
Definition: agent.hpp:49
concept agent_compatible
Concept that defines the requirements for an agent.
Definition: agent.hpp:143
A type-erased class that represents an agent in the simulation.
Definition: agent.hpp:149
any_agent(Agent a)
Definition: agent.hpp:201
A non-owning wrapper to an atomic region in the airspace.
Definition: permit.hpp:21
auto downcast() const -> const R &
Definition: permit.hpp:38
Defines region utilities and the permit class.
Class to define the default behavior of an agent.
Definition: agent.hpp:68
virtual auto stop(uint_t time, int seed) -> bool=0
virtual auto on_bought([[maybe_unused]] const R &region, [[maybe_unused]] uint_t time, [[maybe_unused]] value_t value) -> void
Definition: agent.hpp:117
virtual auto bid_phase([[maybe_unused]] uint_t time, [[maybe_unused]] bid_fn bid, [[maybe_unused]] permit_public_status_fn status, [[maybe_unused]] int seed) -> void
Definition: agent.hpp:86
virtual auto ask_phase([[maybe_unused]] uint_t time, [[maybe_unused]] ask_fn ask, [[maybe_unused]] permit_public_status_fn status, [[maybe_unused]] int seed) -> void
Definition: agent.hpp:105
virtual auto on_sold([[maybe_unused]] const R &region, [[maybe_unused]] uint_t time, [[maybe_unused]] value_t value) -> void
Definition: agent.hpp:128
Represents the public status of a permit that is available for trading.
Definition: agent.hpp:33
value_t min_value
The minimum value that can be offered for the permit.
Definition: agent.hpp:34
std::span< const trade_value_t > trades
Definition: agent.hpp:38
Represents the public status of a permit that is owned by the agent.
Definition: agent.hpp:43
Represents the public status of a permit that is not available for trading.
Definition: agent.hpp:29
Represents the public values in a trade of a permit.
Definition: agent.hpp:19
value_t highest_bid
The highest bid for the permit.
Definition: agent.hpp:21
value_t min_value
The minimum value the owner asked for the permit.
Definition: agent.hpp:20
std::size_t uint_t
Default unsigned integer type.
Definition: type.hpp:29
double value_t
Default type for price.
Definition: type.hpp:35