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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
package structure;
import java.util.*;
import java.util.Map.Entry;
public class Grid {
public enum Protocol {
AODV, DSDV, CUSTOM
}
private ArrayList<Router> routers=new ArrayList<>();
private ArrayList<ArrayList<Integer>> links=new ArrayList<>();
private int[] pMoy={50,59,92,50,4,8,6,13,7,1,51,6};
private int bestLink;
private Protocol protocol;
private int counterCUSTOM=5;
private Random rand = new Random();
private final int maxWeight=100;
/**
* Build a 3x3 Grid
*/
public Grid(Protocol protocol){
// Build router liste
for(int i=0;i<9;i++){
this.routers.add(new Router());
}
this.buildEdgeWithRandomWeigth();
//Build fixed link
this.buildPath();
this.protocol=protocol;
switch(protocol){
case AODV:
this.bestLink=this.getBestLinkIndex();
break;
case DSDV:
case CUSTOM:
HashMap<Integer,Integer> currentBestLink=new HashMap<>();
for(int i=0;i<1000;i++){
int current=this.getBestLinkIndex();
if(currentBestLink.containsKey(current)){
currentBestLink.put(current, currentBestLink.get(current)+1);
}
else{
currentBestLink.put(current, 1);
}
this.buildEdgeWithRandomWeigth();
}
Set<Integer> entryTMP = currentBestLink.keySet();
int max=currentBestLink.get(entryTMP.iterator().next());
int maxId=0;
entryTMP = currentBestLink.keySet();
Iterator<Integer> it=entryTMP.iterator();
while(it.hasNext()) {
int entryId=it.next();
int entry=currentBestLink.get(entryId);
if(entry> max){
max=entry;
maxId=entryId;
}
System.out.println("Id : "+ entryId + " max "+ entry);
}
this.bestLink=maxId;
System.out.println("Retenu :"+maxId);
break;
}
}
public void buildEdgeWithRandomWeigth(){
// First line
this.buildLinkWithRandomWeight(routers.get(0), routers.get(1), 1);
this.buildLinkWithRandomWeight(routers.get(1), routers.get(2),1);
// Second line
this.buildLinkWithRandomWeight(routers.get(3), routers.get(4),69);
this.buildLinkWithRandomWeight(routers.get(4), routers.get(5),20);
// Third line
this.buildLinkWithRandomWeight(routers.get(6), routers.get(7),23);
this.buildLinkWithRandomWeight(routers.get(7), routers.get(8),54);
// First column
this.buildLinkWithRandomWeight(routers.get(0), routers.get(3),14);
this.buildLinkWithRandomWeight(routers.get(3), routers.get(6),11);
// Second column
this.buildLinkWithRandomWeight(routers.get(1), routers.get(4),33);
this.buildLinkWithRandomWeight(routers.get(4), routers.get(7),22);
// Third column
this.buildLinkWithRandomWeight(routers.get(2), routers.get(5),11);
this.buildLinkWithRandomWeight(routers.get(5), routers.get(8),47);
}
private void buildPath(){
// Link1
ArrayList<Integer> link1=new ArrayList<>();
link1.add(0);link1.add(1);link1.add(2);link1.add(5);link1.add(4);link1.add(3);link1.add(6);link1.add(7);link1.add(8);
this.links.add(link1);
// Link2
ArrayList<Integer> link2=new ArrayList<>();
link2.add(0);link2.add(1);link2.add(2);link2.add(5);link2.add(4);link2.add(7);link2.add(8);
this.links.add(link2);
// link3
ArrayList<Integer> link3=new ArrayList<>();
link3.add(0);link3.add(1);link3.add(2);link3.add(5);link3.add(8);
this.links.add(link3);
// link4
ArrayList<Integer> link4=new ArrayList<>();
link4.add(0);link4.add(1);link4.add(4);link4.add(5);link4.add(8);
this.links.add(link4);
// link5
ArrayList<Integer> link5=new ArrayList<>();
link5.add(0);link5.add(3);link5.add(4);link5.add(5);link5.add(8);
this.links.add(link5);
// link6
ArrayList<Integer> link6=new ArrayList<>();
link6.add(0);link6.add(3);link6.add(4);link6.add(7);link6.add(8);
this.links.add(link6);
// link7
ArrayList<Integer> link7=new ArrayList<>();
link7.add(0);link7.add(3);link7.add(6);link7.add(7);link7.add(8);
this.links.add(link7);
// link8
ArrayList<Integer> link8=new ArrayList<>();
link8.add(0);link8.add(3);link8.add(6);link8.add(7);link8.add(4);link8.add(5);link8.add(8);
this.links.add(link8);
// link9
ArrayList<Integer> link9=new ArrayList<>();
link9.add(0);link9.add(3);link9.add(6);link9.add(7);link9.add(4);link9.add(1);link9.add(2);link9.add(5);link9.add(8);
this.links.add(link9);
// link10
ArrayList<Integer> link10=new ArrayList<>();
link10.add(0);link10.add(3);link10.add(4);link10.add(1);link10.add(2);link10.add(5);link10.add(8);
this.links.add(link10);
// link11
ArrayList<Integer> link11=new ArrayList<>();
link11.add(0);link11.add(1);link11.add(4);link11.add(3);link11.add(6);link11.add(7);link11.add(8);
this.links.add(link11);
}
private void buildLinkWithRandomWeight(Router router1, Router router2, int pMoy){
router1.buildLink(router2, rand.nextInt(pMoy));
}
public int getBestLinkIndex(){
int currentBestLink=0;
int currentBestLinkBottleneck=0;
for(int i=0;i<this.links.size();i++){
ArrayList<Integer> currentLink=this.links.get(i);
int currentLinkBottleneck=this.getMaxBottleneck(currentLink);
if(currentBestLinkBottleneck<currentLinkBottleneck){
currentBestLink=i;
currentBestLinkBottleneck=currentLinkBottleneck;
}
}
return currentBestLink;
}
private int getMaxBottleneck(ArrayList<Integer> link){
int max=this.getWeigthOfLink(link.get(0), link.get(1));
for(int j=1;j<link.size()-1;j++){
int currentMax=this.getWeigthOfLink(link.get(j), link.get(j+1));
if(max>currentMax){
max=currentMax;
}
}
return max;
}
private int getWeigthOfLink(int router1,int router2){
return this.routers.get(router1).getWeight(this.routers.get(router2));
}
public void printLinkWeight(){
for(int i=0;i<this.links.size();i++){
ArrayList<Integer> link=this.links.get(i);
System.out.print("Link number " + i + " ==> ");
for(int j=0;j<link.size()-1;j++){
System.out.print(this.getWeigthOfLink(link.get(j), link.get(j+1)) + " ");
}
System.out.println(" Goulot :"+this.getMaxBottleneck(link));
//System.out.println();
}
}
public boolean isEdgeOfLink(ArrayList<Integer>link, Router src, Router dest){
for(int j=0;j<link.size()-1;j++){
Router current=this.routers.get(link.get(j));
if(src.name.equals(current.name)){
if(j<link.size()-1){
Router currentDest=this.routers.get(link.get(j+1));
if(currentDest.name.equals(dest.name)){
return true;
}
}
}
}
for(int j=0;j<link.size()-1;j++){
Router current=this.routers.get(link.get(j));
if(dest.name.equals(current.name)){
if(j<link.size()-1){
Router currentDest=this.routers.get(link.get(j+1));
if(currentDest.name.equals(src.name)){
return true;
}
}
}
}
return false;
}
public ArrayList<Router> getGrid() {
return routers;
}
public void setGrid(ArrayList<Router> grid) {
this.routers = grid;
}
public ArrayList<ArrayList<Integer>> getLinks() {
return links;
}
public void setLinks(ArrayList<ArrayList<Integer>> links) {
this.links = links;
}
/**
* @return the bestLinkByProtocol
*/
public int getBestLinkByProtocol() {
if(this.protocol==Protocol.CUSTOM){
this.counterCUSTOM--;
if(this.counterCUSTOM==0){
this.bestLink=this.getBestLinkIndex();
this.counterCUSTOM=5;
}
}
return bestLink;
}
}
|