How to design game AI with genetic algorithm

Source: Internet
Author: User
Tags cos sin

Thank Huang Teacher's help, under her guidance to complete the writing of the paper, has been retrieved by EI.

Before the idea of design Gobang AI, then want to use the game tree, a layer of search to complete, my first version really did so. Later saw this method has rotten street, and derived a lot of varieties, improvement is also very big, so try to innovate.

Inadvertently read an article: Genetic algorithm: In-memory evolution, vividly explain what is the genetic algorithm and function, I suddenly feel that can get the design game AI inside, because the genetic algorithm is a simple way to solve complex operations, a large number of game tree search is often not needed.

Considered for a long time, the general idea is to play the game of the two sides with the genotype coding, and then judge the situation on the field, and try to quantify the fitness function, the length of the genotype and the depth of the game is proportional to, for example, a for the person, b for the computer, then the genotype code for ABABAB on behalf of people and computers The adaptability function is used to give the adaptive value, and then a large number of codes are used for the genetic iteration, and finally the optimal solution is chosen. This optimal solution may not be our best solution, but it must be a solution that does not get the upper hand, such as you and others can get a perfect situation, but he finished the layout before you, you should stop him to complete his deployment, the process will be reflected in the fitness function, set a standard, Then in the calculation of fitness to reflect the current situation of the score, it is worth noting that we can not only consider the layout of the score, for example, according to a game process in our form a good, the other side is in a meaningless layout so this game process is obviously a failure, The essence of the game is to consider each other to play the greatest strength of the premise of how to overcome each other.

Of course, there are many details, such as how to quantify the situation, how to ensure optimal genetics, and even how to design the depth of the code is worth serious consideration. This is not explained in detail here.

Attached 1. With AI

2. A simple framework for genetic algorithms to calculate the following equation as an example:

Max:f (x, Y, z) =sin (×) + (1+cos (y+z)) ^ (2+sin (ln (1+abs (TAN)))/(Y+z))

