@@ -488,6 +488,18 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
488
488
{
489
489
using namespace std ;
490
490
491
+ // make a const demand visit
492
+ vector<int > demandVisitedConst;
493
+ UListD* pD=demand->next ->next ;
494
+ while (pD)
495
+ {
496
+ demandVisitedConst.push_back (pD->data );
497
+ pD=pD->next ;
498
+ }
499
+
500
+ // initialize a probability class
501
+ Probablity demandProb (demandVisitedConst.size ());
502
+
491
503
492
504
// clock_t start = clock();
493
505
@@ -531,7 +543,14 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
531
543
// int autoSize=reflect1.size()/2;
532
544
// autoSize=(autoSize==0?1:autoSize);
533
545
int randIndex=clock ()%reflect1.size ();// //rand->clock
534
-
546
+ // printf("\nThe stack now is:\n");
547
+ // printf("\n_________________________\n");
548
+ // for(unsigned int i=0;i<reflect1.size();i++)
549
+ // {
550
+ // printf("[%d] pos:%d idx%d\n",i,reflect1[i].stepth,reflect1[i].nodeID);
551
+ // }
552
+ // printf("\n_________________________\n");
553
+
535
554
// printf("****%lu\n",clock());
536
555
// printf("****%lu\n",reflect1.size());
537
556
// pair<int,int>tmp=reflect1[randIndex];//`````````````````edit
@@ -542,15 +561,26 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
542
561
543
562
// reflect1.erase(reflect1.begin()+randIndex);
544
563
// printf("reflect1 size: %lu",reflect1.size());
545
-
564
+ path.resize (tmp.stepth +1 );// rebuild the path
565
+ pathEdge.resize (tmp.stepth +1 );
546
566
// EdgeNode *p=graph->adjList[tmp.second].firstEdge;
547
567
// path[tmp.first]=tmp.second;
548
568
EdgeNode *p=graph->adjList [tmp.nodeID ].firstEdge ;
549
569
path[tmp.stepth ]=tmp.nodeID ;
550
570
pathEdge[tmp.stepth ]=tmp.edgeID ;
551
571
572
+ printf (" \n Before resize\n " );
573
+ printf (" \n tmp.stepth:%d pathsize:%lu\n " ,tmp.stepth ,path.size ());
574
+ for (unsigned int i=0 ;i<path.size ();i++)
575
+ {
576
+ printf (" %d->" ,path[i]);
577
+ }
578
+
579
+
552
580
// init demanded vex;
553
581
vector<int > demandVisited;
582
+ vector<int > demandInPath;// extract from path ,have access demand point,don't need to clear
583
+
554
584
UListD* pD=demand->next ->next ;
555
585
while (pD)
556
586
{
@@ -563,9 +593,21 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
563
593
visited[i]=false ;
564
594
}
565
595
566
- path.resize (tmp.stepth +1 );// rebuild the path
567
- pathEdge.resize (tmp.stepth +1 );
568
596
597
+
598
+
599
+
600
+
601
+ printf (" \n reflect ind:%d back stepth:%d back node:%d\n " ,randIndex,tmp.stepth ,tmp.nodeID );
602
+ printf (" \n After back PathNode(%lu) is :\n " ,path.size ());
603
+ for (unsigned int i=0 ;i<path.size ();i++)
604
+ {
605
+ printf (" %d->" ,path[i]);
606
+ }
607
+ // getchar();
608
+
609
+
610
+ // initialize the visited Node and demandNode
569
611
for (unsigned int i=0 ;i<path.size ();i++)
570
612
{
571
613
visited[path[i]]=true ;
@@ -575,6 +617,7 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
575
617
if (demandVisited[j]==path[i])
576
618
{
577
619
demandVisited.erase (demandVisited.begin ()+j);
620
+ demandInPath.push_back (path[i]);
578
621
}
579
622
}
580
623
}
@@ -583,6 +626,14 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
583
626
584
627
while (path.back ()!=des&&p)
585
628
{
629
+
630
+ // printf("\nPath(%lu) is :\n",path.size());
631
+ // for(unsigned int i=0;i<path.size();i++)
632
+ // {
633
+ // printf("%d->",path[i]);
634
+ // }
635
+
636
+
586
637
int maxVal=-1 ;
587
638
// int minVal=graph->vexNum+1;//high weight
588
639
@@ -616,28 +667,49 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
616
667
p=p->next ;
617
668
}
618
669
619
-
620
670
621
- if (choice==-1 )
622
- break ;
671
+ // judge whether the node is the required node
672
+ // if yes judge it if it is the best one
673
+ // chose it as the next node if hit probability
674
+ // otherwise don't take it
675
+ if (choice!=-1 )
676
+ {
677
+ for (unsigned int i=0 ;i<demandVisited.size ();i++)
678
+ {
679
+ if (demandVisited[i]==choice)
680
+ {
681
+ demandInPath.push_back (demandVisited[i]);
682
+
683
+ if (demandProb.chooseOrNot (demandInPath)==false )
684
+ {
685
+ printf (" choose false\n " );
686
+ choice=-2 ;
687
+ demandInPath.pop_back ();
688
+ break ;
689
+ }
690
+ else
691
+ {
692
+ printf (" choose true\n " );
693
+ demandInPath.pop_back ();
694
+ break ;
695
+ }
696
+
697
+ }
698
+ }
699
+ }
700
+
701
+ if (choice==des)
702
+ {
703
+ choice=-1 ;
704
+ }
705
+
706
+
707
+
708
+
623
709
624
- // random to choose a not best path,but the worst path
625
- // int u=clock()%100;
626
- // if(u>=100)
627
- // {
628
- // choice=choiceBad;
629
- // choiceEdge=choiceEdgeBad;
630
- // // printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!choose bad(%d)!!!!!!!!!!!!!!!!!!!!!!!\n",u);
631
- // }
632
- // else
633
- // {
634
- // // printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!choose good(%d)!!!!!!!!!!!!!!!!!!!!!!!\n",u);
635
- // }
636
710
637
711
638
- visited[choice]=true ;
639
712
640
- // printf("here???\n");
641
713
// push the some biggest node into stack
642
714
while (_p)
643
715
{
@@ -653,7 +725,7 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
653
725
tmp.edgeID =_p->edgeID ;
654
726
reflect1.push_back (tmp);
655
727
}
656
- else if (((float )graph->adjList [_p->adjvex ].infoVal )/(float )maxVal>0.1 )
728
+ else if (((float )graph->adjList [_p->adjvex ].infoVal )/(float )maxVal>= 0 ) // >0.1
657
729
{
658
730
// reflect.push(make_pair(path.size(),_p->adjvex));
659
731
// reflect1.push_back(make_pair(path.size(),_p->adjvex));
@@ -666,8 +738,31 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
666
738
}
667
739
}
668
740
_p=_p->next ;
741
+
669
742
}
670
743
744
+
745
+
746
+
747
+
748
+ // break if quit the next step
749
+ printf (" \n choice=%d\n " ,choice);
750
+ if (choice==-2 )
751
+ {
752
+ printf (" not choose the best\n " );
753
+ break ;
754
+ }
755
+ if (choice==-1 )// can't find the next node,maybe find a loop
756
+ {
757
+ printf (" dead route\n " );
758
+ // getchar();
759
+ demandProb.refresh (demandInPath);
760
+ break ;
761
+ }
762
+ visited[choice]=true ;
763
+
764
+
765
+ // printf("path.push_back(choice);\n");
671
766
// printf("<<<%lu\n",path.size());
672
767
path.push_back (choice);
673
768
pathEdge.push_back (choiceEdge);
@@ -676,21 +771,23 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
676
771
// finalIdex++;
677
772
p=graph->adjList [choice].firstEdge ;
678
773
774
+ // printf("for(unsigned int i=0;i<demandVisited.size();i++)\n");
679
775
// erase the required node visited
680
776
for (unsigned int i=0 ;i<demandVisited.size ();i++)
681
777
{
682
778
if (demandVisited[i]==choice)
683
779
{
684
780
demandVisited.erase (demandVisited.begin ()+i);
781
+ demandInPath.push_back (choice);
685
782
}
686
783
}
687
784
688
785
if (demandVisited.size ()==0 )
689
786
{
787
+ printf (" all demand node are found\n " );
690
788
satisfied=true ;
691
789
break ;
692
790
}
693
-
694
791
// //accelerate
695
792
// int perCost=graph->vexNum/demand->size;
696
793
// int predicCost=perCost*(demand->size-demandVisited.size());
@@ -700,6 +797,7 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
700
797
// printf("break\n");
701
798
// break;
702
799
// }
800
+ // printf("%d %d\n",path.back(),des);
703
801
}
704
802
705
803
// the demand are found
@@ -740,11 +838,11 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
740
838
741
839
}
742
840
743
- // printf("\nPathNode(%lu) is :\n",path.size());
744
- // for(unsigned int i=0;i<path.size();i++)
745
- // {
746
- // printf("%d->",path[i]);
747
- // }
841
+ printf (" \n PathNode(%lu) is :\n " ,path.size ());
842
+ for (unsigned int i=0 ;i<path.size ();i++)
843
+ {
844
+ printf (" %d->" ,path[i]);
845
+ }
748
846
749
847
// // // pathEdge.erase(pathEdge.begin());
750
848
// printf("\nPathEdge(%lu) is :\n",pathEdge.size());
@@ -753,12 +851,23 @@ int goThrough(ALGraph* graph,ListD* demand,int src,int des,std::vector<int> &out
753
851
// printf("%d->",pathEdge[i]);
754
852
// }
755
853
756
- printf (" \n Demand(%lu) is :\n " ,demandVisited.size ());
757
- for (unsigned int i=0 ;i<demandVisited.size ();i++)
758
- {
759
- printf (" %d " ,demandVisited[i]);
760
- }
761
- printf (" \n The reflect stack sizels: %lu\n " ,reflect1.size ());
854
+
855
+ // printf("\nDemand(%lu) is :\n",demandVisited.size());
856
+ // for(unsigned int i=0;i<demandVisited.size();i++)
857
+ // {
858
+ // printf("%d ",demandVisited[i]);
859
+ // }
860
+
861
+ // printf("\nDemand Visited(%lu) is :\n",demandInPath.size());
862
+ // for(unsigned int i=0;i<demandInPath.size();i++)
863
+ // {
864
+ // printf("%d ",demandInPath[i]);
865
+ // }
866
+
867
+ printf (" \n The reflect stack size is: %lu\n " ,reflect1.size ());
868
+ printf (" it is ready to back" );
869
+ // getchar();
870
+
762
871
763
872
}
764
873
// PathNodeLink result;//=(PathNodeLink)malloc(sizeof(PathNode));;
@@ -936,8 +1045,10 @@ Probablity::Probablity(unsigned int _numDemand)
936
1045
numDemand=_numDemand;
937
1046
// variateRate=20;
938
1047
}
1048
+ Probablity::~Probablity ()
1049
+ {}
939
1050
940
- int Probablity::refresh (std::vector<int > demandPath)
1051
+ int Probablity::refresh (std::vector<int > & demandPath)
941
1052
{
942
1053
ProbNodeLink p;
943
1054
p=head;
@@ -949,12 +1060,16 @@ int Probablity::refresh(std::vector<int> demandPath)
949
1060
tmp->child =new std::map<int ,ProbNode*>();
950
1061
tmp->depth =i+1 ;
951
1062
tmp->val =numDemand*4 ;
1063
+ p->child ->insert (std::pair<int ,ProbNodeLink>(demandPath[i],tmp));
952
1064
}
953
1065
954
1066
std::map<int ,ProbNode*>& pTmp=*(p->child );
955
1067
p=pTmp[demandPath[i]];
956
1068
957
1069
p->val -=p->depth ;
1070
+ // cout<<"index: "<<demandPath[i]<<" ";
1071
+ // cout<<"val: "<<p->val<<" ";
1072
+ // cout<<"depth: "<<p->depth<<endl;
958
1073
if (p->val <0 )
959
1074
{
960
1075
p->val =0 ;
@@ -964,7 +1079,7 @@ int Probablity::refresh(std::vector<int> demandPath)
964
1079
return 0 ;
965
1080
}
966
1081
967
- bool Probablity::chooseOrNot (std::vector<int > demandPath)
1082
+ bool Probablity::chooseOrNot (std::vector<int > & demandPath)
968
1083
{
969
1084
ProbNodeLink p;
970
1085
p=head;
@@ -1003,14 +1118,41 @@ bool Probablity::chooseOrNot(std::vector<int> demandPath)
1003
1118
max=i->second ->val ;
1004
1119
maxIdx=i->first ;
1005
1120
}
1121
+ // printf("%d\n",i->first);
1122
+ }
1123
+
1124
+ printf (" \n *************************************************\n " );
1125
+ printf (" best Idx: %d max pro: %d \n " ,maxIdx,max);
1126
+ printf (" demandPath: \n " );
1127
+ for (unsigned int i=0 ;i<demandPath.size ();i++)
1128
+ {
1129
+ printf (" %d " ,demandPath[i]);
1006
1130
}
1007
1131
1132
+ printf (" \n The vals are: \n " );
1133
+ for (std::map<int ,ProbNodeLink>::iterator i=p->child ->begin ();i!=p->child ->end ();i++)
1134
+ {
1135
+ printf (" %d(%d)" ,i->first ,i->second ->val );
1136
+ // printf("%d\n",i->first);
1137
+ }
1138
+
1139
+
1140
+ printf (" \n *************************************************\n " );
1141
+ // getchar();
1142
+
1143
+
1144
+ std::map<int ,ProbNodeLink> &tmpchild=*(p->child );
1008
1145
if (demandPath.back ()==maxIdx)
1009
1146
{
1010
- if (clock ()%100 >20 )
1147
+ // getchar();
1148
+ if (clock ()%100 <20 &&tmpchild[maxIdx]->val !=(int )numDemand*4 )
1011
1149
{
1012
- return true ;
1150
+ // getchar();
1151
+ printf (" \n variation\n " );
1152
+ return false ;
1013
1153
}
1154
+ return true ;
1155
+
1014
1156
}
1015
1157
1016
1158
return false ;
@@ -1030,8 +1172,6 @@ void search_route(char *topo[5000],unsigned int edge_num, char *demand)
1030
1172
EdgeLink e;
1031
1173
1032
1174
1033
-
1034
-
1035
1175
for (unsigned int i=0 ;i<MAX_NUM;i++)
1036
1176
{
1037
1177
graph.adjList [i].firstEdge =NULL ;
0 commit comments