请教关于遗传算法的

csuc20 2004-10-20 10:05:04
遗传算法是学术研究中应用比较普遍的算法。但是我看到的很多都是概念上的冬冬,没有实际的代码可以参考。请教有没有大侠有这方面的代码实例?有没有应用与机器人仿真的例子?
...全文
62 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzwu 2004-10-20
  • 打赏
  • 举报
回复
其余的我想不发送了,你可以从

www.ai-junkie.com

网站上自己去下载完整的代码,而且前面还有详细的说明.
zzwu 2004-10-20
  • 打赏
  • 举报
回复
从网上找到了一个,也很长,要分次发送:

//-----------------------------------------ga_tutorial.cpp--------------------------------------
//
// code to illustrate the use of a genetic algorithm to solve the problem described
// at www.ai-junkie.com
//
// by Mat Buckland aka fup
//
//-----------------------------------------------------------------------------------------------
#include <string>
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#include <math.h>

using std::string;

#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POP_SIZE 100 //must be an even number
#define CHROMO_LENGTH 300
#define GENE_LENGTH 4
#define MAX_ALLOWABLE_GENERATIONS 400

//returns a float between 0 & 1
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))

//----------------------------------------------------------------------------------------
//
// define a data structure which will define a chromosome
//
//----------------------------------------------------------------------------------------
struct chromo_typ
{
//the binary bit string is held in a std::string
string bits;

float fitness;

chromo_typ(): bits(""), fitness(0.0f){};
chromo_typ(string bts, float ftns): bits(bts), fitness(ftns){}
};


/////////////////////////////////prototypes/////////////////////////////////////////////////////

void PrintGeneSymbol(int val);
string GetRandomBits(int length);
int BinToDec(string bits);
float AssignFitness(string bits, int target_value);
void PrintChromo(string bits);
void PrintGeneSymbol(int val);
int ParseBits(string bits, int* buffer);
string Roulette(int total_fitness, chromo_typ* Population);
void Mutate(string &bits);
void Crossover(string &offspring1, string &offspring2);



//-------------------------------main--------------------------------------------------
//
//-------------------------------------------------------------------------------------
int main()
{
//seed the random number generator
srand((int)time(NULL));

//just loop endlessly until user gets bored :0)
while (true)
{
//storage for our population of chromosomes.
chromo_typ Population[POP_SIZE];

//get a target number from the user. (no error checking)
float Target;
cout << "\nInput a target number: ";
cin >> Target;
cout << endl << endl;

//first create a random population, all with zero fitness.
for (int i=0; i<POP_SIZE; i++)
{
Population[i].bits = GetRandomBits(CHROMO_LENGTH);
Population[i].fitness = 0.0f;
}

int GenerationsRequiredToFindASolution = 0;

//we will set this flag if a solution has been found
bool bFound = false;

//enter the main GA loop
while(!bFound)
{
//this is used during roulette wheel sampling
float TotalFitness = 0.0f;

// test and update the fitness of every chromosome in the
// population
for (int i=0; i<POP_SIZE; i++)
{
Population[i].fitness = AssignFitness(Population[i].bits, Target);

TotalFitness += Population[i].fitness;
}

// check to see if we have found any solutions (fitness will be 999)
for (i=0; i<POP_SIZE; i++)
{
if (Population[i].fitness == 999.0f)
{
cout << "\nSolution found in " << GenerationsRequiredToFindASolution << " generations!" << endl << endl;;

PrintChromo(Population[i].bits);

bFound = true;

break;
}
}

// create a new population by selecting two parents at a time and creating offspring
// by applying crossover and mutation. Do this until the desired number of offspring
// have been created.

//define some temporary storage for the new population we are about to create
chromo_typ temp[POP_SIZE];

int cPop = 0;

//loop until we have created POP_SIZE new chromosomes
while (cPop < POP_SIZE)
{
// we are going to create the new population by grabbing members of the old population
// two at a time via roulette wheel selection.
string offspring1 = Roulette(TotalFitness, Population);
string offspring2 = Roulette(TotalFitness, Population);

//add crossover dependent on the crossover rate
Crossover(offspring1, offspring2);

//now mutate dependent on the mutation rate
Mutate(offspring1);
Mutate(offspring2);

//add these offspring to the new population. (assigning zero as their
//fitness scores)
temp[cPop++] = chromo_typ(offspring1, 0.0f);
temp[cPop++] = chromo_typ(offspring2, 0.0f);

}//end loop

//copy temp population into main population array
for (i=0; i<POP_SIZE; i++)
{
Population[i] = temp[i];
}

++GenerationsRequiredToFindASolution;

// exit app if no solution found within the maximum allowable number
// of generations
if (GenerationsRequiredToFindASolution > MAX_ALLOWABLE_GENERATIONS)
{
cout << "No solutions found this run!";

bFound = true;
}

}

cout << "\n\n\n";

}//end while

return 0;
}

[待续]

zzwu 2004-10-20
  • 打赏
  • 举报
回复
我在书上看到了,但很长,如何给你?

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