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

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);

}

}

776 篇文章92 人订阅

0 条评论

## 相关文章

2534

3204

3074

### 计算机如何理解我们的语言？NLP is fun！

【导读】我们从日常每天都会用到的推荐系统到现在研究火热的开放性聊天、对话机器人，越来越多的产品与应用的背后都需要自然语言处理（NLP）和知识图谱的技术。也有越来...

883

### Java豆瓣电影爬虫——使用Word2Vec分析电影短评数据

在上篇实现了电影详情和短评数据的抓取。到目前为止，已经抓了2000多部电影电视以及20000多的短评数据。 　　数据本身没有规律和价值，需要通过分析提炼成知...

4448

881

1735

3746

701

2004