.
.
(Serialization 6)
.
From <artificial intelligence technology in game programming> Chapter 3
Tsinghua University Press
(Translated by zzwu)
3.4.1 chromosome code
(Ecoding the chromosome)
Each chromosome must include every action of Bob in the code. Bob's actions are limited to four directions.:East(East ),South(South ),To the west(West ),North(North)Therefore, the encoded chromosome is a string that represents the information in the four directions. The traditional coding method is to convert the direction into binary code. Only two digits are required in four directions, as shown in the following table:
| Binary Code |
Decimal Decoding |
Direction |
| 00 |
0 |
North |
| 01 |
1 |
South |
| 10 |
2 |
East |
| 11 |
3 |
To the west |
In this way, if you get a random binary string, you can extract it from what Bob follows
A series of directions. For example, chromosome:
111110011011101110010101
The Representative gene is: 11, 11, 10, 01, 10, 11, 10, 11, 10, 01, 01,01When the binary code is translated into decimal, it becomesPut these in a table, you can believe that this is the same concept:
| Binary Code |
Decimal Decoding |
Direction |
| 11 |
3 |
West |
| 11 |
3 |
West |
| 10 |
2 |
East |
| 01 |
1 |
South |
| 10 |
2 |
East |
| 11 |
3 |
West |
| 10 |
2 |
East |
| 11 |
3 |
West |
| 10 |
2 |
East |
| 01 |
1 |
South |
| 01 |
1 |
South |
| 01 |
1 |
South |
At this point, all you have to do is place Bob at the starting point of the maze, and then tell him to step by step based on the directions listed in this table. If Bob encounters a wall or obstacle in a certain direction, ignore this direction and continue following the next direction. This continues until all directions are used up or Bob reaches the exit.
If you think there are hundreds of such random chromosomes, you can see some of them may be Bob decoding a set of directions to reach the exit (a solution to the problem ), however, most of them will fail.
The genetic algorithm uses a random binary string (chromosome) as the initial population to test how close each of them can make Bob leave the exit, and then let the best of them hatch future generations, we expect their children to be closer than Bob. Continue until a solution is found, or until Bob is stuck in a corner in despair (you will see that this situation may happen ).
Therefore, we should define a structure that contains a binary string (chromosome) and an adaptive fraction associated with the chromosome. I call this structure the sgenome structure. Its definition is as follows:
Struct sgenome {vector <int> vecbits; double dfitness; sgenome (): dfitness (0) {} sgenome (const int num_bits): dfitness (0) {// create a random binary string for (INT I = 0; I <num_bits; ++ I) {vecbits. push_back (randint (0, 1 ));}}};As you can see, if you pass an integer number as a parameter to the constructor when creating an sgenome object, then it automatically creates a random binary string with the length of this integer,
And the adaptive score is initialized to zero, so that everything in the genome is ready.
Program AnnotationSTD: VectorYesSTL (Standard templete Library)A part of the standard template library, which is a class pre-established to process dynamic arrays. To add data to STL, you can use
Push_back () method.
The following is a simple example.: # Include <vector> for (INT I = 0; I <10; I ++) {myfirstvector. push_back (I); cout <Endl <myfirstvector [I];}To clear a vector, useClear () method:
MyFirstVector.clear();
You can use the size () method to obtain the number of elements in the vector:
NyFirstVector.size()
That's it.
You don't need to worry about memory management-STD: vector can do everything for you! When necessary, I will use it throughout the program. The sgenome structure does not have any knowledge about how to perform Decoding for chromosomes (vecbits). This is a task that needs to be done by the genetic algorithm class. Now let's take a quick look at the definition of this class. I have called it the cgabob class (sometimes I am surprised by my original ideas, but I did ).
Class cgabob {PRIVATE: // genome population vector <sgenome> m_vecgenomes // The Population Size int m_ipopsize double m_dcrossoverrate; double m_dmutationrate; // The number of BITs int m_ichromolength contained in each chromosome; // The number of BITs int m_igenelength for each gene; int m_ifittestgenome; double m_dbestfitnessscore; double m_dtotalfitnessscore; int m_igeneration; // create an instance cbobsmap m_bobsmap for the map class; // another cbobsmap object is used to save a record of the Optimal Path of each generation. It is an array of accessed cells and is used only for display purposes. Cbobsmap m_bobsbrain; // Let you know if the running is still in progress bool m_bbusy; void mutate (vector <int> & vecbits); void crossover (const vector <int> & mum, const vector <int> & Dad, vector <int> & baby1, vector <int> & baby2); sgenome & roulettewheel selection (); // use a new adaptive score to update the original genetic fitness score and calculate the highest adaptive score and the most adaptive score of the population. Void updatefitnessscores (); // translates a single-bit vector into an (integer) vector <int> decode (const vector <int> & bits ); // convert a single vector to a decimal number. Used to decode int bintoint (const vector <int> & V); // creates an initial group void createstartpopulation () for a random binary string; public: cgabob (double cross_rat, double mut_rat, int pop_size, int num_bits, int gene_len): round (cross_rat), m_dmutationrate (mut_rat), m_ipopsize (pop_size), m_ichromolength (num_bits), round (0.0 ), m_igeneration (0), m_igenelength (gene_len), m_bbusy (false) {createstartpopulation ();} void run (hnnd hwnd); void epoch (); void render (INT cxclient, int cyclient, HDC surface); // method int generation () {return m_igeneration;} int getfittest () {return m_ifittestgenome;} bool started () {return m_bbusy ;} void stop () {m_bbusy = false ;}};As you can see, when an instance of this class is created, the constructor initializes all the variables and calls createstartpopulation (). This short function creates a required number of genomic populations.
Each genome contains a chromosome consisting of a random binary string, and its adaptive score is set to zero.
-6-end serialization-