Adding code to Lesson4
This commit is contained in:
parent
7294d6acd5
commit
1eee26598b
6 changed files with 351 additions and 0 deletions
25
eo/tutorial/Lesson4/binary_value.h
Normal file
25
eo/tutorial/Lesson4/binary_value.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <eo>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just a simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
|
||||
template <class Chrom> float binary_value(const Chrom& _chrom)
|
||||
{
|
||||
float sum = 0;
|
||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||
if (_chrom[i])
|
||||
sum += pow(2, _chrom.size() - i - 1);
|
||||
return sum;
|
||||
}
|
||||
|
||||
struct BinaryValue
|
||||
{
|
||||
template <class Chrom> void operator()(Chrom& _chrom)
|
||||
{
|
||||
_chrom.fitness(binary_value(_chrom));
|
||||
}
|
||||
};
|
||||
Reference in a new issue