Antares Simulator
Power System Simulator
grid.h
1 /*
2  * Copyright 2007-2025, RTE (https://www.rte-france.com)
3  * See AUTHORS.txt
4  * SPDX-License-Identifier: MPL-2.0
5  * This file is part of Antares-Simulator,
6  * Adequacy and Performance assessment for interconnected energy networks.
7  *
8  * Antares_Simulator is free software: you can redistribute it and/or modify
9  * it under the terms of the Mozilla Public Licence 2.0 as published by
10  * the Mozilla Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Antares_Simulator is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * Mozilla Public Licence 2.0 for more details.
17  *
18  * You should have received a copy of the Mozilla Public Licence 2.0
19  * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20  */
21 #ifndef CONSTRAINTSBUILDER_BUILDER_GRID_H
22 #define CONSTRAINTSBUILDER_BUILDER_GRID_H
23 
24 #include <numeric>
25 
26 #include <yuni/yuni.h>
27 #include <yuni/core/string.h>
28 
29 #include <antares/study/study.h>
30 
31 namespace Antares::Graph
32 {
33 template<class NodeT>
34 class Node
35 {
36 public:
37  Node(NodeT* data)
38  {
39  nodeProperties = data;
40  }
41 
42  virtual ~Node() = default;
43 
44  virtual std::string getName()
45  {
46  return name;
47  }
48 
49  virtual void setName(std::string newName)
50  {
51  name = newName;
52  }
53 
54 public:
55  std::string name;
56  NodeT* nodeProperties;
57 };
58 
59 template<class NodeT>
60 class Edge
61 {
62 public:
63  Edge(Node<NodeT>* from, Node<NodeT>* to)
64  {
65  originNode = from;
66  destinationNode = to;
67  }
68 
69  long getWeight() const
70  {
71  return weight;
72  }
73 
74  void setWeight(long w)
75  {
76  weight = w;
77  }
78 
79  Node<NodeT>* getOrigin()
80  {
81  return originNode;
82  }
83 
84  Node<NodeT>* getDestination()
85  {
86  return destinationNode;
87  }
88 
89  void setOrigin(Node<NodeT>* from)
90  {
91  originNode = from;
92  }
93 
94  void setDestination(Node<NodeT>* to)
95  {
96  destinationNode = to;
97  }
98 
99 private:
100  long weight;
101  Node<NodeT>* originNode;
102  Node<NodeT>* destinationNode;
103 
104 public:
105  struct addpWeight
106  {
107  long operator()(long i, const Edge<NodeT>* o) const
108  {
109  return (o->getWeight() + i);
110  }
111  };
112 
114  {
115  bool operator()(const Edge<NodeT>* lhs, const Edge<NodeT>* rhs) const
116  {
117  return lhs->getWeight() < rhs->getWeight();
118  }
119  };
120 };
121 
125 template<class NodeT>
126 class Grid
127 {
128 public:
131  typedef std::vector<NodeP> VectorNodeP;
132  typedef std::vector<EdgeP> VectorEdgeP;
133  typedef std::map<NodeP, NodeP> MapNodes;
134  typedef std::vector<bool> EdgeIncidence;
135 
136 public:
138 
139  Grid();
142  ~Grid();
144 
146  NodeP addNode(NodeT&, std::string);
147 
149  EdgeP addEdge(NodeP, NodeP, long weight = 0);
150 
151  uint getNumberOfConnectedComponents();
152 
153  VectorEdgeP findShortestPath(NodeP node1, NodeP node2) const;
154 
156  EdgeP findEdgeFromNodeNames(std::string u, std::string v)
157  {
158  auto edgeIT = std::find_if(pEdgesList.begin(),
159  pEdgesList.end(),
160  [&u, &v](const EdgeP& edgeP) -> bool
161  {
162  if (edgeP->getOrigin()->getName() == u
163  && edgeP->getDestination()->getName() == v)
164  {
165  return true;
166  }
167  if (edgeP->getDestination()->getName() == u
168  && edgeP->getOrigin()->getName() == v)
169  {
170  return true;
171  }
172  else
173  {
174  return false;
175  }
176  });
177  if (edgeIT != pEdgesList.end())
178  {
179  return *edgeIT;
180  }
181 
182  return nullptr;
183  }
184 
185  EdgeP findDrivingEdgeFromNodeNames(std::string u, std::string v)
186  {
187  auto edgeIT = std::find_if(pMinSpanningTree.begin(),
188  pMinSpanningTree.end(),
189  [&u, &v](const EdgeP& edgeP) -> bool
190  {
191  if (edgeP->getOrigin()->getName() == u
192  && edgeP->getDestination()->getName() == v)
193  {
194  return true;
195  }
196  if (edgeP->getDestination()->getName() == u
197  && edgeP->getOrigin()->getName() == v)
198  {
199  return true;
200  }
201  else
202  {
203  return false;
204  }
205  });
206  if (edgeIT != pMinSpanningTree.end())
207  {
208  return *edgeIT;
209  }
210 
211  return nullptr;
212  }
213 
214  // remove an edge from the graph
215  void removeEdge(EdgeP e)
216  {
217  auto edgeIT = std::find(pEdgesList.begin(), pEdgesList.end(), e);
218  auto edge = *edgeIT;
219  if (edgeIT != pEdgesList.end())
220  {
221  pEdgesList.erase(edgeIT);
222 
223  adjency[e->getOrigin()].erase(e->getDestination());
224  adjency[e->getDestination()].erase(e->getOrigin());
225 
226  delete edge;
227  }
228  }
229 
231  NodeP findNodeFromName(std::string name)
232  {
233  auto nodeIT = std::find_if(pNodesList.begin(),
234  pNodesList.end(),
235  [&name](NodeP& n) -> bool { return n->getName() == name; });
236  if (nodeIT != pNodesList.end())
237  {
238  return *nodeIT;
239  }
240  else
241  {
242  return nullptr;
243  }
244  }
245 
252  void kruskal();
253 
258  /*VectorEdgeData getMinSpanningTree()
259  {
260  VectorEdgeData minSpanningTree(pMinSpanningTree.size());
261  auto end = pMinSpanningTree.end();
262  for (auto e = pMinSpanningTree.begin(); e != end; e++)
263  {
264  minSpanningTree.push_back((*e)->edgeProperties);
265  }
266  return minSpanningTree;
267  }*/
268 
274  bool buildMesh();
275 
281  const std::vector<std::vector<int>>& getMeshIndexMatrix()
282  {
283  return meshIndexMatrix;
284  }
285 
286  VectorEdgeP twoLevelPath(VectorNodeP vN);
287 
292  bool getDuplicatedGrid(Grid&);
293 
298  bool cloneGrid(Grid&);
299 
300  const VectorEdgeP& getEdges()
301  {
302  return pEdgesList;
303  }
304 
305 public:
306  EdgeIncidence getIncidenceVector(VectorEdgeP vE)
307  {
308  EdgeIncidence Ei(pEdgesList.size(), false);
309  for (uint i = 0; i < pEdgesList.size(); i++)
310  {
311  if (std::find(vE.begin(), vE.end(), pEdgesList[i]) != vE.end())
312  {
313  Ei[i] = true;
314  }
315  }
316  return Ei;
317  }
318 
319  EdgeIncidence getIncidenceVector(EdgeP vE)
320  {
321  EdgeIncidence Ei(pEdgesList.size(), false);
322  for (uint i = 0; i < pEdgesList.size(); i++)
323  {
324  if (vE == pEdgesList[i])
325  {
326  Ei[i] = true;
327  break;
328  }
329  }
330  return Ei;
331  }
332 
333  VectorEdgeP getEdgeVectorFromIncidence(EdgeIncidence vI)
334  {
335  VectorEdgeP vE;
336  for (uint i = 0; i < pEdgesList.size(); i++)
337  {
338  if (vI[i] == true)
339  {
340  vE.push_back(pEdgesList[i]);
341  }
342  }
343  return vE;
344  }
345 
346  EdgeIncidence incidenceXOR(EdgeIncidence& e1, EdgeIncidence& e2)
347  {
348  EdgeIncidence Ei(e1.size(), false);
349  std::transform(e1.begin(), e1.end(), e2.begin(), Ei.begin(), std::bit_xor<bool>());
350 
351  return Ei;
352  }
353 
354  int incidenceInnerProduct(EdgeIncidence& e1, EdgeIncidence& e2)
355  {
356  int r1 = std::inner_product(e1.begin(), e1.end(), e2.begin(), 0);
357  return r1 % 2;
358  }
359 
360  void clear()
361  {
362  for (auto it = pNodesList.begin(); it != pNodesList.end(); it++)
363  {
364  delete (*it);
365  }
366  pNodesList.clear();
367 
368  for (auto it = pEdgesList.begin(); it != pEdgesList.end(); it++)
369  {
370  delete (*it);
371  }
372  pEdgesList.clear();
373 
374  pMinSpanningTree.clear();
375  pMesh.clear();
376  meshIndexMatrix.clear();
377  adjency.clear();
378  }
379 
380 private:
382 
383  VectorNodeP pNodesList;
386  VectorEdgeP pEdgesList;
388 
390  VectorEdgeP pMinSpanningTree;
392  std::vector<VectorEdgeP> pMesh;
393 
394  std::vector<std::vector<int>> meshIndexMatrix;
395 
396  double inf;
397 
399  std::map<NodeP, std::map<NodeP, EdgeP>> adjency;
400 };
401 
402 } // namespace Antares::Graph
403 
404 #include "grid.hxx"
405 #endif // CONSTRAINTSBUILDER_BUILDER_GRID_H
Definition: grid.h:61
Antares Grid (graph)
Definition: grid.h:127
EdgeP addEdge(NodeP, NodeP, long weight=0)
Add one edge to the graph.
Definition: grid.hxx:75
const std::vector< std::vector< int > > & getMeshIndexMatrix()
Get the min cycle basis mesh.
Definition: grid.h:281
VectorEdgeP findShortestPath(NodeP node1, NodeP node2) const
Find shortest path between the two nodes (Djikstra)
Definition: grid.hxx:423
NodeP addNode(NodeT &, std::string)
Add one node to the graph.
Definition: grid.hxx:54
~Grid()
Destructor.
Definition: grid.hxx:41
NodeP findNodeFromName(std::string name)
find a node from it's name
Definition: grid.h:231
EdgeP findEdgeFromNodeNames(std::string u, std::string v)
find an edge from node names
Definition: grid.h:156
Grid()
Constructor.
Definition: grid.hxx:35
Definition: grid.h:35
Definition: grid.h:106