You are on page 1of 25

............................................................................................................................................................

2
Abstract........................................................................................................................................................... 2
1 ............................................................................................................................................................ 3
1.1 ............................................................................................................................ 3
1.2 ............................................................................................................................ 3
1.3 ............................................................................................................................ 4
1.4 ........................................................................................................................ 4
2 ................................................................................................................... 6
2.1 .................................................................................................................... 6
2.2 ................................................................................................................................ 6
2.3 ................................................................................................................ 7
2.4 ...............................................................................................................11
2.5 .......................................................................................................................... 12
3 ..................................................................................................... 13
3.1 .............................................................................................................................. 13
3.2 ...................................................................................................................... 13
3.3 .............................................................................................................................. 14
3.4 .............................................................................................................................. 15
3.4.1 ................................................................................................................... 15
3.4.2 ............................................................................................... 15
3.4.3 ........................................................................................................................... 17
3.4.4 ....................................................................................................................... 23
.............................................................................................................................................................. 24
...................................................................................................................................................... 25

AIAI

AI
AIAI
AI

; ;

Application of Genetic Algorithm and Neural Network in


game programming
Abstract: Artificial intelligence is the most complex part in game programming . Programer must insure
the AI meet players needs. Nowadays , many foreign companys uses genetic algorithm and neural network
to encode the game AI . It is more superiority than lots of if-else encoding by GA and NN . Make the game
AI become bright and has a strong intelligence. Its more intelligent than former .

Key word: Genetic Algorithm; Neural Network; Artificial Intelligence

1
1.1

[1]

AI

1.2

NO

YES

1.1

1.1

1.3

1.2 [2]

1.2

[3]

1.4

1.3
0
6*10
1
0 60
10
0

1.3

(01)0
[4]
10 50%1
0 [1]

1.4

NO

YES

1.4

2.1

1.
2.
3.
4.
5. 234

2.2

Bob 2.1 2D Bob

2.1

Bob Bob
2 2.1

2.1

00

01

10

11

Bob
18 36 36 18
2 2

2 10 20 20 6.6
10

00
Bob

2.3

1.
2.
3.
CBobsMap

CBobsMap
static const int

map[MAP_HEIGHT][MAP_WIDTH];//

static const int

m_iMapWidth;

//

static const int

m_iMapHeight;

//

static const int

m_iStartX;

// X
7

static const int

m_iStartY;

// Y

static const int

m_iEndX;

// X

static const int

m_iEndY;

// Y

int memory[MAP_HEIGHT][MAP_WIDTH]; //

0bob
1bob
bob Pxy Eendxendy
Fitness =

x endx + y endy + 1

double CBobsMap::TestRoute(const vector<int> &vecPath, CBobsMap &Bobs)


{//
int posX = m_iStartX;
int posY = m_iStartY;
for (int dir=0; dir<vecPath.size(); ++dir)
{
int NextDir = vecPath[dir];
switch(vecPath[dir])
{
case 0: //
//
if ( ((posY-1) < 0 ) || (map[posY-1][posX] == 1) )
{//
break;
}
else
{

//
posY -= 1;

}
break;
8

case 1://
.break;
case 2://
.break;
case 3: //
.break;
}
Bobs.memory[posY][posX] = 1;
//
if(posY==m_iEndY && posX==m_iEndX)
return 1;
}
//
// X Y
Int

DiffX = abs(posX - m_iEndX);

int

DiffY = abs(posY - m_iEndY);

//
return 1/(double)(DiffX+DiffY+1);
}

0 10

10
2.2

2.2

SGenome& CgaBob::RouletteWheelSelection()
{//
double fSlice

= RandFloat() * m_dTotalFitnessScore;

double cfTotal

= 0.0;
9

int SelectedGenome = 0;
//
for (int i=0; i<m_iPopSize; ++i)
{
cfTotal += m_vecGenomes[i].dFitness;
if (cfTotal > fSlice)
{//
SelectedGenome = i;
break;
}
}
return m_vecGenomes[SelectedGenome];
}

