Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

C++
#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]);/*for neighourhood solution*/
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};
  /* printf("eneter the value of tempfactor-");
   scanf("%f",&tempfactor);
   printf("eneter the value of sizefactor-\n");
   scanf("%d",&sizefactor);
   printf("eneter the value of tcent-\n");
   scanf("%f",&tcent);
   printf("eneter the value of N-");
   scanf("%d",&N);*/
   time_t t;
   srand((unsigned)time(&t));
   clock_t begin,end;
   for(i=1;i<=TASK;i++)/*initial solution*/
    {
		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," Tempfactor=%f\n",tempfactor);
   //fprintf(fp," Sizafactorfactor=%d\n",sizefactor);
   //fprintf(fp," Tcent=%f\n",tcent);
   //fprintf(fp," N=%d\n",N);
   fprintf(fp,"no.        Cost            Time Spent\n");
//for(num=1;num<=100;num++)
   while(num<ner)
 {
    
   itr=0;
   time_spent=0.0;
   begin=clock();
   freezecount=0;
   frzlimit=5;
   trials=0;
    cost=0.0;
   temp=1000.0;
   //cost1=0.0;
   cost2=0.0;
   costdiff=0.0;
   changes=0;
   sizefactor=4;
   N=20;
   tempfactor=0.95;
   tcent=0.7;

   minpercent=0.01;
  // changes=0;
	 
   
   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);
		        //rand_p1=rand()%4+1;
       	     //t1=0;
	         costdiff=cost2-cost1;
	   if(costdiff<=0)
	     {
		   changes=changes+1;
		   cost=cost2;
		   /*solution is*/
		   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;

	//fclose(fp);

	/*for(in=1;in<=3;in++)
	{
		fscanf(fp,"%d %f  %f",&num,&cost,&time_spent);
		fprintf(fp,"\n7%d   %10f   %20f\n",num,cost,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);
	  //num++;
	 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);
	           {    
	            //len=0;
	            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);
		    
	    } 
     	/* swapping */
		tempswap=x[rand_t1];
	   x[rand_t1]=x[rand_t2];
	   x[rand_t2]=tempswap;
	   //return(ar);
	   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++)
	 {  
	    //sum=0.0;
		 
		 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];
	   //printf("\ncapacity of vm %d id %f",i,capacity[i]);
	   load[i]=sum;
	   //printf("\nload is %f",load[i]);
	   processing_time[i]=sum/capacity[i];
	   //printf("\nprocessing time for VM %d is %f",i,processing_time[i]);
      tot_capacity=tot_capacity+capacity[i];
      tot_load=tot_load+load[i];
      //printf("\n");
    }  
	   tot_processingtime=tot_load/tot_capacity;
	  // printf("total procssing time  is %f",tot_processingtime);
	return tot_processingtime*10;
	
}
Posted
Updated 11-May-15 22:32pm
v4
Comments
OriginalGriff 9-May-15 13:55pm    
Without your code, we have even less idea what is wrong than you do!

Edit the question, and show us the relevant code fragments!
Use the "Improve question" widget to edit your question and provide better information.
Afzaal Ahmad Zeeshan 9-May-15 13:56pm    
What is your variable that you are using? Make sure that your variables exists even after 10th iteration.
kitytrips 9-May-15 14:37pm    
All the variables do exist .in 11th iteration it is showing garbage but after 11 it shows the desired result again after some iteration it is showing garbage.
Kenneth Haugland 9-May-15 14:44pm    
Show us the code, or this is pure guesswork, and hence not a question.
CPallini 9-May-15 15:04pm    
I get compilation errors using your code, namely 'n' and 'task' variable are undeclared.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900