Sie sind auf Seite 1von 10

(*

This script calculates k-coverage and k-connectivity,


in or outside the context of pseudorandom key pre-distribution.
For k-coverage, Huang et al.'s algorithm described in
"The Coverage Problem in a Wireless Sensor Network",
Mobile Networks and Applications, Springer, is used.
Run this script via the Mathematica GUI front-end or at the command line
:
math < this_script
$Id: kcover.nb,v 1.3 2007/09/27 14:15:18 ywlaw Exp $
*)
<<DiscreteMath`Combinatorica`
(* * * * * * * * * * * Configuration parameters * * * * * * * * * *)
(* Uncomment the first subsequent line to get the script to generate random topo
logy *)
v = {};
(* The following are test topologies *)
(* v = {{{0.1, 0.1}}, {{0.5, 0.5}}, {{0.9, 0.9}}, {{0.3, 0.7}}, {{0.7, 0.3}}}; *
)
(* v = {{{0.1, 0.5}}, {{0.5, 0.9}}, {{0.9, 0.5}}, {{0.5, 0.1}}, {{0.5, 0.5}}}; *
)
(* v = {{{0.1, 0.9}}, {{0.8, 0.8}}, {{0.8, 0.2}}, {{0.1, 0.1}}, {{0.5, 0.5}}}; *
)
(* v = {{{0.1, 0.9}}, {{0.5, 0.9}}, {{0.9, 0.9}},
{{0.2, 0.6}}, {{0.5, 0.5}}, {{0.8, 0.6}},
{{0.05, 0.215}}, {{0.3, 0.14}}, {{0.7, 0.14}}, {{0.95, 0.215}}}; *) (* c
overage 1 *)
(* v = {{{0.1, 0.9}}, {{0.5, 0.9}}, {{0.9, 0.9}},
{{0.2, 0.6}}, {{0.5, 0.5}}, {{0.8, 0.6}},
{{0.05, 0.215}}, {{0.3, 0.14}}, {{0.7, 0.14}}, {{0.95, 0.215}},
{{0.15, 0.85}}, {{0.5, 0.85}}, {{0.85, 0.85}},
{{0.15, 0.5}}, {{0.5, 0.5}}, {{0.85, 0.5}},
{{0.15, 0.15}}, {{0.5, 0.15}}, {{0.85, 0.15}}}; *) (* coverage 2 *)
(* v = {{{0.1, 0.9}}, {{0.5, 0.9}}, {{0.9, 0.9}},
{{0.2, 0.6}}, {{0.5, 0.5}}, {{0.8, 0.6}},
{{0.05, 0.215}}, {{0.3, 0.14}}, {{0.7, 0.14}},
{{0.15, 0.85}}, {{0.5, 0.85}}, {{0.85, 0.85}},
{{0.15, 0.5}}, {{0.5, 0.5}}, {{0.85, 0.5}},
{{0.15, 0.15}}, {{0.5, 0.15}}}; *) (* coverage 0 *)
If[Length[v] == 0,
n = 100,
of nodes *)
n = Length[v];
ogies *)
]
nprime = 20;
ghbours per node *)
rc = N[Sqrt[(nprime+1)/(n*\[Pi])]]
rs = rc;
*)
K = 4;
*)

