我正在寻找有效的添加或省略代码的方法,以帮助我的遗传算法程序返回更快的结果.该程序的目标是接受一个字符串并创建尽可能匹配的其他字符串.无论哪个新制作的字符串与最接近的(前5个)配对匹配并产生后代(其中一些具有将新的随机数放入字符串而不影响长度的突变).这一切都很好,但需要不可思议的几代才能获得一些更长的字符串(4和更高)才能完美匹配.抱歉tl; dr长度,但这是我当前的代码.批评走了!
#include "stdio.h"
#include "fstream"
#include "ctime"
#include "iostream"
#include "string"
#include "windows.h"
#define CHARACTERS 16
#define STRINGS 100
/*
Enter String(max 16 chars)
Generate 100 words of the same length
Check for Fitness(how close each word is to the string)
Every generation: display top 5
Clone the top 5
Top 20 reproduce(mix each other's chars)
1/1000 chance the children might mutate(each newly mixed string or char might have a completely random number)
*/
typedef struct _stringHolder
{
char …Run Code Online (Sandbox Code Playgroud) 我已经知道有这种事情的答案,但我真的不知道如何在我的代码中实现它们.另外,除非必要,否则我想不再使用任何其他功能.这是我的代码:
int main()
{
unsigned seed;
seed = 1;
srand(seed);
std::string starFox[8];
int x[8];
starFox[0] = "Do a barrel roll!";
starFox[1] = "Try a somersault!";
starFox[2] = "Use bombs wisely!";
starFox[3] = "Something is wrong with the G-diffuser";
starFox[4] = "Can't let you do that, Star Fox";
starFox[5] = "Hey Einstein, I'm on your side";
starFox[6] = "Whoa! help me!";
starFox[7] = "Daddy screamed REAL good before he died!";
for(int i=0; i<8; i++)
{
int y = 0 + rand() …Run Code Online (Sandbox Code Playgroud)