-
Notifications
You must be signed in to change notification settings - Fork 1
/
adaptivesystem.cpp
140 lines (128 loc) · 3.59 KB
/
adaptivesystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* AcoPath: Shortest path calculation using Ant Colony Optimization
* Copyright (C) 2014-2021 by Constantine Kyriakopoulos
* zfox@users.sourceforge.net
* @version 0.9.1
*
* @section LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "adaptivesystem.h"
/**
* Constructor for edges.
*/
AdaptiveSystem::Edge::Edge()
{
edgeStart = edgeEnd = weight = id = 0;
}
/**
* Comparison of current instance with the rhs, based on ids.
*
* @param rhs The right-hand side object
* @return bool The indication of current id being less than rhs'
*/
bool AdaptiveSystem::Edge::operator<(const Edge& rhs) const
{
return id < rhs.id;
}
/**
* Comparison of current instance with the rhs, based on ids.
*
* @param rhs The right-hand side object
* @return bool The indication of current id being greater than rhs'
*/
bool AdaptiveSystem::Edge::operator>(const Edge& rhs) const
{
return id > rhs.id;
}
/**
* Comparison of current instance with the rhs for equality, based on ids.
*
* @param rhs The right-hand side object
* @return bool The indication of equality
*/
bool AdaptiveSystem::Edge::operator==(const Edge& rhs) const
{
return id == rhs.id;
}
/**
* Empty constructor for the AdaptiveSystem.
*/
AdaptiveSystem::AdaptiveSystem() { }
/**
* Empty destructor for the AdaptiveSystem.
*/
AdaptiveSystem::~AdaptiveSystem() { }
/**
* Initialises the local topology representation.
*
* @param filename JSON-formatted file containing the topology representation
*/
void AdaptiveSystem::initTopo(const std::string& filename)
{
ptree pt;
boost::property_tree::read_json(filename, pt);
ptree::const_iterator end = pt.end();
for(ptree::const_iterator it = pt.begin(); it != end; ++it)
{
if(!std::strcmp(it->first.c_str(), "number_of_nodes"))
continue;
ptree::const_iterator end2 = it->second.end();
for(ptree::const_iterator it2 = it->second.begin(); it2 != end2; ++it2)
{
ptree::const_iterator end3 = it2->second.end();
int src = 0; int dest = 0; int length = 0;
for(ptree::const_iterator it3 = it2->second.begin(); it3 != end3; ++it3)
{
if(!std::strcmp(it3->first.c_str(), "nodes"))
{
int index = 0;
ptree::const_iterator end31 = it3->second.end();
for(ptree::const_iterator it31 = it3->second.begin(); it31 != end31; ++it31, ++index)
{
if(index == 0)
src = it31->second.get_value<int>();
if(index == 1)
dest = it31->second.get_value<int>();
}
}
if(!std::strcmp(it3->first.c_str(), "length"))
length = it3->second.get_value<int>();
}
insertEdge(src, dest, static_cast<double>(length));
}
}
}
/**
* Inserts an edge.
*
* @param src Starting node
* @param dest Ending node
* @param weight Weight for the edge
*/
void AdaptiveSystem::insertEdge(int src, int dest, double weight) noexcept(false)
{
AdaptiveSystem::Edge edge;
edge.edgeStart = src;
edge.edgeEnd = dest;
edge.weight = weight;
edge.id = ++edgeIdCnt;
edges.push_back(edge);
}
/**
* Used for producing edge IDs.
*/
int AdaptiveSystem::edgeIdCnt = 0;