0100110100000110

1101100101101101

8
1

11011001 00000110

01001101 01101101

Crossover..
void CgaBob::Crossover( const vector<int> &mum,
const vector<int> &dad,

vector<int>

&baby1,

vector<int>

&baby2)

//
if ( (RandFloat() > m_dCrossoverRate) || (mum == dad))
{//
baby1 = mum;
baby2 = dad;
return;
}
//
int cp = RandInt(0, m_iChromoLength - 1);
//
10

for (int i=0; i<cp; ++i)


{
baby1.push_back(mum[i]);
baby2.push_back(dad[i]);
}
for (i=cp; i<mum.size(); ++i)
{
baby1.push_back(dad[i]);
baby2.push_back(mum[i]);
}
}

2.4

Bob

2
pop_sizechromo_length P C

C t*P*C
10 n*C*P
m*C*P x*P y*C*P z*C*P

n*C*P+m*C*P+x*P+y*C*P+z*C*P=(n+m+y+z)*C*P+x*P
Gt*P*C+((n+m+y+z)*C*P+x*P)*G
OP*C*G

36
36

Bob

11

P*C*G

2.5

12

3.1

3.2

ANN

ANN
ANN
3.1
W

W
Output

Nucleus

W
3.1

w ANN weightANN input


-1 1

output
a=w 1 x 1 +w 2 x 2 +w 3 x 3 +..+w n x n
i=n

wx
i=0

activation
0 1
13

1 0 3.2

-2

-1

3.2

1 1 1 0

3.3

3.3

3.3

14

3.4

3.4

3.4.1

3.4

3 .4.2

4 x1y1
x2y2
xy 6

y1-x1,y2-x2 3.5

15

3.5

CPU
3.6

RotForce = m_lTrack - m_rTrack;

//

m_dSpeed = m_lTrack + m_rTrack;

//

0 1
0 1 4

3.6

0 1 0 1
0 1
3.7

output

1
1 + e a / p

e e 2.7183p

16

0.5

0
3.7

3.4.3

struct SNeuron
{
//
int

m_NumInputs;

//
vector<double>

m_vecWeight;

SNeuron(int NumInputs);
};
struct SNeuronLayer
{
//
int

m_NumNeurons;

//
vector<SNeuron>

m_vecNeurons;

SNeuronLayer(int NumNeurons, int NumInputsPerNeuron);


};
class CNeuralNet
{
private:
17

int

m_NumInputs;

//

int

m_NumOutputs;

//

int

m_NumHiddenLayers;

//

int

m_NeuronsPerHiddenLyr;

//

vector<SNeuronLayer>

m_vecLayers;//

CNeuralNet();
//
void

CreateNet();

//
vector<double>

GetWeights()const;

//
int

GetNumberOfWeights()const;

//
void

PutWeights(vector<double> &weights);

//S
inline double

Sigmoid(double activation, double response);

//
vector<double>

Update(vector<double> &inputs);

};

0 1

2000 2000 1
2000 m
2000

Fitness =

m
i =1

struct SGenome
{
vector <double>
double

vecWeights;
dFitness;

SGenome():dFitness(0){}
18

SGenome( vector <double> w, double f): vecWeights(w), dFitness(f){}


// '<'
friend bool operator<(const SGenome& lhs, const SGenome& rhs)
{
return (lhs.dFitness < rhs.dFitness);
}
};
void CGenAlg::Mutate(vector<double> &chromo)
{
for (int i=0; i<chromo.size(); ++i)
{
if (RandFloat() < m_dMutationRate)
{
chromo[i] += (RandomClamped() * CParams::dMaxPerturbation);
}
}
}

