Beispiel #1
0
/*!
	\brief This method makes sure a new MIME type will be selected.
	
	If it's not in the list yet, it will be selected as soon as it's
	added.
*/
void
MimeTypeListView::SelectNewType(const char* type)
{
	if (SelectType(type))
		return;

	fSelectNewType = type;
}
Beispiel #2
0
void FloatGA::FGA_init()
{
  Sprng *sprng_stream;
  sprng_stream = SelectType ( 1 );
  sprng_stream->init_rng( 0, 3, SEED, SPRNG_DEFAULT );

  std::cout << "\nInitializing Function Definition...";
  initrandomnormaldeviate();
  allocate_memory();
  initialize();
  std::cout << " Done!\n";

  std::cout << "\nInitializing GA...";
  double temp [ nreal * 5 ];
  std::vector<FloatGA*> init_generation_set;

  for ( int i = 0; i < n_genes; i++ )
    gene_domain_range [ i ] = gene_upper_bound [ i ] - gene_lower_bound [ i ];

  for ( int i = 0; i < nreal*5; i++ )
    temp [ i ] = 0.0;

  for ( int i = 0; i < pop_size; i++ )
    {
      temp_fga_population [ i ].randomize_genome(
          sprng_stream );
      temp_fga_population [ i ].evaluate( sprng_stream, temp );
      init_generation_set.push_back( temp_fga_population + i );
//      init_generation_set [ i ]->print();
    }
  // Decending sort
  std::sort(
      init_generation_set.begin(),
      init_generation_set.end(),
      greater_than );

  for ( int i = 0; i < pop_size; i++ )
    fga_population [ i ].copy( init_generation_set [ i ] );

  std::cout << " Done!\n";
}
Beispiel #3
0
int main(int argc, char *argv[])
{
  int streamnum, nstreams;
  Sprng *stream;
  double rn;
  int i, myid, nprocs, len;
  MPI_Status  status;
  char *packed;
  int gtype;  
            
  MPI_Init(&argc, &argv);	
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  if(nprocs < 2)
  {
    fprintf(stderr,"ERROR: At least 2 processes required\n");
    MPI_Finalize();
    exit(1);
  }

  if(myid == 0)
  {
#include "gen_types_menu.h"
    printf("Type in a generator type (integers: 0,1,2,3,4,5):  ");
    scanf("%d", &gtype);
  }
  MPI_Bcast(&gtype,1,MPI_INT,0,MPI_COMM_WORLD );

  if (myid==0)	
  {
    streamnum = 0;
    nstreams = 1;
    stream = SelectType(gtype);
    stream->init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT);
    printf("\n\nProcess %d: Print information about stream:\n",myid);
    stream->print_sprng();

    printf("Process %d: Print 2 random numbers in [0,1):\n", myid);
    for (i=0;i<2;i++)
    {
      rn = stream->sprng();	
      printf("Process %d: %f\n", myid, rn);
    }

    len = stream->pack_sprng(&packed); 
    MPI_Send(&len, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); 
    MPI_Send(packed, len, MPI_BYTE, 1, 0, MPI_COMM_WORLD); 

    free(packed);
    nstreams = stream->free_sprng();

    printf(" Process 0 sends stream to process 1\n");
    printf(" %d generators now exist on process 0\n", nstreams);
  }
  else if(myid == 1)
  { 
    MPI_Recv(&len, 1, MPI_INT, 0, MPI_ANY_TAG,
             MPI_COMM_WORLD, &status);

    if ((packed = (char *) malloc(len)) == NULL)
    {
      fprintf(stderr,"ERROR: process %d: Cannot allocate memory\n", myid);
      MPI_Finalize();
      exit(1);
    }

    MPI_Recv(packed, len, MPI_BYTE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); 
    
    stream = SelectType(gtype);
    stream->unpack_sprng(packed); 

    printf(" Process 1 has received the packed stream\n");
    printf("Process %d: Print information about stream:\n",myid);
    stream->print_sprng();
    free(packed);

    printf(" Process 1 prints 2 numbers from received stream:\n");
    for (i=0;i<2;i++)		
      {
	rn = stream->sprng();
	printf("Process %d: %f\n", myid, rn);
      }
   
      stream->free_sprng();   
  }

  MPI_Finalize();		

  return 0;
}
int main(int argc, char *argv[])
{
  int streamnum, nstreams;
  Sprng *stream;
  double rn;
  int i, myid, nprocs, j, k;
  int n = 3;
  int gtype;  /*---    */
  
  double *A, *eVec, *eVal;
  
  /*************************** MPI calls ***********************************/

  MPI_Init(&argc, &argv);	/* Initialize MPI                          */
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);	/* find process id                 */
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes      */

  /************************** Initialization *******************************/

  streamnum = myid;	
  nstreams = nprocs;		/* one stream per processor                */
