00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.util.*;
00036 import java.lang.Math;
00037
00038
00039 public class Network implements java.io.Serializable
00040 {
00041 public Vector<NodeGroup> groups;
00042 public Vector<Node> nodes;
00043 protected int field_width;
00044 protected boolean random;
00045 protected String identifier;
00046 transient static final int group_threshold = 5;
00047 transient static final int node_threshold = 40;
00048 transient static final int sensor_threshold = 1;
00049
00050 transient static int TOTAL_NODES = 0;
00051
00052 public Network() {
00053 TOTAL_NODES = 0;
00054 field_width = 600;
00055 groups = new Vector<NodeGroup>();
00056 nodes = new Vector<Node>();
00057 random = false;
00058 identifier = "";
00059 }
00060
00061 public Network(Network n) {
00062 this();
00063 for (int i=0; i < n.getGroups().size(); i++) {
00064 NodeGroup curr_grp =
00065 new NodeGroup((NodeGroup)n.getGroups().elementAt(i));
00066 for (int j=0; j < curr_grp.getNodes().size(); j++)
00067 nodes.addElement((Node)curr_grp.getNodes().elementAt(j));
00068 groups.addElement(curr_grp);
00069 }
00070
00071 TOTAL_NODES = nodes.size();;
00072 identifier = n.getIdentifier();
00073
00074 }
00075
00076 public int getField_width() {
00077 return field_width;
00078 }
00079
00080 public Vector<NodeGroup> getGroups() {
00081 return groups;
00082 }
00083
00084 public Vector<Node> getNodes() {
00085 return nodes;
00086 }
00087
00088 public boolean getRandom() {
00089 return random;
00090 }
00091
00092 public String getIdentifier() {
00093 return identifier;
00094 }
00095
00096 public void setField_width(int i) {
00097 field_width = i;
00098 }
00099
00100 public void setGroups(Vector<NodeGroup> v) {
00101 groups = v;
00102 }
00103
00104 public void setNodes(Vector<Node> v) {
00105 nodes = v;
00106 }
00107
00108 public void setRandom(boolean b) {
00109 random = b;
00110 Random seed = SenseGui.global_rand;
00111 Random r = new Random(seed.nextLong());
00112
00113 int num_groups = r.nextInt() % group_threshold;
00114 Vector<NodeGroup> g = getGroups();
00115 int total_num_nodes = r.nextInt() % node_threshold;
00116 Vector<Node> n = getNodes();
00117 int nodes_left = total_num_nodes;
00118
00119 for (int g_i=0; g_i<num_groups; g_i++) {
00120 NodeGroup ng = new NodeGroup();
00121
00122 int num_sensors = r.nextInt() % sensor_threshold;
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 int num_nodes = r.nextInt() % (nodes_left -
00134 (2 * (num_groups - g_i)));
00135 Vector<Node> g_n = ng.getNodes();
00136 for (int g_n_i=0; g_n_i<num_nodes; g_n_i++) {
00137 Node node = new Node();
00138 node.setRand_loc(true);
00139 node.setMygroup(ng);
00140 g_n.addElement(node);
00141 n.addElement(node);
00142 }
00143 g.addElement(ng);
00144 }
00145 }
00146
00147 public void setIdentifier(String s) {
00148 identifier = s;
00149 }
00150
00151
00152 public Node findNode(int node_id) {
00153 for (int i=0; i<nodes.size(); i++) {
00154 Node n = (Node)nodes.elementAt(i);
00155 if (n.getId() == node_id)
00156 return n;
00157 }
00158 return null;
00159 }
00160
00161
00163 public void initConnectivity() {
00164 System.err.println("Initializing network connectivity.");
00165
00166 for (int i=0; i<nodes.size(); i++) {
00167 Node current = (Node)nodes.elementAt(i);
00168 for (int j=i+1; j<nodes.size(); j++) {
00169 Node peer = (Node)nodes.elementAt(j);
00170 if (peer != null) {
00171 double dist = Node.distance(current, peer);
00172 }
00173 }
00174 }
00175 }
00176
00177
00178 public NodeGroup getGroup(int i) {
00179 return (NodeGroup)(groups.elementAt(i));
00180 }
00181
00182 public Node getNode(int i) {
00183 return (Node)(nodes.elementAt(i));
00184 }
00185
00186 public String toString() {
00187 StringBuffer s = new StringBuffer();
00188 s.append(nodes.size() + " nodes in " + groups.size() + " groups.\n");
00189 s.append("\n NODES:\n");
00190 for (int i=0; i<nodes.size(); i++)
00191 s.append(" " + ((Node)nodes.elementAt(i)).toString());
00192 s.append("\n GROUPS:\n");
00193 for (int i=0; i<groups.size(); i++)
00194 s.append(" " + ((NodeGroup)groups.elementAt(i)).toString());
00195 return s.toString();
00196 }
00197
00198
00200 private class SortNode
00201 {
00202 int num_adj;
00203 Node node;
00204 Vector<Node> adj;
00205 int id;
00206
00207 SortNode() {
00208 adj = new Vector<Node>();
00209 num_adj = 0;
00210 }
00211 }
00212
00213
00214 private interface Compare
00215 {
00216 public int compare(Object a, Object b);
00217
00218 public class StringCompare implements Compare
00219 {
00220 public final int compare (Object a, Object b) {
00221 return ((String) a).compareTo((String) b);
00222 }
00223 }
00224
00225 public class IntegerCompare implements Compare
00226 {
00227 public final int compare (Object a, Object b) {
00228 return ((Integer) a).intValue() - ((Integer) b).intValue();
00229 }
00230 }
00231
00232 public class SortNodeCompare implements Compare
00233 {
00234 public final int compare (Object a, Object b) {
00235 if (((SortNode)a).num_adj < ((SortNode)b).num_adj)
00236 return -1;
00237 else if (((SortNode)a).num_adj > ((SortNode)b).num_adj)
00238 return 1;
00239 else {
00240 if (((SortNode)a).id < ((SortNode)b).id)
00241 return -1;
00242 else if (((SortNode)a).id > ((SortNode)b).id)
00243 return 1;
00244 else
00245 return 0;
00246 }
00247 }
00248 }
00249 }
00250
00251 private class QuickSort
00252 {
00253 private Compare delegate;
00254 private Object [] userArray;
00255
00256 private void quicksort(int p, int r) {
00257 if (p < r) {
00258 int q = partition(p, r);
00259 if (q == r)
00260 q--;
00261 quicksort(p, q);
00262 quicksort(q+1, r);
00263 }
00264 }
00265
00266 private int partition(int lo, int hi) {
00267 Object pivot = userArray[lo];
00268 while (true) {
00269 while (delegate.compare(userArray[hi],pivot) >= 0 && lo < hi)
00270 hi--;
00271 while (delegate.compare(userArray[lo],pivot) < 0 && lo < hi)
00272 lo++;
00273 if (lo < hi) {
00274 Object T = userArray[lo];
00275 userArray[lo] = userArray[hi];
00276 userArray[hi] = T;
00277 } else return hi;
00278 }
00279 }
00280
00281 private boolean isAlreadySorted() {
00282 for (int i=1; i<userArray.length; i++)
00283 if (delegate.compare(userArray[i], userArray[i-1]) < 0)
00284 return false;
00285 return true;
00286 }
00287 }
00288
00289
00290
00291 public void partitionNet(int k) {
00292 int q = (int)Math.ceil((double)nodes.size() / (double)k);
00293
00294 SortNode[] sorted = new SortNode[nodes.size()];
00295 for (int s=0; s<nodes.size(); s++) {
00296 sorted[s] = new SortNode();
00297 }
00298 QuickSort h = new QuickSort();
00299 h.delegate = new Compare.SortNodeCompare();
00300 h.userArray = sorted;
00301 if(!h.isAlreadySorted())
00302 h.quicksort(0, sorted.length-1);
00303
00304 int index = 0;
00305 for (int i=0; i<k && index<nodes.size(); i++) {
00306 int p = 0;
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 }
00321 }
00322 }