Esempio n. 1
0
int main()
{
    int i ,j;
    float *fitnessArray;
    int **Q0 , **Q1 = NULL, **Q2 = NULL, **Q3 = NULL;
    printf(" How many iterations do you want ?\n ");
    scanf("%d",&TotalIterations);

    Q0 = initPop();     // Initializing population.
    fitnessArray = calculateFitness( Q0) ;
    MaxIndex = maximum( fitnessArray);     // Calculating the index of the best fit string in old population.
    MaxFitValue = fitnessArray[MaxIndex];  // Getting the fitness value of the best fit string in old population.

    int N = 1;
    while (N<=TotalIterations) {   // Iteration
        Q1 = selection(Q0, fitnessArray);
        Q2 = crossover( Q1 );
        Q3 = mutation(Q0, N);
        fitnessArray = calculateFitness( Q3) ;

        MaxIndex1 = maximum( fitnessArray );     // Calculating the index of the best fit string in fitnessArray obtained by new population Q3.
        MaxFitValue1 = fitnessArray[MaxIndex1];  // Calculating the fitness value of the best fit string for new population.
        if (MaxFitValue1 >= MaxFitValue )  {      // Got a better or equivalent fit string.
            MaxIndex = MaxIndex1;
            MaxFitValue = MaxFitValue1;
            Q0 = Q3;
        }
        else {    // MaxFitValue1 < MaxFitValue ...Did not get better fit string.
            for ( j = 0; j < L; j++) { //replace a string in Q3 by original best string from Q0.
                Q3[0][j] = Q0[MaxIndex][j];
            }
            Q0 = Q3;
        }
        N = N + 1;
     }

     fitnessArray = calculateFitness( Q3) ;
     MaxIndex = maximum( fitnessArray);     // Calculating the index of the best fit string in old population.
     MaxFitValue = fitnessArray[MaxIndex];  // Getting the fitness value of the best fit string in old population.
     printf(" Final Population is : \n");
     for ( i = 0; i < M; i++){
        printf("\n");
        for ( j= 0; j < L; j++)
            printf(" %d,",Q3[i][j]);
     }
     printf("\n\n And Fitness value of each string is :\n");
     for ( i = 0; i < M; i++)
        printf("%f,",fitnessArray[i]);
     printf("\n\n Maxima point x = %f\n",g(Q3[MaxIndex]));
     printf(" Maximum value f(x) = %f\n",MaxFitValue);
}
Esempio n. 2
0
int main(int argc, char **argv)
	 
