本文整理汇总了C++中GAPopulation::geneticAlgorithm方法的典型用法代码示例。如果您正苦于以下问题:C++ GAPopulation::geneticAlgorithm方法的具体用法?C++ GAPopulation::geneticAlgorithm怎么用?C++ GAPopulation::geneticAlgorithm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GAPopulation
的用法示例。
在下文中一共展示了GAPopulation::geneticAlgorithm方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
// This is an implementation of speciation using the sharing method described
// by goldberg in his book. This requires a user-defined distance function in
// order to work. The distance function returns a value between
// 0 and 1 inclusive to tell us how similar two genomes are to each other.
// A value of 0 means that the two genomes are identical to each other, a
// value of 1 means they are completely different.
// A single genome is identical to itself, so d(i,i) is 0.
// If alpha is 1 then we don't use pow().
// If we have a comparator to use, use it. If not, use the comparator of
// each genome.
// We can cut in half the number of calls to the sharing function by keeping
// one half of the ixj matrix. This is because d(i,j) is the same as d(j,i).
// We cache the distances in an upper right triangular matrix stored as a
// series of floats.
// If the population is maximizing then we derate by dividing. If the
// population is minimizing then we derate by multiplying. First we check to
// see if there is a GA using the population. If there is, we use its min/max
// flag to determine whether or not we should be minimizing or maximizing. If
// there is not GA with the population, then we use the population's sort order
// as the basis for whether to minimize or maximize.
// *** This could be done with n*n/2 instead of n*n, to reduce storage, but we
// can't reduce computation any more...
// *** probably should use the diversity built-in to the population...
void
GASharing::evaluate(const GAPopulation& p) {
if(p.size() > (int)N){
delete [] d;
N = p.size();
d = new float[N*N];
}
int n = p.size();
int i, j;
if(df) {
for(i=0; i<n; i++){ // calculate and cache the distances
d[i*n+i] = 0.0; // each genome is same as itself
for(j=i+1; j<n; j++)
d[i*n+j] = d[j*n+i] = (*df)(p.individual(i), p.individual(j));
}
}
else {
for(i=0; i<n; i++){ // calculate and cache the distances
d[i*n+i] = 0.0; // each genome is same as itself
for(j=i+1; j<n; j++)
d[i*n+j] = d[j*n+i] = p.individual(i).compare(p.individual(j));
}
}
int mm;
if(_minmax == 0) {
if(p.geneticAlgorithm())
mm = p.geneticAlgorithm()->minimaxi();
else
mm = ((p.order() == GAPopulation::HIGH_IS_BEST) ?
GAGeneticAlgorithm::MAXIMIZE : GAGeneticAlgorithm::MINIMIZE);
}
else {
mm = _minmax;
}
for(i=0; i<n; i++){ // now derate the fitness of each genome
double sum = 0.0;
for(j=0; j<n; j++) {
if(d[i*n+j] < _sigma) {
if(_alpha == 1)
sum += ((d[i*n+j] >= _sigma) ? 0.0 : 1.0 - d[i*n+j]/_sigma);
else
sum += ((d[i*n+j]>=_sigma) ? 0.0 : 1.0-pow(d[i*n+j]/_sigma,_alpha));
}
}
double f;
if(mm == GAGeneticAlgorithm::MINIMIZE)
f = p.individual(i).score() * sum;
else
f = p.individual(i).score() / sum;
p.individual(i).fitness((float)f); // might lose information here!
}
}
示例2: GAPDEvaluator
void PetriDish::GAPDEvaluator( GAPopulation & pop )
{
assert(pop.size() > 0);
// Since this is a static method, get this population's associated PetriDish
PetriDish* thisPetriDish = dynamic_cast<PetriDish*>(pop.geneticAlgorithm());
assert(thisPetriDish);
#ifdef USING_GASIMPLEGA
// A workaround for oldPop not copying our Evaluator to the next set of Genomes (as opposed to hacking GASimpleGA.C)
if ( thisPetriDish->_oldPopInitialized == false )
{
thisPetriDish->_oldPopInitialized = true;
thisPetriDish->oldPop->initialize();
}
#endif//USING_GASIMPLEGA
// Use all of the available cores (as reported by the processor) for threads
unsigned numberOfThreadsToUse;
#if USE_BOOST
numberOfThreadsToUse = boost::thread::hardware_concurrency();
#else//USE_BOOST
numberOfThreadsToUse = (unsigned)sysconf( _SC_NPROCESSORS_ONLN );
int errorCode;
#endif//USE_BOOST
// Allocate an array of pointers for the threads, as well as the associated background information
#if USE_BOOST
boost::thread** backgroundThreads = new boost::thread*[numberOfThreadsToUse];
#else//USE_BOOST
pthread_t* backgroundThreads = new pthread_t[numberOfThreadsToUse];
#endif//USE_BOOST
assert( backgroundThreads );
memset( backgroundThreads, 0, numberOfThreadsToUse * sizeof( void* ) );
BackgroundEvaluator** backgroundEvaluators = new BackgroundEvaluator*[numberOfThreadsToUse];
assert( backgroundEvaluators );
memset( backgroundEvaluators, 0, numberOfThreadsToUse * sizeof( BackgroundEvaluator* ) );
#if DEBUG
cout << "Evaluating new GAPopulation ( " << pop.size( ) << " )... ( using " << numberOfThreadsToUse << " threads )" << std::endl;
#endif//DEBUG
int completed = 0;
int indIndex = 0;
u_int32_t tIndex = 0U;
#if DEBUG
float highestScoreSoFar = 0.0f;
float total = 0.0f;
#endif//DEBUG
// Loop until every population member has been evaluated.
while ( completed < pop.size( ) && ( thisPetriDish->_interrupt == false ) )
{
// If we still have members to evaluate, and there's a free thread open
if ( ( indIndex < pop.size( ) ) && ( backgroundThreads[tIndex] == NULL ) )
{
// If we haven't allocated space for the evaluator thread information
if( backgroundEvaluators[tIndex] == NULL )
{
backgroundEvaluators[tIndex] = new BackgroundEvaluator( &pop.individual( indIndex ), indIndex );
assert( backgroundEvaluators[tIndex] != NULL );
}
else
{
assert(backgroundEvaluators[tIndex]->finished() == true);
backgroundEvaluators[tIndex]->newIndividual( &pop.individual( indIndex ), indIndex);
}
#if DEBUG
cout << "Starting individual #" << indIndex + 1 << " ( of " << pop.size( ) << " )" << std::endl;
#endif//DEBUG
// Kick off the thread
#if USE_BOOST
try
{
backgroundThreads[tIndex] = new boost::thread( boost::ref(*backgroundEvaluators[tIndex]) );
}
catch(const std::exception& e)
{
cerr << "boost::thread exception: " << e.what() << std::endl;
return;
}
#else//USE_BOOST
errorCode = pthread_create(&backgroundThreads[tIndex], NULL, backgroundEvaluate, backgroundEvaluators[tIndex]);
if (errorCode!=0)
{
cerr << "pthread_create: Error #"<< errorCode << " (" << strerror(errorCode) << ")" << std::endl;
return;
}
#endif//USE_BOOST
assert(backgroundThreads[tIndex]);
indIndex++;
}
// Our cyclic thread index checks for the completion of a running thread
//.........这里部分代码省略.........