Example #1
0
int main(int argc, char **argv) 
{
    MPI_Init(&argc, &argv); 
    atexit([]{ MPI_Finalize(); }); 

    const std::size_t population = 100; 
    const std::size_t generations = 200; 
    const std::size_t variables = 5; 
    const std::size_t max_crossover_attempts = 100; 
    const float       migration_prob = .2f;
    const float       elite_percentage = .3f;

    std::vector< float > min_constraints(variables); 
    std::vector< float > max_constraints(variables); 
    fill(begin(min_constraints), end(min_constraints), -5.12f); 
    fill(begin(max_constraints), end(max_constraints), +5.12f); 

    libga_mpi::sga< float > sga(population, elite_percentage
        , min_constraints, max_constraints); 
    libga_mpi::island_model< libga_mpi::sga< float >> im(sga, migration_prob); 

    const bool success 
	= sga.start(rastrigin, libga_mpi::xover::blxa<float>()
	      , [generations](std::size_t g) { return g == generations; }
		, [&](const float *x, std::size_t n) 
		{ return is_within_constraints(x, n
		       , min_constraints, max_constraints); }
		, libga_mpi::mutation::mutate_none<float>()
		, max_crossover_attempts); 
    
    if(success) 
    {
	int rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
	const std::size_t f = fittest(sga);
	std::ofstream stream("rastrigin_data_" + std::to_string(rank)); 
	for(std::size_t i=0; i < variables; ++i) 
	    stream << im.island().genome()[f*variables + i] << " "; 
	stream << im.island().ospace()[f]; 
    }
    else
    {
	std::cout << "Algorithm stalled. " << std::endl; 
	return 1; 
    }
    return 0; 
}
Example #2
0
File: main.c Project: Miguelrn/TFG
int main(int argc, char **argv) {


	//Variables para calcular el tiempo
	double t0, t1;

	float *imagen;
	char *tipo;
	int lineas, muestras, bandas, i;
	int endmember;
	pos *solucion;

	//Tener menos de 3 argumentos es incorrecto
	if (argc < 2) {
		fprintf(stderr, "Uso incorrecto de los parametros ./exe 'ruta imagen' 'numero de Endmemebers'\n");
		exit(1);
	}

	endmember = atoi(argv[2]);
	imagen = lectura_archivo(argv[1], &muestras, &lineas, &bandas, tipo);
	//for(i = 0; i < muestras; i++) printf("%f - ",imagen[i]);


	t0 = get_time();
	solucion = sga(imagen, endmember, muestras, lineas, bandas);
	t1 = get_time();

	if(DEBUG) printf("Ha tardado en ejecutarse: %f \n", t1-t0);

	for(i = 0; i < endmember; i++ ){
		printf("%d: %d - %d\n",i+1,solucion[i].columnas,solucion[i].filas);//cuprite -> (298,194)(39,208)(298,206)(297,193)(63,162)
	}


	return 0;

}
size_t	subdark(const ImageSequence&, ImageMean<T>& im,
	const Subgrid grid, unsigned int k = 3) {
	debug(LOG_DEBUG, DEBUG_LOG, 0, "processing subgrid %s",
		grid.toString().c_str());
	// we also need the mean of the image to decide which pixels are
	// too far off to consider them "sane" pixels
	T	mean = im.mean(grid);
	T	var = im.variance(grid);

	// now find out which pixels are bad, and mark them using NaNs.
	// we consider pixels bad if the deviate from the mean by more
	// than three standard deviations
	T	stddevk = k * sqrt(var);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "found mean: %f, variance: %f, "
		"stddev%u = %f", mean, var, k, stddevk);
	size_t	badpixelcount = 0;
	SubgridAdapter<T>	sga(*im.image, grid);
	ImageSize	size = sga.getSize();
	for (int x = 0; x < size.width(); x++) {
		for (int y = 0; y < size.height(); y++) {
			T	v = sga.pixel(x, y);
			// skip NaNs
			if (v != v) {
				break;
			}
			if (fabs(v - mean) > stddevk) {
				sga.writablepixel(x, y)
					= std::numeric_limits<T>::quiet_NaN();
				badpixelcount++;
			}
		}
	}

	debug(LOG_DEBUG, DEBUG_LOG, 0, "found %u bad pixels", badpixelcount);
	return badpixelcount;
}
Example #4
0
void main_function()
{
  const unsigned SIZE = 8;
  unsigned i, j;
  eoBooleanGenerator gen;

  Chrom chrom(SIZE), chrom2;
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));

  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = false; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));

  std::cout << "chrom.className() = " << chrom.className() << std::endl;

  std::cout << "chrom:  " << chrom << std::endl
       << "chrom2: " << chrom2 << std::endl;

  std::ostringstream os;
  os << chrom;
  std::istringstream is(os.str());
  is >> chrom2; chrom.fitness(binary_value(chrom2));

  std::cout << "\nTesting reading, writing\n";
  std::cout << "chrom:  " << chrom << "\nchrom2: " << chrom2 << '\n';

  std::fill(chrom.begin(), chrom.end(), false);
  std::cout << "--------------------------------------------------"
       << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl;

  eoInitFixedLength<Chrom>
      random(chrom.size(), gen);

  random(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinRandom ............ " << chrom << std::endl;

  eoOneBitFlip<Chrom> bitflip;
  bitflip(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBitFlip .............. " << chrom << std::endl;

  eoBitMutation<Chrom> mutation(0.5);
  mutation(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl;

  eoBitInversion<Chrom> inversion;
  inversion(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinInversion ......... " << chrom << std::endl;

  eoBitNext<Chrom> next;
  next(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinNext .............. " << chrom << std::endl;

  eoBitPrev<Chrom> prev;
  prev(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinPrev .............. " << chrom << std::endl;

  std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom));
  std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2));
  std::cout << "--------------------------------------------------"
       << std::endl << "eoBinOp's aplied to ... "
       << chrom << " " << chrom2 << std::endl;

  eo1PtBitXover<Chrom> xover;
  std::fill(chrom.begin(), chrom.end(), false);
  std::fill(chrom2.begin(), chrom2.end(), true);
  xover(chrom, chrom2);
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
  std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl;

  for (i = 1; i < SIZE; i++)
    {
      eoNPtsBitXover<Chrom> nxover(i);
      std::fill(chrom.begin(), chrom.end(), false);
      std::fill(chrom2.begin(), chrom2.end(), true);
      nxover(chrom, chrom2);
      chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
      std::cout << "eoBinNxOver(" << i << ") ........ "
	   << chrom << " " << chrom2 << std::endl;
    }

  for (i = 1; i < SIZE / 2; i++)
    for (j = 1; j < SIZE / 2; j++)
      {
	eoBitGxOver<Chrom> gxover(i, j);
	std::fill(chrom.begin(), chrom.end(), false);
	std::fill(chrom2.begin(), chrom2.end(), true);
	gxover(chrom, chrom2);
	chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
	std::cout  << "eoBinGxOver(" << i << ", " << j << ") ..... "
	      << chrom << " " << chrom2 << std::endl;
      }

    // test SGA algorithm
    eoGenContinue<Chrom> continuator1(50);
    eoFitContinue<Chrom> continuator2(65535.f);

    eoCombinedContinue<Chrom> continuator(continuator1, continuator2);

    eoCheckPoint<Chrom> checkpoint(continuator);

    eoStdoutMonitor monitor;

    checkpoint.add(monitor);

    eoSecondMomentStats<Chrom> stats;

    monitor.add(stats);
    checkpoint.add(stats);

    eoProportionalSelect<Chrom> select;
    eoEvalFuncPtr<Chrom>  eval(binary_value);

    eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint);

    eoInitFixedLength<Chrom> init(16, gen);
    eoPop<Chrom> pop(100, init);

    apply<Chrom>(eval, pop);

    sga(pop);

    pop.sort();

    std::cout << "Population " << pop << std::endl;

    std::cout << "\nBest: " << pop[0].fitness() << '\n';

  /*

    Commented this out, waiting for a definite decision what to do with the mOp's

    // Check multiOps
    eoMultiMonOp<Chrom> mOp( &next );
    mOp.adOp( &bitflip );
    std::cout << "before multiMonOp............  " << chrom << std::endl;
    mOp( chrom );
    std::cout << "after multiMonOp .............. " << chrom << std::endl;

    eoBinGxOver<Chrom> gxover(2, 4);
    eoMultiBinOp<Chrom> mbOp( &gxover );
    mOp.adOp( &bitflip );
    std::cout << "before multiBinOp............  " << chrom << " " << chrom2 << std::endl;
    mbOp( chrom, chrom2 );
    std::cout << "after multiBinOp .............. " << chrom << " " << chrom2 <<std::endl;
  */
}
T	ImageMean<T>::variance(const Subgrid grid) const {
	Variance<T, T>	varianceoperator;
	ConstSubgridAdapter<T>	sga(*image, grid);
	return varianceoperator(sga);
}
T	ImageMean<T>::mean(const Subgrid grid) const {
	Mean<T, T>	meanoperator;
	ConstSubgridAdapter<T>	sga(*image, grid);
	return meanoperator(sga);
}