Samuelson77 commited on
Commit
261949d
·
verified ·
1 Parent(s): 8716835

Upload swarm_wci_dtn_node.cpp

Browse files
Files changed (1) hide show
  1. middleware/swarm_wci_dtn_node.cpp +237 -0
middleware/swarm_wci_dtn_node.cpp ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // File: middleware/swarm_wci_dtn_node.cpp
2
+ // Standard: C++20 | Role: 6-Bot Cooperative WCI, GCC-AKF, DP Scheduler & CCSDS DTN Node
3
+
4
+ #include <iostream>
5
+ #include <vector>
6
+ #include <array>
7
+ #include <cmath>
8
+ #include <algorithm>
9
+ #include <iomanip>
10
+ #include <string>
11
+
12
+ namespace qsnasb::middleware {
13
+
14
+ using Mat3 = std::array<std::array<double, 3>, 3>;
15
+ using Vec3 = std::array<double, 3>;
16
+
17
+ struct BotNeighborMeasurement {
18
+ int bot_id{};
19
+ Vec3 relative_pos_m{};
20
+ Mat3 cov_nominal{};
21
+ double snr_db{24.0};
22
+ double innovation_norm_m{0.15};
23
+ double gdop{1.2};
24
+ bool link_active{true};
25
+ };
26
+
27
+ // Phase 3: Quality-Aware Logical AND-Gate Neighbor Filter
28
+ class QualityAwareAndGateFilter {
29
+ public:
30
+ static bool admit_neighbor(const BotNeighborMeasurement& meas,
31
+ double min_snr_db = 12.0,
32
+ double max_innovation_m = 2.5,
33
+ double max_gdop = 4.5) noexcept
34
+ {
35
+ if (!meas.link_active) return false;
36
+ bool f_snr = (meas.snr_db >= min_snr_db);
37
+ bool f_innov = (meas.innovation_norm_m <= max_innovation_m);
38
+ bool f_geom = (meas.gdop <= max_gdop);
39
+ // Logical AND-Gate: exclude neighbor if any operational quality factor fails
40
+ return f_snr && f_innov && f_geom;
41
+ }
42
+ };
43
+
44
+ // Phase 4: Subspace-Weighted Covariance Intersection (WCI) + Group Covariance Compensated AKF
45
+ class WeightedCovarianceIntersectionEngine {
46
+ private:
47
+ Mat3 subspace_weights_W_{{{1.0, 0.0, 0.0},
48
+ {0.0, 1.0, 0.0},
49
+ {0.0, 0.0, 0.25}}}; // W = S^T * S subspace scaling
50
+
51
+ static Mat3 invert_diagonal_3x3(const Mat3& A) noexcept {
52
+ Mat3 inv{};
53
+ for (size_t i = 0; i < 3; ++i) {
54
+ inv[i][i] = (std::abs(A[i][i]) > 1e-12) ? (1.0 / A[i][i]) : 1e12;
55
+ }
56
+ return inv;
57
+ }
58
+
59
+ static double weighted_trace(const Mat3& W, const Mat3& P) noexcept {
60
+ double tr = 0.0;
61
+ for (size_t i = 0; i < 3; ++i) {
62
+ tr += W[i][i] * P[i][i];
63
+ }
64
+ return tr;
65
+ }
66
+
67
+ public:
68
+ // Optimizes omega* = argmin_{omega in (0,1)} Tr(S^T S P(omega))
69
+ double optimize_wci_weight(const Mat3& P_local_prior, const Mat3& R_neighbor_corr) const noexcept {
70
+ Mat3 P_inv_1 = invert_diagonal_3x3(P_local_prior);
71
+ Mat3 P_inv_2 = invert_diagonal_3x3(R_neighbor_corr);
72
+
73
+ double best_omega = 0.5;
74
+ double min_cost = 1e30;
75
+
76
+ for (int step = 1; step < 100; ++step) {
77
+ double omega = static_cast<double>(step) / 100.0;
78
+ Mat3 P_fused_inv{};
79
+ for (size_t i = 0; i < 3; ++i) {
80
+ P_fused_inv[i][i] = omega * P_inv_1[i][i] + (1.0 - omega) * P_inv_2[i][i];
81
+ }
82
+ Mat3 P_fused = invert_diagonal_3x3(P_fused_inv);
83
+ double cost = weighted_trace(subspace_weights_W_, P_fused);
84
+ if (cost < min_cost) {
85
+ min_cost = cost;
86
+ best_omega = omega;
87
+ }
88
+ }
89
+ return best_omega;
90
+ }
91
+
92
+ // Performs WCI update and applies Group Covariance Compensation (GCC) for AKF
93
+ Mat3 fuse_and_compensate_akf(const Mat3& P_local_prior,
94
+ const Mat3& R_neighbor_corr,
95
+ const Vec3& innovation_vec,
96
+ double& omega_out,
97
+ Mat3& akf_estimated_R_out) const noexcept
98
+ {
99
+ omega_out = optimize_wci_weight(P_local_prior, R_neighbor_corr);
100
+ Mat3 P_inv_1 = invert_diagonal_3x3(P_local_prior);
101
+ Mat3 P_inv_2 = invert_diagonal_3x3(R_neighbor_corr);
102
+
103
+ Mat3 P_wci_inv{};
104
+ for (size_t i = 0; i < 3; ++i) {
105
+ P_wci_inv[i][i] = omega_out * P_inv_1[i][i] + (1.0 - omega_out) * P_inv_2[i][i];
106
+ }
107
+ Mat3 P_wci = invert_diagonal_3x3(P_wci_inv);
108
+
109
+ // Resolve CI-AKF Blindness Paradox:
110
+ // CI inflates P_wci by factor ~ 1/omega_out. Without GCC rescaling,
111
+ // subtracting P_wci from innovation covariance underestimates observation noise R.
112
+ double gcc_deinflation_factor = 2.0 * omega_out * (1.0 - omega_out) + 0.5;
113
+ for (size_t i = 0; i < 3; ++i) {
114
+ double empirical_innov_cov = innovation_vec[i] * innovation_vec[i];
115
+ double compensated_prior = gcc_deinflation_factor * P_wci[i][i];
116
+ akf_estimated_R_out[i][i] = std::max(1e-4, empirical_innov_cov - compensated_prior);
117
+ }
118
+ return P_wci;
119
+ }
120
+ };
121
+
122
+ // Dynamic Programming (DP) Reactive Imaging Scheduler (55-deg Field of Regard)
123
+ struct GroundGridPoint {
124
+ int gp_id{};
125
+ double off_nadir_angle_deg{};
126
+ double science_hazard_value{};
127
+ double timestamp_s{};
128
+ };
129
+
130
+ class ReactiveImagingScheduler {
131
+ public:
132
+ static constexpr double MAX_FOR_ANGLE_DEG = 55.0; // 5x nadir distortion limit
133
+ static constexpr double SLEW_RATE_DEG_PER_S = 2.5;
134
+
135
+ static std::vector<int> compute_optimal_schedule(
136
+ const std::vector<GroundGridPoint>& candidate_gps)
137
+ {
138
+ // Filter points inside the 55-deg Field of Regard
139
+ std::vector<GroundGridPoint> valid_gps;
140
+ for (const auto& gp : candidate_gps) {
141
+ if (std::abs(gp.off_nadir_angle_deg) <= MAX_FOR_ANGLE_DEG) {
142
+ valid_gps.push_back(gp);
143
+ }
144
+ }
145
+ size_t n = valid_gps.size();
146
+ if (n == 0) return {};
147
+
148
+ std::vector<double> dp_val(n, 0.0);
149
+ std::vector<int> parent(n, -1);
150
+
151
+ for (size_t i = 0; i < n; ++i) {
152
+ dp_val[i] = valid_gps[i].science_hazard_value;
153
+ for (size_t j = 0; j < i; ++j) {
154
+ double dt = valid_gps[i].timestamp_s - valid_gps[j].timestamp_s;
155
+ double d_angle = std::abs(valid_gps[i].off_nadir_angle_deg -
156
+ valid_gps[j].off_nadir_angle_deg);
157
+ double required_slew_time = d_angle / SLEW_RATE_DEG_PER_S;
158
+ if (dt >= required_slew_time) {
159
+ if (dp_val[j] + valid_gps[i].science_hazard_value > dp_val[i]) {
160
+ dp_val[i] = dp_val[j] + valid_gps[i].science_hazard_value;
161
+ parent[i] = static_cast<int>(j);
162
+ }
163
+ }
164
+ }
165
+ }
166
+
167
+ // Backtrack optimal DP path and discard suboptimal branches
168
+ auto max_it = std::max_element(dp_val.begin(), dp_val.end());
169
+ int curr = static_cast<int>(std::distance(dp_val.begin(), max_it));
170
+ std::vector<int> optimal_path;
171
+ while (curr != -1) {
172
+ optimal_path.push_back(valid_gps[curr].gp_id);
173
+ curr = parent[curr];
174
+ }
175
+ std::reverse(optimal_path.begin(), optimal_path.end());
176
+ return optimal_path;
177
+ }
178
+ };
179
+
180
+ // CubeSat QKD (100 MHz Weak Coherent Pulse + HDBC T&S) & CCSDS DTN Link Calculator
181
+ struct CubeSatQkdDtnLink {
182
+ static double compute_pass_secret_key_mbits(double elevation_deg,
183
+ double pass_duration_s = 340.0) noexcept
184
+ {
185
+ if (elevation_deg < 20.0) return 0.0; // Link unavailable below 20-deg mask
186
+ double rad = elevation_deg * (3.141592653589793 / 180.0);
187
+ // 1.99 Mbits distilled over 340 s pass at 100 MHz repetition rate
188
+ return 1.99 * (pass_duration_s / 340.0) * std::sin(rad);
189
+ }
190
+ };
191
+
192
+ } // namespace qsnasb::middleware
193
+
194
+ int main() {
195
+ using namespace qsnasb::middleware;
196
+
197
+ WeightedCovarianceIntersectionEngine wci_engine{};
198
+ Mat3 P_local{{{0.22, 0.0, 0.0}, {0.0, 0.25, 0.0}, {0.0, 0.0, 0.08}}};
199
+ Mat3 R_bot2{{{0.14, 0.0, 0.0}, {0.0, 0.16, 0.0}, {0.0, 0.0, 0.12}}};
200
+
201
+ std::vector<BotNeighborMeasurement> swarm_inputs = {
202
+ {1, {12400.0, 150.0, -30.0}, R_bot2, 26.5, 0.12, 1.1, true},
203
+ {4, {48200.0, 999.0, 850.0}, R_bot2, 6.2, 9.85, 6.1, true} // Degraded / Spoofed outlier
204
+ };
205
+
206
+ std::cout << "=== Q-SNASB LAYER 2 SWARM WCI, GCC-AKF & DTN MIDDLEWARE ===\n";
207
+ for (const auto& m : swarm_inputs) {
208
+ bool admitted = QualityAwareAndGateFilter::admit_neighbor(m);
209
+ std::cout << "Bot-" << m.bot_id << " AND-Gate Validation : "
210
+ << (admitted ? "ADMITTED" : "REJECTED (ANOMALY ISOLATED)") << "\n";
211
+ if (admitted) {
212
+ double omega_opt = 0.0;
213
+ Mat3 akf_R{};
214
+ Mat3 P_fused = wci_engine.fuse_and_compensate_akf(
215
+ P_local, m.cov_nominal, {0.35, 0.28, 0.15}, omega_opt, akf_R);
216
+ std::cout << " -> Optimal WCI Weight omega* : " << std::fixed << std::setprecision(3)
217
+ << omega_opt << "\n";
218
+ std::cout << " -> Fused Covariance P_xx : " << P_fused[0][0] << " m^2\n";
219
+ std::cout << " -> GCC-AKF Corrected R_xx : " << akf_R[0][0] << " m^2\n";
220
+ }
221
+ }
222
+
223
+ std::vector<GroundGridPoint> targets = {
224
+ {101, 15.0, 8.5, 0.0},
225
+ {102, 48.0, 9.2, 25.0},
226
+ {103, 62.0, 99.0, 40.0}, // Outside 55-deg FOR -> automatically pruned
227
+ {104, 22.0, 14.0, 50.0}
228
+ };
229
+ auto schedule = ReactiveImagingScheduler::compute_optimal_schedule(targets);
230
+ std::cout << "DP Reactive Schedule (FOR<=55d): [ ";
231
+ for (int id : schedule) std::cout << "GP-" << id << " ";
232
+ std::cout << "]\n";
233
+
234
+ std::cout << "CubeSat QKD Distilled Key (65d): "
235
+ << CubeSatQkdDtnLink::compute_pass_secret_key_mbits(65.0) << " Mbits\n";
236
+ return 0;
237
+ }