Unit genetic;interfaceuses system.sysutils, system.variants, System.classes,generics.collections,math;type      Tchromosome=record//Chromosome Gen:array [0..2] of Word;                      Gene Va:single;  Phenotype end;procedure heaps (list:tlist<single>); Built-in a heap sort function heapadjust (List:tlist<single>;s,m:integer): boolean;       Heap Sort Filter type Tgenetic=class private par:tlist<tchromosome>;      Parent char:tlist<tchromosome>;           Offspring Geneamount:integer;   Gene size (length) Stablecount:integer;              Procedure value (var sample:tchromosome);    Adaptive functions function Select (Pro:array of single): integer;                 Select function (Roulette) procedure exc (Var p1,p2:tchromosome);   Chromosome crossover, mutation procedure c_original ();   Procedure c_posterity ();    Public g_varitionrate:single;       mutation probability g_crossrate:single;      Crossover probability G_oriamount:integer;         Original parent number G_popmax:integer;      The maximum population of G_selectpar:integer;      Descendant selection number G_genecount:integer; //population evolutionary algebra g_maxevolu:integer;   Maximum Evolutionary algebra g_gebest:tlist<tchromosome>;    Each generation of the optimal solution G_stablecount:integer; Stable genetic procedure g_evolution (); Evolutionary process constructor Create;  End;implementationconstructor Tgenetic.create;begin par:=tlist<tchromosome>.create;  char:=tlist<tchromosome>.create;  g_gebest:=tlist<tchromosome>.create;  g_oriamount:=1000;  g_popmax:=2000;  g_selectpar:=200;  g_genecount:=0;  g_maxevolu:=50;  geneamount:=10000;  g_varitionrate:=0.01;  G_crossrate:=1;  g_stablecount:=10;  N-generation invariant is considered stable end;procedure tgenetic.g_evolution;begin g_genecount:=0;  c_original;   While True does begin c_posterity;   Inc (G_genecount); G_gebest. ADD (par.    Items[0]); If G_genecount>=g_stablecount then if (g_gebest. Items[g_genecount-1].va=g_gebest.    Items[g_genecount-g_stablecount].va) then break; If G_genecount>=g_maxevolu then Break;end;end;procedure Tgenetic.value (Var sample:tchromosome); Varx,y,z:integer;  Begin X:=SAMPLE.GEN[0]; Y:=SAMPLE.GEN[1];  Z:=SAMPLE.GEN[2];  Sample.va:=sin (x) +power (1+cos (y+z), 2+sin (ln (1+abs (y+z)))/(x+y+1)); Sample.va:=7*x+ln (y+1) +z;end;procedure tgenetic.c_original;vari,j:integer;s:tchromosome;begin for I: = 1 to G_  Oriamount do begin for J: = 0 to 2 do s.gen[j]:=random (Geneamount);  Value (s); Par.  ADD (s); End;end;procedure Tgenetic.c_posterity;vari:integer;sum:double;prob:array of SINGLE;INDEX:INTEGER;P1,P2: Tchromosome;listpx:tlist<tchromosome>;listv,listv0:tlist<single>;begin Listv:=Tlist<single>.  Create  listv0:=tlist<single>.create;  listpx:=tlist<tchromosome>.create;  sum:=0;  SetLength (Prob,par.count);  Probability for I: = 0 to Par.count-1 do Sum:=sum+par.items[i].va;  For I: = 0 to Par.count-1 do prob[i]:=par.items[i].va/sum;  Index:=1; While index<= (G_popmax Div 2) does begin p1:=par.   Items[select (prob)]; P2:=par.   Items[select (prob)];   EXC (P1,P2);   Value (p1);   Value (p2); Char.   Add (p1); Char.   ADD (p2);  Inc (index);  End Par.  Clear; For I: =0 to Char. Count-1 do listpx. Add (char.  Items[i]); Char.  Clear; For I: = 0 to LISTPX. Count-1 do begin LISTV. ADD (LISTPX.   Items[i].va); Listv0. ADD (LISTPX.  Items[i].va);  End          Heaps (LISTV); Heap sort for I: = listpx. Count-g_selectpar to LISTPX. Count-1 do par. ADD (LISTPX. Items[listv0. IndexOf (Listv. Items[i]); End;function Tgenetic.select (Pro:array of single): Integer;vari:integer;num:double;sum:double;areap:                Array of single;  Roulette Low,high,mid:integer;begin SetLength (Areap,length (pro));  i:=0;  sum:=0;   For I: = 0 to Length (PRO)-1 does begin areap[i]:=sum+pro[i];  Sum:=areap[i];  End    Num:=randomrange (1,9999999)/10000000;   Roulette algorithm if (num>=0) and (num<=areap[0]) then BEGIN result:=0;  Exit  End   if (Num>=areap[length (AREAP)-1]) and (num<=1) THEN begin Result:=length (AREAP)-1;  Exit  End  Low:=1; High:=length (AREAP);  while (Low<=high) do//binary find begin Mid:=trunc ((Low+high)/2); if (Num>=areap[mid-1]) and (num<=areap[MID]) then break; If NUM&GT;=AREAP[MID-1] then low:=mid else high:=mid; End;result:=mid;end;procedure Tgenetic.exc (var p1,p2:tchromosome); VAREXN:INTEGER;I:INTEGER;TEMP:INTEGER;EXCR:      Single;vari:single;begin Exn:=random (3);    3 USD if G_crossrate>random (/1000 then for I: = EXN to 2 do//cross begin TEMP:=P1.GEN[I];    P1.gen[i]:=p2.gen[i];    P2.gen[i]:=temp;  End  Excr:=random (10000)/10000;   If Excr<g_varitionrate then//mutation begin I:=RANDOM (3);  P1.gen[i]:=random (Geneamount);  End   If Excr<g_varitionrate then//mutation begin I:=RANDOM (3);  P2.gen[i]:=random (Geneamount);  End  p1.va:=0; P2.va:=0;end;procedure heaps (list:tlist<single>); vari:integer;temp:real;begini:=list. Count Div 2;while i>0 do//built large top heap begin Heapadjust (list,i,list.  COUNT-1);  Dec (i); End;i:=list. Count;while i>1 do begin temp:=list.  Items[0]; List. Items[0]:=list.  ITEMS[I-1]; List.  Items[i-1]:=temp;  Heapadjust (list,1,i-1); Dec (i);  End;end;function Heapadjust (list:tlist<system.single>; s:integer; m:integer): Boolean;var J:integer; Temp:real;begintemp:=list. Items[s-1];j:=2*s;while (j<=m) do BEGIN if (j<m) and (list. Items[j-1]<list.  ITEMS[J] Then Inc (J); If Temp>=list.  Items[j-1] then break; List. Items[s-1]:=list.  ITEMS[J-1];  S:=j;  J:=2*j;  End List. Items[s-1]:=temp;end;end.

  

How to design game AI with genetic algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.