⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga.cpp

📁 自己编写的人工智能/遗传算法
💻 CPP
字号:
// GA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GenericAlgorithm.h"

#include <stdlib.h>
#include <iostream>


int main(int argc, char* argv[])
{
	using namespace std;

	MySample samples[POPULATION];	// sample population
	MySample *lCsver = NULL;	// left and right operant for cross over
	size_t tms = 0;	// timer to count generation unchanged times
	double sumfit = 0;	// sum of all fitness value for samples
	double curMax = DOUBLE_MIN; // current max fitness value
	double tmp = 0;	
	size_t i;

	while (tms < MAXUNCHANGE)
	{
		sumfit = 0;
		lCsver = NULL;

		// begin this generation
		tms++;

		// calculate sample's fitness value 
		for (i = 0; i < POPULATION; ++i)
		{
			// TODO:
			tmp = samples[i].Fitness();

			// refresh current max fitness value
			if (curMax < tmp)
			{
				curMax = tmp;

				// reset timer
				tms = 0;
			}

			sumfit += tmp;
		}

		for (i = 0; i < POPULATION; ++i)
		{
			samples[i].CalcuPro(sumfit);
		}

		// begin cross over
		for (i = 0; i < POPULATION; ++i)
		{
			if (samples[i].NextAction() == CROSSOVER)
			{
				if (lCsver == NULL)
				{
					lCsver = &samples[i];
				}
				else
				{
					samples[i].CrossOver(*lCsver);
					lCsver = NULL;
				}
			}

		}

		// begin mutate
		for (i = 0 ; i < POPULATION; ++i)
		{
			if (samples[i].NextAction() == CROSSOVER)
			{
				samples[i].Mutate();
			}
		}

	}

	cout <<curMax<<endl;






	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -