-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgp.cc
1684 lines (1250 loc) · 40.8 KB
/
rgp.cc
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ROUTE GUIDANCE PROTOCOL (RGP)
Coded for Final year project by
Kannan KV - 20072319
Naveen G - 20072334
Vidhoon V - 20072366
under the guidance of Dr.Ranjani Parthasarathi
The protocol has been coded in Routing layer for
assisting nodes in taking decisions related to movements
In future developments, this movement decisions can be used to
determine the routing mechanism of packets also.
*/
#include <rgp/rgp.h>
#include <rgp/rgp_pkt.h>
#include <random.h>
#include <cmu-trace.h>
/*
TCL Hooks
*/
/*inheriting basic packet header class members and functions for protocol packet header */
int hdr_rgp_pkt::offset_;
static class RgpHeaderClass : public PacketHeaderClass {
public:
RgpHeaderClass() : PacketHeaderClass("PacketHeader/Rgp",
sizeof(hdr_all_rgp)) {
bind_offset(&hdr_rgp_pkt::offset_);
}
} class_Rgp_hdr;
/* inheriting Tcl methods and members for binding protocol to the Tcl interpreter*/
static class Rgpclass : public TclClass {
public:
Rgpclass() : TclClass("Agent/Rgp") {}
TclObject* create(int argc, const char*const* argv) {
assert(argc == 5);
return (new Rgp((nsaddr_t)Address::instance().str2addr(argv[4])));
//return (new Rgp());
}
} class_Rgp;
/* tcl to C++ bridge*/
int Rgp::command(int argc, const char*const* argv) {
if(argc == 2) {
//creating a tcl instance handle for access to interpreter from C++
Tcl& tcl = Tcl::instance();
//command to return the current_road_id of the vehicle
if(strncasecmp(argv[1], "get_current_road", 16) == 0) {
printf("in get_current_road %d\n",current_road_id_);
tcl.resultf("%d", current_road_id_);
return TCL_OK;
}
if(strncasecmp(argv[1], "get_travel_time", 16) == 0) {
printf("in get_travel_time %lf\n",travel_time);
tcl.resultf("%lf", travel_time);
return TCL_OK;
}
//mandatory function to start an agent - intializations done in constructor
if(strncasecmp(argv[1], "start", 2) == 0) {
printf("in start I am %d\n",ra_addr_);
return TCL_OK;
}
//command to find next road_id after a timer break
if(strncasecmp(argv[1], "quick_slave", 2) == 0) {
printf("i am %d - in quick mode slave %d %d\n",ra_addr_,current_road_id_,destination_road_id_);
rst.display();
//getchar();
int new_road_id=0;
int src_junc=gps_object.getjunc2(current_road_id_);
int dest_junc=gps_object.getjunc1(destination_road_id_);
double junc_x=getx_for_dp(current_road_id_);
double junc_y=gety_for_dp(current_road_id_);
double new_junc_x=0;
double new_junc_y=0;
double xdiffer,ydiffer;
double actual_x,actual_y;
double diff,duration;
iNode->update_position();
actual_x=iNode->X();
actual_y=iNode->Y();
printf("DEBUG Co-ordinates %lf %lf %lf %lf\n",actual_x,junc_x,actual_y,junc_y);
diff=actual_x-junc_x+actual_y-junc_y;
printf("DEBUG diff val %lf\n",diff);
if(diff<2.0)
{
printf("DEBUG in quick slave deciding point *******\n");
//if(total_distance==0.0)
prepare_paramArray();
new_road_id=choose_path(gps_object.dp_count,src_junc,dest_junc);
printf("%lf %d\n",speed_accumulator,visits);
//added after speed guidance
//double speed_avg=speed_accumulator/visits;
//duration=gps_object.getroadlength(current_road_id_)/speed_avg;
//cache_rtt(current_road_id_,duration,false);
current_road_id_=new_road_id;
//re initializing
visits++;
speed_accumulator=0;
}
// printf("after decision %d\n",current_road_id_);
new_junc_x=getx_for_dp(current_road_id_);
new_junc_y=gety_for_dp(current_road_id_);
printf("\nDEBUG moving from %lf %lf to %lf %lf\n",iNode->X(),iNode->Y(),new_junc_x,new_junc_y);
xdiffer=new_junc_x-iNode->X();
ydiffer=new_junc_y-iNode->Y();
// iNode->set_destination(new_junc_x,new_junc_y,50.0);
printf("\nold X - %f old Y - %f New X - %f New Y - %f\n",iNode->X(),iNode->Y(),new_junc_x,new_junc_y);
//travel_time=((new_junc_y-iNode->Y())+(new_junc_x-iNode->X()))/50.0;
//if(travel_time<0)
// travel_time*=(-1);
//getchar();
return TCL_OK;
}
//command to display Road trip table contents - for testing purpose only
if(strncasecmp(argv[1], "disp_rtt", 8) == 0) {
printf("in display rtt\n");
rtt.display();
return TCL_OK;
}
// to start speed guidance
if(strncasecmp(argv[1], "speed_guide", 10) == 0) {
iNode->update_position();
double actualx,actualy,destx,desty;
actualx=iNode->X();
actualy=iNode->Y();
destx=getx_for_dp(current_road_id_);
desty=gety_for_dp(current_road_id_);
printf("in speed guide %d pos(%lf,%lf) dest(%lf,%lf) currentroad: %d\n",ra_addr_,actualx,actualy,destx,desty,current_road_id_);
int junc2=gps_object.getjunc2(current_road_id_);
int juncid=gps_object.isnearjunc(actualx,actualy,current_road_id_);
if(juncid==-1)
{
//the vehicle is not near junction, do speed processing
mindist=INFINITY;
send_srequest();
tcl.resultf("1");
}
else
{
//the vehicle is near junction
// double distance=sqrt((localdestx-actualx)*(localdestx-actualx)+(localdesty-actualy)*(localdesty-actualy));
// travel_time=distance/10.0; //10.0 is slow speed near junction -> vehicles slow down near junction
//if(destx-actualx+desty-actualy)
iNode->set_destination(destx,desty,10.0);
//setting speed_accum,visits
visits++;
speed_accumulator+=10.0;
//after speed guidance at last stop
double speed_avg=speed_accumulator/visits;
double duration=gps_object.getroadlength(current_road_id_)/speed_avg;
cache_rtt(current_road_id_,duration,false);
//reset parameters
visits=1;
speed_accumulator=0.0;
//setting roadentrytime for new road
roadentrytime=CURRENT_TIME;
tcl.resultf("0");
}
//getchar();
return TCL_OK;
}
// to process speed guidance replies
if(strncasecmp(argv[1], "move_intelligent", 15) == 0) {
printf("in moving intelligent vehicle\n");
intelligent_move();
return TCL_OK;
}
}
else if(argc == 3) {
Tcl& tcl = Tcl::instance();
// invokes the route guidance mechanism coded in guide_agent() function
if(strncasecmp(argv[1], "guide_me", 8) == 0) {
op_mode_=atoi(argv[2]);
guide_agent(); //sending mode of computation 0-shortest,1-quickest,2-optimal
return TCL_OK;
}
//command to assign the destination road id
else if(strncasecmp(argv[1], "dest_id", 7) == 0) {
printf("in dest_id\n");
destination_road_id_=atoi(argv[2]);
return TCL_OK;
}
//command to assign the current road id
else if(strncasecmp(argv[1], "road_id", 7) == 0) {
current_road_id_=atoi(argv[2]);
printf("in roadid %d \n",current_road_id_);
return TCL_OK;
}
//command to change the mode of operation
else if(strncasecmp(argv[1], "change_mode", 11) == 0) {
op_mode_=atoi(argv[2]);
return TCL_OK;
}
//mandatory command to intialize port called from nslib.tcl
else if (strcmp(argv[1], "port-dmux") == 0) {
dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);
if (dmux_ == 0) {
fprintf(stderr, "%s: %s lookup of %s failed\n",__FILE__,argv[1],argv[2]);
return TCL_ERROR;
}
return TCL_OK;
}
// intializes a handle to the trace file - not yet used
else if (strcmp(argv[1], "log-target") == 0 || strcmp(argv[1], "tracetarget") == 0) {
logtarget_ = (Trace*)TclObject::lookup(argv[2]);
if (logtarget_ == 0)
return TCL_ERROR;
return TCL_OK;
}
else if (strcmp(argv[1], "set-ll") == 0)
{
if( (obj = TclObject::lookup(argv[2])) == 0)
{
fprintf(stderr, "GeocastAgent(set-ll): %s lookup of %s failed\n",argv[1], argv[2]);
return (TCL_ERROR);
}
ll = (NsObject*) obj;
return (TCL_OK);
}
}
else if(argc == 5) {
if(strncasecmp(argv[1], "move_me", 8) == 0) {
//printf("in move me\n");
double destx,desty;
double speed=atof(argv[4]);
destx=atof(argv[2]);
desty=atof(argv[3]);
move_dummynode(destx,desty,speed);
return TCL_OK;
}
}
return Agent::command(argc, argv);
}
/*
Constructor
*/
Rgp::Rgp(nsaddr_t id) : Agent(PT_RGP)
{
//printf("control in constructor\n");
/*begin of GPS code */
//pkt_timer_=new Rgp_PktTimer(this);
iNode=(MobileNode*) (Node::get_node_by_address(id));
x_pos_=iNode->X();
y_pos_=iNode->Y();
/*end of GPS code */
ra_addr_=id; /* assign IP address - verify with somesh */
current_road_entry_time_=CURRENT_TIME; /* assign current time initially */
speed_accumulator=0;
prev_junc=-1;
visits=1;
/* performance evaluation parameter */
total_time=0;
myspeed=0;
mindist=INFINITY;
localdestx=0.0;
localdesty=0.0;
lastmotiontime=0.0;
roadentrytime=0;
communicated=false;
}
Rgp::~Rgp() {
printf("destroyed\n");
}
void Rgp::intelligent_move()
{
iNode->update_position();
double actualx,actualy;
actualx=iNode->X();
actualy=iNode->Y();
//no reply for speed guidance
if(myspeed==0.0 && !communicated)
{
printf("no vehicles replied for speed guidance - is the road empty???\n");
myspeed=50.0; //some default speed for empty road
localdestx=getx_for_dp(current_road_id_);
localdesty=gety_for_dp(current_road_id_);
}
printf("moving intelligent move final speed:%lf currentroad %d pos(%lf,%lf) dest(%lf,%lf)\n",myspeed,current_road_id_,actualx,actualy,localdestx,localdesty);
//getchar();
if(myspeed==0.0)
travel_time=0.0;
else
{
double distance=0.0;
distance=sqrt((localdestx-actualx)*(localdestx-actualx)+(localdesty-actualy)*(localdesty-actualy));
travel_time=distance/myspeed;
iNode->set_destination(localdestx,localdesty,myspeed);
}
//setting speed_accum,visits
visits++;
speed_accumulator+=myspeed;
//resetting myspeed and destination
myspeed=0.0;
localdestx=0.0;
localdesty=0.0;
//resetting communicated
communicated=false;
}
/* new version code
Mar 25 2011
@@@@@
@ coded by vidhoon v
@@@@@
*/
void Rgp::move_dummynode(double destx,double desty,double speed)
{
//printf("DEBUG ctrl in move_dummynode %d\n",ra_addr_);
//printf("here %lf %d %lf %lf %lf\n",speed_accumulator,visits,speed,destx,desty);
int current_road_id;
double actualx,actualy;
iNode->update_position();
actualx=iNode->X();
actualy=iNode->Y();
//cache last motion time
lastmotiontime=CURRENT_TIME;
current_road_id=gps_object.get_my_road(actualx,actualy,destx,desty);
if(current_road_id==-1)
{
//printf("old road %d for vehicle %d now am at (%lf,%lf) time %lf\n",current_road_id_,ra_addr_,actualx,actualy,CURRENT_TIME);
//getchar();
current_road_id_=-1;
for(int i=1;i<20;i++)
{
current_road_id=gps_object.get_my_road(actualx+i,actualy,destx,desty);
if(current_road_id!=-1)
{
current_road_id_=current_road_id;
//printf("glitch correction1\n");
break;
}
current_road_id=gps_object.get_my_road(actualx-i,actualy,destx,desty);
if(current_road_id!=-1)
{
current_road_id_=current_road_id;
//printf("glitch correction1b\n");
break;
}
current_road_id=gps_object.get_my_road(actualx,actualy+i,destx,desty);
if(current_road_id!=-1)
{
current_road_id_=current_road_id;
//printf("glitch correction2\n");
break;
}
current_road_id=gps_object.get_my_road(actualx,actualy-i,destx,desty);
if(current_road_id!=-1)
{
current_road_id_=current_road_id;
//printf("glitch correction2b\n");
break;
}
}
//if(current_road_id_==-1)
//printf("NO fix\n");
//move the vehicle
iNode->set_destination(destx,desty,speed);
return;
}
current_road_id_=current_road_id;
if(speed<1.0)
{
// the vehicle is facing congestion
cache_rtt_dummy(current_road_id,CURRENT_TIME-roadentrytime,true);
//cache_rtt_dummy(current_road_id,INFINITY*1.0/10.0,true); // setting exception status and inserting congestion entry to RTT
//move the vehicle
iNode->set_destination(destx,desty,speed);
//increment parameters
visits++;
speed_accumulator+=speed;
// printf("congestion road -> %d %lf\n",current_road_id_,CURRENT_TIME);
//getchar();
}
else
{
int juncid=gps_object.isnearjunc(actualx,actualy,current_road_id);
if(juncid==-1)
{
// the node is not near a juncton
//printf("not near junc vehicle %d\n",ra_addr_);
//cache record
cache_rtt_dummy(current_road_id,-2,true); //setting exception status and inserting no congestion entry into RTT
//rtt.display();
//printf("road id cached %d\n",current_road_id);
// getchar();
//move the node
iNode->set_destination(destx,desty,speed);
//increment parameters
visits++;
speed_accumulator+=speed;
}
else
{
// the node is near junction
//calculate duration
if(speed_accumulator!=0)
{
double speed_avg=speed_accumulator/visits;
double duration=(gps_object.getroadlength(current_road_id)*1.0)/speed_avg;
//cache record
cache_rtt_dummy(current_road_id,duration,false); //setting no exception status and inserting data to RTT
//printf("near junc\n");
//rtt.display();
//getchar();
}
//move the node
iNode->set_destination(destx,desty,speed);
// resetting dummy node parameters
speed_accumulator=0;
visits=1;
//setting roadentrytime for new road
roadentrytime=CURRENT_TIME;
}
}
}
/*
old version code
void Rgp::move_dummynode(double destx,double desty,double speed)
{
printf("DEBUG ctrl in move_dummynode %d\n",ra_addr_);
double actual_x,actual_y;
double junc_x,junc_y;
double diff;
int juncid;
double i=0;
iNode->update_position();
actual_x=iNode->X();
actual_y=iNode->Y();
iNode->set_destination(destx,desty,speed);
// horizontal case
juncid=gps_object.isnearjunc(destx,desty);
if(juncid==-1)
{
//if(ra_addr_==0)
//printf("DEBUG control check %lf %lf %d %lf\n",destx,desty,ra_addr_,CURRENT_TIME);
// it has not reached a junction
speed_accumulator+=speed;
visits+=1;
}
else if(juncid!=prev_junc)
{
double speed_avg=0.0;
if(speed<1.0)
{
cache_rtt_dummy(current_road_id_,INFINITY*1.0/100.0);
speed_accumulator=0.0;
visits=1;
}
else
{
if(visits==1)
speed_avg=speed;
else
speed_avg=speed_accumulator/visits;
// it has reached a junction
if(prev_junc==-1)
{
// this is the first junction it has come across
prev_junc=gps_object.getjunc2(current_road_id_);
}
else
{
// it has already crossed a junction -> have to change current road id
int new_road_id=gps_object.getroadid(prev_junc,juncid);
current_road_id_=new_road_id;
prev_junc=juncid;
}
double duration=(gps_object.getroadlength(current_road_id_)*1.0)/(speed_avg);
cache_rtt_dummy(current_road_id_,duration);
speed_accumulator=0.0;
visits=1;
}
}
}
*/
/* implementation of getx_for_dp(int current_road_id) function
returns the x-coordinate of a decision point at the end of the road segment
*/
double Rgp::getx_for_dp(int current_road_id) {
printf("control in getx_for_dp\n");
int junction_id=gps_object.getjunc2(current_road_id);
double junc_x=gps_object.get_junc_x_pos(junction_id);
//printf("DEBUG cid %d jid %d juncx %d\n",current_road_id,junction_id,junc_x);
return junc_x;
}
/* implementation of gety_for_dp(int current_road_id) function
returns the y-coordinate of a decision point at the end of the road segment
*/
double Rgp::gety_for_dp(int current_road_id) {
printf("control in gety_for_dp\n");
int junction_id=gps_object.getjunc2(current_road_id);
double junc_y=gps_object.get_junc_y_pos(junction_id);
//printf("DEBUG cid %d jid %d juncy %d\n",current_road_id,junction_id,junc_y);
return junc_y;
}
/* implementation of is_checkpoint(int xpos,int ypos) function
returns true if given coordinates are of a check point else false
*/
/*
implementation of choose_path(int dp_count,int source_junc,int dest_junc)
computes the best path for the present state and returns the next road id to travel in
*/
int Rgp::choose_path(int dp_count,int source_junc,int dest_junc)
{
printf("control in choose path %d --> %d %d --> %d %d\n",source_junc,dest_junc,current_road_id_,destination_road_id_,dp_count);
if(source_junc==dest_junc)
{
// printf("DEBUG caught return\n");
return destination_road_id_;
}
int i,j;
float L[dp_count][dp_count];
int C[dp_count];
float D[dp_count];
int prev[dp_count];
int rank = dp_count;
int trank=0;
for(i = 0; i < rank; i++)
{
for(j=0; j<rank; j++) {
L[i][j]=cost_array[i][j];
//printf("%f\t",cost_array[i][j]);
}
//printf("\n");
}
for (i = 0; i < rank; i++)
{
C[i] = i;
prev[i]=source_junc;
}
C[source_junc] = -1;
for (i = 0; i < rank; i++)
D[i] = L[source_junc][i];
for(trank = 1; trank<rank; trank++)
{
// each iteration if dijistra
float minValue = INFINITY;
int minNode = source_junc;
for (i = 0; i < rank; i++)
{
if (C[i] == -1)
continue;
if (D[i] > 0 && D[i] < minValue)
{
minValue = D[i];
minNode = i;
}
}
C[minNode] = -1;
for (i = 0; i < rank; i++)
{
if (L[minNode][i] < 0)
continue;
if (D[i] < 0) {
D[i] = minValue + L[minNode][i];
prev[i]=minNode;
continue;
}
if ((D[minNode] + L[minNode][i]) < D[i]){
D[i] = minValue + L[minNode][i];
prev[i]=minNode;
}
}
//end of iteration
}
//for(i=0;i<p;i++)
// cout<<D[i]<<endl;
// printf("PREV : %d\n ",prev[0]);
// for(int i=0;i<dp_count;i++)
// {
// printf("%d %d\n",i,prev[i]);
// }
int rvalue,ans;
float total_journey=0.0;
float total_journey_time=0.0;
printf("\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPath to reach reverse %lf:\n",D[dest_junc]);
while(dest_junc!=source_junc)
{
rvalue=dest_junc;
printf("- %d -\t",dest_junc);
dest_junc=prev[dest_junc];
/* coded for calculating total distance of journey if completed*/
int road_in_path=gps_object.getroadid(dest_junc,rvalue);
total_journey+=gps_object.getroadlength(road_in_path);
total_journey_time+=cost_array[dest_junc][rvalue];
}
printf("total journey length %lf total journey time %lf\n",total_journey,total_journey_time);
//getchar();
// printf("RRRRR :%d \t",dest_junc);
ans=gps_object.getroadid(source_junc,rvalue);
// printf("AAAAAAA : %d %d %d %d \n",source_junc,rvalue,dest_junc,ans);
// getchar();
FILE *fp;
char filename[20];
strcpy(filename,"vehicleid");
filename[9]=(ra_addr_/10)+'0';
filename[10]=(ra_addr_%10)+'0';
filename[11]='\0';
// strcat(filename,".txt");
// fp=fopen(filename,"a+");
//fprintf(fp,"\n At Junction %d \n Road ID %d { %s } ---------> Road ID %d { %s } \n",gps_object.r[current_road_id_].junc2,current_road_id_,gps_object.r[current_road_id_].desc,ans,gps_object.r[ans].desc);
// if(rvalue==gps_object.r[destination_road_id_].junc1)
// fprintf(fp,"\n At Junction %d \n Road ID %d { %s } ---------> Road ID %d { %s }\n",gps_object.r[ans].junc2,ans,gps_object.r[ans].desc,destination_road_id_,gps_object.r[destination_road_id_].desc);
//
// fclose(fp);
// if(rvalue==gps_object.getjunc1(destination_road_id_))
// ans=destination_road_id_;
/*
code for storing performance evaluation parameter
*/
total_time+=cost_array[source_junc][rvalue];
total_distance+=gps_object.getroadlength(ans);
printf("end of choose path %d Distance travelled so far %lf Time taken so far %lf\n",ans,total_distance,total_time);
getchar();
return ans;
}
/*
implementation of prepare_paramArray()
prepares the cost array for the graph to be used in evaluation of short,quick or optimal path
*/
void Rgp::prepare_paramArray()
{
printf("control in paramArray\n");
int i,j;
int juncs=gps_object.dp_count;
int roads=gps_object.n;
switch(op_mode_) {
case SHORT_MODE:
/*fetch road length from gpsobject and int dest_id=rq->destination_id_;fill the cost_array */
for(int i=0;i<juncs;i++)
for(int j=0;j<juncs;j++)
cost_array[i][j]=INFINITY;
for(int i=0;i<roads;i++)
{
int x,y;
x=gps_object.getjunc1(i);
y=gps_object.getjunc2(i);
// printf("inside short mode %d %d %d \n",i,x,y);
cost_array[x][y]=gps_object.getroadlength(i);
}
break;
case QUICK_MODE:
/*fetch cost of road from rstable and fill the array */
for(int i=0;i<juncs;i++)
for(int j=0;j<juncs;j++)
cost_array[i][j]=INFINITY;
for(int i=0;i<roads;i++)
{
int x,y;
x=gps_object.getjunc1(i);
y=gps_object.getjunc2(i);
cost_array[x][y]=(gps_object.getroadlength(i)*1.0)/20.0; // 20.0 - given by KVK as avg speed on free road
}
// rst.display();
rst.r=rst.head->next;
// printf("%d %d my addr %d",rst.r,rst.head->next,ra_addr_);
// if(rst.r==NULL)
// printf("$$$$$$$dead\n");
//rst.display();
while(rst.r!=NULL)
{
// printf("$$$$$$$$$$$$$$$$$$$$$here\n");
// compute CI for each record
int rid;
double t1,t0;
double ci;
rid=rst.r->roadid;
t1=rst.r->ltimeonroad;
// t0=gps_object.gettimeonfreeroad(rid);
// ci=(t0-t1)/t0;
ci=t1;
printf("cost %f %f %f\n",ci,t0,t1);
int x,y;
x=gps_object.getjunc1(rid);
y=gps_object.getjunc2(rid);
cost_array[x][y]=ci;
rst.r=rst.r->next;
}
for(int i=0;i<juncs;i++)
for(int j=0;j<juncs;j++)
cost_array[i][j]+=0.0;
break;
case OPTIMAL_MODE:
double alpha=0.2;
double beta=1-alpha;
for(int i=0;i<juncs;i++)
for(int j=0;j<juncs;j++)
cost_array[i][j]=INFINITY;
for(int i=0;i<roads;i++)
{
int x,y;
x=gps_object.getjunc1(i);
y=gps_object.getjunc2(i);
/*old method
double timefactor=(gps_object.getroadlength(i)*15.0)/(50.0*gps_object.total_roadlength);
double distfactor=gps_object.getroadlength(i)*1.0/gps_object.total_roadlength;
*/
//new method
double timetaken=gps_object.getroadlength(i)/20.0;
double timefactor=timetaken/(400+timetaken);
double distfactor=gps_object.getroadlength(i)*1.0/(400+gps_object.getroadlength(i));
cost_array[x][y]=alpha*timefactor+beta*distfactor;
}
rst.r=rst.head->next;
while(rst.r!=NULL)
{
// compute CI for each record
int rid;
double t1,t0;
double ci;
rid=rst.r->roadid;
t1=rst.r->ltimeonroad;
t0=gps_object.getroadlength(rid)/20.0;
ci=(t1-t0)/t0;
int x,y;
double d,di;
x=gps_object.getjunc1(rid);
y=gps_object.getjunc2(rid);
d=gps_object.getroadlength(rid);
/*old method
double timefactor=15*t1/gps_object.total_roadlength;
double distfactor=d/gps_object.total_roadlength;
// di=d/d0;
//if(rst.r->exception==1)
// timefactor=timefactor/100;
*/
//new method
double timefactor=t1/(400+t1); //max time to travel a road : 400 (assumption because in congestion values go upto 300)
double distfactor=d/(400+d); //400 is length of longest road
cost_array[x][y] = alpha*timefactor+beta*distfactor; //50.0 ->speed on free road
// printf("time factor %lf distance factor %lf (alpha*time factor) %lf (beta*distance factor) %lf \n",timefactor,distfactor,alpha*timefactor,beta*distfactor);
printf("time factor %lf distance factor %lf distance %lf congestion index: %lf (t1,t0): %lf %lf\n",timefactor,distfactor,d,ci,t1,t0);
//getchar();
// cost_array[x][y]=0.5*t1+0.1*d;
rst.r=rst.r->next;
}
break;
}
printf("at end of paramArray\n");
}
/*
implementation of cache_rtt(int new_road_id)
records the parameters used in calculation after the node reaches end of a road segment
*/
void Rgp::cache_rtt(int road_id,double time_taken,bool exception) {
/*currentroadid enter exit timestamp
newroadid add entry timestamp */
//printf("control in cache_rtt\n");
rtt.insert(road_id,time_taken,CURRENT_TIME,exception);
//printf("cached road id->%d by vehicle %d\n",road_id,ra_addr_);
//getchar();
return;
}
void Rgp::cache_rtt_dummy(int road_id,double total_time_taken,bool exception) {
/*currentroadid enter exit timestamp
newroadid add entry timestamp */