예제 #1
0
/*Algoritmo Principal*/
int main(int argc, char *argv[])
{
    char name[20];// = "_kita_1";
    char archiveName[20] = "archive";
	char fitputName[20] = "fitput";
	char varputName[20] = "varput";
	char hvName[20]= "hv";
	unsigned int i, j, t;
	unsigned int fun, gen;
	clock_t  startTime, endTime;
	double duration, clocktime;
	FILE *fitfile,*varfile;
/*Parametros iniciales*/

    strcpy(name,argv[1]);
    fun = atoi(argv[2]);
    gen = atoi(argv[3]);

    printf("%s %d %d \n",name,fun,gen);

    initialize_data(fun,gen);
/*Iniciar variables*/
	initialize_memory();

	/*
	sprintf(name,"kita_1_");
	sprintf(archiveName,strcat(name,"archive.out"));
	sprintf(fitputName, strcat(name,"fitput.out"));
	sprintf(varputName, strcat(name,"varput.out"));
	sprintf(hvName,strcat(name,"hv.out"));
*/
	//fitfile = fopen(strcat(strcat(fitputName,name),".out"),"w");
	//varfile = fopen(strcat(strcat(varputName,name),".out"),"w");
	//hv = fopen(strcat(strcat(hvName,name),".out"),"w");
/* Iniciar generador de aleatorios*/
	initialize_rand();
	startTime = clock();
/* Iniciar contador de generaciones*/
	t = 0;
/* Iniciar valores de la poblacion de manera aleatoria*/
	initialize_pop();
/* Calcular velocidad inicial*/
	initialize_vel();
/* Evaluar las particulas de la poblacion*/
	evaluate();
/* Almacenar el pBest inicial (variables and valor de aptitud) de las particulas*/
	store_pbests();
/* Insertar las particulas no domindas de la poblacion en el archivo*/

	insert_nondom();
	//printf("\n%d",nondomCtr);
/*Ciclo Principal*/
	while(t <= maxgen)
	{
		//clocktime = (clock() - startTime)/(double)CLOCKS_PER_SEC;
		/*if(verbose > 0 && t%printevery==0 || t == maxgen)
		{
			fprintf(stdout,"Generation %d Time: %.2f sec\n",t,clocktime);
			fflush(stdout);
		}*/
		/*if(t%printevery==0 || t == maxgen)
		{
			fprintf(outfile,"Generation %d Time: %.2f sec\n",t,clocktime);
		}*/
    /* Calcular la nueva velocidad de cada particula en la pooblacion*/
		//printf("\n 1");
		compute_velocity();

    /* Calcular la nueva posicion de cada particula en la poblacion*/
        //printf("\n 2");
        compute_position();
    /* Mantener las particulas en la poblacion de la poblacion en el espacio de busqueda*/
		//printf("\n 3");
		maintain_particles();
    /* Pertubar las particulas en la poblacion*/
		if(t < maxgen * pMut)
			mutate(t);

    /* Evaluar las particulas en la poblacion*/
		//printf("\n 4");
		evaluate();
    /* Insertar nuevas particulas no domindas en el archivo*/
		//printf("\n 5");
		update_archive();
    /* Actualizar el pBest de las particulas en la poblacion*/
		//printf("\n 6");
		update_pbests();
    /* Escribir resultados del mejor hasta ahora*/
    //verbose > 0 && t%printevery==0 || t == maxgen
		/*if(t%printevery==0 || t == maxgen)
		{
			//fprintf(outfile, "Size of Pareto Set: %d\n", nondomCtr);
			fprintf(fitfile, "%d\n",t);
			fprintf(varfile, "%d\n",t);
			for(i = 0; i < nondomCtr; i++)
			{
			    for(j = 0; j < maxfun; j++)
                    fprintf(fitfile, "%f ", archiveFit[i][j]);
                fprintf(fitfile, "\n");
			}
			fprintf(fitfile, "\n\n");
			fflush(fitfile);
			for(i = 0; i < nondomCtr; i++)
			{
			    for(j = 0; j < maxfun; j++)
                    fprintf(varfile, "%f ", archiveVar[i][j]);
                fprintf(varfile, "\n");
			}
			fprintf(varfile, "\n\n");
			fflush(varfile);
		}*/
    /* Incrementar contador de generaciones*/
        t++;
        //printf("%d\n",t);
	}
 /* Escribir resultados en el archivo */
	save_results(strcat(strcat(archiveName,name),".out"));
	endTime = clock();
	duration = ( endTime - startTime ) / (double)CLOCKS_PER_SEC;
	fprintf(stdout, "%lf sec\n", duration);
	//fclose(fitfile);
	//fclose(varfile);
	free_memory();
	return EXIT_SUCCESS;
}
예제 #2
0
int main(int argc, char **argv){
  
  /*positions of all particles*/
  FLOAT *x;
  FLOAT *y;
  FLOAT *z;
  
  /*velocities of all particles*/
  FLOAT *v_x;
  FLOAT *v_y;
  FLOAT *v_z;

  /*accelerations of all particles*/
  FLOAT *a_x;
  FLOAT *a_y;
  FLOAT *a_z;
    
/*terms for runge-kutta*/
  
  FLOAT *a_x_old;
  FLOAT *a_y_old;
  FLOAT *a_z_old;
    
    
  FLOAT  *k_1_x;
  FLOAT  *k_1_y;
  FLOAT  *k_1_z;
    
  FLOAT  *k_1_v_x;
  FLOAT  *k_1_v_y;
  FLOAT  *k_1_v_z;
    
  FLOAT  *k_2_x;
  FLOAT  *k_2_y;
  FLOAT  *k_2_z;
    
  FLOAT  *k_2_v_x;
  FLOAT  *k_2_v_y;
  FLOAT  *k_2_v_z;
    
  FLOAT  *k_3_x;
  FLOAT  *k_3_y;
  FLOAT  *k_3_z;

  FLOAT  *k_3_v_x;
  FLOAT  *k_3_v_y;
  FLOAT  *k_3_v_z;
    
  FLOAT  *k_4_x;
  FLOAT  *k_4_y;
  FLOAT  *k_4_z;
    
  FLOAT  *k_4_v_x;
  FLOAT  *k_4_v_y;
  FLOAT  *k_4_v_z;
    
  FLOAT *xtemp;
  FLOAT *ytemp;
  FLOAT *ztemp;
  FLOAT *v_xtemp;
  FLOAT *v_ytemp;
  FLOAT *v_ztemp;
  FLOAT *a_xtemp;
  FLOAT *a_ytemp;
  FLOAT *a_ztemp;
    
    
  /*masses*/
  FLOAT *mass;

  /*timestep variables*/
  FLOAT h= 0.001;
  int n_steps = (int)(100/h);
  int n_points = 3;
  FLOAT radius = 100.0;
  FLOAT unit_mass = 1.0; 
  FLOAT vel_initial = sqrt((11.0/3.0) * G_GRAV * unit_mass / (sqrt(3.0)*radius));
  FLOAT kinetic;
  FLOAT potential;
  int i,j,k;
  
  /*memory allocation*/
  x = get_memory(n_points);
  y = get_memory(n_points);
  z = get_memory(n_points);
  v_x = get_memory(n_points);
  v_y = get_memory(n_points);
  v_z = get_memory(n_points);
  a_x = get_memory(n_points);
  a_y = get_memory(n_points);
  a_z = get_memory(n_points);
  mass = get_memory(n_points);
  k_1_x = get_memory(n_points);
  k_1_y = get_memory(n_points);
  k_1_z = get_memory(n_points);
  k_1_v_x = get_memory(n_points);
  k_1_v_y = get_memory(n_points);
  k_1_v_z = get_memory(n_points);
  k_2_x = get_memory(n_points);
  k_2_y = get_memory(n_points);
  k_2_z = get_memory(n_points);
  k_2_v_x = get_memory(n_points);
  k_2_v_y = get_memory(n_points);
  k_2_v_z = get_memory(n_points);
  k_3_x = get_memory(n_points);
  k_3_y = get_memory(n_points);
  k_3_z = get_memory(n_points);
  k_3_v_x = get_memory(n_points);
  k_3_v_y = get_memory(n_points);
  k_3_v_z = get_memory(n_points);
  k_4_x = get_memory(n_points);
  k_4_y = get_memory(n_points);
  k_4_z = get_memory(n_points);
  k_4_v_x = get_memory(n_points);
  k_4_v_y = get_memory(n_points);
  k_4_v_z = get_memory(n_points);
  xtemp = get_memory(n_points);
  ytemp = get_memory(n_points);
  ztemp = get_memory(n_points);
  v_xtemp = get_memory(n_points);
  v_ytemp = get_memory(n_points);
  v_ztemp = get_memory(n_points);
  a_xtemp = get_memory(n_points);
  a_ytemp = get_memory(n_points);
  a_ztemp = get_memory(n_points);

  initialize_pos(x,y,z, n_points, radius);
  initialize_vel(v_x,v_y,v_z, n_points, vel_initial, radius);
  initialize_mass(mass, n_points, unit_mass);

  /*implementation of a second order runge kutta integration*/
    
    FILE *in;
    in = fopen("3cuerpos.dat","w");
    
  for(i=0;i<n_steps;i++){
      
      a_x_old = a_x;
      a_y_old = a_y;
      a_z_old = a_z;
      
    get_acceleration(a_x, a_y, a_z, x, y, z, mass, n_points);
    for(j=0;j<n_points;j++){

        k_1_x[j] = v_x[j];
        k_1_y[j] = v_y[j];
        k_1_z[j] = v_z[j];
        
        k_1_v_x[j] = a_x[j];
        k_1_v_y[j] = a_y[j];
        k_1_v_z[j] = a_z[j];
        
        
        //FIRST STEP
        
        xtemp[j] = x[j] + (h/2.0)*k_1_x[j];
        ytemp[j] = y[j] + (h/2.0)*k_1_y[j];
        ztemp[j] = z[j] + (h/2.0)*k_1_z[j];
        
        v_xtemp[j] = v_x[j] + (h/2.0)*k_1_v_x[j];
        v_ytemp[j] = v_y[j] + (h/2.0)*k_1_v_y[j];
        v_ztemp[j] = v_z[j] + (h/2.0)*k_1_v_z[j];
        
        k_2_x[j] = v_xtemp[j];
        k_2_y[j] = v_ytemp[j];
        k_2_z[j] = v_ztemp[j];
        
    }
      
      get_acceleration(a_xtemp,a_ytemp,a_ztemp,xtemp,ytemp,ztemp, mass, n_points);
      
      for(j=0;j<n_points;j++){
        
        k_2_v_x[j] = a_xtemp[j];
        k_2_v_y[j] = a_ytemp[j];
        k_2_v_z[j] = a_ztemp[j];
        
          
        //SECOND STEP
        
        xtemp[j] = x[j] + (h/2.0)*k_2_x[j];
        ytemp[j] = y[j] + (h/2.0)*k_2_y[j];
        ztemp[j] = z[j] + (h/2.0)*k_2_z[j];
          
        v_xtemp[j] = v_x[j] + (h/2.0)*k_2_v_x[j];
        v_ytemp[j] = v_y[j] + (h/2.0)*k_2_v_y[j];
        v_ztemp[j] = v_z[j] + (h/2.0)*k_2_v_z[j];
          
        k_3_x[j] = v_xtemp[j];
        k_3_y[j] = v_ytemp[j];
        k_3_z[j] = v_ztemp[j];
          
        }
      
      get_acceleration(a_xtemp,a_ytemp,a_ztemp,xtemp,ytemp,ztemp, mass, n_points);
      
      for(j=0;j<n_points;j++){
          
          k_3_v_x[j] = a_xtemp[j];
          k_3_v_y[j] = a_ytemp[j];
          k_3_v_z[j] = a_ztemp[j];
          
          
          //THIRD STEP
          
          xtemp[j] = x[j] + h*k_3_x[j];
          ytemp[j] = y[j] + h*k_3_y[j];
          ztemp[j] = z[j] + h*k_3_z[j];
          
          v_xtemp[j] = v_x[j] + h*k_3_v_x[j];
          v_ytemp[j] = v_y[j] + h*k_3_v_y[j];
          v_ztemp[j] = v_z[j] + h*k_3_v_z[j];
          
          k_4_x[j] = v_xtemp[j];
          k_4_y[j] = v_ytemp[j];
          k_4_z[j] = v_ztemp[j];
          
      }
      
      get_acceleration(a_xtemp,a_ytemp,a_ztemp,xtemp,ytemp,ztemp, mass, n_points);
      
      for(j=0;j<n_points;j++){
          
          k_4_v_x[j] = a_xtemp[j];
          k_4_v_y[j] = a_ytemp[j];
          k_4_v_z[j] = a_ztemp[j];
          
          
          //FOURTH STEP
          
          x[j] = x[j] + h*(1.0/6.0)*(k_1_x[j]+2*k_2_x[j]+2*k_3_x[j]+k_4_x[j]);
          y[j] = y[j] + h*(1.0/6.0)*(k_1_y[j]+2*k_2_y[j]+2*k_3_y[j]+k_4_y[j]);
          z[j] = z[j] + h*(1.0/6.0)*(k_1_z[j]+2*k_2_z[j]+2*k_3_z[j]+k_4_z[j]);
          
          v_x[j] = v_x[j] + h*(1.0/6.0)*(k_1_v_x[j]+2*k_2_v_x[j]+2*k_3_v_x[j]+k_4_v_x[j]);
          v_y[j] = v_y[j] + h*(1.0/6.0)*(k_1_v_y[j]+2*k_2_v_y[j]+2*k_3_v_y[j]+k_4_v_y[j]);
          v_z[j] = v_z[j] + h*(1.0/6.0)*(k_1_v_z[j]+2*k_2_v_z[j]+2*k_3_v_z[j]+k_4_v_z[j]);
      }
      for(k=0;k<n_points;k++){
          fprintf(in," %f %f %f ", x[k], y[k], z[k]);
      }
      kinetic = get_kinetic(x, y, z, v_x, v_y, v_z, a_x, a_y, a_z, mass, n_points);
      potential = get_potential(x, y, z, v_x, v_y, v_z, a_x, a_y, a_z, mass, n_points);
      fprintf(in,"%f %f \n",kinetic, potential);
  }
    fclose(in);
    
}