安装程序步骤 [英] installation procedure steps

查看:68
本文介绍了安装程序步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经下载了wsn本地化模拟器zip文件和dll代码文件。如何在我的Windows 7中安装此模拟器。我找不到zip文件中的任何设置/应用程序文件。请帮助


i have downloaded the wsn localization simulator zip file and the dll code files. how can i install this simulator in my windows 7. i don find any setup / application files available with the zip file. kindly help

推荐答案

你应该发布文章的链接。

无线传感器网络本地化模拟器v1.1 [ ^ ]



您可以下载源代码和二进制文件其中 [ ^ ]。只需提取它们并导航到 WSN_Localization_Simulator \ WSN Localization Simulator \bin \Debug 。您将找到无线传感器网络Simulator.exe ,运行它。
You should have posted the link to article.
Wireless Sensor Network Localization Simulator v1.1[^]

You can download source code and binaries here[^]. Just extract them and navigate to WSN_Localization_Simulator\WSN Localization Simulator\bin\Debug. You will find Wireless Sensor Network Simulator.exe, run it.


使用系统;

使用系统.IO;

使用System.Text;

使用System.Drawing;

使用System.Collections;

使用System.Windows.Forms;

使用networkdll;

使用Mapack;

命名空间MCL

{

公共类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 =计数器;

}



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; //传感器索引,计算平均误差

int ersum,avger = 0; //错误求和

int comm_cost = 0; //通信费用=否。生成的数据包

浮动覆盖率= 0; //没有。局部传感器节点的数量

int no_samples = 0,avg_samples = 0;

//字符串结果=节点,EstX,EstY,ActualX,ActualY,错误,
// 1hAnchor,2hAnchor,Neighbors,No.samples,Constraint \ n;

// String result1 =; //累积所有插槽的所有结果



String ext =,del =;

//文件扩展名和列分隔符类型逗号或标签



字符串结果=;

字符串result1 =; //累计所有插槽的所有结果



if(delimiter == 0)

{

del =,; ext =xls;

result =Node+ del +EstX+ del +EstY+ del +

ActualX+ del +ActualY + del +错误+ del +

1hAnchor+ del +2hAnchor+ del +Neighbors+

del +No.samples+ del +Constraint\\\
;



}

else if(delimiter == 1)

{

del =\t; ext =doc;

result =Node+ del +EstX+ del +EstY+ del +

ActualX+ del +ActualY + del +错误+ del +

1hAnchor+ del +2hAnchor+ del +Neighbors+

del +No.samples+ del +Constraint\\\
;

结果+ ============================== =========================================== \\\
; <无线电通信/>
}

float ax = 0,ay = 0; // float estx = 0,esty = 0;

double processing_time;

//计算处理能力

double ProcessingPower = 0.029;

if(SensorModel == 0)// Mica2

ProcessingPower = 0.029; //瓦特

否则if(SensorModel == 1)// TelosB

ProcessingPower = 0.038 ; //瓦特

if(counter%slot_duration == 0)//锚路径

