source: proiecte/ptvs/src/vnsim/socialNet/Mobility.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 15.3 KB
Line 
1package vnsim.socialNet;
2
3import java.util.ArrayList;
4import java.util.Random;
5
6import vnsim.vehicular.routePlan.RouteComputingUtils;
7import vnsim.vehicular.simulator.Location;
8import vnsim.vehicular.simulator.RouteSegment;
9import vnsim.vehicular.scenarios.Route;
10
11public class Mobility {
12        public ArrayList<ArrayList<Integer>> groups;
13
14        public CellInformation cells[][];
15       
16        public double sideLengthX;
17        public double sideLengthY;
18       
19        public ArrayList<NodeInformation> ni;
20       
21        public Mobility(){
22                groups = new ArrayList<ArrayList<Integer>>();
23                cells = new CellInformation[GlobalNetwork.ROWS][GlobalNetwork.COLUMNS];
24               
25                sideLengthX=GlobalNetwork.LENGTHX/GlobalNetwork.COLUMNS;
26                sideLengthY=GlobalNetwork.LENGTHY/GlobalNetwork.ROWS;
27               
28                System.out.println("side length X "+sideLengthX);
29                System.out.println("side length Y "+sideLengthY);
30               
31                double lat = GlobalNetwork.pointMin.getLatitude();
32                double lon = GlobalNetwork.pointMin.getLongitude();
33               
34                for (int i=0;i<GlobalNetwork.ROWS;i++){
35                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
36                                cells[i][j] = new CellInformation();
37                                cells[i][j].setMinPoint(lon+(double)j*sideLengthX,lat+(double)i*sideLengthY);
38                                cells[i][j].setMaxPoint(lon+(double)(j+1)*sideLengthX,lat+(double)(i+1)*sideLengthY);; 
39                        }
40                }
41               
42                for (int i=0;i<GlobalNetwork.ROWS;i++){
43                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
44                                if(Util.intersecRoad(cells[i][j])==true)
45                                        cells[i][j].enable();
46                                else
47                                        cells[i][j].disable();
48                        }
49                }
50               
51                ni = new ArrayList<NodeInformation>();
52               
53                for(int i=0;i<GlobalNetwork.nrHosts;i++){
54                        ni.add(new NodeInformation(i,GlobalNetwork.nrHosts));
55                }
56        }
57       
58        public void setPriorityEntry(){
59                Random rand = new Random();
60                for (int i=0;i<GlobalNetwork.ROWS;i++){
61                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
62                                if(cells[i][j].isAvailable()==false){
63                                        cells[i][j].setPriorityEntry(0);
64                                }else{
65                                        cells[i][j].setPriorityEntry(rand.nextInt(10)+1);
66                                }
67                        }
68                }
69        }
70       
71        public void setPriorityExit(){
72                Random rand = new Random();
73                for (int i=0;i<GlobalNetwork.ROWS;i++){
74                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
75                                int r = rand.nextInt(10)+1;
76                                while(r==cells[i][j].priorityEntry){
77                                        r = rand.nextInt(10)+1;
78                                }
79                                if(cells[i][j].isAvailable()==false){
80                                        cells[i][j].setPriorityExit(0);
81                                }else{
82                                        cells[i][j].setPriorityExit(r);
83                                }
84                        }
85                }
86        }
87       
88        public void setPriorityEntryCenter(boolean center){
89                Random rand = new Random();
90                int valcenter = 0;
91                int valedge = 0;
92
93                for (int i=0;i<GlobalNetwork.ROWS;i++){
94                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
95                                if(center==false){
96                                        valcenter = rand.nextInt(5)+1;
97                                        valedge = rand.nextInt(5)+5;
98                                }else{
99                                        valcenter = rand.nextInt(5)+5;
100                                        valedge = rand.nextInt(5)+1;
101                                }
102                                if(cells[i][j].isAvailable()==false){
103                                        cells[i][j].setPriorityEntry(0);
104                                }else{
105                                        if((i>=GlobalNetwork.ROWS/3)&&(i<=2*GlobalNetwork.ROWS/3)){
106                                                if((j>=GlobalNetwork.COLUMNS/3)&&(j<=2*GlobalNetwork.COLUMNS/3))
107                                                        cells[i][j].setPriorityEntry(valcenter);
108                                        }else{
109                                                cells[i][j].setPriorityEntry(valedge);
110                                        }
111                                }
112                        }
113                }
114        }
115       
116        public void setPriorityExitCenter(boolean center){
117                Random rand = new Random();
118                int valcenter = 0;
119                int valedge = 0;
120
121                for (int i=0;i<GlobalNetwork.ROWS;i++){
122                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
123                                if(center==false){
124                                        valcenter = rand.nextInt(5)+1;
125                                        valedge = rand.nextInt(5)+5;
126                                }else{
127                                        valcenter = rand.nextInt(5)+5;
128                                        valedge = rand.nextInt(5)+1;
129                                }
130                                if(cells[i][j].isAvailable()==false){
131                                        cells[i][j].setPriorityExit(0);
132                                }else{
133                                        if((i>=GlobalNetwork.ROWS/3)&&(i<=2*GlobalNetwork.ROWS/3)){
134                                                if((j>=GlobalNetwork.COLUMNS/3)&&(j<=2*GlobalNetwork.COLUMNS/3))
135                                                        cells[i][j].setPriorityExit(valcenter);
136                                        }else{
137                                                cells[i][j].setPriorityExit(valedge);
138                                        }
139                                }
140                        }
141                }
142        }
143       
144        public void print_cell(){
145                for (int i=0;i<GlobalNetwork.ROWS;i++){
146                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
147                                System.out.println("[ij] "+i+" "+j+" Min "+cells[i][j].pointMin.getLatitude()+" "+cells[i][j].pointMin.getLongitude());
148                                System.out.println("[ij] "+i+" "+j+" Max "+cells[i][j].pointMax.getLatitude()+" "+cells[i][j].pointMax.getLongitude());
149                                System.out.println("[ij] "+i+" "+j+" ok to use = "+cells[i][j].isAvailable());
150                                System.out.println("[ij] "+i+" "+j+" entrie = "+cells[i][j].priorityEntry);
151                                System.out.println("[ij] "+i+" "+j+" exit = "+cells[i][j].priorityExit);
152                        }
153                }
154        }
155       
156        public void print_groups(){
157                System.out.println("Grupuri");
158                for(int i=0;i<groups.size();i++){
159                        for(int j=0;j<groups.get(i).size();j++){
160                                System.out.print(" "+groups.get(i).get(j));
161                        }
162                        System.out.println();
163                }
164        }
165       
166        public void print_adjacency(){
167                int i,j;
168                for(i=0;i<ni.size();i++){
169                        for(j=0;j<GlobalNetwork.nrHosts;j++){
170                                System.out.print(" "+ni.get(i).getAdjacency(j));
171                        }
172                        System.out.println();
173                }
174        }
175       
176        public void print_interaction(){
177                int i,j;
178                for(i=0;i<ni.size();i++){
179                        for(j=0;j<GlobalNetwork.nrHosts;j++){
180                                System.out.print(" "+ni.get(i).getIneraction(j));
181                        }
182                        System.out.println();
183                }
184        }
185       
186        public void createInteraction(int nrHosts,int nrGroups){
187
188                int numberOfMembers[] = new int[nrGroups];
189                int groups[][] = new int[nrHosts][nrHosts];
190               
191                for (int i=0;i<nrGroups;i++) {
192                        numberOfMembers[i]=0;
193                }
194               
195                for(int i=0;i<nrHosts;i++){
196                        for(int j=0;j<nrHosts;j++){
197                                groups[i][j]=0;
198                        }
199                }
200               
201                for (int i=0;i<nrHosts;i++) {
202                        int groupId=i%nrGroups;
203                        groups[groupId][numberOfMembers[groupId]]=i+1;
204                        numberOfMembers[groupId]+=1;   
205                }
206               
207                /*for(int i=0;i<ni.size();i++){
208                        for(int j=0;j<GlobalNetwork.nrHosts;j++){
209                                System.out.print(" "+groups[i][j]);
210                        }
211                        System.out.println();
212                }*/
213               
214                int seed = (int)GlobalNetwork.SEED;
215                int th = (int)(100*GlobalNetwork.THESHOLD);
216                Random generator;
217                if(seed != 0){
218                        generator = new Random(seed);
219                }
220                else{
221                        generator = new Random();
222                }
223               
224                for(int i=0;i<nrHosts;i++){
225                        for (int j=0;j<nrHosts;j++) {
226                                 if (Util.areInAGroup(i+1,j+1,groups,nrGroups,numberOfMembers)==true) {
227                                        double rand = 1.0-(generator.nextInt(th)/100.0);
228                                        ni.get(i).setIneraction(j, rand);
229                                 } else{ //the hosts are not in the same group
230                                         double rand = generator.nextInt(th)/100.0;
231                                         ni.get(i).setIneraction(j, rand);
232                                        }
233                        }
234                }
235               
236                //print_interaction();
237               
238                for(int i=0;i<nrHosts;i++){
239                        for (int j=0;j<nrHosts;j++) {
240                                if (Util.areInAGroup(i+1,j+1,groups,nrGroups,numberOfMembers)==true) {
241                                        double rand1 = generator.nextInt((int)10*th)/100.0;
242                                        if ((rand1)<GlobalNetwork.REWIRE && i!=j){
243                                                boolean found=false;
244                                                for (int z=0;z<nrHosts;z++){
245                                                        if (Util.areInAGroup(i+1,z+1,groups,nrGroups,numberOfMembers)==false){
246                                                                        if((ni.get(i).getIneraction(z)<GlobalNetwork.THESHOLD)&&(found==false)){
247                                                                                //System.out.println("final z "+z+" i "+i+" j "+j+" rand1 "+rand1);
248                                                                                double rand = 1.0-(generator.nextInt(th)/100.0);
249                                                                                ni.get(i).setIneraction(z, rand);
250                                                                                ni.get(z).setIneraction(i, 1.0-(generator.nextInt(th)/100.0));
251                                                                                found=true;
252                                                                        }
253                                                        }
254                                                }
255                                                if(found==true){
256                                                        //System.out.println("ds "+i+" "+j);
257                                                        double rand = generator.nextInt(th)/100.0;
258                                                        ni.get(i).setIneraction(j, rand);
259                                                        ni.get(j).setIneraction(i, generator.nextInt(th)/100.0);
260                                                }
261                                        }
262                                }
263                        }
264                }
265        }
266
267
268        public void deleteGroups(){
269                groups = new ArrayList<ArrayList<Integer>>();
270        }
271       
272        public void reconfigureEntrie(){
273               
274                int nrHosts = GlobalNetwork.nrHosts;
275                int nrGroups = (int)GlobalNetwork.GROUPS;
276               
277                createInteraction(nrHosts,nrGroups);
278               
279                for(int i=0;i<nrHosts;i++){
280                        ni.get(i).generateAdjacency();
281                }               
282
283                //print_interaction();
284                //print_adjacency();
285
286                double previousModth=0.0;
287                double modth=0.1;
288               
289                do {
290                        double betw[] = new double[nrHosts];
291                       
292                        for (int i=0;i<nrHosts; i++)
293                                betw[i]=0;
294                       
295                        Util.calculate_betweenness(betw,ni,nrHosts);
296                               
297                        /*System.out.println("Betweeness");
298                        for(int i=0;i<betw.length;i++){
299                                System.out.print(betw[i]+" ");
300                        }*/
301               
302       
303                        deleteGroups();
304                       
305                        int numberOfGroups=Util.getGroups(ni, groups ,nrHosts);
306                               
307                        //System.out.println("Avem "+numberOfGroups+" grupuri");
308
309                        previousModth=modth;
310       
311                        modth=Util.splitNetwork_Threshold(ni, betw, nrHosts, modth);
312               
313                }
314                while ((previousModth<modth)&&(modth>-1));
315               
316                //print_adjacency();
317                print_groups();
318                GlobalNetwork.GROUPS = groups.size();
319        }
320       
321        public boolean existsPriority(int r, int priority){
322                for (int i=0;i<GlobalNetwork.ROWS;i++){
323                        for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
324                                if(priority == 0){
325                                        if(cells[i][j].priorityEntry == r)
326                                                return true;
327                                }
328                                else{
329                                        if(cells[i][j].priorityExit == r)
330                                                return true;
331                                }
332                        }
333                }
334                return false;
335        }
336       
337        public void placeNodes(int priority){
338                Random rand;
339               
340                //print_cell();
341               
342                if(GlobalNetwork.SEED == 0){
343                        rand = new Random();
344                }
345                else{
346                        rand = new Random();
347                }
348               
349                for (int i=0;i<groups.size();i++) {
350                        int cellIdX=rand.nextInt(GlobalNetwork.ROWS);
351                        int cellIdY=rand.nextInt(GlobalNetwork.COLUMNS);
352                       
353                        if(GlobalNetwork.PRIORITY == true){
354                                int r = rand.nextInt(10)+1;
355                                while(!existsPriority(r,priority)){
356                                        r = rand.nextInt(10);
357                                }
358                                //System.out.println("Grup "+i+" prior "+r);
359                               
360                                if(priority==0){
361                                        while((!cells[cellIdX][cellIdY].isAvailable()) || (cells[cellIdX][cellIdY].priorityEntry<r)){
362                                                cellIdX=rand.nextInt(GlobalNetwork.ROWS);
363                                                cellIdY=rand.nextInt(GlobalNetwork.COLUMNS);
364                                        }
365                                        //System.out.println("Winner entrie:"+cellIdX+" "+cellIdY+" prior "+cells[cellIdX][cellIdY].priorityEntry);
366                                }else{
367                                        while((!cells[cellIdX][cellIdY].isAvailable()) || (cells[cellIdX][cellIdY].priorityExit<r)){
368                                                cellIdX=rand.nextInt(GlobalNetwork.ROWS);
369                                                cellIdY=rand.nextInt(GlobalNetwork.COLUMNS);
370                                        }
371                                        //System.out.println("Winner exit:"+cellIdX+" "+cellIdY+" prior "+cells[cellIdX][cellIdY].priorityExit);
372                                }
373                        }
374                        else{
375                                while(!cells[cellIdX][cellIdY].isAvailable()){
376                                        cellIdX=rand.nextInt(GlobalNetwork.ROWS);
377                                        cellIdY=rand.nextInt(GlobalNetwork.COLUMNS);
378                                }
379                        }
380                       
381                        System.out.println("pentru grupul "+i+" cell x "+cellIdX+" y "+cellIdY);
382
383                        for (int j=0;j<groups.get(i).size();j++) {
384                                int hostId=groups.get(i).get(j);
385                                ni.get(hostId-1).cellX=cellIdX+1;
386                                ni.get(hostId-1).cellY=cellIdY+1;
387
388                                cells[cellIdX][cellIdY].numberOfHosts+=1;
389                        }
390                }
391        }
392       
393       
394        public void initalPositions(){
395               
396                Random rand;
397                if(GlobalNetwork.SEED == 0){
398                        rand = new Random();
399                }
400                else{
401                        rand = new Random();
402                }
403       
404                for (int k=0;k<GlobalNetwork.nrHosts;k++) {     
405                        int index = rand.nextInt(cells[ni.get(k).cellX-1][ni.get(k).cellY-1].getNoPoints());
406                        ni.get(k).addStartLocation(cells[ni.get(k).cellX-1][ni.get(k).cellY-1].getValidLocation(index));       
407                        ni.get(k).addStartPoint(cells[ni.get(k).cellX-1][ni.get(k).cellY-1].getValidPoint(index));
408                }
409/*             
410                for (int i=0;i<GlobalNetwork.nrHosts;i++) {             
411                        System.out.println("nodul "+i+" set X "+ni.get(i).start.getLongitude()+" set Y "+ni.get(i).start.getLatitude()+" in patratul ("+ni.get(i).cellX+" "+ni.get(i).cellY+")");                                         
412                }
413                */
414        }
415       
416        public void deleteAll(){
417                groups = new ArrayList<ArrayList<Integer>>();
418               
419                ni = new ArrayList<NodeInformation>();
420               
421                for(int i=0;i<GlobalNetwork.nrHosts;i++){
422                        ni.add(new NodeInformation(i,GlobalNetwork.nrHosts));
423                }
424        }
425       
426        public void reconfigureExit(){
427                int nrHosts = GlobalNetwork.nrHosts;
428                int nrGroups = (int)GlobalNetwork.GROUPS;
429               
430                createInteraction(nrHosts,nrGroups);
431               
432                for(int i=0;i<nrHosts;i++){
433                        ni.get(i).generateAdjacency();
434                }               
435
436                int numberOfGroups=Util.getGroups(ni, groups ,nrHosts);
437               
438                createInteraction(nrHosts,nrGroups);
439               
440                for(int i=0;i<nrHosts;i++){
441                        ni.get(i).generateAdjacency();
442                }
443               
444                //print_adjacency();
445                print_groups();
446        }
447       
448        public ArrayList<Location> getLocations(){
449                ArrayList<Location> list = new ArrayList<Location>();
450               
451                for(int i=0;i<ni.size();i++){
452                        list.add(ni.get(i).getLocation());
453                }
454               
455                return list;
456        }
457       
458        public void getPositions(int cellX[], int cellY[]){
459                for(int i=0;i<ni.size();i++){
460                        cellX[i] = ni.get(i).cellX-1;
461                        cellY[i] = ni.get(i).cellY-1;
462                }
463        }
464       
465        ArrayList<Location> SegmentsRoute;
466       
467        public int[] findCA(int xinit, int xfinal, int yinit, int yfinal,int CA[][]){
468                int CAmax = 0;
469                int xgasit=-1,ygasit=-1;
470                for(int i=xinit;i<xfinal;i++){
471                        for(int j=yinit;j<yfinal;j++){
472                                if(CA[i][j]>CAmax){
473                                        CAmax = CA[i][j];
474                                        xgasit = i;
475                                        ygasit = j;
476                                }
477                        }
478                }
479               
480                int ret[] = new int[3];
481                ret[0]= 0;
482                if((CAmax==0)){
483                        ret[0]=1;
484                }else
485                if(!cells[xgasit][ygasit].isAvailable()){
486                        ret[0]=1;
487                }       
488                ret[1] = xgasit;
489                ret[2] = ygasit;
490        //      System.out.println("in find"+xinit+" "+yinit+" "+xfinal+" "+yfinal+" "+xgasit+" "+ygasit);
491
492                if(xgasit==xfinal && ygasit==yfinal)
493                        ret[0]=1;
494                return ret;
495        }
496       
497        public void findPath(int xinit, int xfinal, int yinit, int yfinal,int CA[][]){
498                int gasit[]=null;
499                System.out.println(xinit+" "+yinit+" "+xfinal+" "+yfinal);
500                if(xfinal<xinit && yfinal<yinit){
501                        gasit = findCA(xfinal,xinit,yfinal,yinit,CA);
502                }
503                if(xfinal<xinit && yfinal>yinit){
504                        gasit = findCA(xfinal,xinit,yinit,yfinal,CA);
505                }
506                if(xfinal>xinit && yfinal<yinit){
507                        gasit = findCA(xinit,xfinal,yfinal,yinit,CA);
508                }
509                if(xfinal>xinit && yfinal>yinit){
510                        gasit = findCA(xinit,xfinal,yinit,yfinal,CA);
511                }
512                if(gasit!=null){
513                        int xgasit,ygasit;
514                        boolean recursiv = true;
515                        xgasit = gasit[1];
516                        ygasit = gasit[2];
517                        if(gasit[0]==1){ //we have an error
518                                xgasit = xfinal;
519                                ygasit = yfinal;
520                                recursiv = false;
521                        }
522                        if(xgasit==xinit && ygasit==yinit){
523                                xgasit = xfinal;
524                                ygasit = yfinal;
525                                recursiv = false;
526                        }
527                        if(xgasit==xfinal && ygasit==yfinal){
528                                recursiv = false;
529                        }
530                        Location loc;
531                        Random rand = new Random();
532                //      System.out.println(cells[xgasit][ygasit].isAvailable());
533                        int index = rand.nextInt(cells[xgasit][ygasit].getNoPoints());
534                        loc = cells[xgasit][ygasit].getValidLocation(index);
535                        SegmentsRoute.add(loc);
536                       
537                        if(recursiv)
538                                findPath(xgasit,xfinal,ygasit,yfinal,CA);
539                }
540        }
541       
542        public ArrayList<RouteSegment> getSocialAtractionRoute(int i,int cellX[], int cellY[]){
543                int CA[][] = new int[GlobalNetwork.ROWS][GlobalNetwork.COLUMNS];
544
545                for(int j=0;j<GlobalNetwork.ROWS;j++){
546                        for(int k=0;k<GlobalNetwork.COLUMNS;k++){
547                                CA[j][k] = 0;
548                        }
549                }
550                for(int j=0;j<GlobalNetwork.nrHosts;j++){
551                        if(ni.get(i).getAdjacency(j)==1){
552                                CA[cellX[j]][cellY[j]] += 1;
553                        }
554                }
555
556                int xfinal = ni.get(i).cellX-1;
557                int yfinal = ni.get(i).cellY-1;
558                CA[xfinal][yfinal]=1;
559                System.out.println("Pentru nodul "+i);
560                /*      for (int k=0;k<GlobalNetwork.ROWS;k++){
561                                for (int j=0;j<GlobalNetwork.COLUMNS;j++) {
562                                        System.out.print(" "+CA[k][j]);
563                                }
564                                System.out.println();
565                        }*/
566                SegmentsRoute = new ArrayList<Location>();
567                findPath(cellX[i],xfinal,cellY[i],yfinal,CA);
568                //we have a list of locations
569                ArrayList<RouteSegment> rs = vnsim.vehicular.routePlan.RouteComputingUtils.mergeRoute(SegmentsRoute);
570                return rs;
571        }
572       
573}
Note: See TracBrowser for help on using the repository browser.