# 游戏开发中的人工智能：遗传算法

1.建立第一代：整个族群会载入一组起始特征

2.适合度分等：该族群开始和环境互动后，必须以某种方式对个体做等级分类，这将告诉我们族群中的哪些个体最为优良

3.选择：选出族群中的某些个体繁衍后代，用上一代最为优良的特征繁殖下一代

4.演化：结合这些特征繁衍出适应力更强的下一代的过程

//例15-1：编码

class ai_World

{

public:

int currentTemperature; //目前温度

int currentWater; //目前水质

int currentSunlight; //目前阳光

int currentNutrient; //目前养分

int currentBeneficialInsect;//目前益虫

int currentHarmfulInsect; //目前害虫

ai_World();

~ai_World();

};

//例15-2：环境条件

#define kMaxFlowers 11

class ai_World

{

public:

int temperature[kMaxFlowers];

int water[kMaxFlowers];

int sunlight[kMaxFlowers];

int nutrient[kMaxFlowers];

int beneficialInsect[kMaxFlowers];

int harmfulInsect[kMaxFlowers];

int currentTemperature;

int currentWater;

int currentSunlight;

int currentNutrient;

int currentBeneficialInsect;

int currentHarmfulInsect;

ai_World();

~ai_World();

};

//例15-3：第一代花朵

void ai_World::Encode(void)

{

int i;

for (i=1;i

{

temperature[i]=tb_Rnd(1,75);

water[i]=tb_Rnd(1,75);

sunlight[i]=tb_Rnd(1,75);

nutrient[i]=tb_Rnd(1,75);

beneficialInsect[i]=tb_Rnd(1,75);

harmfulInsect[i]=tb_Rnd(1,75);

}

currentTemperature=tb_Rnd(1,75);

currentWater=tb_Rnd(1,75);

currentSunlight=tb_Rnd(1,75);

currentNutrient=tb_Rnd(1,75);

currentBeneficialInsect=tb_Rnd(1,75);

currentHarmfulInsect=tb_Rnd(1,75);

}

//例15-4：花朵适合度函数

int ai_World::Fitness(int flower)

{

int theFitness;

theFitness=fabs(temperature[flower]-currentTemperature);

theFitness=theFitness+fabs(water[flower]-currentWater);

theFitness=theFitness+fabs(sunlight[flower]-currentSunlight);

theFitness=theFitness+fabs(nutrient[flower]-currentNutrient);

theFitness=theFitness+fabs(beneficialInsect[flower]-currentBeneficialInsect);

theFitness=theFitness+fabs(harmfulInsect[flower]-currentHarmfulInsect);

return (theFitness);

}

//例15-5：花朵演化

void ai_World::Evolve(void)

{

int fitTemperature[kMaxFlowers];

int fitWater[kMaxFlowers];

int fitSunlight[kMaxFlowers];

int fitNutrient[kMaxFlowers];

int fitBeneficialInsect[kMaxFlowers];

int fitHarmfulInsect[kMaxFlowers];

int i;

int leastFit=0;

int leastFitIndex;

for (i=1;i

if (Fitness(i) >leastFit)

{

leastFit=Fitness(i);

leastFitIndex=i;

}

temperature[leastFitIndex]=temperature[tb_Rnd(1,10)];

water[leastFitIndex]=water[tb_Rnd(1,10)];

sunlight[leastFitIndex]=sunlight[tb_Rnd(1,10)];

nutrient[leastFitIndex]=nutrient[tb_Rnd(1,10)];

beneficialInsect[leastFitIndex]=beneficialInsect[tb_Rnd(1,10)];

harmfulInsect[leastFitIndex]=harmfulInsect[tb_Rnd(1,10)];

for (i=1;i

{

fitTemperature[i]=temperature[tb_Rnd(1,10)];

fitWater[i]=water[tb_Rnd(1,10)];

fitSunlight[i]=sunlight[tb_Rnd(1,10)];

fitNutrient[i]=nutrient[tb_Rnd(1,10)];

fitBeneficialInsect[i]=beneficialInsect[tb_Rnd(1,10)];

fitHarmfulInsect[i]=harmfulInsect[tb_Rnd(1,10)];

}

for (i=1;i

{

temperature[i]=fitTemperature[i];

water[i]=fitWater[i];

sunlight[i]=fitSunlight[i];

nutrient[i]=fitNutrient[i];

beneficialInsect[i]=fitBeneficialInsect[i];

harmfulInsect[i]=fitHarmfulInsect[i];

}

for (i=1;i

{

if (tb_Rnd(1,100)==1)

temperature[i]=tb_Rnd(1,75);

if (tb_Rnd(1,100)==1)

water[i]=tb_Rnd(1,75);

if (tb_Rnd(1,100)==1)

sunlight[i]=tb_Rnd(1,75);

if (tb_Rnd(1,100)==1)

nutrient[i]=tb_Rnd(1,75);

if (tb_Rnd(1,100)==1)

beneficialInsect[i]=tb_Rnd(1,75);

if (tb_Rnd(1,100)==1)

harmfulInsect[i]=tb_Rnd(1,75);

}

}

774 篇文章97 人订阅

0 条评论

## 相关文章

9410

21120

### Python自然语言处理分析倚天屠龙记

? 转载自：Python中文社区 ID:python-china 最近在了解到，在机器学习中，自然语言处理是较大的一个分支。存在许多挑战。例如： 如何...

49560

22440

16230

26450

49360

47140

34840

904100