遗传算法 解决 y = x ^ 2 在[0, 31] 内的最大值问题

用  遗传算法 验证 y = x ^2 在区间[0, 31] 内的最大值;

#include 
#include 
#include 
#include
#include
#include
using namespace std;
const double pc = 0.2;//交叉互换的概率
const double pv = 0.02;//变异的概率
const int number_bits = 5;//二进制表示11111
const int number_agent = 10;//每代的个数 
const int NC_max = 1000;
/*
[0, 31] 关于最大值用11111表示
适应度函数用 y = x^2 表示
*/
class Number {
public:
	vector _bits;
	int _fitness;
	Number() :_fitness(0)//初始化
	{
		for (int i = 0; i < number_bits; i++)
		{
			_bits.push_back(rand()%2);
		}
		UpdateFit();
	}
	bool operator <(const Number& n)//重载 > 运算符
	{
		return _fitness < n._fitness;
	}
	void Init_bits(vector& x)//手动初始化
	{
		_bits.clear();
		for (int i = 0; i < number_bits; i++)
		{
			_bits.push_back(x[i]);
		}
		UpdateFit();
	}
	int UpdateFit()//更新适应度
	{
		_fitness = 0;
		for (int i = 0; i < number_bits; i++)
		{
			_fitness = _fitness * 2 + _bits[i];
		}
		_fitness = _fitness * _fitness;
		return  _fitness;
	}
	void Mutations()//自身发生变异
	{
		int P = rand() % 101;
		if (P <= pv * 100)
		{
			int pos = rand() % number_bits;
			if (_bits[pos] == 0)
				_bits[pos] = 1;
			else
				_bits[pos] = 0;
			UpdateFit();
		}
	}
	void Print()//检验用
	{
		for(int i = 0; i < number_bits; i++)
		{
			cout << "  " <<  _bits[i];
		}
	}
};
void Print(Number* n)
{
	for (int i = 0; i < number_agent; i++)
	{
		n[i].Print();
		cout << "  Fitness: " << n[i]._fitness;
		if (i % 2 == 0)
			cout << "            ";
		else
			cout << endl;
	}
}
bool cmp1(Number& n1, Number& n2)//比较函数
{
	return n1 < n2;
}
void Cross_swap(Number& n1, Number& n2, Number& n3)//交叉互换
{

	vector bit1(n1._bits), bit2(n2._bits);
	int pos1, pos2, P = rand() % 101;
	if (P <= pc * 100)
	{
		pos1 = rand() % number_bits;
		pos2 = rand() % number_bits;
		if (pos1 > pos2)
		{
			int temp = pos1;
			pos1 = pos2;
			pos2 = temp;
		}
		for (int i = 0; i < number_bits; i++)
		{
			if (i >= pos1 && i <= pos2)
			{
				bit1[i] = n2._bits[i];
				bit2[i] = n1._bits[i];
			}
		}
	}
	n3.Init_bits(bit1);
}
int main()
{
	srand((int)time(NULL));
	Number* n, * nn;
	int while_number = 1;
	n = new Number[number_agent];
	int Max = 00;
	while (while_number)
	{
		sort(n, n + number_agent, cmp1);
		if (n[number_agent - 1]._fitness > Max)
			Max = n[number_agent - 1]._fitness;
		cout << "第" << while_number << "代的最大为: " << n[number_agent - 1]._fitness << endl;
		Print(n);
		if (while_number == NC_max)
		{
			break;
		}
		
		//轮盘赌,随机选取两个父代
		int fit_sum = 0;
		for (int i = 0; i < number_agent; i++)
		{
			fit_sum += n[i]._fitness;
		}
		//生成和后代
		nn = new Number[number_agent];
		for (int i = 0; i < number_agent; i++)
		{
			int pos1, pos2, P, sum = 0;
			P = rand() % (fit_sum);
			for (int j = 0; j < number_agent; j++)
			{
				sum += n[j]._fitness;
				if (sum > P)
				{
					pos2 = j;
					break;
				}
			}
			sum = 0;
			P = rand() % (fit_sum);
			for (int j = number_agent - 1; j >= 0 ; j--)
			{
				sum += n[j]._fitness;
				if (sum > P)
				{
					pos1 = j;
					break;
				}
			}
			Cross_swap(n[pos1], n[pos2], nn[i]);
			nn[i].Mutations();
		}
		delete []n;
		n = nn;
		while_number++;
	}
	cout << "the lagest number is :" << Max << " x is :" << sqrt(Max) << endl;
}

 

你可能感兴趣的:(c++,算法)