Code:
/* -: Write in the File :- */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<alloc.h>
#include<time.h>
#define SIZE 24 // No of node in the Physical Network.
#define SIZE1 9000
//#define SIZE2 10 // Maximum traffic demand.
//#define SIZE3 10 // No of possible light path taken.
#define wavelength 8 // No of wavelength per Fiber.
#define MAX_CALL 750 // Maximun no of call to be served per row
#define CONVERTER 15
#define I 10000
unsigned short int ph_nw[24][24]={I,I,1,I,I,I,I,I,I,I,1,I,I,1,I,I,1,I,I,I,I,I,I,I,
I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,1,I,I,I,I,1,1,1,
1,I,I,I,I,I,I,1,I,I,I,I,I,I,I,I,I,I,I,I,I,I,1,1,
I,I,I,I,1,1,I,I,I,I,I,I,I,I,I,I,I,I,1,I,1,I,I,I,
I,I,I,1,I,I,I,I,I,I,I,I,1,I,I,I,I,1,I,I,1,I,I,I,
I,I,I,1,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,1,I,1,1,I,
I,I,I,I,I,I,I,I,I,1,I,I,I,I,I,I,1,I,I,I,I,1,I,1,
I,I,1,I,I,I,I,I,1,I,I,I,I,I,I,I,I,I,1,1,I,I,I,I,
I,I,I,I,I,I,I,1,I,I,1,I,I,I,I,I,I,1,I,I,I,1,I,I,
I,I,I,I,I,I,1,I,I,I,I,I,I,I,I,I,1,I,1,I,I,I,1,I,
1,I,I,I,I,I,I,I,1,I,I,I,I,I,I,I,I,I,1,I,1,I,I,I,
I,I,I,I,I,I,I,I,I,I,I,I,I,1,I,1,I,I,I,1,I,I,I,1,
I,I,I,I,1,I,I,I,I,I,I,I,I,I,I,1,I,1,I,I,1,I,I,I,
1,I,I,I,I,I,I,I,I,I,I,1,I,I,1,1,I,I,I,I,I,I,I,I,
I,I,I,I,I,I,I,I,I,I,I,I,I,1,I,1,I,1,I,1,I,I,I,I,
I,I,I,I,I,I,I,I,I,I,I,1,1,1,1,I,I,I,I,I,I,I,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;
unsigned short int d_node :6;
unsigned short int cost :4;
struct pth{
unsigned short int node:8;
}ph[SIZE];
unsigned short int wav :8;
}path_info[SIZE*MAX_CALL];
struct queue{
unsigned short int p_node;
unsigned short int c_node;
unsigned int cost :8;
bool w[wavelength];
};
typedef struct queue type_queue;
struct wavlength{
bool w[wavelength];
unsigned short int uses :8;
}wav[SIZE][SIZE];
struct c_info{
unsigned short int source_node :8;
unsigned short int desti_node :8;
double arrival_time;
double exit_time;
}call_info[20000];
int find_primary_path_use_dij(int,int);
void primary_light_path_allocation(int);
void write_file(void);
void display(void);
int level=0,temp=0,conv_place[CONVERTER],total_call;
clock_t start, end;
FILE *fp,*fp1;
int main()
{
int i,i1,j1,j,k,k1,k2,i5,i6,j6,demand,ct=0,mx,mxx,c=0,row_to_ca;
struct rc{
unsigned short int node;
unsigned short int val;
}rec[SIZE];
float val;
remove("c:\\file\\joe.txt");
if((fp=fopen("c:\\file\\joe.txt","wb+"))==NULL)
{
printf("joe can not open"); {getch();exit(0);}
}
if((fp1=fopen("c:\\file\\joe1.txt","rb+"))==NULL)
{
printf("joe1 can not open"); {getch();exit(0);}
}
fread(&call_info,sizeof(call_info),1,fp1);
fread(&total_call,sizeof(total_call),1,fp1);
fclose(fp1);
srand(50);
randomize();
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
if(ph_nw[i][j]==1) for(j1=0;j1<wavelength;wav[i][j].w[j1]=1,j1++);
else for(j1=0;j1<wavelength;wav[i][j].w[j1]=0,j1++);
wav[i][j].uses=0;
}
// for(i=0;i<CONVERTER;conv_place[i].row=0,conv_place[i].col=0,i++);
/*
for(i1=0;i1<80;i1++)
{
i=rand()%14;
j=rand()%14;
j1=rand()%100;
if((i!=j)&&(j1!=0)&&(traffic[i][j]==0)) traffic[i][j]=j1;
else i1--;
}
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(traffic[i][j]!=0) c++;
cout<<" c:"<<c<<"\n";
ct=0;
for(i=0;i<SIZE;i++)
{
for(j=0,row_to_ca=0;j<SIZE;row_to_ca=row_to_ca+traffic[i][j],j++);
if(row_to_ca==0) continue;
for(j=0,k2=0;j<SIZE;j++)
{
k1=(int)floor((traffic[i][j]*MAX_CALL/(float)row_to_ca)+0.5);
for(k=0;k<k1;k++)
{
if(find_primary_path_use_dij(i+1,j+1)==1) ct++;
level++;
}
}
}
for(j=0;j<(SIZE*MAX_CALL);j++)
{
for(cout<<"\n",i=0;(i<SIZE)&&(path_info[j].ph[i].node!=0);cout<<path_info[j].ph[i].node<<"->",i++);
// cout<<" wav :"<<path_info[j].wav;
// if(j%20==19) getch();
}
*/
ct=0;
level=0;
for(i=0;i<total_call;i++)
{
if(find_primary_path_use_dij(call_info[i].source_node,call_info[i].desti_node)) ct++;
level++;
}
cout<<"\n\tsuccess:"<<ct<<"\total:"<<level;
val=(1-(ct/(float)(level)))*100;
printf("\n call blocking ::%0.2f\n\n",val);
c=0;ct=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
if(ph_nw[i][j]==I) continue;
c++;
for(i1=0;i1<wavelength;i1++)
if(wav[i][j].w[i1]==0) ct++;
}
cout<<"c :"<<c<<" ct :"<<ct;
val=(ct/(float)(c*wavelength))*100;
printf("\n Avarage wavelength uses ::%0.2f\n\n",val);
for(j=0;j<(SIZE*MAX_CALL);j++)
{
for(i1=1;(i1<SIZE)&&(path_info[j].ph[i1+1].node!=0);i1++)
{
wav[path_info[j].ph[i1].node-1][path_info[j].ph[i1+1].node-1].uses=wav[path_info[j].ph[i1].node-1][path_info[j].ph[i1+1].node-1].uses+1;
wav[path_info[j].ph[i1+1].node-1][path_info[j].ph[i1].node-1].uses=wav[path_info[j].ph[i1+1].node-1][path_info[j].ph[i1].node-1].uses+1;
}
}
/*
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;cout<<wav[i][j].uses<<" ",j++)
{
if((wav[i][j].uses==0)||(i>=j)) continue;
if(wav[i][j].uses>temp)
{
for(i1=CONVERTER-1;i1>=0;i1--)
{
if((wav[i][j].uses>wav[conv_place[i1].row-1][conv_place[i1].col-1].uses))
{
conv_place[i1+1].row=conv_place[i1].row;
conv_place[i1+1].col=conv_place[i1].col;
}
else break;
}
conv_place[i1+1].row=i+1;
conv_place[i1+1].col=j+1;
if(conv_place[4].col!=0) temp=wav[conv_place[4].row][conv_place[4].col].uses;
else temp=0;
}
}
// if(ph_nw[i][j]==I) continue;
// else cout<<wav[i][j].uses<<" ";
cout<<"\n";
}
*/
for(i=0;i<SIZE;i++)
for(j=0,rec[i].val=0,rec[i].node=i+1,cout<<"\n";j<SIZE;cout<<" "<<wav[i][j].uses,j++)
rec[i].val+=wav[j][i].uses;
for(i=0,cout<<"\n";i<SIZE;cout<<" "<<rec[i].val,i++);
getch();
for(i=0;i<SIZE-1;i++)
for(j=0;j<(SIZE-i-1);j++)
if(rec[j].val<rec[j+1].val)
{
temp=rec[j].val;
rec[j].val=rec[j+1].val;
rec[j+1].val=temp;
temp=rec[j].node;
rec[j].node=rec[j+1].node;
rec[j+1].node=temp;
}
for(i1=0;i1<CONVERTER;i1++)
conv_place[i1]=rec[i1].node;
for(i1=0;i1<CONVERTER;cout<<" c:"<<conv_place[i1],i1++);
fwrite(&conv_place,sizeof(conv_place),1,fp);
fclose(fp);
getch();
return 0;
}
int find_primary_path_use_dij(int source_node,int desti_node)
{
type_queue temp_queue[SIZE1];
int front=0,rear=-1,flag=0,i,desti_node1,i1,i2,cost,pos=0;
register j2;
int dest,path[SIZE],top=-1,j;
bool temp_wav[wavelength];
for(i=0;i<wavelength;temp_wav[i]=1,i++);
path_info[level].cost=0;
path_info[level].s_node=0;
path_info[level].d_node=0;
for(j=0;j<SIZE;path_info[level].ph[j].node=0,j++);
for(i=0;i<SIZE1;temp_queue[i].p_node=0,temp_queue[i].c_node=0,temp_queue[i].cost=0,i++)
for(i1=0;i1<wavelength;temp_queue[i].w[i1]=1,i1++);
temp_queue[++rear].p_node=source_node;
temp_queue[rear].c_node=source_node;
temp_queue[rear].cost=0;
while(1)
{
if((front>rear)||(temp_queue[front].c_node==0)) {return 0;}
i1=(temp_queue[front].c_node-1);
for(i2=0;i2<SIZE;i2++)
{
if(ph_nw[i1][i2]==I) continue;
if((i2+1)==desti_node)
{
temp_queue[++rear].p_node=temp_queue[front].c_node;
temp_queue[rear].c_node=desti_node;
temp_queue[rear].cost=temp_queue[front].cost+1;
for(j2=0;j2<wavelength;temp_queue[rear].w[j2]=wav[i1][i2].w[j2],j2++);
flag=1; top=-1;
cost=temp_queue[rear].cost;
desti_node1=desti_node;
path[++top]=desti_node1;
for(j=rear;j>=0;j--)
{
if((desti_node1==temp_queue[j].c_node)&&(cost==temp_queue[j].cost))
{
desti_node1=temp_queue[j].p_node;
path[++top]=desti_node1;
if(desti_node1==source_node) break;
if(flag==1) {flag=0;j=(front+1);}
cost--;
flag=0;
for(i=0;i<top;i++)
if(temp_queue[j].p_node==path[i])
{
flag=2;break;
}
if(flag==2) break;
}
}
if(flag!=2)
{
for(i=0;i<SIZE;path_info[level].ph[i].node=0,path_info[level].wav=0,i++);
path_info[level].cost=top;
for(i=0;top>=0;top--,path_info[level].ph[i].node=path[top+1],i++);
path_info[level].s_node=source_node;
path_info[level].d_node=desti_node;
for(j2=0;j2<wavelength;temp_wav[j2]=1,j2++);
for(i=0;(i<SIZE)&&(path_info[level].ph[i+1].node!=0);i++)
for(j=0;j<wavelength;j++)
{
temp_wav[j]=temp_wav[j]&wav[path_info[level].ph[i].node-1][path_info[level].ph[i+1].node-1].w[j];
}
for(j=0;j<wavelength;j++)
if(temp_wav[j]==1) break;
if(j!=wavelength)
{
primary_light_path_allocation(j);
path_info[level].wav=j;
// for(i=0;(i<SIZE)&&(path_info[level].ph[i+1].node!=0);path_info[level].ph[i].wav=j,i++);
return 1;
}
path_info[level].cost=0;
path_info[level].s_node=0;
path_info[level].d_node=0;
for(j=0;j<SIZE;j++)
path_info[level].ph[j].node=0;
for(j=0;j<wavelength;temp_wav[j]=1,j++);
rear--;
}
rear--;
}
else if((i2+1)==source_node) continue;
else
{
temp_queue[++rear].p_node=i1+1;
temp_queue[rear].c_node=i2+1;
temp_queue[rear].cost=temp_queue[front].cost+1;
if(rear>(SIZE1-2)) {return 0;}
}
}
front++;
}
}
void primary_light_path_allocation(int w1)
{
int i,j;
for(i=0;(i<SIZE)&&(path_info[level].ph[i+1].node!=0);i++)
{
wav[path_info[level].ph[i].node-1][path_info[level].ph[i+1].node-1].w[w1]=0;
wav[path_info[level].ph[i+1].node-1][path_info[level].ph[i].node-1].w[w1]=0;
}
}
joe........