Esempio n. 1
0
			static void append(InputIterator first1,
			                   InputIterator last1,
			                   const Distance length1,
			                   InputIterator first2,
			                   InputIterator last2,
			                   const Distance length2,
			                   OutputIterator result)
			{
				const auto min = std::min(length1, length2);

				if (min < 3)
				{
					throw std::length_error("Chromosome too short.");
				}

				random::RandomEngine eng = random::default_engine();
				std::uniform_int_distribution<typename std::remove_const<decltype(min)>::type> dist1(0, min - 2);
				const auto separator1 = dist1(eng);

				std::uniform_int_distribution<typename std::remove_const<decltype(min)>::type> dist2(separator1 + 1, length2 - 1);
				const auto separator2 = dist2(eng);

				Chromosome offspring(length2);

				std::copy_n(first1, separator1, begin(offspring));
				std::copy_n(first2 + separator1, separator2 - separator1, begin(offspring) + separator1);
				std::copy_n(first1 + separator2, length2  - separator2, begin(offspring) + separator2);

				*result++ = offspring;
			}
Esempio n. 2
0
void testSelectMany(eoSelect<EOT> & _select, std::string _name)
{
    unsigned i;
  std::cout << "\n\n" << fitnessType + _name << std::endl;
  std::cout << "===============\n";

    eoDummyPop parents(parentsOrg);
    eoDummyPop offspring(0);

    // do the selection
    _select(parents, offspring);

    // compute stats
    std::vector<unsigned> nb(parents.size(), 0);
    for (i=0; i<offspring.size();  i++)
      {
	unsigned trouve = isInPop<Dummy>(offspring[i], parents);
	if (trouve == parents.size()) // pas trouve
	  throw std::runtime_error("Pas trouve ds parents");
	nb[trouve]++;
       }
    // dump to file so you can plot using gnuplot - dir name is hardcoded!
    std::string fName = "ResSelect/" + fitnessType + _name + ".select";
    std::ofstream os(fName.c_str());
    for (i=0; i<parents.size();  i++)
      {
	std::cout << i << " -> " << ( (double)nb[i])/offspring.size() << std::endl;
	os << i << " " << ( (double)nb[i])/offspring.size() << std::endl;
      }

}
Esempio n. 3
0
individual individualsFactory::createIndividual(individual *p1, individual *p2)
{
  individual offspring(NULL);

  switch(individualStrategyId)
  {
    case neuralIndividual:
      offspring = createNeuralIndividual();
      break;
    default:
      offspring = individual(NULL);
      break;
  }

  p1->cross(p2->getSolution(), &offspring);

  return offspring;
}
IndividualABCD IndividualABCD::operator* (const IndividualABCD&other) const
{
	float areaOfParents_A = abs(this->a - other.a); //calculating the crossover
	float areaOfParents_B = abs(this->b - other.b);
	float areaOfParents_C = abs(this->c - other.c);
	float areaOfParents_D = abs(this->d - other.d);
	float dFactor_A = ((float)rand()/(float)(RAND_MAX)) * 1.5 - 0.25; //make the area vary by -0.25 to 1.25 the original size
	float dFactor_B = ((float)rand()/(float)(RAND_MAX)) * 1.5 - 0.25;
	float dFactor_C = ((float)rand()/(float)(RAND_MAX)) * 1.5 - 0.25;
	float dFactor_D = ((float)rand()/(float)(RAND_MAX)) * 1.5 - 0.25;
	float averageOfParents_A = ((this->a + other.a) / 2);
	float averageOfParents_B = ((this->b + other.b) / 2);
	float averageOfParents_C = ((this->c + other.c) / 2);
	float averageOfParents_D = ((this->d + other.d) / 2);
	float newArea_A = areaOfParents_A * dFactor_A;
	float newArea_B = areaOfParents_B * dFactor_B;
	float newArea_C = areaOfParents_C * dFactor_C;
	float newArea_D = areaOfParents_D * dFactor_D;
	float lowerLimit_A = averageOfParents_A - (newArea_A/2);
	float lowerLimit_B = averageOfParents_B - (newArea_B/2);
	float lowerLimit_C = averageOfParents_C - (newArea_C/2);
	float lowerLimit_D = averageOfParents_D - (newArea_D/2);
	float offspring_A = ((float)rand()/(float)(RAND_MAX)) * newArea_A + lowerLimit_A; //child attributes depends on the possible area and a range defined by parents
	float offspring_B = ((float)rand()/(float)(RAND_MAX)) * newArea_B + lowerLimit_B;
	float offspring_C = ((float)rand()/(float)(RAND_MAX)) * newArea_C + lowerLimit_C;
	float offspring_D = ((float)rand()/(float)(RAND_MAX)) * newArea_D + lowerLimit_D;
	float mutate_area = 0.02; //mutation range from -0.01 to 0.01
	float mutate_lowerLimit_A = offspring_A - 0.01;
	float mutate_lowerLimit_B = offspring_B - 0.01;
	float mutate_lowerLimit_C = offspring_C - 0.01;
	float mutate_lowerLimit_D = offspring_D - 0.01;
	offspring_A = ((float)rand()/(float)(RAND_MAX)) * mutate_area + mutate_lowerLimit_A;
	offspring_B = ((float)rand()/(float)(RAND_MAX)) * mutate_area + mutate_lowerLimit_B;
	offspring_C = ((float)rand()/(float)(RAND_MAX)) * mutate_area + mutate_lowerLimit_C;
	offspring_D = ((float)rand()/(float)(RAND_MAX)) * mutate_area + mutate_lowerLimit_D;
	IndividualABCD offspring(offspring_A,offspring_B,offspring_C,offspring_D);
	return offspring;
}
Esempio n. 5
0
int the_main(int argc, char **argv)
{ 
  eoParser parser(argc, argv);
  eoValueParam<unsigned int> parentSizeParam(10, "parentSize", "Parent size",'P');
  parser.processParam( parentSizeParam );
    unsigned int pSize = parentSizeParam.value();

  eoValueParam<unsigned int> offsrpringSizeParam(10, "offsrpringSize", "Offsrpring size",'O');
  parser.processParam( offsrpringSizeParam );
    unsigned int oSize = offsrpringSizeParam.value();

  eoValueParam<unsigned int> tournamentSizeParam(2, "tournamentSize", "Deterministic tournament size",'T');
  parser.processParam( tournamentSizeParam );
    unsigned int tSize = tournamentSizeParam.value();

  eoValueParam<double> tournamentRateParam(0.75, "tournamentRate", "Stochastic tournament rate",'R');
  parser.processParam( tournamentRateParam );
    double tRate = tournamentRateParam.value();

  eoValueParam<double> sParentsElitismRateParam(0.1, "sParentsElitismRateParam", "Strong elitism rate for parents",'E');
  parser.processParam( sParentsElitismRateParam );
    double sParentsElitismRate = sParentsElitismRateParam.value();

  eoValueParam<double> sParentsEugenismRateParam(0, "sParentsEugenismRateParam", "Strong Eugenism rate",'e');
  parser.processParam( sParentsEugenismRateParam );
    double sParentsEugenismRate = sParentsEugenismRateParam.value();

  eoValueParam<double> sOffspringElitismRateParam(0, "sOffspringElitismRateParam", "Strong elitism rate for parents",'E');
  parser.processParam( sOffspringElitismRateParam );
    double sOffspringElitismRate = sOffspringElitismRateParam.value();

  eoValueParam<double> sOffspringEugenismRateParam(0, "sOffspringEugenismRateParam", "Strong Eugenism rate",'e');
  parser.processParam( sOffspringEugenismRateParam );
    double sOffspringEugenismRate = sOffspringEugenismRateParam.value();

    if (parser.userNeedsHelp())
      {
        parser.printHelp(std::cout);
        exit(1);
      }

    unsigned i;

    std::cout << "Testing the replacements\nParents SIze = " << pSize 
	 << " and offspring size = " << oSize << std::endl;

    rng.reseed(42);


    eoDummyPop orgParents(pSize);
    eoDummyPop orgOffspring(oSize);

    // initialize so we can recognize them later!
    for (i=0; i<pSize; i++)
      orgParents[i].fitness(2*i+1);
    for (i=0; i<oSize; i++)
      orgOffspring[i].fitness(2*i);

std::cout << "Initial parents (odd)\n" << orgParents << "\n And initial offsprings (even)\n" << orgOffspring << std::endl;

    // now the ones we're going to play with
    eoDummyPop parents(0);
    eoDummyPop offspring(0);

// the replacement procedures under test
    eoGenerationalReplacement<Dummy> genReplace;
    eoPlusReplacement<Dummy> plusReplace;
    eoEPReplacement<Dummy> epReplace(tSize);
    eoCommaReplacement<Dummy> commaReplace;
    eoWeakElitistReplacement<Dummy> weakElitistReplace(commaReplace);
    // the SSGA replacements
    eoSSGAWorseReplacement<Dummy> ssgaWorseReplace;
    eoSSGADetTournamentReplacement<Dummy> ssgaDTReplace(tSize);
    eoSSGAStochTournamentReplacement<Dummy> ssgaDSReplace(tRate);

    // here we go
    // Generational
    parents = orgParents;
    offspring = orgOffspring;

    std::cout << "eoGenerationalReplacement\n";
    std::cout << "=========================\n";
    genReplace(parents, offspring);
std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (orogonally even\n" << offspring << std::endl;

    // Plus
    parents = orgParents;
    offspring = orgOffspring;

    std::cout << "eoPlusReplacement\n";
    std::cout << "=================\n";
    plusReplace(parents, offspring);
std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;

    // EP (proche d'un PLUS
    parents = orgParents;
    offspring = orgOffspring;

    std::cout << "eoEPReplacement\n";
    std::cout << "===============\n";
    epReplace(parents, offspring);
std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;

    // Comma
    parents = orgParents;
    offspring = orgOffspring;

    if (parents.size() > offspring.size() )
	std::cout << "Skipping Comma Replacement, more parents than offspring\n";
    else
      {
	std::cout << "eoCommaReplacement\n";
	std::cout << "==================\n";
	commaReplace(parents, offspring);
	std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;

	// Comma with weak elitism
	parents = orgParents;
	offspring = orgOffspring;

	std::cout << "The same, with WEAK elitism\n";
	std::cout << "===========================\n";
	weakElitistReplace(parents, offspring);
	std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
      }

	// preparing SSGA replace worse
	parents = orgParents;
	offspring = orgOffspring;

    if (parents.size() < offspring.size() )
	std::cout << "Skipping all SSGA Replacements, more offspring than parents\n";
    else
      {
	std::cout << "SSGA replace worse\n";
	std::cout << "==================\n";
	ssgaWorseReplace(parents, offspring);
	std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;

    // SSGA deterministic tournament
	parents = orgParents;
	offspring = orgOffspring;

	std::cout << "SSGA deterministic tournament\n";
	std::cout << "=============================\n";
	ssgaDTReplace(parents, offspring);
	std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;

    // SSGA stochastic tournament
	parents = orgParents;
	offspring = orgOffspring;

	std::cout << "SSGA stochastic tournament\n";
	std::cout << "==========================\n";
	ssgaDTReplace(parents, offspring);
	std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
      }

    // the general replacement
    eoDeterministicSaDReplacement<Dummy> sAdReplace(sParentsElitismRate, sParentsEugenismRate, sOffspringElitismRate, sOffspringEugenismRate);// 10% parents survive

    parents = orgParents;
    offspring = orgOffspring;

    std::cout << "General - strong elitism\n";
    std::cout << "========================\n";
    sAdReplace(parents, offspring);
    std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;


    return 1;
}
Esempio n. 6
0
int main( int argc, char ** argv ) 
{

    std::map<std::string, std::string> options;
    for (int i = 1; i < argc; i++)
    {
        std::string opt       = argv[i];
        std::string delimiter = "=";

        size_t del_pos =  opt.find(delimiter);
        if (del_pos == std::string::npos || del_pos+1 == opt.size())
        {
            std::cerr << "Ill posed command line argument: " << argv[i] << std::endl;
            return 64;
        }

        std::string key       = opt.substr(0, opt.find(delimiter));
        std::string value     = opt.substr(opt.find(delimiter)+1, opt.size());
        options[key] = value;
        std::cout << "Registering option " << key << " : " << value << std::endl;
    }


    /* Set the random seed for the gsl random generator */
    int seed;
    if (options.count(OPT_SEED) == 1)
    {
       seed = atoi ( options[OPT_SEED].c_str() );
    }
    else
    {
        seed = (int) time(0);
    }


    /* Set the initial policy file */
    std::string start_policy;
    if (options.count(OPT_START_POL_FILE) == 1)
    {
        start_policy = MDPTETRIS_DATA_PATH(options[OPT_START_POL_FILE]);
    }
    else
    {
        start_policy = MDPTETRIS_DATA_PATH("starting_policy01.dat");
    }


    /* Set the initial policy file */
    std::string piece_file;
    if (options.count(OPT_PIECE_FILE) == 1)
    {
        piece_file = MDPTETRIS_DATA_PATH(options[OPT_PIECE_FILE]);
    }
    else
    {
        piece_file = MDPTETRIS_DATA_PATH("pieces4.dat");
    }

    /* Optionally set the initial Sigma
     * For Cross entropy, this sets all the variance
     * intries to the value given.
     * In CMA this is the initial step-size.
     * */
    ExperimentOptionType<double> initialSigma(false, 1.0);
    if (options.count(OPT_INITIAL_SIGMA) == 1)
    {
        double s = atof ( options[OPT_INITIAL_SIGMA].c_str() );
        initialSigma = ExperimentOptionType<double>(true, s);
    }

    /* CMA specific lower bound.
     * The will force the CMA step-size to keep
     * the smallest eignvalue of the covariance matrix
     * multiplied by the step-size to stay above this
     * value.
     * */
    ExperimentOptionType<double> lowerBound(false, 1E-20);
    if (options.count(OPT_LOWER_BOUND) == 1)
    {
        double s = atof ( options[OPT_LOWER_BOUND].c_str() );
        lowerBound = ExperimentOptionType<double>(true, s);
    }


    /* Register option for population size */
    ExperimentOptionType<unsigned int> lambda(false, 100);
    if (options.count(OPT_LAMBDA) == 1)
    {
        int i = atoi( options[OPT_LAMBDA].c_str() );
        lambda = ExperimentOptionType<unsigned int>(true, i);
    }

    /* Register option for offspriong size */
    ExperimentOptionType<unsigned int> offspring(false, 100);
    if (options.count(OPT_OFFSPRING) == 1)
    {
        int i = atoi( options[OPT_OFFSPRING].c_str() );
        offspring = ExperimentOptionType<unsigned int>(true, i);
    }


    unsigned int nbGames = 1;
    if (options.count(OPT_NB_GAMES) == 1)
    {
        nbGames = atoi ( options[OPT_NB_GAMES].c_str() );
    }

    unsigned int nbLearnGames = 30;
    if (options.count(OPT_NB_LEARNING_GAMES) == 1)
    {
        nbLearnGames = atoi ( options[OPT_NB_LEARNING_GAMES].c_str() );
    }

    shark::CrossEntropy::SamplingNoise noiseType = shark::CrossEntropy::NONE;
    if (options.count(OPT_NOISETYPE) == 1)
    {
        switch (atoi( options[OPT_NOISETYPE].c_str() ))
        {
            case 0:
            {
                noiseType = shark::CrossEntropy::NONE;
                break;
            }
            case 1:
            {
                noiseType = shark::CrossEntropy::CONSTANT;
                break;
            }
            case 2:
            {
                noiseType = shark::CrossEntropy::LINEAR_DECREASING;
                break;
            }
            default:
            {
                break;
            }
        }
    }

    /* Cross Entropy specific for noise type */
    double noise = 0;
    if (options.count(OPT_NOISE) == 1)
    {
        noise = atof( options[OPT_NOISE].c_str() );
    }

    unsigned int boardWidth = 10;
    unsigned int boardHeight = 20;

    StoppingCriteria stoppingCriteria = STOP_BY_ITERATION;
    unsigned int maxIterations = 80;  /* Default stop at 80 iterations */
    unsigned int maxAgents = 80000;   /* Default agents to evaluate is 80000 */
    if (options.count(OPT_MAXITER) == 1)
    {
        maxIterations = atoi ( options[OPT_MAXITER].c_str() );
        stoppingCriteria = STOP_BY_ITERATION;
    }
    else if (options.count(OPT_MAX_AGENTS) == 1)
    {
        maxAgents = atoi ( options[OPT_MAX_AGENTS].c_str() );
        stoppingCriteria = STOP_BY_AGENTS_EVALUATED;
    }

    std::string outputfile = std::string("");
    if (options.count(OPT_OUTPUTNAME) == 1)
    {
        outputfile = std::string(options[OPT_OUTPUTNAME]) + ".txt";
    }

    if ( options.count(OPT_OPTIMIZER) == 1 )
    {
        if ( options[OPT_OPTIMIZER].compare("cma") == 0 )
        {
            useCMA(
                    start_policy,
                    piece_file,
                    nbGames,
                    nbLearnGames,
                    boardWidth,
                    boardHeight,
                    seed,
                    initialSigma,
                    maxIterations,
                    maxAgents,
                    stoppingCriteria,
                    std::cout,
                    outputfile,
                    lowerBound,
                    lambda,
                    offspring
            );
        }
        else if ( options[OPT_OPTIMIZER].compare("ce") == 0 )
        {
            useCE(
                    start_policy,
                    piece_file,
                    nbGames,
                    nbLearnGames,
                    boardWidth,
                    boardHeight,
                    seed,
                    initialSigma,
                    maxIterations,
                    maxAgents,
                    stoppingCriteria,
                    std::cout,
                    outputfile,
                    noiseType,
                    noise,
                    lambda,
                    offspring
            );
        }
    }
    else
    {
        std::cerr << "Optimizer not recognized!" << std::endl;
        return 64;
    }

}