Node.java
资源名称:mcl.tar.gz [点击查看]
上传用户:kingseaxu
上传日期:2009-01-01
资源大小:49k
文件大小:14k
源码类别:
3G开发
开发平台:
Java
- import java.util.*;
- import java.io.*;
- import java.net.*;
- import java.lang.*;
- import java.awt.Point;
- import Jama.*;
- public class Node {
- int id; /* id of node */
- Point p; /* coordinate of node */
- int r; /* radio range of node */
- boolean is_seed; /* if node is seed or not */
- Point dst; /* the destination in random waypoint model */
- double dst_length; /* the length of next destination */
- double dst_direction; /* the direciton of next destination */
- Location_info this_time; /* the location info of this time */
- Location_info last_time; /* the location info of last time */
- public Node(int i, int nr, boolean seed) {
- int x, y;
- x = (int)Math.round(Network.x_range * Math.random());
- y = (int)Math.round(Network.y_range * Math.random());
- p = new Point(x, y);
- dst = null;
- dst_length = -1;
- id = i;
- r = nr;
- is_seed = seed;
- this_time = null;
- last_time = null;
- }
- /* Adapted from Tian's APIT simulation code */
- /** Centroid localization */
- public void Centroid_Localization (Vector neighbors, Point[] position) {
- int i, j, seed_id;
- int es_x, es_y, es_num;
- es_x = 0;
- es_y = 0;
- es_num = 0;
- for (i = 0; i < neighbors.size(); i++) {
- seed_id = ((Integer)neighbors.get(i)).intValue();
- if ( seed_id < Network.seed_num ) {
- es_x += position[seed_id].x;
- es_y += position[seed_id].y;
- es_num++;
- }
- }
- if (es_num > 0)
- this_time.es_p.setLocation(es_x / es_num, es_y / es_num);
- else
- this_time.es_p.setLocation(Network.x_range / 2, Network.y_range / 2);
- }
- /* Adapted from Tian's APIT simulation code */
- /** Amporphous Computing localization */
- public void Multilateration(Point [] position, double [] path_length)
- {
- int i, j;
- int numAnchors;
- int Anchors[] = new int[Network.seed_num];
- double [][] array_x, array_y;
- Matrix X = new Matrix(1, 1);
- Matrix Y = new Matrix(1, 1);
- numAnchors = 0;
- for (j = 0; j < Network.seed_num; j++)
- if (path_length[j] > 0) {
- Anchors[numAnchors] = j;
- numAnchors++;
- }
- if (numAnchors < 3) {
- this_time.es_p.setLocation(Network.x_range / 2, Network.y_range / 2);
- return;
- }
- array_x = new double[numAnchors - 1][2];
- array_y = new double[numAnchors - 1][1];
- for (j = 0; j < numAnchors - 1; j++) {
- array_x[j][0] = 2 * (position[Anchors[numAnchors - 1]].x - position[Anchors[j]].x);
- array_x[j][1] = 2 * (position[Anchors[numAnchors - 1]].y - position[Anchors[j]].y);
- X = new Matrix(array_x);
- array_y[j][0] = path_length[Anchors[j]] * path_length[Anchors[j]]
- - path_length[Anchors[numAnchors - 1]] * path_length[Anchors[numAnchors - 1]]
- + position[Anchors[numAnchors - 1]].x * position[Anchors[numAnchors - 1]].x
- + position[Anchors[numAnchors - 1]].y * position[Anchors[numAnchors - 1]].y
- - position[Anchors[j]].x * position[Anchors[j]].x
- - position[Anchors[j]].y * position[Anchors[j]].y;
- Y = new Matrix(array_y);
- }
- Matrix TempM1 = X.transpose();
- Matrix TempM2 = TempM1.times(X);
- Matrix TempM3 = TempM2.inverse();
- Matrix TempM4 = TempM3.times(TempM1);
- Matrix retMatrix = TempM4.times(Y);
- this_time.es_p.setLocation(retMatrix.get(0, 0), retMatrix.get(1, 0));
- }
- /** MCL localizaiton */
- public void MCLocalization (int []relation, Point []position, Point ref) {
- int i, j, x, y, l, n;
- Point pt;
- String s, k;
- if (this_time != null )
- last_time = this_time;
- this_time = new Location_info();
- /* update one hop and two hop list */
- for (i = 0; i < relation.length; i++) {
- if (relation[i] == 1) { /* one hop list */
- this_time.one_hop_list.put(Integer.toString(i), pointToString(position[i]));
- }
- else if (relation[i] == 2) { /* two hop list */
- this_time.two_hop_list.put(Integer.toString(i), pointToString(position[i]));
- }
- }
- /* update sample points */
- if ( (last_time == null) || (last_time.sample_num == 0)) { /* not initialized yet */
- System.out.println("node " + id + " has no sample points");
- if (!this_time.one_hop_list.isEmpty()) {
- Enumeration e = this_time.one_hop_list.elements();
- k = (String)e.nextElement();
- pt = stringToPoint(k);
- /* try absolute samples */
- for (i = 0; i < 10000; i++) {
- pt = getSample(pt, Network.seed_r);
- if (meetCondition(pt) >= 1) {
- this_time.sample_points[this_time.sample_num] = pt;
- this_time.sample_weight[this_time.sample_num] = 1;
- this_time.sample_num++;
- }
- if (this_time.sample_num == this_time.max_sample_num)
- break;
- }
- /* if no enough absolute samples, try relaxed samples */
- if ( (i == 10000) && (this_time.sample_num < this_time.max_sample_num)) {
- for (i = 0; i < 10000; i++) {
- pt = getSample(pt, Network.seed_r);
- if (meetCondition(pt) >= 0) {
- this_time.sample_points[this_time.sample_num] = pt;
- this_time.sample_weight[this_time.sample_num] = 1;
- this_time.sample_num++;
- }
- if (this_time.sample_num == this_time.max_sample_num)
- break;
- }
- }
- }
- }
- else { /* already initialized */
- Point[] temp = new Point[200 * this_time.max_sample_num];
- int[] weight = new int[200 * this_time.max_sample_num];
- int temp_num = 0;
- /* try absolute samples */
- for (i = 0; i < 200; i++) {
- for (j = 0; j < last_time.sample_num; j++) {
- pt = last_time.sample_points[j];
- pt = getSample(pt, Network.max_v);
- int cond = meetCondition(pt);
- if (cond >= 1) {
- temp[temp_num] = pt;
- weight[temp_num] = 1;
- temp_num++;
- }
- }
- if (temp_num >= this_time.max_sample_num)
- break;
- }
- /* no enough absolute sample, then try relaxed samples */
- if (temp_num < this_time.max_sample_num) {
- System.out.println("node " + id + ", not enough absolute sample");
- for (i = 0; i < 200; i++) {
- for (j = 0; j < last_time.sample_num; j++) {
- pt = last_time.sample_points[j];
- pt = getSample(pt, Network.max_v);
- int cond = meetCondition(pt);
- if (cond >= 0) {
- temp[temp_num] = pt;
- weight[temp_num] = 1;
- temp_num++;
- }
- }
- if (temp_num >= this_time.max_sample_num)
- break;
- }
- }
- if (temp_num < this_time.max_sample_num) {
- System.out.println ("node " + id + ", not enough samples");
- for (i = 0;i < temp_num; i++) {
- this_time.sample_points[i]= temp[i];
- this_time.sample_weight[i] = weight[i];
- }
- this_time.sample_num = temp_num;
- }
- else {
- for (i = 0; i < this_time.max_sample_num; i++) {
- int index = (int)(temp_num * Math.random());
- this_time.sample_points[i] = temp[index];
- this_time.sample_weight[i] = weight[index];
- temp[index] = temp[temp_num - 1];
- temp_num--;
- }
- this_time.sample_num = this_time.max_sample_num;
- }
- }
- /* estimate location */
- int es_x = 0;
- int es_y = 0;
- int es_num = 0;
- for (i = 0; i < this_time.sample_num; i++) {
- es_x += this_time.sample_points[i].x * this_time.sample_weight[i];
- es_y += this_time.sample_points[i].y * this_time.sample_weight[i];
- es_num += this_time.sample_weight[i];
- }
- if (this_time.sample_num == 0)
- this_time.es_p.setLocation(ref.x + Network.x_range / 2, ref.y + Network.y_range / 2);
- else
- this_time.es_p.setLocation(es_x / es_num, es_y / es_num);
- this_time.sample_range = 0;
- for (i = 0; i < this_time.sample_num; i++)
- if (this_time.es_p.distance(this_time.sample_points[i]) > this_time.sample_range)
- this_time.sample_range = (float)this_time.es_p.distance(this_time.sample_points[i]);
- }
- /** Print location information */
- public void print() {
- /* print one hop list */
- System.out.println("one hop list.........................................");
- Enumeration e = this_time.one_hop_list.keys() ;
- while (e.hasMoreElements()) {
- int k = Integer.parseInt((String)(e.nextElement()));
- String s = (String)(this_time.one_hop_list.get(Integer.toString(k)));
- Point p = stringToPoint(s);
- System.out.println("node: " + id + " key: " + k + " value: " + p.toString() );
- }
- /* print two hop list */
- System.out.println("two hop list...........................................");
- e = this_time.two_hop_list.keys() ;
- while (e.hasMoreElements()) {
- int k = Integer.parseInt((String)(e.nextElement()));
- String s = (String)(this_time.two_hop_list.get(Integer.toString(k)));
- Point p = stringToPoint(s);
- System.out.println("node: " + id + " key: " + k + " value: " + p.toString() );
- }
- }
- /** Convert string to point */
- public Point stringToPoint(String s) {
- String temp[] = s.split(",");
- int x = Integer.parseInt(temp[0]);
- int y = Integer.parseInt(temp[1]);
- Point p = new Point(x,y);
- return p;
- }
- /** Convert point to string */
- public String pointToString(Point p) {
- String s = "";
- s += p.x;
- s += ",";
- s += p.y;
- return s;
- }
- /** Test if a point satisfies the filtering condition */
- /* 1: absolutely meet, 0: meet after relaxing by "fdelta", -1: Not meet */
- public int meetCondition(Point p) {
- Enumeration e1 = this_time.one_hop_list.keys();
- Enumeration e2 = this_time.two_hop_list.keys();
- String k, s;
- Point pt;
- int retval = 1;
- while (e1.hasMoreElements()) {
- k = (String)e1.nextElement();
- s = (String)this_time.one_hop_list.get(k);
- pt = stringToPoint(s);
- if (pt.distance(p) >= Network.seed_r + Network.delta)
- return (-1);
- else if (pt.distance(p) >= Network.seed_r) {
- retval = 0;
- }
- }
- while (e2.hasMoreElements()) {
- k = (String)e2.nextElement();
- s = (String)this_time.two_hop_list.get(k);
- pt = stringToPoint(s);
- if ( (pt.distance(p) < Network.seed_r - Network.delta) || (pt.distance(p) >= Network.seed_r + r + Network.delta))
- return (-1);
- else if ( (pt.distance(p) < Network.seed_r) || (pt.distance(p) >= Network.seed_r + r)) {
- retval = 0;
- }
- }
- return retval;
- }
- /** get samples from a circle centered at p with radius */
- public Point getSample(Point p, int radius) {
- Point sample_p;
- int x, y;
- for (; ;) {
- x = p.x - radius + (int)Math.round(2 * radius * Math.random());
- y = p.y - radius + (int)Math.round(2 * radius * Math.random());
- sample_p = new Point(x, y);
- if (sample_p.distance(p) < radius)
- return sample_p;
- }
- }
- /** random waypoint mobility model */
- public void random_waypoint() {
- int x, y;
- float v, d, dx, dy;
- if (dst == null) {
- x = (int)Math.round(Network.x_range * Math.random());
- y = (int)Math.round(Network.y_range * Math.random());
- dst = new Point(x, y);
- }
- d = (float)p.distance(dst);
- dx = dst.x - p.x;
- dy = dst.y - p.y;
- v = Network.max_v * (float)Math.random();
- if (v < d) {
- x = p.x + (int)Math.round(v * dx / d);
- y = p.y + (int)Math.round(v * dy / d);
- p.setLocation(x, y);
- }
- else {
- p.setLocation(dst);
- dst = null;
- }
- }
- /** group waypoint mobility model */
- public void group_waypoint(double v, double a, Point ref) {
- int x, y, group_x, group_y;
- double way_v;
- if (dst_length < 0) {
- x = (int)Math.round(Network.x_range * Math.random());
- y = (int)Math.round(Network.y_range * Math.random());
- dst = new Point(ref.x + x, ref.y + y);
- dst_length = p.distance(dst);
- dst_direction = Math.acos((dst.x- p.x) / dst_length);
- if (dst.y < p.y)
- dst_direction = 2 * Math.PI - dst_direction;
- }
- group_x = (int)Math.round(v * Math.cos(a));
- group_y = (int)Math.round(v * Math.sin(a));
- way_v = (Network.max_v - Network.group_v) * Math.random();
- if (dst_length <= way_v) {
- way_v = dst_length;
- dst_length = -1;
- }
- else
- dst_length -= way_v;
- x = p.x + (int)Math.round(way_v * Math.cos(dst_direction)) + group_x;
- y = p.y + (int)Math.round(way_v * Math.sin(dst_direction)) + group_y;
- p.setLocation(x, y);
- }
- public void logToFile()
- throws FileNotFoundException, IOException {
- }
- }