fix examples using binary_value

This commit is contained in:
gustavo 2000-07-18 11:30:39 +00:00
commit 01e44cf828
7 changed files with 143 additions and 129 deletions

View file

@ -8,6 +8,7 @@ typedef eoBin<float> Chrom;
the fitnes.
@param _chrom A binary chromosome
*/
float binary_value(const Chrom& _chrom)
{
float sum = 0;
@ -16,3 +17,11 @@ float binary_value(const Chrom& _chrom)
sum += pow(2, _chrom.size() - i - 1);
return sum;
}
struct BinaryValue
{
template <class Chrom> void operator()(Chrom& _chrom)
{
_chrom.fitness(binary_value(_chrom));
}
};

View file

@ -17,7 +17,7 @@ int main()
cout << "chrom1 = " << chrom1 << endl
<< "chrom2 = " << chrom2 << endl;
return 1;
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -51,11 +51,13 @@ main()
eoBinRandom<Chrom> random;
eoPop<Chrom> pop;
BinaryValue eval;
for (i = 0; i < POP_SIZE; ++i)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
eval(chrom);
pop.push_back(chrom);
}
@ -73,7 +75,7 @@ main()
breeder(pop);
// reevaluation of fitness
for_each(pop.begin(), pop.end(), binary_value);
for_each(pop.begin(), pop.end(), eval);
cout << "new population:" << endl;
for (i = 0; i < pop.size(); ++i)

View file

@ -49,6 +49,7 @@ main()
eoBinRandom<Chrom> random;
eoPop<Chrom> pop;
// Evaluation
eoEvalFuncPtr<Chrom> eval( binary_value );

View file

@ -19,6 +19,7 @@ main()
const unsigned CHROM_SIZE = 4;
eoBinRandom<Chrom> random;
BinaryValue eval;
for (unsigned POP_SIZE = 4; POP_SIZE <=6; POP_SIZE++)
{
@ -28,7 +29,7 @@ main()
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
eval(chrom);
pop.push_back(chrom);
}
@ -40,7 +41,7 @@ main()
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
eval(chrom);
pop2.push_back(chrom);
}

View file

@ -105,3 +105,4 @@ main()
}
//-----------------------------------------------------------------------------

View file

@ -58,7 +58,7 @@ main()
for (i = 0; i < POP_SIZE; ++i) {
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
BinaryValue()(chrom);
pop.push_back(chrom);
}