{



ersum = 0; avger = 0;

//////////////////////////// Slot 0 //////// ////////////////



foreach(asensor中的networkdll.Class1.WirelessSensor传感器)

{



if(sensor.ResidualEnergy> 0)

{

////// /使用Monte Carlo进行本地化//////////////

if(counter == 0)

{

String location =,write_location =,

samples =,sample =,sample1 =,pos =;

location =路径+\\sensor+ s +。 + ext;

write_location = path +\\slot_+((counter)/ slot_duration)+

_sensor+ s +。 + ext;

int xg = 0,yg = 0,得分,得分1,ns = 0,ns1 = 0;

浮点数sx = 0,sy = 0,sxs = 0,sys = 0,sx1 = 0,sy1 = 0;

ax = 0; ay = 0;

for(int i = 0; i< 50; i ++)

//在部署字段中生成随机样本

{

随机rs =新随机(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)

//没有听到锚点==>没有过滤==>高本地化错误

{

浮动erx,ery; //错误(x,y)

ax =(sxs / 50); / /样本数= 50

ay =(sys / 50);

pos = ax + del + ay;

erx =数学。 Abs(ax - sensor.x);

ery = Math.Abs​​(ay - sensor.y);

no_samples + = 50; //累积样本数

结果+ = 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 +没有听到锚点+\ n ;;

ersum + =(int)Math.Sqrt(erx * erx + ery * ery);

count ++;

//文件存储每个插槽的过滤样本

使用(FileStream fs = new FileStream(write_location,FileMode.Create))

{

using( StreamWriter w = new StreamWriter(fs,Encoding.UTF8))

{

w.WriteLine(samples);

}

}

//文件只存储当前插槽的估计位置

使用(FileStream fs = new FileStream(location,FileMode.Create))

{

使用(StreamWriter w = new StreamWriter(fs,Encoding.UTF8))

{

w.WriteLine(pos);

}

}

}

其他//听到锚点==>过滤完成

{

int j = 0; samples =;

while(sensor.two_hop_apkts.Count> 0?ns1< 50:ns< 50)

{

int i = 0;

foreach(sensor.apkts中的networkdll.Class1.pkt数据包)

{

随机rs =新的随机数( i + j + 1000 * sensor.ID);

xg = rs.Next((int)(packet.x - locatorRadius),

(int)(数据包。 x + locatorRadius));

yg = rs.Next((int)(packet.y - locatorRadius),

(int)(packet.y + locatorRadius)) ;

if(xg< 20)

xg = 20;

if(yg< 20)

yg = 20;



样本+ = xg + del + yg +\ n;



得分= 0; score1 = 0;

foreach(sensor.llkts中的networkdll.Class1.pkt packet1)

//单跳锚包

{

float as1Radius =(float)Math.Sqrt(

Math.Pow(packet1.x - xg,2)+ Math.Pow(packet1.y - yg,2) );

if(as1Radius< = locatorRadius)

得分++;

}

foreach(sensor.two_hop_apkts中的networkdll.Class1.two_hop_apkts packet2)

{

float as2Radius = (float)Math.Sqrt(Math.Pow(

packet2.x - xg,2)+ Math.Pow(packet2.y - yg,2));

if(as2Radius> = locatorRadius &&

as2Radius< = 2 * locatorRadius)

score1 ++;只有

}

if(score1 == sensor.two_hop_apkts.Count && score == sensor.apkts.Count

&&得分!= 0 && score1!= 0)

//按1跳和2跳锚点过滤

{

sx1 + = xg;

sy1 + = yg;

ns1 ++;

sample1 + = xg + del + yg +\ n;

}

else if(score == sensor.apkts.Count &&得分!= 0 && score1 == 0)

{

sx + = xg;

sy + = yg;

ns ++;

sample + = xg + del + yg +\ n;

}

i ++;

}

j ++;

if(j> 50)休息;

}

//文件存储随机样本生成

使用(FileStream fs = new FileStream(path +

\\samples_of_sensor_+ s +。+ ext,FileMode.Create))

{

使用(StreamWriter w = new StreamWriter (fs,Encoding.UTF8))

{

w.WriteLine(样本);

}

}

if(ns1!= 0)// 2跳过滤

{

float erx,ery; //错误(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;

//沟通费用



no_samples + = ns1; //累积样本数

结果+ = 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跳锚+\ n; // del +

ersum + =(int)Math.Sqrt(erx * erx + ery * ery);

count ++;

//用于存储每个插槽的过滤样本的文件

using(FileStream fs = new FileStream(write_location,FileMode.Create))

{

使用(StreamWriter w = new StreamWriter(fs,Encoding.UTF8))

{

w.WriteLine(sample1) ;

}

}

//文件只存储当前插槽的估计位置

using(FileStream fs = new FileStream(location,FileMo de.Create))

{

使用(StreamWriter w = new StreamWriter(fs,Encoding.UTF8))

{

w.WriteLine(pos);

}

}

}

else if(ns != 0)//单跳过滤

{



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 - sen sor.y);

comm_cost + = sensor.apkts.Count; //通讯费用

no_samples + = ns; //累积样本数

结果+ = 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 +单跳锚+\ n;

ersum + =(int)Math.Sqrt(erx * erx + ery * ery);

count ++;

//用于存储每个插槽的过滤样本的文件

using(FileStream fs = new FileStream(write_location,FileMode.Creat) e))

{

使用(StreamWriter w = new StreamWriter(fs,Encoding.UTF8))

{

w.WriteLine(样本);

}

}

//文件只存储当前插槽的估计位置

using(FileStream fs = new FileStream(location,FileMode.Create))

{

using(StreamWriter w = new StreamWriter(fs,Encoding.UTF8) )

{

w.WriteLine(pos);

}

}

}

}

// MCL的处理时间=每个节点100毫秒

if(传感器。 apkts.Count> 0)

processing_time = 0.1; // 0.1秒= 100毫秒

其他

processing_time = 0.0;

//处理任务(本地化)后计算传感器节点的剩余能量

battery.DecrProcessingEnergy(sensor,processing_time,ProcessingPower);



/ /将本地化事件写入跟踪文件

trace.Localize_Trace(path1,L,计数器+ 3.0 * sensor.rcvtime,

S+ sensor.ID,传感器.x,sensor.y,

(int)ax,(int)ay,sensor.ResidualEnergy);

}

/// ////Slot1,2,3..n/////////////

其他

{

String location =,read_location =

,write_location =,

samples =,sample = ,

sample1 =,pos =;



ax = 0; ay = 0;



location = path +\\\
snsor+ s +。 + ext;

//用于存储当前插槽协调的采样点的文件



read_location = path +\\slot_+ (((专柜)/

slot_duration)-1)+_sensor+ s +

。 + ext; //从上一个插槽读取

write_location = path +\\slot_+((计数器)/

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

//增量后(减量),x1,x2,y1,y2:累计点火样本



尝试

{

Matrix samples_matrix = new Matrix(50,2);

//文件读取prevoius插槽中传感器的随机样本

using (StreamReader re = new StreamReader(read_location))

{



for( int row = 0;行< samples_matrix.Rows;行++)//

{



string line = re.ReadLine();

string []的值;

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.EndOfStr eb)休息;

}

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];

随机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;

样本+ = sx + del + sy +\ n;



得分1 = 0;得分2 = 0;

////////////////单跳锚约束/////////////////

foreach(sensor.llkts中的networkdll.Class1.pkt packet1)

//单跳锚包

{

float as1Radius =(float)Math.Sqrt(

Math.Pow(packet1.x - sx,2)+ Math.Pow(packet1.y - sy,2));

if(as1Radius< = locatorRadius)

得分+1 ++; //仅用于单跳锚点数的计数器

}

//////////// 2跳锚定约束///// /////////////

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 &&得分2!= 0)

{

x2 + = sx;

y2 + = sy;

ns2 ++;

sample1 + = sx + del + sy +\ n;

}

else if(score1 == sensor.apkts。算上&&

得分1!= 0 &&得分2 == 0

{

x1 + = sx;

y1 + = sy;

ns1 ++;

sample + = sx + del + sy +\ n;

}

if(sensor.two_hop_apkts.Count> 0)// 2跳

{

if(ns2 == 50)

goto aa;

}

其他//单跳

if(ns1 == 50)

goto aa;

}

k ++;

if(k> 50)

goto aa;

}

}

aa://文件到s tore 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.Co unt + 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\\"\" +

d el +

/*\"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();

}

}

}

}

}

}
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();
}
}
}
}
}
}


这篇关于安装程序步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