/*--- node 0 is reading in a generator type */
  if(myid == 0)
  {
      if(argc < 2){
          gtype = 0;
      }else{
          gtype = atoi(argv[1]);
    }
      
}
  MPI_Bcast(&gtype,1,MPI_INT,0,MPI_COMM_WORLD );
    
  stream = SelectType(gtype);
  stream->init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT);	/* initialize stream */
  
  A = (double*)malloc(sizeof(double) * n * n);
  eVec = (double*)malloc(sizeof(double) * n * n);
  eVal = (double*)malloc(sizeof(double) * n);
  
  //printf("\n\nProcess %d, print information about stream:\n", myid);
  //stream->print_sprng();

  /*********************** print random numbers ****************************/

//  for (i=0;i<3;i++)
//  {
//    rn = stream->sprng();		/* generate double precision random number */
//    printf("Process %d, random number %d: %.14f\n", myid, i+1, rn);
//  }

  /*************************** free memory *********************************/

  /******************* Fill Random Matrix *******************************/
  for( i = 0; i < n; i++){
        A[i + i*n] = stream->sprng();
        for(j = 0; j < i; j++){
            A[i + j*n] = stream->sprng();
            A[i*n + j] = A[i + j*n];
        }
      
  }
  
  find_eigen_vectors(A, n, eVal, eVec );

  /***************** Output data into a MATLAB readible file ***********/
  char *outfilename;

  outfilename = (char*)malloc(sizeof(char) * 56);
  
  sprintf( outfilename, "output%d.m", myid);
  
  //char *mode="r";
  FILE *ifp;
  if((ifp = fopen(outfilename, "w"))==NULL){
      printf("Could not open file!!!\n");
       return 2;
    }
 fprintf(ifp, "A%d = [ ", myid);
 for(i = 0; i < n; i++){
	for(j=0;j<n; j++){
		fprintf(ifp, " %1.15lg ", A[i+j*n]); 

	}
	if(i < n-1)
		fprintf(ifp, ";");
	else
		fprintf(ifp, "];\n");
} 

 fprintf(ifp, "eVec%d = [ ", myid);
 for(i = 0; i < n; i++){
        for(j=0;j<n; j++){
                fprintf(ifp, " %1.15lg ", eVec[i+j*n]); 

        }
        if(i < n-1)
                fprintf(ifp, ";");
        else
                fprintf(ifp, "];\n");
}

