Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
grid.h
1/*
2** Copyright 2007-2024, 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
31namespace Antares
32{
33namespace Graph
34{
35template<class NodeT>
36class Node
37{
38public:
39 Node(NodeT* data)
40 {
41 nodeProperties = data;
42 }
43
44 virtual ~Node() = default;
45
46 virtual std::string getName()
47 {
48 return name;
49 }
50
51 virtual void setName(std::string newName)
52 {
53 name = newName;
54 }
55
56public:
57 std::string name;
58 NodeT* nodeProperties;
59};
60
61template<class NodeT>
62class Edge
63{
64public:
65 Edge(Node<NodeT>* from, Node<NodeT>* to)
66 {
67 originNode = from;
68 destinationNode = to;
69 }
70
71 long getWeight() const
72 {
73 return weight;
74 }
75
76 void setWeight(long w)
77 {
78 weight = w;
79 }
80
81 Node<NodeT>* getOrigin()
82 {
83 return originNode;
84 }
85
86 Node<NodeT>* getDestination()
87 {
88 return destinationNode;
89 }
90
91 void setOrigin(Node<NodeT>* from)
92 {
93 originNode = from;
94 }
95
96 void setDestination(Node<NodeT>* to)
97 {
98 destinationNode = to;
99 }
100
101private:
102 long weight;
103 Node<NodeT>* originNode;
104 Node<NodeT>* destinationNode;
105
106public:
108 {
109 long operator()(long i, const Edge<NodeT>* o) const
110 {
111 return (o->getWeight() + i);
112 }
113 };
114
116 {
117 bool operator()(const Edge<NodeT>* lhs, const Edge<NodeT>* rhs) const
118 {
119 return lhs->getWeight() < rhs->getWeight();
120 }
121 };
122};
123
127template<class NodeT>
128class Grid
129{
130public:
133 typedef std::vector<NodeP> VectorNodeP;
134 typedef std::vector<EdgeP> VectorEdgeP;
135 typedef std::map<NodeP, NodeP> MapNodes;
136 typedef std::vector<bool> EdgeIncidence;
137
138public:
140
141
142 Grid();
144 ~Grid();
146
148 NodeP addNode(NodeT&, std::string);
149
151 EdgeP addEdge(NodeP, NodeP, long weight = 0);
152
153 uint getNumberOfConnectedComponents();
154
155 VectorEdgeP findShortestPath(NodeP node1, NodeP node2) const;
156
158 EdgeP findEdgeFromNodeNames(std::string u, std::string v)
159 {
160 auto edgeIT = std::find_if(pEdgesList.begin(),
161 pEdgesList.end(),
162 [&u, &v](const EdgeP& edgeP) -> bool
163 {
164 if (edgeP->getOrigin()->getName() == u
165 && edgeP->getDestination()->getName() == v)
166 {
167 return true;
168 }
169 if (edgeP->getDestination()->getName() == u
170 && edgeP->getOrigin()->getName() == v)
171 {
172 return true;
173 }
174 else
175 {
176 return false;
177 }
178 });
179 if (edgeIT != pEdgesList.end())
180 {
181 return *edgeIT;
182 }
183
184 return nullptr;
185 }
186
187 EdgeP findDrivingEdgeFromNodeNames(std::string u, std::string v)
188 {
189 auto edgeIT = std::find_if(pMinSpanningTree.begin(),
190 pMinSpanningTree.end(),
191 [&u, &v](const EdgeP& edgeP) -> bool
192 {
193 if (edgeP->getOrigin()->getName() == u
194 && edgeP->getDestination()->getName() == v)
195 {
196 return true;
197 }
198 if (edgeP->getDestination()->getName() == u
199 && edgeP->getOrigin()->getName() == v)
200 {
201 return true;
202 }
203 else
204 {
205 return false;
206 }
207 });
208 if (edgeIT != pMinSpanningTree.end())
209 {
210 return *edgeIT;
211 }
212
213 return nullptr;
214 }
215
216 // remove an edge from the graph
217 void removeEdge(EdgeP e)
218 {
219 auto edgeIT = std::find(pEdgesList.begin(), pEdgesList.end(), e);
220 auto edge = *edgeIT;
221 if (edgeIT != pEdgesList.end())
222 {
223 pEdgesList.erase(edgeIT);
224
225 adjency[e->getOrigin()].erase(e->getDestination());
226 adjency[e->getDestination()].erase(e->getOrigin());
227
228 delete edge;
229 }
230 }
231
233 NodeP findNodeFromName(std::string name)
234 {
235 auto nodeIT = std::find_if(pNodesList.begin(),
236 pNodesList.end(),
237 [&name](NodeP& n) -> bool { return n->getName() == name; });
238 if (nodeIT != pNodesList.end())
239 {
240 return *nodeIT;
241 }
242 else
243 {
244 return nullptr;
245 }
246 }
247
254 void kruskal();
255
260 /*VectorEdgeData getMinSpanningTree()
261 {
262 VectorEdgeData minSpanningTree(pMinSpanningTree.size());
263 auto end = pMinSpanningTree.end();
264 for (auto e = pMinSpanningTree.begin(); e != end; e++)
265 {
266 minSpanningTree.push_back((*e)->edgeProperties);
267 }
268 return minSpanningTree;
269 }*/
270
276 bool buildMesh();
277
283 const std::vector<std::vector<int>>& getMeshIndexMatrix()
284 {
285 return meshIndexMatrix;
286 }
287
288 VectorEdgeP twoLevelPath(VectorNodeP vN);
289
294 bool getDuplicatedGrid(Grid&);
295
300 bool cloneGrid(Grid&);
301
302 const VectorEdgeP& getEdges()
303 {
304 return pEdgesList;
305 }
306
307public:
308 EdgeIncidence getIncidenceVector(VectorEdgeP vE)
309 {
310 EdgeIncidence Ei(pEdgesList.size(), false);
311 for (uint i = 0; i < pEdgesList.size(); i++)
312 {
313 if (std::find(vE.begin(), vE.end(), pEdgesList[i]) != vE.end())
314 {
315 Ei[i] = true;
316 }
317 }
318 return Ei;
319 }
320
321 EdgeIncidence getIncidenceVector(EdgeP vE)
322 {
323 EdgeIncidence Ei(pEdgesList.size(), false);
324 for (uint i = 0; i < pEdgesList.size(); i++)
325 {
326 if (vE == pEdgesList[i])
327 {
328 Ei[i] = true;
329 break;
330 }
331 }
332 return Ei;
333 }
334
335 VectorEdgeP getEdgeVectorFromIncidence(EdgeIncidence vI)
336 {
337 VectorEdgeP vE;
338 for (uint i = 0; i < pEdgesList.size(); i++)
339 {
340 if (vI[i] == true)
341 {
342 vE.push_back(pEdgesList[i]);
343 }
344 }
345 return vE;
346 }
347
348 EdgeIncidence incidenceXOR(EdgeIncidence& e1, EdgeIncidence& e2)
349 {
350 EdgeIncidence Ei(e1.size(), false);
351 std::transform(e1.begin(), e1.end(), e2.begin(), Ei.begin(), std::bit_xor<bool>());
352
353 return Ei;
354 }
355
356 int incidenceInnerProduct(EdgeIncidence& e1, EdgeIncidence& e2)
357 {
358 int r1 = std::inner_product(e1.begin(), e1.end(), e2.begin(), 0);
359 return r1 % 2;
360 }
361
362 void clear()
363 {
364 for (auto it = pNodesList.begin(); it != pNodesList.end(); it++)
365 {
366 delete (*it);
367 }
368 pNodesList.clear();
369
370 for (auto it = pEdgesList.begin(); it != pEdgesList.end(); it++)
371 {
372 delete (*it);
373 }
374 pEdgesList.clear();
375
376 pMinSpanningTree.clear();
377 pMesh.clear();
378 meshIndexMatrix.clear();
379 adjency.clear();
380 }
381
382private:
384
385
386 VectorNodeP pNodesList;
388 VectorEdgeP pEdgesList;
390
392 VectorEdgeP pMinSpanningTree;
394 std::vector<VectorEdgeP> pMesh;
395
396 std::vector<std::vector<int>> meshIndexMatrix;
397
398 double inf;
399
401 std::map<NodeP, std::map<NodeP, EdgeP>> adjency;
402};
403
404} // namespace Graph
405} // namespace Antares
406
407#include "grid.hxx"
408#endif // CONSTRAINTSBUILDER_BUILDER_GRID_H
Definition grid.h:63
Antares Grid (graph)
Definition grid.h:129
EdgeP addEdge(NodeP, NodeP, long weight=0)
Add one edge to the graph.
Definition grid.hxx:77
VectorEdgeP findShortestPath(NodeP node1, NodeP node2) const
Find shortest path between the two nodes (Djikstra)
Definition grid.hxx:425
NodeP addNode(NodeT &, std::string)
Add one node to the graph.
Definition grid.hxx:56
const std::vector< std::vector< int > > & getMeshIndexMatrix()
Get the min cycle basis mesh.
Definition grid.h:283
~Grid()
Destructor.
Definition grid.hxx:43
NodeP findNodeFromName(std::string name)
find a node from it's name
Definition grid.h:233
EdgeP findEdgeFromNodeNames(std::string u, std::string v)
find an edge from node names
Definition grid.h:158
Grid()
Constructor.
Definition grid.hxx:37
Definition grid.h:37