{
  unsigned long	i = 1;
  char		Err[BUFSIZ];
#ifdef PVM
  unsigned int	j = 0L;
#endif

  
  /* There is no signal handling under parix. */
  if(((int)signal(SIGINT, (void (*)(int)) myInterrupt) == -1) ||
	 ((int)signal(SIGFPE, (void (*)(int)) Arithmetic) == -1))
	panic(A_FATAL, "child-main", "signal call went wrong :%s : %d",
		  __FILE__, __LINE__);
  
  strcpy(hlp, argv[0]);
  program_name = hlp;
  
  initDef(&eps);				/* initialize structure */
  ipOptPrc(&eps, argc, argv);	/* process options. */
  
#ifdef PVM
  eps.tid  = pvm_mytid();        /* tid of process. */
  strcpy(eps.Group, eps.Suffix); /* Create Group name. */
  if((eps.inst = pvm_joingroup(eps.Group)) < 0)
	panic(A_FATAL, "child-main", 
		  "pvm grouping for group %s failed : %s : %d\n",
		  eps.Group, __FILE__, __LINE__);

  printf("Slave with tid %d and inst %d started\n", eps.tid, eps.inst);

  /* To get more diversity, each Population gets other external parameters.*/
  
  if(eps.SigDim > 0)
	eps.SigStart += (0.1 * (double) eps.inst);

  /*
  if(eps.SigDim > 0) {
	if(eps.SigDim == 1)
	  eps.TauOne *= (0.1 * (double) eps.inst);
	else
	  eps.TauLcl *= (0.1 * (double) eps.inst);
  }
  
  if(eps.PDim > 0) {
	if(eps.PDim == 1) {
	  eps.GamOne *= (double) eps.inst;
	  eps.PStart *= (double) eps.inst;
	}
	else
	  eps.GamLcl *= (double) eps.inst;
  }
  
#ifdef DEBUG
  fprintf(stderr, "%d:Tau = %g, Gamma = %g\n",
          eps.inst, eps.TauLcl, eps.GamOne);
#endif 
  */

  sprintf(&hlp[strlen(hlp)], ".%d", eps.tid);
  if(pvm_barrier(eps.Group, eps.Tasks+1) < 0)
	panic(A_WARN, "child-main", "pvm barrier error : %s : %d\n",
		  __FILE__, __LINE__);
  printf("\n%s as instance %d activ.\n", program_name, eps.inst);
  
#endif /* PVM */
  
  if(ipOptChk(&eps, Err)) {	  /* check parameter consistency */
	fprintf(stderr,"%s",Err); /* print warning message */
	panic(A_FATAL, "ipOptPrc", "Parameter inconsistency :\n%s\n%s : %d", 
		  Err, __FILE__, __LINE__);
  }
  
#ifndef PVM
  printf("\n%s activ.\n", program_name);
#endif	
  
#ifdef PVM
  if(NULL == (MigrationBuffer = (individual_tPtr *)
			  calloc(eps.Neighbours, sizeof(individual_tPtr))))
	panic(E_FATAL, "main-child", "not enough memory : %s : %d",
		  __FILE__, __LINE__);
  
  for(j = 0; j < eps.Neighbours; j++)
	MigrationBuffer[j]= inNewIndividual(eps.XDim, eps.DDim, eps.SigDim,
										eps.AlpDim, eps.PDim);
#endif

  initSuffix(&eps);			/* initialize suffix,
							   here string from ES.in*/
  parsFormatString(&eps);	/* pars format string */
  
  pop = poNewPopulation(&eps);
  
  for (i = 1; i <= eps.TotExp; i++) {  /* experiment loop */
	
	ExpNbr = i;
	
	initPop(&eps, pop, i);

#ifdef PVM
	/* Initialization of MigrationBuffer. */
	for(j = 0; j < eps.Neighbours; j++) {
	  if(j < pop->mu)
		inCopyIndividual(poGetIndividual(j+1, pop),
						 MigrationBuffer[j]);
	  else
		inCopyIndividual(poGetIndividual(pop->mu, pop),
						 MigrationBuffer[j]);
	}
#endif
	
	dataParLog(&eps);		/* dump parameters */
	fioTimeOut(&eps);		/* get start time */
	
	PopulationEVAL(&eps, pop, "parents");
	dataCol(&eps, pop, i);
	if (eps.GfxFlg)
	  gnInitGnuPipe(&eps);
	
#ifdef PVM
	send_Best(&eps, pop);
#endif
	
	while (!termExp(&eps)) { /* ES main loop */
	  
	  eps.GenCnt++;
	  PopulationCREATE(&eps, pop);
	  /* recombination and mutation including the
		 concept of letal mutations. */
	  PopulationEVAL(&eps, pop, "offspring");
	  PopulationSELECT(&eps, pop);
	  dataCol(&eps, pop, i);
	  if(eps.GfxFlg && (eps.GenCnt % eps.GfxIvl == 0))
		gnPlotGnuPipe(&eps);     /* plot data */

#ifdef PVM
	  send_Best(&eps, pop);
	  if(eps.GenCnt % eps.IsolationTime == 0) {
		send_Neighbours(&eps, pop);
		if(eps.Communication == COMMUNICATION_SYNCHRON)
		  recv_sync(&eps, MigrationBuffer);
	  }
	  if(eps.Communication == COMMUNICATION_ASYNCHRON)
		recv_async(&eps, MigrationBuffer);
	  for(j = 0; j < eps.Neighbours && j < pop->mu; j++)
		inCopyIndividual(MigrationBuffer[j],
					     poGetIndividual(j+1, pop));
#endif

	}  /* end ES main loop */
	
	dataBst(&eps, pop, i); /* dump best ind. */
	fioTimeOut(&eps);	   /* get end time */
	
  } /* end experiment loop */
  
  if(eps.GfxFlg)
	gnQuitGnuPipe(&eps);	/* stop plotting */
  
  reportExperiments(&eps);
  
  pop = poDeletePopulation(pop);
  
#ifdef PVM
  if(pvm_initsend(PvmDataRaw) < 0)
	panic(A_FATAL,"child-main", "initsend for group %d failed : %s : %d",
		  eps.inst, __FILE__, __LINE__);
  
  if((ptid = pvm_parent()) == PvmNoParent)
	panic(A_FATAL,"child-main", "pvm_parent for group %d failed : %s : %d",
		  eps.inst, __FILE__, __LINE__);
  
  if(pvm_pkint(&(eps.tid), 1, 1) < 0)
	panic(A_FATAL,"child-main", "packing of tid failed : %s : %d",
		  __FILE__, __LINE__);
  
  if(pvm_send(ptid, MSGTAG_END) < 0)
	panic(A_WARN, "child-main", 
		  "sending end message to master failed : %s : %d",
		  __FILE__, __LINE__);
  pvm_barrier(eps.Group, eps.Tasks+1);
  pvm_lvgroup(eps.Group);
	
  for(j = 0; j < eps.Neighbours; j++)
	inDeleteIndividual(MigrationBuffer[j]);
  free(MigrationBuffer);
#endif
	
  utGlobalTermination(&eps);
	
  printf("\n%s 1.0 terminated.\n", program_name);

#ifdef PVM
  pvm_exit();
#endif

  exit(0);
}