求函数的最大值——遗传算法
函数 f(x)如下:
f(x) = x * sin(10 * pi * x) + 1 (在[-1, 2]内的最大值)
22 位的二进制串作为染色体,表示(仅仅是表示,而不等于)x
的实数值,
染色体可以表示的整数的范围是 0~4194304
const int MAX = 4194304;
const int MIN = 0;
计算 f(x)评价函数
#include <iostream>
#include <time.h> //产生随机数 time(int),每次产生数不同
#include <math.h> //sin(float)
using namespace std;
const float pi = 3.1415926;
class Individuality {
private:
int chromosome; //染色体
public:
void set(int chromosome)
{ this->chromosome = chromosome; }
Individuality operator = (Individuality c)