-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgo4.cpp
More file actions
184 lines (142 loc) · 5.9 KB
/
Algo4.cpp
File metadata and controls
184 lines (142 loc) · 5.9 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <limits>
class Node {
public:
int node_id;
double energy;
double distance_to_bs;
double dormancy_factor;
Node(int id, double e, double d)
: node_id(id), energy(e), distance_to_bs(d), dormancy_factor(0) {}
void calculate_dormancy_factor() {
if (distance_to_bs != 0) {
dormancy_factor = energy / distance_to_bs;
} else {
dormancy_factor = std::numeric_limits<double>::infinity();
}
}
};
double calculateDistance(const Node& node1, const Node& node2) {
return sqrt(pow(node1.node_id - node2.node_id, 2) + pow(node1.distance_to_bs - node2.distance_to_bs, 2));
}
double calculateGCH(const Node& node, const Node& clusterCenter, const Node& baseStation, double alpha, double beta) {
double distanceToCenter = calculateDistance(node, clusterCenter);
double distanceToBS = clusterCenter.distance_to_bs;
return (node.energy) / (alpha * distanceToCenter + beta * distanceToBS);
}
std::vector<Node> node_dormancy_algorithm(const std::vector<Node>& cluster_nodes, int total_nodes,
int surviving_nodes, int num_clusters) {
std::vector<Node> mutable_nodes = cluster_nodes;
for (auto& node : mutable_nodes) {
node.calculate_dormancy_factor();
}
double P = static_cast<double>((total_nodes * num_clusters - surviving_nodes)) / (total_nodes * num_clusters);
std::sort(mutable_nodes.begin(), mutable_nodes.end(), [](const Node& a, const Node& b) {
return a.dormancy_factor < b.dormancy_factor;
});
int num_dormant_nodes = static_cast<int>(P * mutable_nodes.size());
std::vector<Node> dormant_nodes(mutable_nodes.begin(), mutable_nodes.begin() + num_dormant_nodes);
return dormant_nodes;
}
std::vector<std::vector<Node>> clusterSetup(std::vector<Node>& objects, int k) {
std::vector<std::vector<Node>> clusters;
for (const auto& obj : objects) {
clusters.push_back({obj});
}
int kPrime = clusters.size();
while (kPrime > k) {
double maxClusteringFactor = -1;
std::pair<int, int> mergeIndices;
for (int i = 0; i < kPrime; ++i) {
for (int j = i + 1; j < kPrime; ++j) {
double distanceMean = 0;
std::vector<double> distanceSet;
for (const auto& n1 : clusters[i]) {
for (const auto& n2 : clusters[j]) {
double distance = calculateDistance(n1, n2);
distanceSet.push_back(distance);
distanceMean += distance;
}
}
distanceMean /= distanceSet.size();
double distanceVariance = 0;
for (const auto& distance : distanceSet) {
distanceVariance += pow(distance - distanceMean, 2);
}
distanceVariance /= distanceSet.size();
double clusteringFactor = 1 / (distanceMean + 0.5 * distanceVariance);
if (clusteringFactor > maxClusteringFactor) {
maxClusteringFactor = clusteringFactor;
mergeIndices = {i, j};
}
}
}
if (maxClusteringFactor > -1) {
clusters[mergeIndices.first].insert(clusters[mergeIndices.first].end(),
clusters[mergeIndices.second].begin(), clusters[mergeIndices.second].end());
clusters.erase(clusters.begin() + mergeIndices.second);
kPrime--;
}
}
return clusters;
}
Node electClusterHead(const std::vector<Node>& nodes, const Node& baseStation, double alpha, double beta) {
Node clusterCenter = {0, 0, 0};
int numNodes = nodes.size();
for (const auto& node : nodes) {
clusterCenter.node_id += node.node_id;
clusterCenter.energy += node.energy;
clusterCenter.distance_to_bs += node.distance_to_bs;
}
clusterCenter.node_id /= numNodes;
clusterCenter.energy /= numNodes;
clusterCenter.distance_to_bs /= numNodes;
double maxGCH = -1;
Node selectedCH = {0, 0, 0};
for (const auto& node : nodes) {
double gch = calculateGCH(node, clusterCenter, baseStation, alpha, beta);
if (gch > maxGCH) {
maxGCH = gch;
selectedCH = node;
}
}
return selectedCH;
}
int main() {
// Sample nodes
std::vector<Node> nodes = {
Node(1, 100, 50),
Node(2, 80, 30),
Node(3, 60, 70),
Node(4, 40, 20),
Node(5, 20, 90),
};
int total_nodes = nodes.size();
int surviving_nodes = 3;
int num_clusters = 1;
Node baseStation = {0, 0, 0};
int k = 2;
std::vector<std::vector<Node>> clusters = clusterSetup(nodes, k);
for (const auto& cluster : clusters) {
std::cout << "Cluster:\n";
for (const auto& node : cluster) {
std::cout << "Node ID: " << node.node_id
<< ", Energy: " << node.energy
<< ", Distance to BS: " << node.distance_to_bs << "\n";
}
Node ch = electClusterHead(cluster, baseStation, 0.5, 0.5);
std::cout << "Cluster Head: (" << ch.node_id << ", Energy: " << ch.energy << ")\n";
std::vector<Node> dormant_nodes = node_dormancy_algorithm(cluster, total_nodes, surviving_nodes, num_clusters);
std::cout << "Dormant Nodes:\n";
for (const auto& node : dormant_nodes) {
std::cout << "Node ID: " << node.node_id
<< ", Energy: " << node.energy
<< ", Distance to BS: " << node.distance_to_bs
<< ", Dormancy Factor: " << node.dormancy_factor << "\n";
}
}
return 0;
}