using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using networkdll;
using Mapack;
namespace MCL
{
public class MCL
{
public int counter, slot_duration, delimiter = 0;
public DateTime timeStart;
public String path, path1;
public float locatorRadius = 0, sensorRadius = 0;
public MCL(ArrayList asensor, ArrayList alocator, int counter, int slot_duration,
String path, String path1, float locatorRadius, float sensorRadius,
int delimiter, int SensorModel, double initialenergy)
{
set_parameters(locatorRadius, sensorRadius, counter, slot_duration);
localize(asensor, alocator, locatorRadius, path,
path1, delimiter, SensorModel, initialenergy);
}
public void set_parameters(float locatorRadius,
float sensorRadius, int counter, int slot_duration)
{
this.locatorRadius = locatorRadius;
this.sensorRadius = sensorRadius;
this.slot_duration = slot_duration;
this.counter = counter;
}
public void localize(ArrayList asensor, ArrayList alocator,
float locatorRadius, String path, String path1,
int delimiter, int SensorModel, double initialenergy)
{
networkdll.Class1.Trace trace = new Class1.Trace();
networkdll.Class1.Battery battery = new Class1.Battery();
String time = "";
long start, end, executiontime;
start = System.DateTime.Now.Ticks;
int s = 0, count = 0; //sensor index,count for average error
int ersum, avger = 0;//error summation
int comm_cost = 0;//communication cost = no. of packets generated
float coverage = 0; //no. of localized sensor nodes
int no_samples = 0, avg_samples = 0;
//String result = "Node,EstX,EstY,ActualX,ActualY,Error,
// 1hAnchor,2hAnchor,Neighbors,No.samples,Constraint\n";
//String result1 = "";//cumulate all results for all slot
String ext = "", del = "";
//file extension and column delimiter type "comma or tab"
String result = "";
String result1 = "";//cumulate all results for all slot
if (delimiter == 0)
{
del = ","; ext = "xls";
result = "Node" + del + "EstX" + del + "EstY" + del +
"ActualX" + del + "ActualY" + del + "Error" + del +
"1hAnchor" + del + "2hAnchor" + del + "Neighbors" +
del + "No.samples" + del + "Constraint\n";
}
else if (delimiter == 1)
{
del = "\t"; ext = "doc";
result = "Node" + del + "EstX" + del + "EstY" + del +
"ActualX" + del + "ActualY" + del + "Error" + del +
"1hAnchor" + del + "2hAnchor" + del + "Neighbors" +
del + "No.samples" + del + "Constraint\n";
result += "========================================================================\n";
}
float ax = 0, ay = 0;//float estx = 0, esty = 0;
double processing_time;
//Calculate processing power
double ProcessingPower = 0.029;
if (SensorModel == 0)//Mica2
ProcessingPower = 0.029;//watt
else if (SensorModel == 1)//TelosB
ProcessingPower = 0.038;//watt
if (counter % slot_duration == 0)//anchor path
{
ersum = 0; avger = 0;
////////////////////////////Slot 0////////////////////////
foreach (networkdll.Class1.WirelessSensor sensor in asensor)
{
if (sensor.ResidualEnergy > 0)
{
///////Localization using Monte Carlo//////////////
if (counter == 0)
{
String location = "", write_location = "",
samples = "",sample = "",sample1 = "", pos = "";
location = path + "\\sensor" + s + "." + ext;
write_location = path + "\\slot_" + ((counter) / slot_duration) +
"_sensor" + s + "." + ext;
int xg = 0, yg = 0, score, score1, ns = 0, ns1 = 0;
float sx = 0, sy = 0, sxs = 0, sys = 0, sx1 = 0, sy1 = 0;
ax = 0; ay = 0;
for (int i = 0; i < 50; i++)
//generate random samples in the deployment field
{
Random rs = new Random(i + 1000 * sensor.ID);
xg = rs.Next(0, 550);
yg = rs.Next(0, 550);
sxs += xg;
sys += yg;
samples += xg + del + yg + "\n";
}
if (sensor.apkts.Count == 0)
//No anchors heard ==> No filtering ==> high localization error
{
float erx, ery;//error (x,y)
ax = (sxs / 50);//number of samples = 50
ay = (sys / 50);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
no_samples += 50;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del + sensor.y + del +
(int)Math.Sqrt(erx * erx + ery * ery) + del + sensor.apkts.Count + del +
sensor.two_hop_apkts.Count + del + "--" +
del + 50 + del + "No anchors heard" +"\n";;
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
//file to store filtered samples for each slot
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(samples);
}
}
//file to store estimated position for current slot only
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
else//anchors heard ==> filtering is done
{
int j = 0; samples = "";
while (sensor.two_hop_apkts.Count > 0 ? ns1 < 50 : ns < 50)
{
int i = 0;
foreach (networkdll.Class1.pkt packet in sensor.apkts)
{
Random rs = new Random(i + j + 1000 * sensor.ID);
xg = rs.Next((int)(packet.x - locatorRadius),
(int)(packet.x + locatorRadius));
yg = rs.Next((int)(packet.y - locatorRadius),
(int)(packet.y + locatorRadius));
if (xg < 20)
xg = 20;
if (yg < 20)
yg = 20;
samples += xg + del + yg + "\n";
score = 0; score1 = 0;
foreach (networkdll.Class1.pkt packet1 in sensor.apkts)
//1-hop anchor packet
{
float as1Radius = (float)Math.Sqrt(
Math.Pow(packet1.x - xg, 2) + Math.Pow(packet1.y - yg, 2));
if (as1Radius <= locatorRadius)
score++;
}
foreach (networkdll.Class1.two_hop_apkts packet2 in sensor.two_hop_apkts)
{
float as2Radius = (float)Math.Sqrt(Math.Pow(
packet2.x - xg, 2) + Math.Pow(packet2.y - yg, 2));
if (as2Radius >= locatorRadius &&
as2Radius <= 2 * locatorRadius)
score1++; only
}
if (score1 == sensor.two_hop_apkts.Count && score == sensor.apkts.Count
&& score != 0 && score1 != 0)
//filtering by 1-hop and 2-hop anchors
{
sx1 += xg;
sy1 += yg;
ns1++;
sample1 += xg + del + yg + "\n";
}
else if (score == sensor.apkts.Count && score != 0 && score1 == 0)
{
sx += xg;
sy += yg;
ns++;
sample += xg + del + yg + "\n";
}
i++;
}
j++;
if (j > 50) break;
}
//file to store random samples generated
using (FileStream fs = new FileStream(path +
"\\samples_of_sensor_" + s + "." +ext, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(samples);
}
}
if (ns1 != 0)//2-hop filtering
{
float erx, ery;//error (x,y)
ax = (sx1 / ns1);
ay = (sy1 / ns1);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count + sensor.two_hop_apkts.Count;
//communication cost
no_samples += ns1;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del +
sensor.y + del + (int)Math.Sqrt(erx * erx + ery * ery) +
del + sensor.apkts.Count + del + sensor.two_hop_apkts.Count + del +
"--" + del + ns1 + del +
"2-hop anchor" + "\n";// del +
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
//file to store filtered samples for each slot
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample1);
}
}
//file to store estimated position for current slot only
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
else if (ns != 0)//1-hop filtering
{
float erx, ery;//error (x,y)
ax = (sx / ns);
ay = (sy / ns);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count;//communication cost
no_samples += ns;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del +
sensor.y + del + (int)Math.Sqrt(erx * erx + ery * ery) + del +
sensor.apkts.Count + del + sensor.two_hop_apkts.Count + del +
"--" + del + ns + del + "1-hop anchor" + "\n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
//file to store filtered samples for each slot
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample);
}
}
//file to store estimated position for current slot only
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
}
//processing time for MCL = 100 msec per node
if (sensor.apkts.Count > 0)
processing_time = 0.1;//0.1 sec = 100 msec
else
processing_time = 0.0;
//Compute residual energy for sensor node after processing task (localization)
battery.DecrProcessingEnergy(sensor, processing_time, ProcessingPower);
//writing localize event to trace file
trace.Localize_Trace(path1, "L", counter + 3.0 * sensor.rcvtime,
"S" + sensor.ID, sensor.x, sensor.y,
(int)ax, (int)ay, sensor.ResidualEnergy);
}
///////Slot1,2,3..n/////////////
else
{
String location = "", read_location =
"", write_location = "",
samples = "", sample = "",
sample1 = "", pos = "";
ax = 0; ay = 0;
location = path + "\\sensor" + s + "." + ext;
//file to store sample points coordinated of current slot
read_location = path + "\\slot_" + (((counter) /
slot_duration)-1) + "_sensor" + s +
"." + ext;//to read from previous slot
write_location = path + "\\slot_" + ((counter) /
slot_duration) + "_sensor" + s + "." + ext;
int score1 = 0, score2 = 0, ns1 = 0, ns2 = 0;
float sxx = 0, syy = 0, sx = 0, sy = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
//sxx.syy:estimated samplex,sx,sy:samplex
// after increment(decrement),x1,x2,y1,y2:cumulative fired samples
try
{
Matrix samples_matrix = new Matrix(50, 2);
//file to read random samples of sensor in prevoius slot
using (StreamReader re = new StreamReader(read_location))
{
for (int row = 0; row < samples_matrix.Rows; row++)//
{
string line = re.ReadLine();
string[] values;
if (del == ",")
values = line.Split(',');
else
values = line.Split('\t');
for (int col = 0; col < 2; col++)
{
samples_matrix[row, col] = double.Parse(values[col]);
}
// row++;
if (re.EndOfStream) break;
}
int k = 0;
if (sensor.apkts.Count > 0)
{
hile (sensor.two_hop_apkts.Count > 0 ? ns2 < 50 : ns1 < 50)
{
for (int i = 0; i < 50; i++)
{
sxx = (float)samples_matrix[i, 0];
syy = (float)samples_matrix[i, 1];
Random rs = new Random(i + k + 1000 * sensor.ID);
sx = rs.Next((int)Math.Abs(sxx - sensorRadius),
(int)(sxx + sensorRadius));
sy = rs.Next((int)Math.Abs(syy - sensorRadius),
(int)(syy + sensorRadius));
if (sx < 20)
sx = 20;
if (sy < 20)
sy = 20;
samples += sx + del + sy + "\n";
score1 = 0; score2 = 0;
////////////////1-hop anchor constraint/////////////////
foreach (networkdll.Class1.pkt packet1 in sensor.apkts)
//1-hop anchor packet
{
float as1Radius = (float)Math.Sqrt(
Math.Pow(packet1.x - sx, 2) + Math.Pow(packet1.y - sy, 2));
if (as1Radius <= locatorRadius)
score1++; //counter for number of 1-hop anchors only
}
////////////2-hop anchor constraint//////////////////
foreach (networkdll.Class1.two_hop_apkts packet2
in sensor.two_hop_apkts)
{
float as2Radius = (float)Math.Sqrt(
Math.Pow(packet2.x - sx, 2) + Math.Pow(packet2.y - sy, 2));
if (as2Radius >= locatorRadius &&
as2Radius <= 2 * locatorRadius)
score2++; }
if (score2 == sensor.two_hop_apkts.Count &&
score1 == sensor.apkts.Count &&
score1 != 0 && score2 != 0)
{
x2 += sx;
y2 += sy;
ns2++;
sample1 += sx + del + sy + "\n";
}
else if (score1 == sensor.apkts.Count &&
score1 != 0 && score2 == 0
{
x1 += sx;
y1 += sy;
ns1++;
sample += sx + del + sy + "\n";
}
if (sensor.two_hop_apkts.Count > 0)//2-hop
{
if (ns2 == 50)
goto aa;
}
else//1-hop
if (ns1 == 50)
goto aa;
}
k++;
if (k > 50)
goto aa;
}
}
aa: //file to store random samples generated
using (FileStream fs = new FileStream(path +
"\\samples_of_sensor_" + s +
"." + ext, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(samples);
}
}
if (ns2 != 0)//1-hop and 2-hop filtering
{
float erx, ery;//error (x,y)
ax = (x2 / ns2);
ay = (y2 / ns2);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count + sensor.two_hop_apkts.Count;
//communication cost
no_samples += ns2;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del + sensor.y + del +
(int)Math.Sqrt(erx * erx + ery * ery) + del + sensor.apkts.Count + del +
sensor.two_hop_apkts.Count + del + del + ns2 + del +
"1-hop and 2-hop anchors" + del + "\n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
//file to store filtered samples for each slot
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample1);
}
}
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
else if (ns1 != 0)//1-hop filtering
{
float erx, ery;//error (x,y)
ax = (x1 / ns1);
ay = (y1 / ns1);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count;//communication cost
no_samples += ns1;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del + sensor.y + del +
(int)Math.Sqrt(erx * erx + ery * ery) + del + sensor.apkts.Count + del +
sensor.two_hop_apkts.Count + del + del + ns1 + del +
"1-hop anchors" + del + "\n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
//file to store filtered samples for each slot
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample);
}
}
//file to store estimated position for current slot only
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
else if (ns1 == 0 && ns2 == 0)
{
Matrix sample_matrix = new Matrix(1, 2);
//file to read last stored estimated position
using (StreamReader re = new StreamReader(location))
{
for (int row = 0; row < 1; row++)
{
string line = re.ReadLine();
string[] values;
if (del == ",")
values = line.Split(',');
else
values = line.Split('\t');
for (int col = 0; col < 2; col++)
{
sample_matrix[row, col] = double.Parse(values[col]);
}
}
}
float erx, ery;//error (x,y)
ax = (float)sample_matrix[0, 0];
ay = (float)sample_matrix[0, 1];
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
no_samples += 50;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del + sensor.y + del +
(int)Math.Sqrt(erx * erx + ery * ery) + del + sensor.apkts.Count + del +
sensor.two_hop_apkts.Count + del + "--" +
del + 0 + del + "No anchors heard\n"
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
}
}
catch
{
if (sensor.apkts.Count == 0)
{
Matrix samples_matrix = new Matrix(1, 2);
//file to read last stored estimated position
using (StreamReader re = new StreamReader(location))
{
//int row = 0;
for (int row = 0; row < 1; row++)//
{
string line = re.ReadLine();
string[] values;
if (del == ",")
values = line.Split(',');
else
values = line.Split('\t');
for (int col = 0; col < 2; col++)
{
samples_matrix[row, col] = double.Parse(values[col]);
}
}
}
float erx, ery;//error (x,y)
ax = (float)samples_matrix[0, 0];
ay = (float)samples_matrix[0, 1];
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
no_samples += 50;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x +
del + sensor.y + del + (int)Math.Sqrt(erx *
erx + ery * ery) + del + sensor.apkts.Count +
del + sensor.two_hop_apkts.Count + del +
"--" + del + 0 + del +
"No anchors heard \n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
}
else//anchors heard ==> filtering is done
{
int j = 0;
while (sensor.two_hop_apkts.Count > 0 ? ns2 < 50 : ns1 < 50)
{
int i = 0;
foreach (networkdll.Class1.pkt packet in sensor.apkts)
{
Random rs = new Random(i + j + 1000 * sensor.ID);
sx = rs.Next((int)(packet.x - locatorRadius), (int)(packet.x + locatorRadius));
sy = rs.Next((int)(packet.y - locatorRadius), (int)(packet.y + locatorRadius));
if (sx < 20)
sx = 20;
if (sy < 20)
sy = 20;
samples += sx + del + sy + "\n";
score1 = 0; score2 = 0;
foreach (networkdll.Class1.pkt packet1 in sensor.apkts)
{
float as1Radius = (float)Math.Sqrt(
Math.Pow(packet1.x - sx, 2) + Math.Pow(packet1.y - sy, 2));
if (as1Radius <= locatorRadius)
score1++;
}
foreach (networkdll.Class1.two_hop_apkts packet2 in sensor.two_hop_apkts)
{
float as2Radius = (float)Math.Sqrt(Math.Pow(
packet2.x - sx, 2) + Math.Pow(packet2.y - sy, 2));
if (as2Radius >= locatorRadius &&
as2Radius <= 2 * locatorRadius)
score2++; //counter for number of 2-hop anchors only
}
if (score2 == sensor.two_hop_apkts.Count &&
score1 == sensor.apkts.Count &&
score1 != 0 && score2 != 0)
{
x2 += sx;
y2 += sy;
ns2++;
sample1 += sx + del + sy + "\n";
}
else if (score1 == sensor.apkts.Count &&
score1 != 0 && score2 == 0)
{
x1 += sx;
y1 += sy;
ns1++;
sample += sx + del + sy + "\n";
}
i++;
}
j++;
if (j > 50)
break;
}
using (FileStream fs = new FileStream(path +
"\\samples_of_sensor_" + s + "." +
ext, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(samples);
}
}
if (ns2 != 0)//1-hop and 2-hop filtering
{
float erx, ery;//error (x,y)
ax = (x2 / ns2);
ay = (y2 / ns2);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count + sensor.two_hop_apkts.Count;//communication cost
no_samples += ns2;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del +
sensor.y + del + (int)Math.Sqrt(erx * erx +
ery * ery) + del + sensor.apkts.Count + del +
sensor.two_hop_apkts.Count + del + del +
ns2 + del + "1-hop and 2-hop anchors" +
del + "\n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample1);
}
}
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
else if (ns1 != 0)//1-hop filtering
{
float erx, ery;//error (x,y)
ax = (x1 / ns1);
ay = (y1 / ns1);
pos = ax + del + ay;
erx = Math.Abs(ax - sensor.x);
ery = Math.Abs(ay - sensor.y);
comm_cost += sensor.apkts.Count;//communication cost
no_samples += ns1;//cumulative number of samples
result += s + del + ax + del + ay + del + sensor.x + del +
sensor.y + del + (int)Math.Sqrt(erx * erx + ery * ery) + del +
sensor.apkts.Count + del + sensor.two_hop_apkts.Count + del +
del + ns1 + del + "1-hop anchors" + del + "\n";
ersum += (int)Math.Sqrt(erx * erx + ery * ery);
count++;
using (FileStream fs = new FileStream(write_location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(sample);
}
}
using (FileStream fs = new FileStream(location, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(pos);
}
}
}
}
}
if (sensor.apkts.Count > 0)
processing_time = 0.1;//0.1 sec = 100 msec
else
processing_time = 0.0;
battery.DecrProcessingEnergy(sensor, processing_time, ProcessingPower);
trace.Localize_Trace(path1, "L", counter + 3.0 * sensor.rcvtime,
"S" + sensor.ID, sensor.x, sensor.y,
(int)ax, (int)ay, sensor.ResidualEnergy);
}
sensor.estx = (int)ax;
sensor.esty = (int)ay;
}
s++;
}
end = System.DateTime.Now.Ticks;
executiontime = end - start;
time = " " + (double)(executiontime / 10000);
trace.Percent_Residual_Energy(path1, asensor, counter / slot_duration, initialenergy);
if (count != 0) { avger = ersum / count; avg_samples = no_samples / count; }
if (s != 0 && count != 0) coverage = (float)((float)count / (float)s) * 100;
using (FileStream fs = new FileStream(path + "\\MCL_of_slot" +
((counter) / slot_duration) + "." + ext, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(result + "\n\n" + "\nAverage" +
del + "error = " + del + avger + del + "m" +
"\nPercent" + del + " error = " + del +
(avger / sensorRadius) + del + "R" + "\nCommunication" + del +
"cost = " + del + comm_cost + del + "packet\n" +
/*"Computation" + del + "cost =" + del + time + del +
"ms\n"*/"Avg.Number of" + del +
"samples = " + del + avg_samples + del + "sample\n"/* +
"Localized" + del + "sensors =" + del +
coverage + del + "%"*/);
w.Dispose();
}
}
if (delimiter == 1)
result1 += (((counter)) / slot_duration) + del + (avger / sensorRadius) + del +
del + del + comm_cost + del + del + del + /*time*/avg_samples /*+ del + del + coverage*/;
else
result1 += (((counter)) / slot_duration) + del + (avger / sensorRadius) +
del + comm_cost + del + /*time*/avg_samples/* + del + coverage*/;
if (counter == 0)
using (FileStream fs = new FileStream(path +
"\\MCL_total_results." + ext, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
if (delimiter == 1)
w.WriteLine("Slot" + del + "Localization" +
del + "Communication" + del +
/*"Computation"*/"Number of" +
del + /*"Localized*/"\nNo." +
del + "Error \"R\"" + del + del +
"Cost \"packet\"" + del +
/*"Cost \"ms\""*/"samples \"sample\"" +
del +
/*"Sensors \"%\*/"\n===========
========================================================\n" + result1);
else
w.WriteLine("Slot" + del +
"Localization" + del + "Communication" +
del + /*"Computation"*/"Number of" +
del + /*"Localized*/"\nNo." +
del + "Error \"R\"" + del +
"Cost \"packet\"" + del +
/*"Cost \"ms\""*/"samples \"sample\"" +
del + /*"Sensors \"%\*/"\n" + result1);
w.Dispose();
}
}
else
using (FileStream fs = new FileStream(path +
"\\MCL_total_results." + ext, FileMode.Append))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(result1);w.Dispose();
}
}
}
}
}
}