Network.java
资源名称:mcl.tar.gz [点击查看]
上传用户:kingseaxu
上传日期:2009-01-01
资源大小:49k
文件大小:15k
源码类别:
3G开发
开发平台:
Java
- import java.util.*;
- import java.awt.Point;
- import java.io.*;
- class Network {
- static double x_range; /* the maximum x coordinate of network */
- static double y_range; /* the maximum y coordinate of network */
- static int node_num; /* number of nodes */
- static int seed_num; /* number of seed node, 0..seed_num-1 are seeds, seed_num..node_num-1 are other nodes */
- static int min_v; /* minimum velocity */
- static int max_v; /* maximum velocity */
- static int group_v; /* maximum group velocity */
- static int node_r; /* transmission range of node */
- static int seed_r; /* transmission range of seed */
- static double doi; /* radio range error */
- static double delta; /* possible error of sample points */
- Vector[] neighbors; /* neighbor lists of nodes */
- Point[] seed_pos; /* the positions of seeds */
- int relation[][]; /* relationship of nodes to seeds (within one hop or two hops) */
- double[][] path_length; /* estimated distance from nodes to seeds, for MIT algorithm only */
- Node[] node; /* nodes in the network */
- double avg_MIT_error; /* average amorphous computing estimate error */
- double avg_USC_error; /* average centroid estimate error */
- double avg_MCL_error; /* average monte calo estimate error */
- Point group_ref; /* accumulated offset of group motion */
- public Network(Input in) {
- int i;
- x_range = Double.parseDouble(in.getParameter("x_range"));
- y_range = Double.parseDouble(in.getParameter("y_range"));
- node_num = Integer.parseInt(in.getParameter("node_num"));
- seed_num = Integer.parseInt(in.getParameter("seed_num"));
- node_r = Integer.parseInt(in.getParameter("node_r"));
- seed_r = Integer.parseInt(in.getParameter("seed_r"));
- min_v = Integer.parseInt(in.getParameter("min_v"));
- max_v = Integer.parseInt(in.getParameter("max_v"));
- group_v = Integer.parseInt(in.getParameter("group_v"));
- delta = Integer.parseInt(in.getParameter("delta"));
- doi = Integer.parseInt(in.getParameter("doi"));
- avg_MIT_error = 0;
- avg_USC_error = 0;
- avg_MCL_error = 0;
- node = new Node[node_num];
- for (i = 0; i < seed_num; i++)
- node[i] = new Node(i, seed_r, true);
- for (i = seed_num; i < node_num; i++)
- node[i] = new Node(i, node_r, false);
- neighbors = new Vector[node_num];
- for (i = 0; i < node_num; i++)
- neighbors[i] = new Vector();
- path_length = new double[node_num][seed_num];
- relation = new int[node_num][seed_num];
- seed_pos = new Point[seed_num];
- group_ref = new Point(0, 0);
- }
- /** update locations and corresponding information of all nodes */
- public void updateLocation() {
- for (int i = 0; i < seed_num; i++)
- seed_pos[i] = new Point(node[i].p);
- Establish_NeighborList();
- Establish_Relation();
- Establish_Path(false);
- }
- /** update locations and corresponding information of nodes from start to end */
- public void updateLocation(int start, int end) {
- for (int i = 0; i < seed_num; i++)
- seed_pos[i] = new Point(node[i].p);
- Establish_NeighborList(start, end);
- Establish_Relation(start, end);
- }
- /** establish neighbor list of nodes */
- public void Establish_NeighborList() {
- int i, j;
- for (i = 0; i < node_num; i++)
- neighbors[i].clear();
- for (i = 0; i < node_num; i++) {
- for (j = 0; j < seed_num; j++)
- if ( (i != j) && CanHear(node[i].p.distance(node[j].p), seed_r))
- neighbors[i].add(new Integer(j));
- for (j = seed_num; j < node_num; j++)
- if ( (i != j) && CanHear(node[i].p.distance(node[j].p), node_r))
- neighbors[i].add(new Integer(j));
- }
- }
- public void Establish_NeighborList(int start, int end) {
- int i, j;
- for (i = 0; i < seed_num; i++) {
- neighbors[i].clear();
- for (j = 0; j < node_num; j++) {
- if ( (i != j) && CanHear(node[i].p.distance(node[j].p), seed_r))
- neighbors[i].add(new Integer(j));
- boolean can_hear = CanHear(node[i].p.distance(node[j].p), seed_r);
- if ( (i != j) && can_hear && !neighbors[j].contains(new Integer(i)))
- neighbors[j].add(new Integer(i));
- if ( (i != j) && !can_hear && neighbors[j].contains(new Integer(i)))
- neighbors[j].remove(new Integer(i));
- }
- for (j = seed_num; j < node_num; j++) {
- if ( (i != j) && CanHear(node[i].p.distance(node[j].p), node_r))
- neighbors[i].add(new Integer(j));
- boolean can_hear = CanHear(node[i].p.distance(node[j].p), node_r);
- if ( (i != j) && can_hear && !neighbors[j].contains(new Integer(i)))
- neighbors[j].add(new Integer(i));
- if ( (i != j) && !can_hear && neighbors[j].contains(new Integer(i)))
- neighbors[j].remove(new Integer(i));
- }
- }
- }
- /** Establish relations of nodes to seeds (direct or indirect seeds) */
- public void Establish_Relation() {
- int i, j, id;
- /* 0: no relation; 1: within one hop, 2: within two hops */
- for (i = seed_num; i < node_num; i++)
- for (j = 0; j < seed_num; j++) {
- relation[i][j] = 0;
- if (neighbors[i].contains(new Integer(j))) {
- relation[i][j] = 1;
- for (int k = 0; k < neighbors[i].size(); k++) {
- id = ((Integer)neighbors[i].get(k)).intValue();
- if ( (id >= seed_num) && (relation[id][j] == 0))
- relation[id][j] = 2;
- }
- }
- }
- }
- public void Establish_Relation(int start, int end) {
- int i, j, id;
- /* 0: no relation; 1: within one hop, 2: within two hops */
- for (i = start; i < end; i++)
- for (j = 0; j < seed_num; j++) {
- relation[i][j] = 0;
- if (neighbors[i].contains(new Integer(j))) {
- relation[i][j] = 1;
- for (int k = 0; k < neighbors[i].size(); k++) {
- id = ((Integer)neighbors[i].get(k)).intValue();
- if ( (id >= seed_num) && !neighbors[id].contains(new Integer(j)))
- relation[id][j] = 2;
- }
- }
- }
- }
- /* Adapted from Tian's APIT simulation code */
- /** Establish path length for amorphous computing */
- void Establish_Path(boolean SMOOTH ) {
- int i, j, k, id;
- LinkedList queue = new LinkedList();
- for (i = 0; i < node_num; i++)
- for (j = 0; j < seed_num; j++) {
- if (i == j)
- path_length[i][j] = 0;
- else
- path_length[i][j] = -1;
- }
- /* use breadth first search to find shortest path */
- for (i = 0; i < seed_num; i++) {
- queue.add(new Integer(i));
- while (queue.size() > 0) {
- j = ((Integer)queue.getFirst()).intValue();
- queue.removeFirst();
- for (k = 0; k < neighbors[j].size(); k++) {
- id = ((Integer)neighbors[j].get(k)).intValue();
- if (path_length[id][i] < 0) {
- path_length[id][i] = path_length[j][i] + 1;
- queue.addLast(new Integer(id));
- }
- }
- }
- }
- if (SMOOTH) {
- int num[] = new int[seed_num];
- for (i = 0; i < node_num; i++) {
- for (k = 0; k < seed_num; k++)
- num[k] = 1;
- for (j = 0; j < neighbors[i].size(); j++) {
- id = ((Integer)neighbors[i].get(j)).intValue();
- for (k = 0; k < seed_num; k++) {
- if (path_length[i][k] < 0)
- continue;
- if (path_length[j][k] >= 0) {
- path_length[i][k] += path_length[j][k];
- num[k]++;
- }
- }
- }
- for (k = 0; k < seed_num; k++)
- path_length[i][k] = (float)(path_length[i][k] / num[k]) - (float)0.5;
- }
- }
- /* estimate real distance */
- double dhop = node_r * KleinrockFomula();
- for (i = 0; i < node_num; i++)
- for (j = 0; j < seed_num; j++)
- if (path_length[i][j] > 0)
- path_length[i][j] *= dhop;
- }
- /* Adapted from Tian's APIT simulation code */
- /** decide if two nodes are neighbors */
- public boolean CanHear(double Distance,double RadioRange) {
- if ( Distance < (1 + doi * (2 * Math.random() - 1)) * RadioRange)
- return true;
- else
- return false;
- }
- /* Adapted from Tian's APIT simulation code */
- /** amorphous computing formular */
- public double IntegralPart(double t, double AvgNumberOfNeighbors) {
- double PI = 3.14159265;
- return Math.exp( -(AvgNumberOfNeighbors / PI) * (Math.acos(t) - t * Math.sqrt(1 - t * t)));
- }
- /* Adapted from Tian's APIT simulation code */
- /** amorphous computing formular */
- public double KleinrockFomula(){
- double PI = 3.14159265;
- int STEP_NUMBER = 10000;
- int i;
- double AvgNumberOfNeighbors;
- double sum = 0;
- double lowerbound = -1.0;
- double upperbound = 1.0;
- double step = (upperbound- lowerbound)/STEP_NUMBER;
- AvgNumberOfNeighbors = PI * node_num * node_r * node_r / x_range / y_range;
- for( i = 0; i < STEP_NUMBER - 2 ; i = i + 2){
- sum += ( IntegralPart(lowerbound + i * step, AvgNumberOfNeighbors) +
- 4 * IntegralPart(lowerbound + (i + 1) * step, AvgNumberOfNeighbors) +
- IntegralPart(lowerbound + (i + 2) * step, AvgNumberOfNeighbors)
- ) * step / 3;
- }
- return 1 + Math.exp(-AvgNumberOfNeighbors) - sum;
- }
- /** statistics of estimate error */
- public double statistics(int TYPE, int id, int step) {
- /* TYPE: 1-->MCL, 2-->Centroid, 3-->Amorphous Computing */
- double es_error = node[id].p.distance(node[id].this_time.es_p);
- return es_error;
- }
- /** log data to filename */
- public static void logToFile(String filename, String data)
- throws FileNotFoundException, IOException {
- BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(filename, true));
- os.write(data.getBytes(), 0, data.length());
- os.close();
- }
- public static void main(String[] argv) {
- Network net;
- int start; /* start of simulated node */
- int end; /* end of simulated node */
- int stable_step = 50; /* stablized step */
- int step_num = 80; /* totoal number of steps */
- int iteration_num = 10; /* number of iterations */
- int maxv; /* maximum node speed */
- double MCL_error, MIT_error, USC_error;
- double final_MCL, final_MIT, final_USC;
- Input in = new Input("Network.prop");
- for (maxv = 10; maxv <= 50; maxv += 10) {
- final_MCL = 0;
- final_MIT = 0;
- final_USC = 0;
- for (int iteration = 0; iteration < iteration_num; iteration++) {
- System.out.println("maximum speed is " + maxv + ", in iteration " + iteration + "......");
- in.setParameter("max_v", Integer.toString(maxv));
- net = new Network(in);
- start = net.seed_num;
- end = net.seed_num + 32;
- for (int step = 0; step < step_num; step++) {
- System.out.println("in iteration " + iteration + ", in Step " + step + "......");
- net.updateLocation();
- MCL_error = 0;
- MIT_error = 0;
- USC_error = 0;
- for (int i = start; i < end; i++) {
- net.node[i].MCLocalization(net.relation[i], net.seed_pos, net.group_ref);
- MCL_error += net.statistics(1, i, step) / (end - start);
- net.node[i].Centroid_Localization(net.neighbors[i], net.seed_pos);
- USC_error += net.statistics(2, i, step) / (end - start);
- net.node[i].Multilateration(net.seed_pos, net.path_length[i]);
- MIT_error += net.statistics(3, i, step) / (end - start);
- }
- for (int i = 0; i < Network.node_num; i++)
- net.node[i].random_waypoint();
- System.out.println("In step " + step + ", in MCL algorithm, the average estimation error is: " + MCL_error);
- System.out.println("In step " + step + ", in USC algorithm, the average estimation error is: " + USC_error);
- System.out.println("In step " + step + ", in MIT algorithm, the average estimation error is: " + MIT_error);
- System.out.println("");
- System.out.println("");
- System.out.println("");
- if (step >= stable_step) {
- net.avg_MCL_error += MCL_error / (step_num - stable_step);
- net.avg_USC_error += USC_error / (step_num - stable_step);
- net.avg_MIT_error += MIT_error / (step_num - stable_step);
- }
- }
- System.out.println("In MCL algorithm, the average estimation error is: " + net.avg_MCL_error);
- System.out.println("In USC algorithm, the average estimation error is: " + net.avg_USC_error);
- System.out.println("In MIT algorithm, the average estimation error is: " + net.avg_MIT_error);
- System.out.println("");
- final_MCL += net.avg_MCL_error / iteration_num;
- final_USC += net.avg_USC_error / iteration_num;
- final_MIT += net.avg_MIT_error / iteration_num;
- }
- System.out.println(" For maximum speed = " + maxv + ", in MCL algorithm is: " + final_MCL);
- System.out.println(" For maximum speed = " + maxv + ", in USC algorithm is: " + final_USC);
- System.out.println(" For maximum speed = " + maxv + ", in MIT algorithm is: " + final_MIT);
- System.out.println("");
- String MCL_data = Double.toString((double)maxv / Network.seed_r);
- String USC_data = Double.toString((double)maxv / Network.seed_r);
- String MIT_data = Double.toString((double)maxv / Network.seed_r);
- MCL_data += "t";
- USC_data += "t";
- MIT_data += "t";
- MCL_data += Double.toString(final_MCL / Network.seed_r);
- USC_data += Double.toString(final_USC / Network.seed_r);
- MIT_data += Double.toString(final_MIT / Network.seed_r);
- MCL_data += "rn";
- USC_data += "rn";
- MIT_data += "rn";
- try {
- Network.logToFile("MCL_max_speed_error", MCL_data);
- Network.logToFile("USC_max_speed_error", USC_data);
- Network.logToFile("MIT_max_speed_error", MIT_data);
- } catch (Exception e) {System.err.print(e);}
- }
- }
- }