fprintf(ifp, "eVal%d = [ ", myid);
 for(i = 0; i < n; i++){
                fprintf(ifp, " %1.15lg ", eVal[i]); 

        if(i < n-1)
                fprintf(ifp, ";");
        else
                fprintf(ifp, "];\n");

}



  fclose(ifp);



  /************************* Free Memory and End *******************/
  stream->free_sprng();		/* free memory used to store stream state  */

  free(A);
  free(eVec);
  free(eVal);
  free(outfilename);
  
  
  MPI_Finalize();		/* Terminate MPI                           */

  return 0;
}
Beispiel #5
0
void FloatGA::FGA_run()
{

  for ( int i = 0; i < parallel_blocks; i++ )
    {
      sprng_streams [ i ] = SelectType ( 1 );
      sprng_streams [ i ]->init_rng( 0, 3, SEED + i + 1, SPRNG_DEFAULT );
    }

  timeval generation_timeA, generation_timeB;
  double elapsedTime;

  unsigned long long int g = 0;
  double min_fitness, avg_fitness, max_fitness, best_solution_fitness;
  double max_time, min_time;

  best_solution_fitness = max_time = -INF;
  min_time = INF;

  std::vector<FloatGA*> new_generation_set;
  std::vector<FloatGA*>::iterator new_generation_it;

  // GA Initialization
  FGA_init();

  std::cout << "\nRunning GA...";
  while ( g < generations )
    {

      if ( DEBUG == 3 )
        {
          std::cout << "POP Print <START>\n";
          for ( unsigned int i = 0; i < pop_size; i++ )
            {
              std::cout << "[" << i << "]" << std::endl;
              fga_population [ i ].print();
            }
          std::cout << "POP Print <END>\n";
        }

      gettimeofday( &generation_timeA, NULL );

      unsigned int new_generation_size = 0;

      // GA Parent Selection
      for ( unsigned int i = 0; i < pop_size; i++ )
        {
          double x = sprng_streams [ omp_get_thread_num() ]->sprng();
          unsigned int tournament_idxs [ tournament_size ];
          if ( x <= cross_over_rate )
            {
              for ( unsigned char n = 0; n < tournament_size; n++ )
                tournament_idxs [ n ] =
                    uint_range_rand( sprng_streams [ omp_get_thread_num() ], 0, pop_size );

              parents [ i ] =
                tournament_idxs [
                                 fga_stochastic_tournament_selector(
                    sprng_streams [ omp_get_thread_num() ],
                    tournament_idxs ) ];
            }
          else
            {
              parents [ i ] = -1;
            }
        }

      if ( DEBUG == 2 )
        {
          std::cout << "\n\tParents <start>" << std::endl;
          for ( unsigned int i = 0; i < pop_size; i++ )
            std::cout << "[" << i << "]"  << parents[i] << std::endl;
          std::cout << "\tParents <end>\n" << std::endl;
        }

      // GA Offspring Generation ( Cross over )
      if ( cross_type == 0 )
        for ( unsigned int i = 0; i < pop_size; i++ )
          {
            temp_fga_population [ i ].copy( fga_population + i );
            new_generation_size++;
          }
      else
        {
          #pragma omp parallel for \
          num_threads ( parallel_blocks ) \
          schedule ( static, ( pop_size + parallel_blocks ) / parallel_blocks )
          for ( unsigned int i = 0; i < pop_size; i++ )
            {
              unsigned int parent = parents [ i ];
              unsigned int offspring_idx = 0;
              if ( parent != -1 )
                {
                  #pragma omp critical
                  {
                    offspring_idx = new_generation_size;
                    new_generation_size += offspring_pairs * 2;
                  }

                  unsigned int curr_cp;
                  unsigned int cp [ cross_over_points ];

                  for ( unsigned int j = 0; j < offspring_pairs; j++ )
                    {

                      switch ( cross_type )
                      {
                      case 1:
                        curr_cp = 0;
                        // Generate new random cross points
                        for ( int j = 0; j < cross_over_points; j++ )
                          cp [ j ] = curr_cp =
                              uint_range_rand(
                                  sprng_streams [ omp_get_thread_num() ],
                                  curr_cp,
                                  n_genes - cross_over_points + j );

                        cross_over_simple( cp,
                            alpha,
                            fga_population [ i ].genome,
                            fga_population [ parent ].genome,
                            temp_fga_population [ offspring_idx ].genome,
                            temp_fga_population [ offspring_idx + 1 ].genome);
                        break;
                      case 2:
                        cross_over_single(
                            uint_range_rand(
                                sprng_streams [ omp_get_thread_num() ],
                                0,
                                n_genes ),
                            alpha,
                            fga_population [ i ].genome,
                            fga_population [ parent ].genome,
                            temp_fga_population [ offspring_idx ].genome,
                            temp_fga_population [ offspring_idx + 1 ].genome);
                        break;
                      case 3:
                        cross_over_whole(
                            alpha,
                            fga_population [ i ].genome,
                            fga_population [ parent ].genome,
                            temp_fga_population [ offspring_idx ].genome,
                            temp_fga_population [ offspring_idx + 1 ].genome);
                        break;
                      default:
                        cross_over_duplicate(
                            fga_population [ i ].genome,
                            fga_population [ parent ].genome,
                            temp_fga_population [ offspring_idx ].genome,
                            temp_fga_population [ offspring_idx + 1 ].genome);
                        break;
                      }
                      // Reset temporal population fitness
                      temp_fga_population [ offspring_idx++ ].fitness = -INF;
                      temp_fga_population [ offspring_idx++ ].fitness = -INF;
                    }
                }
            }
        }

      // Fill up population set
      for ( unsigned int i = elite_set_size; new_generation_size < ( pop_size - elite_set_size ); i++ )
        {
          temp_fga_population [ new_generation_size ].copy(
              fga_population + i );
          new_generation_size++;
        }

      // New Generation Mutation
      #pragma omp parallel for \
      num_threads ( parallel_blocks ) \
      schedule ( static, ( new_generation_size + parallel_blocks ) / parallel_blocks )
      for ( unsigned int i = 0; i < new_generation_size; i++ )
        {
          switch ( mutation_type )
          {
          case 1:
            temp_fga_population [ i ].mutate_uniform(
                sprng_streams [ omp_get_thread_num() ] );
            break;
          case 2:
            temp_fga_population [ i ].mutate_nonuniform(
                sprng_streams [ omp_get_thread_num() ], g );
            break;
          default:
            break;
          }
        }

      // Copy Elite
      for ( unsigned int i = 0; i < elite_set_size; i++ )
        {
          temp_fga_population [ new_generation_size ].copy(
              fga_population + i );
          new_generation_size++;
        }

      // New Generation Evaluation
      #pragma omp parallel for \
      num_threads ( parallel_blocks ) \
      schedule ( static, ( new_generation_size + parallel_blocks ) / parallel_blocks )
      for ( unsigned int i = 0; i < new_generation_size; i++ )
        {
          double temp[ nreal*5 ];
          temp_fga_population [ i ].evaluate(
              sprng_streams [ omp_get_thread_num() ],
              temp );
        }

      if ( DEBUG == 3 )
        {
          std::cout << "\tTEMP POP Print <START>\n";
          for ( unsigned int i = 0; i < new_generation_size; i++ )
            {
              std::cout << "[" << i << "]" << std::endl;
              temp_fga_population [ i ].print();
            }
          std::cout << "\tTEMP POP Print <END>\n";
        }

      // Sort new generation (ascending fitness)
      for( unsigned int i = 0; i < new_generation_size; i++ )
        new_generation_set.push_back( temp_fga_population + i );

      std::sort( new_generation_set.begin(), new_generation_set.end(), greater_than );

      // Copy back to population array
      for ( unsigned int i = 0; i < pop_size; i++ )
        {
          fga_population [ i ].copy( new_generation_set [ i ] );
//          fga_population [ i ].print();
        }

      // Free Memory
      new_generation_set.clear();
      g++;

      gettimeofday( &generation_timeB, NULL );

      // sec to ms
      elapsedTime =
          ( generation_timeB.tv_sec - generation_timeA.tv_sec ) * 1000.0;
      // us to ms
      elapsedTime +=
          ( generation_timeB.tv_usec - generation_timeA.tv_usec ) / 1000.0;

      // Print Partial Results / Statistics

      if ( DEBUG == -1 )
        {
          if ( elapsedTime > max_time )
            max_time = elapsedTime;
          if ( elapsedTime < min_time )
            min_time = elapsedTime;

          fga_pop_stats ( min_fitness, avg_fitness, max_fitness );

          std::cout << g <<
              ' ' << min_fitness <<
              ' ' << avg_fitness <<
              ' ' << max_fitness <<
              ' ' << elapsedTime << ";\n";
        }
      else
        {
          std::cout << "\rGeneration: " << g << " / " << generations <<
              " in " << elapsedTime << " ms.";
        }

      // Update/Print best solution

      if ( best_solution_fitness < fga_population [ 0 ].fitness )
        {
          best_solution_fitness = fga_population [ 0 ].fitness;
          if ( DEBUG == 4 )
            {
              std::cout << std::endl;
              fga_population [ 0 ].print();
              std::cout.flush();
            }
        }

      if ( threshold != 0 &&
          abs( best_solution_fitness - optimum_value ) < threshold )
        break;
    }

  free_memory();
  std::cout << " Done!\n";

  std::cout << "Best Individual:\n\n";
  fga_population [ 0 ].print();
}