(* Total number
(* For pre-defined topol
(* Average number of nei
(* Transmission range *)
(* Sensory range
(* Key ring size

Pstart = 10;
*)
Pend = 10;
size *)

(* Initial key pool size


(* Last key pool

If[Length[v] == 0,
nsim = 50,
rations per P *)
nsim = 1;
nse to simulate once for a fixed topology *)
];
euclidean = False;
ean or toroidal distances *)

(* Number of ite
(* Only makes se

(* Whether to use Euclid

findCoverage = False;
findConnectivity = False;
findAPC = True;
debug = False;
(* * * * * * * * * * DO NOT MODIFY BELOW THIS LINE * * * * * * * * * *)
(* * * * Constants * * * *)
H = 1;
T = -1;
INFTY = 999999;
(* * * * Functions * * * *)
edist[xi_, yi_, xj_, yj_] := {Sqrt[(xi - xj)^2 + (yi - yj)^2], xi, yi};
(*
Usage: Returns a tuple of {dist, xiprime, yiprime}.
Dimension of toroidal area is 1 x 1.
*)
tdist[xi_, yi_, xj_, yj_] := Module[{min, xiprime, yiprime},
min = edist[xi, yi, xj, yj][[1]];
xiprime = xi;
yiprime = yi;
tmp = edist[xi + 1, yi, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi + 1; yiprime = yi];
tmp = edist[xi - 1, yi, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi - 1; yiprime = yi];
tmp = edist[xi, yi + 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi; yiprime = yi + 1];
tmp = edist[xi, yi - 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi; yiprime = yi - 1];
tmp = edist[xi + 1, yi + 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi + 1; yiprime = yi +
tmp = edist[xi + 1, yi - 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi + 1; yiprime = yi tmp = edist[xi - 1, yi + 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi - 1; yiprime = yi +
tmp = edist[xi - 1, yi - 1, xj, yj][[1]];
If[tmp < min, min = tmp; xiprime = xi - 1; yiprime = yi {min, xiprime, yiprime}
];
(*
Usage: Returns a tuple of {dist, xiprime, yiprime).

1];
1];
1];
1];

Modify this to get Euclidean or toroidal distance. Returns a tuple.


*)
If[euclidean == True,
dist[xi_, yi_, xj_, yj_] := edist[xi, yi, xj, yj],
dist[x_, yi_, xj_, yj_] := tdist[xi, yi, xj, yj]
];
(* * * * * * * * * * * * Main calculation * * * * * * * * * * * *)
For[P = Pstart, P <= Pend, P++,
For[sim = 1, sim <= nsim, sim++,
SeedRandom[sim];
(* Generate vertices if not using a fixed topology {{{ *)
If[Length[v] == 0 || sim > 1,
v = {};
For[i = 1, i <= n, i++, v = Append[v, {{Random[], Random
[]}}]];
];
g = Graph[{}, v];
Print["Vertices: ", v]; (* Debug *)
(* }}} *)
(* Distribute keys randomly {{{ *)
keyrings = {};
For[i = 1, i <= n, i++,
keyring = {};
If[P == K,
For[k = 1, Length[keyring] < K, k++,
keyring = Append[keyring, k]
],
(* P > K *)
For[k = 1, Length[keyring] < K, k++,
key = Random[Integer, {1, P}];
If[MemberQ[keyring, key] == False,
keyring = Append[keyring, key]
]
]
];
keyrings = Append[keyrings, keyring];
];
Print["Key rings: ", keyrings]; (* Debug *)
(* }}} *)
(* Determine edges {{{ *)
For[i = 1, i <= n, i++,
For[j = i + 1, j <= n, j++,
xi = Extract[Extract[Extract[v, i], 1], 1];
xj = Extract[Extract[Extract[v, j], 1], 1];
yi = Extract[Extract[Extract[v, i], 1], 2];
yj = Extract[Extract[Extract[v, j], 1], 2];
If[dist[xi, yi, xj, yj][[1]] < rc,
If[Extract[keyrings, {i}] \[Intersection
] Extract[keyrings, {j}] \[NotEqual] {},
g = AddEdge[g, {{i, j}}],
(* Else *)
Print["No secure link between ",
i, " ", j]
]
]

]
];
ShowGraph[g];
(* }}} *)
If[findCoverage == True,
(* Init coverage variable for each node {{{ *)
coverdb = {};
For[i = 1, i <= n, i++, coverdb = Append[coverdb, {}]];
(* }}} *)
(* Handle border cases {{{ *)
For[i = 1, i <= n, i++,
xi = Extract[Extract[Extract[v, i], 1], 1];
yi = Extract[Extract[Extract[v, i], 1], 2];
(*
Top *)
If[1-yi < rs,
dy = 1-yi;
(* Top left *)
If[xi < rs,
dx = xi;
If[dx^2 + dy^2 < rs^2,
(* 1 arc *)
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2-ArcCos[dy/rs]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi] +ArcCos[dx/rs]], -INFTY}];
If[debug, Print[i, " top
-left border: ", coverdb[[i]]]], (* Debug *)
(* 2 arcs *)
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2-ArcCos[dy/rs]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi] -ArcCos[dx/rs]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi] +ArcCos[dx/rs]], -INFTY}];
If[debug, Print[i, " top
-left border: ", coverdb[[i]]]] (* Debug *)
];
Continue[],
(* Top right *)
If[1-xi < rs,
dx = 1-xi;
If[dx^2 + dy^2 < rs^2,
(* 1 arc *)
coverdb[[i]] = A
ppend[coverdb[[i]], {N[2*\[Pi]-ArcCos[dx/rs]], INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {0,
INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
If[debug, Print[
i, " top-right border: ", coverdb[[i]]]], (* Debug *)

(* 2 arcs *)
coverdb[[i]] = A
ppend[coverdb[[i]], {N[2*\[Pi]-ArcCos[dx/rs]], INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {0,

INFTY}];
coverdb[[i]] = A

ppend[coverdb[[i]], {N[

ArcCos[dx/rs]], -INFTY}];
coverdb[[i]] = A

ppend[coverdb[[i]], {N[\[Pi]/2-ArcCos[dy/rs]], INFTY}];


coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
If[debug, Print[
i, " top-right border: ", coverdb[[i]]]] (* Debug *)
];
Continue[],
(* Top middle *)
\[Alpha] = ArcCos[dy/rs]
;
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2-\[Alpha]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2+\[Alpha]], -INFTY}];
If[debug, Print[i, " top
-middle border: ", coverdb[[i]]]]; (* Debug *)
Continue[]
]
]
];
(*
Bottom *)
If[yi < rs,
dy = yi;
(* Bottom left *)
If[xi < rs,
dx = xi;
If[dx^2 + dy^2 < rs^2,
(* 1 arc *)
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]
-ArcCos[dx/rs]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
If[debug, Print[i, " bot
tom-left border: ", coverdb[[i]]]], (* Debug *)
(* 2 arcs *)
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]

-ArcCos[dx/rs]], INFTY}];

verdb[[i]], {N[\[Pi]

+ArcCos[dx/rs]], -INFTY}];

coverdb[[i]] = Append[co
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2-ArcCos[dy/rs]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
If[debug, Print[i, " bot
tom-left border: ", coverdb[[i]]]] (* Debug *)
];
Continue[],
(* Bottom right *)
If[1-xi < rs,

dx = 1-xi;
If[dx^2 + dy^2 < rs^2,
(* 1 arc *)
coverdb[[i]] = A
ppend[coverdb[[i]], {N[3*\[Pi]/2-ArcCos[dy/rs]], INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {0,
INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[
ArcCos[dx/rs]], -INFTY}];
If[debug, Print[
i, " bottom-right border: ", coverdb[[i]]]], (* Debug *)
(* 2 arcs *)
coverdb[[i]] = A
ppend[coverdb[[i]], {N[3*\[Pi]/2-ArcCos[dy/rs]], INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[3*\[Pi]/2+ArcCos[dy/rs]], -INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[2*\[Pi] -ArcCos[dx/rs]], INFTY}];
coverdb[[i]] = A
ppend[coverdb[[i]], {0,

INFTY}];
coverdb[[i]] = A

ppend[coverdb[[i]], {N[

ArcCos[dx/rs]], -INFTY}];
If[debug, Print[

i, " bottom-right border: ", coverdb[[i]]]] (* Debug *)


];
Continue[],
(* Bottom middle *)
\[Alpha] = ArcCos[dy/rs]
;
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2-\[Alpha]], INFTY}];
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2+\[Alpha]], -INFTY}];
If[debug, Print[i, " bot
tom-middle border: ", coverdb[[i]]]]; (* Debug *)
Continue[]
]
]
];
(*
Left *)
If[xi < rs && yi > rs && 1-yi > rs,
\[Alpha] = ArcCos[xi/rs];
coverdb[[i]] = Append[coverdb[[i]], {N[\
[Pi]-\[Alpha]], INFTY}];
coverdb[[i]] = Append[coverdb[[i]], {N[\
[Pi]+\[Alpha]], -INFTY}];
If[debug, Print[i, " left border: ", cov
erdb[[i]]]]; (* Debug *)
Continue[]
];
(*
Right *)
If[1-xi < rs && yi > rs && 1-yi > rs,
\[Alpha] = ArcCos[(1-xi)/rs];
coverdb[[i]] = Append[coverdb[[i]], {N[2
*\[Pi]-\[Alpha]], INFTY}];
coverdb[[i]] = Append[coverdb[[i]], {0,
INFTY}];
coverdb[[i]] = Append[coverdb[[i]], {N[\

[Alpha]],

-INFTY}];
If[debug, Print[i, " right border: ", co

verdb[[i]]]]; (* Debug *)
Continue[]
]
];
(* }}} *)
(* Handle non-border cases {{{ *)
For[i = 1, i <= n, i++,
For[j = 1, j <= n, j++,
If[i == j, Continue[]];
xi = Extract[Extract[Extract[v, i], 1],
1];
xj = Extract[Extract[Extract[v, j], 1],
1];
yi = Extract[Extract[Extract[v, i], 1],
2];
yj = Extract[Extract[Extract[v, j], 1],
2];
disttuple = dist[xi, yi, xj, yj];
d = disttuple[[1]];
xiprime = disttuple[[2]];
yiprime = disttuple[[3]];
If[debug, Print[" d(", i, ",", j, ")=",
d, ", xiprime=", xiprime, ", yiprime=", yiprime]]; (* Debug *)
If[d > 2*rs || Extract[keyrings, {i}] \[
Intersection] Extract[keyrings, {j}] \[Equal] {}, Continue[]]; (* TODO: This mig
ht be wrong *)
\[Alpha] = N[ArcCos[d/(2*rs)]];
(* Handle non-border special cases *)
If[Abs[xj - xiprime] < 0.000001,
(* yj == yiprime is improbable *
)
If[yj > yiprime,
(* North *)
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2 - \[Alpha]], H}];
coverdb[[i]] = Append[co
verdb[[i]], {N[\[Pi]/2 + \[Alpha]], T}],
(* South *)
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2 - \[Alpha]], H}];
coverdb[[i]] = Append[co
verdb[[i]], {N[3*\[Pi]/2 + \[Alpha]], T}]
];
If[debug, Print["(", i, ",", j,
"): ", coverdb[[i]]]]; (* Debug *)
Continue[]
];
slope = (yj - yiprime)/(xj - xiprime);
If[slope > 0,
(* Positive slope *)
If[xj > xiprime,

(* First quadrant *)
\[Theta] = N[ArcCos[(xj
- xiprime)/d]];
If[\[Theta] > \[Alpha],
coverdb[[i]] = A
ppend[coverdb[[i]], {\[Theta] - \[Alpha], H}];
coverdb[[i]] = A
ppend[coverdb[[i]], {\[Theta] + \[Alpha], T}],
If[\[Theta] < \[
Alpha],
coverdb[
[i]] = Append[coverdb[[i]], {0, H}];
coverdb[
[i]] = Append[coverdb[[i]], {\[Theta] + \[Alpha], T}];
coverdb[
[i]] = Append[coverdb[[i]], {N[2*\[Pi] - (\[Alpha] - \[Theta])], H}],
(* \[The
ta] == \[Alpha] *)
coverdb[
[i]] = Append[coverdb[[i]], {0, H}];
coverdb[
[i]] = Append[coverdb[[i]], {2*\[Alpha], T}]
]
],
(* Third quadrant *)
\[Theta] = N[ArcCos[(xip
rime - xj)/d]];
If[\[Theta] > \[Alpha],
coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi] + (\[Theta] - \[Alpha])], H}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi] + (\[Theta] + \[Alpha])], T}],
If[\[Theta] < \[
Alpha],
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] - (\[Alpha] - \[Theta])], H}];
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] + (\[Theta] + \[Alpha])], T}],
(* \[The
ta] == \[Alpha] *)
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi]], H}];
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] + 2*\[Alpha]], T}]
]
]
],
(* Negative slope *)
If[xj > xiprime,
(* Fourth quadrant *)
\[Theta] = N[ArcCos[(xj
- xiprime)/d]];
If[\[Theta] > \[Alpha],
coverdb[[i]] = A
ppend[coverdb[[i]], {N[2*\[Pi] - (\[Theta] + \[Alpha])], H}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[2*\[Pi] - (\[Theta] - \[Alpha])], T}],
If[\[Theta] < \[

Alpha],
coverdb[
[i]] = Append[coverdb[[i]], {0, H}];
coverdb[
[i]] = Append[coverdb[[i]], {\[Alpha] - \[Theta], T}];
coverdb[
[i]] = Append[coverdb[[i]], {N[2*\[Pi] - (\[Alpha] + \[Theta])], H}],
(* \[The
ta] == \[Alpha] *)
coverdb[
[i]] = Append[coverdb[[i]], {N[2*\[Pi] - 2*\[Alpha]], H}];
]
],
(* Second quadrant *)
\[Theta] = N[ArcCos[(xip
rime - xj)/d]];
If[\[Theta] > \[Alpha],
coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi] - (\[Theta] + \[Alpha])], H}];
coverdb[[i]] = A
ppend[coverdb[[i]], {N[\[Pi] - (\[Theta] - \[Alpha])], T}],
If[\[Theta] < \[
Alpha],
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] - (\[Theta] + \[Alpha])], H}];
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] + (\[Alpha] - \[Theta])], T}],
(* \[The
ta] == \[Alpha] *)
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi] - 2*\[Alpha]], H}];
coverdb[
[i]] = Append[coverdb[[i]], {N[\[Pi]], T}]
]
]
]
];
If[debug, Print["(", i, ",", j, "): ", c
overdb[[i]]]] (* Debug *)
];
coverdb[[i]] = Sort[coverdb[[i]], #1[[1]] < #2[[
1]] || (#1[[1]] == #2[[1]] && #1[[2]] > #2[[2]])&];
If[debug, Print[i, " sorted: ", coverdb[[i]]]] (
* Debug *)
];
(* }}} *)
(* Compute perimeter coverage {{{ *)
netMinCoverage = n;
For [i = 1, i <= n, i++,
segEndPoints = coverdb[[i]];
coverage = 0;
nodeMinCoverage = 0;
For [j = 1, j <= Length[segEndPoints], j++,
rad = Extract[Extract[segEndPoints, j] ,
1];
weight = Extract[Extract[segEndPoints, j

] , 2];
If[Abs[rad] < 0.000001,
coverage += weight;
nodeMinCoverage += weight,
(* Else *)
coverage += weight;
If[coverage < nodeMinCoverage, n
odeMinCoverage = coverage]
];
If[debug, Print[i, ", ", rad, ", ", weig
ht, ": ", coverage, ", ", nodeMinCoverage]] (* Debug *)
];
If[nodeMinCoverage < netMinCoverage, netMinCover
age = nodeMinCoverage];
If[netMinCoverage == 0, Break[]]
];
Print["Coverage = ", netMinCoverage]
(* }}} *)
];
If[findConnectivity == True,
(* Compute connectivity {{{ *)
Print["Connectivity = ", VertexConnectivity[g]];
(* }}} *)
];
If[findAPC == True,
(* Compute average pairwise connectivity {{{ *)
Module[{p=VertexConnectivityGraph[g], k=V[g], i=0, noted
ges, karray},
notedges = ToUnorderedPairs[ GraphComplement[g]
];
karray = {};
While[i++ <= k,
k = Min[Map[(NetworkFlow[p,2 #[[1]],2 #[
[2]]-1])&, Select[notedges,(First[#]==i)&]], k];
karray = Append[karray, k];
];
Print["APC = ", N[Mean[karray]]]
]
(* }}} *)
]
]
]
(*
vim:syn=mma:foldmethod=marker:
*)

Das könnte Ihnen auch gefallen