I have written code for optimisation in C. I have to take average of 100 iteration, for that i have used while loop. But after 11th iteration it is showing some garbage value. Please help me to solve this. I really need your help.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<time.h>
#define TASK 10
#define MAX 100
int arr[100]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float fitness(int arr[100]);
int x[1000]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float neighbourhood_solution(int x[100]);
int in,i,an[MAX]={0,0,0,0,0},a[MAX]={0,0,0,0,0},itr=0,freezecount=0,frzlimit,trials,changes,sizefactor,N;
float temp=100000.0,minpercent=0.01,sum=0.0,avg=0.0,tota_time=0.0,avg_time=0.0;
int num=1,ner=101;
float tempfactor,tcent;
float time_spent;
float cost2,cost1,cost=0.0,costdiff,r,cost2n;
int main(void)
{
int a[MAX]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
time_t t;
srand((unsigned)time(&t));
clock_t begin,end;
for(i=1;i<=TASK;i++)
{
printf("\ntask %d is assigned to the processor ",i);
a[i]=rand()%4+1;
printf("%d",a[i]);
}
cost1=fitness(a);
FILE *fp;
fp=fopen("output.txt","w+");
fprintf(fp,"no. Cost Time Spent\n");
while(num<ner)
{
itr=0;
time_spent=0.0;
begin=clock();
freezecount=0;
frzlimit=5;
trials=0;
cost=0.0;
temp=1000.0;
cost2=0.0;
costdiff=0.0;
changes=0;
sizefactor=4;
N=20;
tempfactor=0.95;
tcent=0.7;
minpercent=0.01;
printf("\n initial cost is %f",cost1);
while(freezecount<frzlimit)
{
changes=trials=0;
while(trials<sizefactor*n)
{
trials=trials+1;
cost2=neighbourhood_solution(a);
printf("\nthe cost of next solution is %f ",cost2);
costdiff=cost2-cost1;
if(costdiff<=0)
{
changes=changes+1;
cost=cost2;
printf("solution is");
for(i=1;i<=TASK;i++)
{
printf("%d ,",a[i]);
}
}
else
{
r=0.5;
if(r<=exp(-costdiff/temp))
{
changes=changes+1;
cost=cost2;
printf("solution is");
for(i=1;i<=TASK;i++)
{
printf("%d ,",a[i]);
}
}
}
itr++;
}
if((changes/trials)>tcent)
{
temp=0.5*temp;
}
else
{
temp=tempfactor*temp;
}
if((changes/trials)>minpercent)
{
freezecount=freezecount+1;
}
else
{
freezecount=0;
}
}
printf("final solution is");
for(i=1;i<=TASK;i++)
{
printf("%d ,",a[i]);
}
printf("no of iteration %d",itr);
printf("final cost =%f",cost);
end=clock();
time_spent=(double)(end-begin)/CLOCKS_PER_SEC;
printf("\n time spent %f ms",time_spent);
fscanf(fp," %f %f ",&cost,&time_spent);
fscanf(fp,"%d",&num);
fprintf(fp," %20.5f %10.8f \n", cost,time_spent);
fprintf(fp,"%d\n",num);
sum=sum+cost;
tota_time=tota_time+time_spent;
num++;
}
avg=sum/100;
avg_time=tota_time/100;
fprintf(fp,"average cost is %f\n",avg);
fprintf(fp,"average time is %f",avg_time);
fprintf(fp,"\nvalue of freezlimit %d",frzlimit);
fprintf(fp,"\nvalue of freezlimit %d",freezecount);
fclose(fp);
getch();
}
float neighbourhood_solution(int x[MAX])
{ int p,c,j,rand_p1=0,len=0,rand_p2=0,len2=0,tempswap=0,l=0,rand_t1=0,rand_t2=0,t1=0,t2=0;
int p1[100]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},p2[100]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float cost2n;
rand_p1=rand()%4+1;
srand(time(NULL));
printf("\nfirst random processor is%d",rand_p1);
{
j=0;
for(c=1;c<=TASK;c++)
{
if(rand_p1==x[c])
{
p1[j]=c;
len++;
printf("\n%d ",p1[j]);
j++;
}
}
printf("length is %d",len);
t1=rand()%len+2;
rand_t1=p1[t1]+1;
if(rand_t1==0)
{
rand_t1=p1[1];
}
printf("\n first random task is %d ",rand_t1);
}
rand_p2=rand()%4+1;
printf("\nsecond random processor is %d",rand_p2);
{
len2=0;
l=0;
for(p=1;p<=TASK;p++)
{
if(rand_p2==x[p])
{
p2[l]=p;
len2++;
printf("\n%d ",p2[l]);
l++;
}
}
printf("%d",len2);
t2=rand()%len2+1;
rand_t2=p2[t2];
if(rand_t2==0)
{
rand_t2=p2[2];
}
printf("\n second random task is %d ",rand_t2);
}
tempswap=x[rand_t1];
x[rand_t1]=x[rand_t2];
x[rand_t2]=tempswap;
cost2n=fitness(x);
return cost2n;
}
float fitness(int arr[100])
{
int p=0,i,c;
float capacity[50],load[50],sum=0.0;
float task_len[60]={2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500,250,2500};
float pe_num[6]={4,1,2,1,1},pe_mips[6]={256,300,256,256,256},vm_bw[6]={100,10,100,100,10};
float tot_load,tot_capacity,tot_processingtime,processing_time[50];
for(i=1;i<=4;i++)
{
for(c=1;c<TASK+1;c++)
{
if(arr[c]==i)
{
sum=sum+task_len[c];
p++;
}
}
capacity[i]=pe_num[i]*pe_mips[i]*vm_bw[i];
load[i]=sum;
processing_time[i]=sum/capacity[i];
tot_capacity=tot_capacity+capacity[i];
tot_load=tot_load+load[i];
}
tot_processingtime=tot_load/tot_capacity;
return tot_processingtime*10;
}