/
SimulatorEngine.java
194 lines (165 loc) · 5.03 KB
/
SimulatorEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package edu.berkeley.sixtus.simul;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
/**
*
* @author xvilajosana
*
*/
public class SimulatorEngine {
public static final int MAX_NET_SIZE = 50;
public static final int MAX_NUM_NEIGHBORS = 10;
public static final int MIN_NUM_NEIGHBORS = 2;
public static final int MAX_TIME_SLOT = 101;
public static final int MAX_CH_OFFSET = 16;
public boolean[][] network = new boolean[MAX_NET_SIZE][MAX_NET_SIZE];// matrix matching neighbours
public Node[] nodes = new Node[MAX_NET_SIZE]; //the nodes
private int num_links_requested;
public SimulatorEngine() {
super();
}
/**
* requires the number of links to be allocated to each neigbour
*
* @param links
*/
public void simulate(int links) {
Random ran = new Random();
this.num_links_requested = links;
initializeNetwork();
// create the network randomly.
createNetworkTopology(ran);
// compute how many neighbors has each node and update each neighbor with that info
computeNumNeighbors();
// for each node in the network request several links to all neighbours
for (int numlink = 1; numlink < this.num_links_requested; numlink++) {
System.out.println("************************ requesting " + numlink + " links");
allocateLinks(ran, numlink);
printResult(numlink);
// reset the nodes: for the next iteration
resetNodes();
// compute how many neighbors has each node and update each neighbor with that info
computeNumNeighbors();
}
}
private void allocateLinks(Random ran, int l) {
for (int k = 0; k < l; k++) {
for (int i = 0; i < MAX_NET_SIZE; i++) {
for (int j = 0; j < MAX_NET_SIZE; j++) {
if (network[i][j]) {
// these are neighbors
int slotNumber = ran.nextInt(MAX_TIME_SLOT);// pick random ts and ch.offset
int channelOffset = ran.nextInt(MAX_CH_OFFSET);
//TODO, check result of the operation and if false then do not reschedule or reset schedule
if (nodes[i].isLinkAvailable(slotNumber,channelOffset,Cell.SlotType.TX, j) &&
nodes[j].isLinkAvailable(slotNumber,channelOffset,Cell.SlotType.TX, j)){
nodes[i].scheduleLink(slotNumber, channelOffset,Cell.SlotType.TX, j);
nodes[j].scheduleLink(slotNumber, channelOffset,Cell.SlotType.RX, i);
}
}
}
}
}
}
private void createNetworkTopology(Random ran) {
for (int i = 0; i < MAX_NET_SIZE; i++) {
// select number of neighbors for that node
int num_nei = ran.nextInt(MAX_NUM_NEIGHBORS);
if (num_nei < MIN_NUM_NEIGHBORS) {
num_nei = MIN_NUM_NEIGHBORS;
}
// set them in the network structure. pick them randomly too.
for (int j = 0; j < num_nei; j++) {
int h = ran.nextInt(MAX_NET_SIZE);
while (h == i) {
h = ran.nextInt(MAX_NET_SIZE);// we don't want to say that
// we are neighbours of
// ourselves
}
network[i][h] = true;// set it as the neighbour
network[h][i] = true;// set it as the neighbour
}
}
}
private void initializeNetwork() {
for (int i = 0; i < MAX_NET_SIZE; i++) {
for (int j = 0; j < MAX_NET_SIZE; j++) {
network[i][j] = false; // initialize network
}
}
resetNodes();
}
private void resetNodes() {
for (int i = 0; i < MAX_NET_SIZE; i++) {
nodes[i] = new Node(i);
}
}
private void printResult(int numlink) {
String head=null;
if (numlink==1){
head = "Node,NumLinksRequested,NumNeighbors,TotalLinks,Allocated Links,Collisions,IdenticaAllocation,% Collision,% Used Links";
System.out.println(head);
}
for (int i = 0; i < MAX_NET_SIZE; i++) {
String content = i
+ ","
+ numlink
+ ","
+ nodes[i].getNumNeighbors()
+ ","
+ (MAX_TIME_SLOT * MAX_CH_OFFSET)
+ ","
+ nodes[i].getNumAllocated()
+ ","
+ nodes[i].getNumCollisions()
+ ","
+ nodes[i].getNumIdenticallyAllocated()
+ ","
+ ((((double) nodes[i].getNumCollisions()) / ((double) nodes[i].getNumAllocated())) * 100.0)
+ ","
+ (((double) nodes[i].getNumAllocated() / (MAX_TIME_SLOT * MAX_CH_OFFSET)) * 100.0);
System.out.println(content);
this.writeToFile("results" + i,head, content);
}
}
public void computeNumNeighbors() {
for (int i = 0; i < MAX_NET_SIZE; i++) {
int count = 0;
for (int j = 0; j < MAX_NET_SIZE; j++) {
if (network[i][j])
count++;
}
nodes[i].setNumNeighbors(count);
}
}
public void writeToFile(String filename,String header, String content) {
FileOutputStream fop = null;
File file;
try {
file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
fop = new FileOutputStream(file, true);
if (header!=null) content = header + '\n' + content;
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.write('\n');
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}