Skip to content

Commit 9186356

Browse files
authored
I like to std::move it std::move it (#278)
* Implement < operator for Agents * Simplified setStreetId * cose sensate * Cose meno sensate * Remove copy constructors from Edges * Working streets * Working streets * Working * Seems to compile * Working on... * Fix one roundabout test * Remove useless headers * Small refactor * Enqueued agents must have null speed * Fix node evoution * Last intersection test * Agent loop optimization * More tests! * Fix mean speed getter * More tests... * Some traffic light's tests * Finish tl tests * One more test * Finish dynamic tests * Revert Adj tests * Update action * Try to fix macos test * Update version * Fix macos tests * Fix examples * Refactor limits * Add ghost agents to `saveMacroscopicObservables` function * Bugfix and add test * Fix test * Add RoadJunction and floating point transport capacity * Working
1 parent 6b1cbc8 commit 9186356

35 files changed

+881
-1014
lines changed

examples/slow_charge_rb.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int main(int argc, char** argv) {
7171
BASE_OUT_FOLDER,
7272
ERROR_PROBABILITY,
7373
std::to_string(SEED))}; // output folder
74-
const auto MAX_TIME{static_cast<unsigned int>(1e6)}; // maximum time of simulation
74+
const auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation
7575

7676
// Clear output folder or create it if it doesn't exist
7777
if (!fs::exists(BASE_OUT_FOLDER)) {
@@ -205,14 +205,15 @@ int main(int argc, char** argv) {
205205
dynamics.evolve(false);
206206

207207
if (dynamics.time() % 2400 == 0) {
208-
deltaAgents = dynamics.agents().size() - previousAgents;
208+
auto const totalDynamicsAgents{dynamics.nAgents()};
209+
deltaAgents = totalDynamicsAgents - previousAgents;
209210
if (deltaAgents < 0) {
210211
++nAgents;
211212
std::cout << "- Now I'm adding " << nAgents << " agents.\n";
212213
std::cout << "Delta agents: " << deltaAgents << '\n';
213214
std::cout << "At time: " << dynamics.time() << '\n';
214215
}
215-
previousAgents = dynamics.agents().size();
216+
previousAgents = totalDynamicsAgents;
216217
}
217218

218219
if (dynamics.time() % 300 == 0) {

examples/slow_charge_tl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main(int argc, char** argv) {
6767
BASE_OUT_FOLDER,
6868
ERROR_PROBABILITY,
6969
std::to_string(SEED))}; // output folder
70-
constexpr auto MAX_TIME{static_cast<unsigned int>(1e6)}; // maximum time of simulation
70+
constexpr auto MAX_TIME{static_cast<unsigned int>(5e5)}; // maximum time of simulation
7171

7272
std::cout << "-------------------------------------------------\n";
7373
std::cout << "Input parameters:\n";
@@ -309,14 +309,15 @@ int main(int argc, char** argv) {
309309
if (dynamics.time() % 2400 == 0 && nAgents > 0) {
310310
// auto meanDelta = std::accumulate(deltas.begin(), deltas.end(), 0) /
311311
// deltas.size();
312-
deltaAgents = dynamics.agents().size() - previousAgents;
312+
auto const totalDynamicsAgents{dynamics.nAgents()};
313+
deltaAgents = totalDynamicsAgents - previousAgents;
313314
if (deltaAgents < 0) {
314315
++nAgents;
315316
std::cout << "- Now I'm adding " << nAgents << " agents.\n";
316317
std::cout << "Delta agents: " << deltaAgents << '\n';
317318
std::cout << "At time: " << dynamics.time() << '\n';
318319
}
319-
previousAgents = dynamics.agents().size();
320+
previousAgents = totalDynamicsAgents;
320321
// deltas.clear();
321322
}
322323

src/dsm/dsm.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <format>
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
8-
static constexpr uint8_t DSM_VERSION_MINOR = 5;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 12;
8+
static constexpr uint8_t DSM_VERSION_MINOR = 6;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 0;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/Agent.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,20 @@ namespace dsm {
3838
public:
3939
/// @brief Construct a new Agent object
4040
/// @param spawnTime The agent's spawn time
41-
/// @param id The agent's id
4241
/// @param itineraryId Optional, The agent's destination node. If not provided, the agent is a random agent
4342
/// @param srcNodeId Optional, The id of the source node of the agent
4443
Agent(Time const& spawnTime,
45-
Id id,
4644
std::optional<Id> itineraryId = std::nullopt,
4745
std::optional<Id> srcNodeId = std::nullopt);
4846
/// @brief Construct a new Agent object
4947
/// @param spawnTime The agent's spawn time
50-
/// @param id The agent's id
5148
/// @param itineraryIds The agent's itinerary
5249
/// @param srcNodeId Optional, The id of the source node of the agent
5350
Agent(Time const& spawnTime,
54-
Id id,
5551
std::vector<Id> const& trip,
5652
std::optional<Id> srcNodeId = std::nullopt);
53+
54+
void setSrcNodeId(Id srcNodeId);
5755
/// @brief Set the street occupied by the agent
5856
/// @param streetId The id of the street currently occupied by the agent
5957
void setStreetId(std::optional<Id> streetId = std::nullopt);

src/dsm/headers/Dynamics.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include <functional>
2727

2828
#include "Network.hpp"
29-
#include "../utility/TypeTraits/is_agent.hpp"
30-
#include "../utility/TypeTraits/is_itinerary.hpp"
3129
#include "../utility/Logger.hpp"
3230
#include "../utility/Typedef.hpp"
3331

src/dsm/headers/Edge.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ namespace dsm {
3131
int capacity = 1,
3232
double transportCapacity = 1.,
3333
std::vector<std::pair<double, double>> geometry = {});
34+
Edge(Edge&&) = default;
35+
Edge(const Edge&) = delete;
36+
virtual ~Edge() = default;
3437

38+
void resetId(Id newId);
3539
void setCapacity(int capacity);
3640
void setTransportCapacity(double capacity);
3741
void setGeometry(std::vector<std::pair<double, double>> geometry);

src/dsm/headers/FirstOrderDynamics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace dsm {
2323
/// @brief Set the speed of an agent
2424
/// @param agentId The id of the agent
2525
/// @throw std::invalid_argument, If the agent is not found
26-
void setAgentSpeed(Size agentId) override;
26+
void setAgentSpeed(std::unique_ptr<Agent> const& pAgent) override;
2727
/// @brief Set the standard deviation of the speed fluctuation
2828
/// @param speedFluctuationSTD The standard deviation of the speed fluctuation
2929
/// @throw std::invalid_argument, If the standard deviation is negative

src/dsm/headers/Intersection.hpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@
88

99
#pragma once
1010

11-
#include "Node.hpp"
11+
#include "Agent.hpp"
12+
#include "RoadJunction.hpp"
1213

1314
#include <map>
15+
#include <memory>
1416
#include <set>
1517

1618
namespace dsm {
1719
/// @brief The Intersection class represents a node in the network.
1820
/// @tparam Id The type of the node's id. It must be an unsigned integral type.
19-
class Intersection : public Node {
21+
class Intersection : public RoadJunction {
2022
protected:
21-
std::multimap<int16_t, Id> m_agents;
23+
std::multimap<int16_t, std::unique_ptr<Agent>> m_agents;
2224
std::set<Id>
2325
m_streetPriorities; // A set containing the street ids that have priority - like main roads
2426
Size m_agentCounter;
2527

2628
public:
2729
/// @brief Construct a new Intersection object
2830
/// @param id The node's id
29-
explicit Intersection(Id id) : Node{id} {};
31+
explicit Intersection(Id id) : RoadJunction{id} {};
3032
/// @brief Construct a new Intersection object
3133
/// @param id The node's id
3234
/// @param coords A std::pair containing the node's coordinates
33-
Intersection(Id id, std::pair<double, double> coords) : Node{id, coords} {};
35+
Intersection(Id id, std::pair<double, double> coords) : RoadJunction{id, coords} {};
3436

35-
Intersection(Node const& node) : Node{node} {};
37+
Intersection(RoadJunction const& node) : RoadJunction{node} {};
38+
39+
Intersection(Intersection const&) = delete;
3640

3741
virtual ~Intersection() = default;
3842

@@ -47,17 +51,17 @@ namespace dsm {
4751
/// The agent with the smallest angle difference is the first one to be
4852
/// removed from the node.
4953
/// @throws std::runtime_error if the node is full
50-
void addAgent(double angle, Id agentId);
54+
void addAgent(double angle, std::unique_ptr<Agent> pAgent);
5155
/// @brief Put an agent in the node
5256
/// @param agentId The agent's id
5357
/// @details The agent's angle difference is used to order the agents in the node.
5458
/// The agent with the smallest angle difference is the first one to be
5559
/// removed from the node.
5660
/// @throws std::runtime_error if the node is full
57-
void addAgent(Id agentId);
58-
/// @brief Removes an agent from the node
59-
/// @param agentId The agent's id
60-
void removeAgent(Id agentId);
61+
void addAgent(std::unique_ptr<Agent> pAgent);
62+
// /// @brief Removes an agent from the node
63+
// /// @param agentId The agent's id
64+
// void removeAgent(Id agentId);
6165
/// @brief Set the node streets with priority
6266
/// @param streetPriorities A std::set containing the node's street priorities
6367
void setStreetPriorities(std::set<Id> streetPriorities) {
@@ -69,11 +73,11 @@ namespace dsm {
6973
/// @brief Returns the node's density
7074
/// @return double The node's density
7175
double density() const override {
72-
return static_cast<double>(m_agents.size()) / m_capacity;
76+
return static_cast<double>(m_agents.size()) / this->capacity();
7377
}
7478
/// @brief Returns true if the node is full
7579
/// @return bool True if the node is full
76-
bool isFull() const override { return m_agents.size() == this->m_capacity; }
80+
bool isFull() const override { return m_agents.size() == this->capacity(); }
7781

7882
/// @brief Get the node's street priorities
7983
/// @details This function returns a std::set containing the node's street priorities.
@@ -83,7 +87,7 @@ namespace dsm {
8387
virtual const std::set<Id>& streetPriorities() const { return m_streetPriorities; };
8488
/// @brief Get the node's agent ids
8589
/// @return std::set<Id> A std::set containing the node's agent ids
86-
const std::multimap<int16_t, Id>& agents() { return m_agents; };
90+
std::multimap<int16_t, std::unique_ptr<Agent>>& agents() { return m_agents; };
8791
/// @brief Returns the number of agents that have passed through the node
8892
/// @return Size The number of agents that have passed through the node
8993
/// @details This function returns the number of agents that have passed through the node

src/dsm/headers/Network.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ namespace dsm {
4848
requires(std::is_base_of_v<node_t, TNode> &&
4949
std::constructible_from<TNode, Id, TArgs...>)
5050
void addNode(Id nodeId, TArgs&&... args);
51+
52+
template <typename TEdge = edge_t>
53+
requires(std::is_base_of_v<edge_t, TEdge>)
54+
void addEdge(TEdge&& edge);
5155
/// @brief Add an edge to the network
5256
/// @tparam TEdge The type of the edge (default is edge_t)
5357
/// @tparam TArgs The types of the arguments
@@ -160,6 +164,15 @@ namespace dsm {
160164
m_nodes.emplace(std::make_pair(
161165
nodeId, std::make_unique<TNode>(nodeId, std::forward<TArgs>(args)...)));
162166
}
167+
template <typename node_t, typename edge_t>
168+
requires(std::is_base_of_v<Node, node_t> && std::is_base_of_v<Edge, edge_t>)
169+
template <typename TEdge>
170+
requires(std::is_base_of_v<edge_t, TEdge>)
171+
void Network<node_t, edge_t>::addEdge(TEdge&& edge) {
172+
assert(!m_edges.contains(edge.id()));
173+
m_edges.emplace(std::make_pair(edge.id(), std::make_unique<TEdge>(std::move(edge))));
174+
m_addMissingNodes(edge.id());
175+
}
163176
template <typename node_t, typename edge_t>
164177
requires(std::is_base_of_v<Node, node_t> && std::is_base_of_v<Edge, edge_t>)
165178
template <typename TEdge, typename... TArgs>

src/dsm/headers/Node.hpp

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,26 @@ namespace dsm {
2929
Id m_id;
3030
std::optional<std::pair<double, double>> m_coords;
3131
std::string m_name;
32-
Size m_capacity;
33-
int m_transportCapacity;
3432

3533
public:
3634
/// @brief Construct a new Node object with capacity 1
3735
/// @param id The node's id
38-
explicit Node(Id id) : m_id{id}, m_name{""}, m_capacity{1}, m_transportCapacity{1} {}
36+
explicit Node(Id id) : m_id{id}, m_name{""} {}
3937
/// @brief Construct a new Node object with capacity 1
4038
/// @param id The node's id
4139
/// @param coords A std::pair containing the node's coordinates (lat, lon)
4240
Node(Id id, std::pair<double, double> coords)
43-
: m_id{id},
44-
m_coords{std::move(coords)},
45-
m_name{""},
46-
m_capacity{1},
47-
m_transportCapacity{1} {}
41+
: m_id{id}, m_coords{std::move(coords)}, m_name{""} {}
4842

4943
Node(Node const& other)
50-
: m_id{other.m_id},
51-
m_coords{other.m_coords},
52-
m_name{other.m_name},
53-
m_capacity{other.m_capacity},
54-
m_transportCapacity{other.m_transportCapacity} {};
44+
: m_id{other.m_id}, m_coords{other.m_coords}, m_name{other.m_name} {};
5545
virtual ~Node() = default;
5646

5747
Node& operator=(Node const& other) {
5848
if (this != &other) {
5949
m_id = other.m_id;
6050
m_coords = other.m_coords;
6151
m_name = other.m_name;
62-
m_capacity = other.m_capacity;
63-
m_transportCapacity = other.m_transportCapacity;
6452
}
6553
return *this;
6654
}
@@ -74,18 +62,6 @@ namespace dsm {
7462
/// @brief Set the node's name
7563
/// @param name The node's name
7664
void setName(const std::string& name) { m_name = name; }
77-
/// @brief Set the node's capacity
78-
/// @param capacity The node's capacity
79-
virtual void setCapacity(Size capacity) { m_capacity = capacity; }
80-
/// @brief Set the node's transport capacity
81-
/// @param capacity The node's transport capacity
82-
virtual void setTransportCapacity(int capacity) {
83-
if (capacity < 1) {
84-
Logger::error(std::format(
85-
"The transport capacity of a node ({}) must be greater than 0.", capacity));
86-
}
87-
m_transportCapacity = capacity;
88-
}
8965
/// @brief Get the node's id
9066
/// @return Id The node's id
9167
Id id() const { return m_id; }
@@ -95,19 +71,6 @@ namespace dsm {
9571
/// @brief Get the node's name
9672
/// @return std::string The node's name
9773
const std::string& name() const { return m_name; }
98-
/// @brief Get the node's capacity
99-
/// @return Size The node's capacity
100-
Size capacity() const { return m_capacity; }
101-
/// @brief Get the node's transport capacity
102-
/// @return Size The node's transport capacity
103-
int transportCapacity() const { return m_transportCapacity; }
104-
105-
virtual double density() const { return 0.; };
106-
virtual bool isFull() const { return true; };
107-
108-
virtual bool isIntersection() const noexcept { return false; }
109-
virtual bool isTrafficLight() const noexcept { return false; }
110-
virtual bool isRoundabout() const noexcept { return false; }
11174

11275
virtual bool isStation() const noexcept { return false; }
11376
};

0 commit comments

Comments
 (0)