Пример #1
0
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Provide two arguments: a probability of failure per bit and a message.\n");
    printf("More than one error can hit the same bit, and a character is made of 1 byte (8 bits).\n");
  }
  const size_t size = strlen(argv[2]);
  char *received = malloc(size + 1);
  strcpy(received, argv[2]);
  const double f = atof(argv[1]);

  gsl_rng *rng = gsl_rng_alloc(gsl_rng_taus2);
  gsl_rng_set(rng, time(NULL)); // Seed with time
  const int errors = gsl_ran_binomial(rng, f, size * 8);
  for (int i = 0; i < errors; ++i) {
    unsigned int toflip = (int)(gsl_rng_uniform(rng) * size);
    bitflip(received + toflip, rng);
  }
  printf("Message sent:\n%s\n", argv[2]);
  printf("Message received:\n%s\n", received);
  printf("Bits flipped: %d\n", errors);
  free(received);
  return EXIT_SUCCESS;
}
Пример #2
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;
  */
}
Пример #3
0
/**
 * GA with a single fitness function
 */
int evolve_both(int* first, int* second, int* winner, 
				int* loser, int* population) {
		int round = 0;
		float fitness_first, fitness_second;
		int first_bitflipped[GENOTYPE_LENGTH], 
			second_bitflipped[GENOTYPE_LENGTH];
		do {
				round++;
				// generate the complementary individuals
				bitflip(first, first_bitflipped);
				bitflip(second, second_bitflipped);

				// mark them as tested in the population
				population[hash(first)] = 1;
				population[hash(second)] = 1;

				// evaluate first and its complementary
				fitness_first = eval_fit(first, first_bitflipped);

				// evaluate second 
				fitness_second = eval_fit(second, second_bitflipped);

				// optimal solution?
				if ( fitness_first == 0.0 )
				{
						copy_individual(first, winner);
						copy_individual(second, loser);
						break;
				}
				if ( fitness_second == 0.0 )
				{
						copy_individual(second, winner);
						copy_individual(first, loser);
						break;
				}

				if ( fitness_first < fitness_second )
				{
						// first wins
						// mutate loser
						if ( mutate_1gene(second, population) < 0 )
						{
								// if no more mutations are possible
								copy_individual(first, winner);
								copy_individual(second, loser);
								break;
						}
				}
				else
				{
						// second wins
						// mutate loser
						if ( mutate_1gene(first, population) < 0 )
						{
								copy_individual(second, winner);
								copy_individual(first, loser);
								break;
						}
				}

		} while ( 1 );

		return round;
}
Пример #4
0
int main ( int argc, char **argv ) {
		// check if param is given and used to seed
		if ( argc <= 1 )
		{
				printf("Usage: %s <seed>\n", argv[0]);
				return -1;
		}

		// get the seed from the user
		int seed = atoi(argv[1]);
		// initialize the random generator
		srand(seed);

		// the most-used variable around :)
		int i;

		/////

		// initialize the population
		int population[MAX_MUTATIONS];
		for ( i = 0 ; i < MAX_MUTATIONS ; i++ )
				population[i] = 0;

		// generate the first random individual
		int a[GENOTYPE_LENGTH];
		rand_init(a);
		// mark it as known in the population
		population[hash(a)] = 1;

		// generate the second random individual (different)
		int b[GENOTYPE_LENGTH];
		do { 
				rand_init(b);
		} while ( population[hash(b)] != 0 );
		population[hash(b)] = 1;

		int winner[GENOTYPE_LENGTH], loser[GENOTYPE_LENGTH];
		int winner_bitflipped[GENOTYPE_LENGTH], 
			loser_bitflipped[GENOTYPE_LENGTH];

		/////

		// the actual GA
		int round_tot = evolve_both(a, b, winner, loser, population);

		/////
	
		// regenerate the complementary individuals 
		bitflip(winner, winner_bitflipped);
		bitflip(loser, loser_bitflipped);

		printf("%d (%d %d)\t", round_tot, sum(winner), 
						prod(winner_bitflipped));
		print_individual(winner);
		printf("\t");
		print_individual(winner_bitflipped);
		printf("\n");


		return 0;
}