class CMinesweeper
{
private:
//
CNeuralNet

m_ItsBrain;

//
m_vPosition;[4]

SVector2D
//
SVector2D

m_vLookAt;

//
double

m_dRotation;

double

m_dSpeed;

//
double

m_lTrack,
m_rTrack;

//
19

double

m_dFitness;

//
double

m_dScale;

//
int

m_iClosestMine;

CMinesweeper();
// 2000
bool

Update(vector<SVector2D> &mines);

//
void

WorldTransform(vector<SPoint> &sweeper);

//
SVector2D

GetClosestMine(vector<SVector2D> &objects);

//
int

CheckForMine(vector<SVector2D> &mines, double size);

void

Reset();

SVector2D

Position()const{return m_vPosition;}

void

IncrementFitness(double val){m_dFitness += val;}

double

Fitness()const{return m_dFitness;}

void

PutWeights(vector<double> &w){m_ItsBrain.PutWeights(w);}

int

GetNumberOfWeights()const{return m_ItsBrain.GetNumberOfWeights();}

};

class CGenAlg
{
private:
//
vector <SGenome>

m_vecPop;

//
int m_iPopSize;
//
int m_iChromoLength;
//
double m_dTotalFitness;
//
double m_dBestFitness;
//
20

double m_dAverageFitness;
//
double m_dWorstFitness;
//
int

m_iFittestGenome;

//
double m_dMutationRate;
//
double m_dCrossoverRate;
//
int
void

m_cGeneration;
Crossover(const vector<double> &mum,
const vector<double> &dad,

void

&baby1,

vector<double>

&baby2);

Mutate(vector<double> &chromo);

SGenome
void

vector<double>

GetChromoRoulette();

GrabNBest(int

NBest,

const int

NumCopies,

vector<SGenome> &vecPop);
void

FitnessScaleRank();

void

CalculateBestWorstAvTot();

void

Reset();

public:
CGenAlg(int

popsize,
double

MutRat,

double CrossRat,
int

numweights);

//
vector<SGenome>

Epoch(vector<SGenome> &old_pop);

vector<SGenome>

GetChromos()const{return m_vecPop;}

double

AverageFitness()const{return m_dTotalFitness / m_iPopSize;}

double

BestFitness()const{return m_dBestFitness;}

};

21

class CController
{
private:
//
vector<SGenome>

m_vecThePopulation;

//
vector<CMinesweeper> m_vecSweepers;
//
vector<SVector2D>

m_vecMines;

//
CGenAlg*

m_pGA;

int

m_NumSweepers;

int

m_NumMines;

int

m_NumWeightsInNN;

//
vector<SPoint>

m_SweeperVB;

vector<SPoint>

m_MineVB;

//
vector<double>

m_vecAvFitness;

//
vector<double>

m_vecBestFitness;

//
HPEN

m_RedPen;

HPEN

m_BluePen;

HPEN

m_GreenPen;

HPEN

m_OldPen;

//
HWND

m_hwndMain;

//
bool

m_bFastRender;

//
int

m_iTicks;

//
int

m_iGenerations;

//
22

int
void

cxClient, cyClient;
PlotStats(HDC surface);

public:
CController(HWND hwndMain);
~CController();
void

Render(HDC surface);

void

WorldTransform(vector<SPoint> &VBuffer,
SVector2D

bool

Update();

bool

FastRender()const

void

FastRender(bool arg){m_bFastRender = arg;}

void

FastRenderToggle()

vPos);

{return m_bFastRender;}

{m_bFastRender = !m_bFastRender;}

};

3.4.4

CPU

23

24

[1] . : [M]. . 1997


[2] Andre LaMothe. AI TECHNIQUES FOR GAME PROGRAMMING[M].Stacy L.Hiquet.2002
[3]

. [M] . .

[4] , . [M].: 1999.11


[5] . C++[M].2006.01
[6] 2007.01 . [M].. 2003.05
[7]

.[M]..2002.05

[8] . [M]..2008.02
[9] . [M]..2004.10
[10] .[M]..2006.06
[11] .[J]..2007. (05)
[12] .[J]..2002.(02)
[13]

. [M]..1997.(05)

[14] . [D]., 2002


[15] . [D], 2004
[16] . BP [D], 2004
[17] . [D], 2001
[18] . [D], 2006
[19]

. [D], 2002

[20]

. [D], 2001

25

You might also like