Genetical tuning

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Genetical tuning

Post by stegemma »

jdart wrote:
8 players (engines), for sample, plays each one 7 game with white and 7 game with black.
The results of a game match of this number of games do not give you a reliable indicator of how good a change is. The standard deviation of the result is very large.

--Jon
That's true but my engine always play the same moves, with the same parameters so playing more times would give me always the same games. I plan to add some randomness in the next test session (or maybe a book).
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
matthewlai
Posts: 793
Joined: Sun Aug 03, 2014 4:48 am
Location: London, UK

Re: Genetical tuning

Post by matthewlai »

stegemma wrote:
jdart wrote:
8 players (engines), for sample, plays each one 7 game with white and 7 game with black.
The results of a game match of this number of games do not give you a reliable indicator of how good a change is. The standard deviation of the result is very large.

--Jon
That's true but my engine always play the same moves, with the same parameters so playing more times would give me always the same games. I plan to add some randomness in the next test session (or maybe a book).
I solved that by taking the CCRL dump, and taking all the unique positions after n moves. Here is the number of positions for each n -

Code: Select all

      19 training/unique_starts/start1.epd
     160 training/unique_starts/start2.epd
     672 training/unique_starts/start3.epd
    1605 training/unique_starts/start4.epd
    3215 training/unique_starts/start5.epd
    5726 training/unique_starts/start6.epd
   10747 training/unique_starts/start7.epd
   16918 training/unique_starts/start8.epd
   23738 training/unique_starts/start9.epd
   31365 training/unique_starts/start10.epd
   39648 training/unique_starts/start11.epd
   48945 training/unique_starts/start12.epd
   59750 training/unique_starts/start13.epd
   71216 training/unique_starts/start14.epd
   86271 training/unique_starts/start15.epd
  101131 training/unique_starts/start16.epd
For my cluster testing I use n=4 because 1605 positions = 3210 games per pair, and that's good enough for +-6 elo.

The files are here if you are interested:
http://matthewlai.ca/download/unique_starts.zip
Disclosure: I work for DeepMind on the AlphaZero project, but everything I say here is personal opinion and does not reflect the views of DeepMind / Alphabet.
whereagles
Posts: 565
Joined: Thu Nov 13, 2014 12:03 pm

Re: Genetical tuning

Post by whereagles »

Exactly what parameters are you tuning? Piece evaluation + positional factors, or something?

The fitness function is just the score vs opponent?
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Genetical tuning

Post by stegemma »

whereagles wrote:Exactly what parameters are you tuning? Piece evaluation + positional factors, or something?

The fitness function is just the score vs opponent?
I tune only positional parameters while piece values are fixed.

The fitness function is only the score against opponents (same engine with different parameters).
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
whereagles
Posts: 565
Joined: Thu Nov 13, 2014 12:03 pm

Re: Genetical tuning

Post by whereagles »

hmmm.. since the search space of your problem seems continuous (a set of parameter values), I would venture to say a particle swarm approach might work better. Genetical algorithms are more suited to combinatorial optimization stuff.

Note that I say this in isolation, without knowing much in terms of detail, so I might be a bit off.

In any case you need a lot of computer power to get meaningful results.. as someone said, the standard deviations are quite large for small sample sizes.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Genetical tuning

Post by Ferdy »

stegemma wrote:
Ferdy wrote:
stegemma wrote:- genetically cross N/4 players with another random player, but the last one
Could you describe in a layman's terms how this is done with the engine?
I have a group of players, each one is an engine with its parameters. The parameters all together describe a single individual in the population (in effect, a single array, in C++). The genetic algorithm works on those sets of parameters. We can think at this sets as they were the DNA of the players, so a single array of parameters is a single DNA of a single player.

I start learning defining:

- the number of iterations
- the number of players (the population)
- the time per move
- the max number of moves, to stop endless games

At any iteration, I play a round-robin match (all against all), in the population; 8 players (engines), for sample, plays each one 7 game with white and 7 game with black. I collect the score of any player and I sort players at the end of the tournament (the best scores first) and now I apply the genetic algorithm:

1) I randomly choose player A out the whole population (but the last)
2) I randomly choose player B (idem)
3) I merge the parameters from player B into player A (this changes player A, not player B)
4) I repeat step 1 for N/4 times (2 times, if population is of 8 players)
5) I set the last player (the worst) with random values then I merge that player with the first and the second one (the best ones)

Of course, because the last player will be changed at any time, in step 1 I avoid choosing that player for random merging.

That's all folks :)
Very good thanks.
So for example I have an engine, say I will create 8 engines with different set of params out from that engine.
eng1 is default
eng2 has mobility changes
eng3 has passer changes and new pawn value other than the default
eng4 has threat changes
eng5 has piece values changes
eng6 has knight outpost changes
eng7 has rook in open file changes
eng8 has king shelter changes
Is that a possibility?

Say after the a certain tournament is done, eng1 is first, eng2 is second, eng8 is last. So in (5) eng8 will use now the values from eng1 and eng2, is that correct?

What typical TC do you use in the tournament?
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Genetical tuning

Post by stegemma »

Ferdy wrote:[...]Very good thanks.
So for example I have an engine, say I will create 8 engines with different set of params out from that engine.
eng1 is default
eng2 has mobility changes
eng3 has passer changes and new pawn value other than the default
eng4 has threat changes
eng5 has piece values changes
eng6 has knight outpost changes
eng7 has rook in open file changes
eng8 has king shelter changes
Is that a possibility?

Say after the a certain tournament is done, eng1 is first, eng2 is second, eng8 is last. So in (5) eng8 will use now the values from eng1 and eng2, is that correct?

What typical TC do you use in the tournament?
Any engine has a different set of parameters, I don't change just one per engine but even this method maybe could be try. Just put all the parameters in one array, any engine has its own array. In my test, I start froma common set for half the engines and put the other with random values but you can also start with all random values. My program is in C++, so that I can do something like this (oversimplified, of course):

Code: Select all


class clsEngine
{
   public:
     int values[17];
     ...
     void SetRandomValues();
     void MergeValues(clsEngine &other_engine);
};

const int N = 8;
clsEngine engines[N];

// set random values 
for&#40;int i=0; i<N/2; i++) engines&#91;i&#93;.SetRandomValues&#40;);

for&#40;int iteration=0; iteration<1000; iteration++)
&#123;

for&#40;int a=0; a<N; a++)
for&#40;int b=a+1; b<N; b++)
&#123;
  int score = PlayGame&#40;a, b&#41; - PlayGame&#40;b, a&#41;;
  a.score += score;
  b.score -= score;
&#125;

Sort&#40;engines&#41;;

for&#40;int i=0; i<N/4; i++)
&#123;
   int a = Rand&#40;N&#41;;
   int b = Rand&#40;N&#41;;
   if&#40;a!=b&#41; &#123; a.MergeValues&#40;b&#41;;  a.score = 0;  &#125;
&#125;

engines&#91;N-1&#93;.SetRandomValues&#40;);
engines&#91;N-1&#93;.MergeValues&#40;engines&#91;0&#93;);
engines&#91;N-1&#93;.MergeValues&#91;engines&#91;1&#93;);
engines&#91;N-1&#93;.score = 0;

&#125;

As you can see, class clsEngine hold the array of parameters of a single engine. Any object of that class has the functions to do the search, of course, so that PlayGame function have only to call that functions:

Code: Select all


int PlayGame&#40;clsEngine &white, clsEngine &black&#41;
&#123;
   for&#40;int m=0; m<50; m++)
   &#123;
       string move = white.Search&#40;);
       white.Execute&#40;move&#41;;
       black.Executemove&#40;move&#41;;
       move = black.Search&#40;);
       white.Execute&#40;move&#41;;
       black.Executemove&#40;move&#41;;
   &#125;
   return white.score;
&#125;
Of course even this has benne oversimplified, because clsEngine class derives froma clsThread class and could be run asynchronously, to ply multiple games at the same time (but I don't do it, for now).

PS: this way I don't have different program running but only one, with multiple engine objects
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Genetical tuning

Post by Ferdy »

stegemma wrote:
Ferdy wrote: Say after the a certain tournament is done, eng1 is first, eng2 is second, eng8 is last. So in (5) eng8 will use now the values from eng1 and eng2, is that correct?

What typical TC do you use in the tournament?
Any engine has a different set of parameters, I don't change just one per engine but even this method maybe could be try. Just put all the parameters in one array, any engine has its own array. In my test, I start froma common set for half the engines and put the other with random values but you can also start with all random values. My program is in C++, so that I can do something like this (oversimplified, of course):

Code: Select all


class clsEngine
&#123;
   public&#58;
     int values&#91;17&#93;;
     ...
     void SetRandomValues&#40;);
     void MergeValues&#40;clsEngine &other_engine&#41;;
&#125;;

const int N = 8;
clsEngine engines&#91;N&#93;;

// set random values 
for&#40;int i=0; i<N/2; i++) engines&#91;i&#93;.SetRandomValues&#40;);

for&#40;int iteration=0; iteration<1000; iteration++)
&#123;

for&#40;int a=0; a<N; a++)
for&#40;int b=a+1; b<N; b++)
&#123;
  int score = PlayGame&#40;a, b&#41; - PlayGame&#40;b, a&#41;;
  a.score += score;
  b.score -= score;
&#125;

Sort&#40;engines&#41;;

for&#40;int i=0; i<N/4; i++)
&#123;
   int a = Rand&#40;N&#41;;
   int b = Rand&#40;N&#41;;
   if&#40;a!=b&#41; &#123; a.MergeValues&#40;b&#41;;  a.score = 0;  &#125;
&#125;

engines&#91;N-1&#93;.SetRandomValues&#40;);
engines&#91;N-1&#93;.MergeValues&#40;engines&#91;0&#93;);
engines&#91;N-1&#93;.MergeValues&#91;engines&#91;1&#93;);
engines&#91;N-1&#93;.score = 0;

&#125;

As you can see, class clsEngine hold the array of parameters of a single engine. Any object of that class has the functions to do the search, of course, so that PlayGame function have only to call that functions:

Code: Select all


int PlayGame&#40;clsEngine &white, clsEngine &black&#41;
&#123;
   for&#40;int m=0; m<50; m++)
   &#123;
       string move = white.Search&#40;);
       white.Execute&#40;move&#41;;
       black.Executemove&#40;move&#41;;
       move = black.Search&#40;);
       white.Execute&#40;move&#41;;
       black.Executemove&#40;move&#41;;
   &#125;
   return white.score;
&#125;
Of course even this has benne oversimplified, because clsEngine class derives froma clsThread class and could be run asynchronously, to ply multiple games at the same time (but I don't do it, for now).

PS: this way I don't have different program running but only one, with multiple engine objects
Here,

Code: Select all

class clsEngine 
&#123; 
   public&#58; 
     int values&#91;17&#93;; 
     ... 
     void SetRandomValues&#40;); 
     void MergeValues&#40;clsEngine &other_engine&#41;; 
&#125;; 
The int values[17], could be,
values[0], for passer at 7th rank
values[1], for knight mobility
values[2], for pawn value
... and so on is that so?
And when you call SetRandomValues(), values[]
will be filled with random numbers appropriate for
specific elements.
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Genetical tuning

Post by stegemma »

Ferdy wrote:[...]
Here,

Code: Select all

class clsEngine 
&#123; 
   public&#58; 
     int values&#91;17&#93;; 
     ... 
     void SetRandomValues&#40;); 
     void MergeValues&#40;clsEngine &other_engine&#41;; 
&#125;; 
The int values[17], could be,
values[0], for passer at 7th rank
values[1], for knight mobility
values[2], for pawn value
... and so on is that so?
And when you call SetRandomValues(), values[]
will be filled with random numbers appropriate for
specific elements.
Yes!
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Genetical tuning

Post by Ferdy »

stegemma wrote:
Ferdy wrote:[...]
Here,

Code: Select all

class clsEngine 
&#123; 
   public&#58; 
     int values&#91;17&#93;; 
     ... 
     void SetRandomValues&#40;); 
     void MergeValues&#40;clsEngine &other_engine&#41;; 
&#125;; 
The int values[17], could be,
values[0], for passer at 7th rank
values[1], for knight mobility
values[2], for pawn value
... and so on is that so?
And when you call SetRandomValues(), values[]
will be filled with random numbers appropriate for
specific elements.
Yes!
This is indeed a very interesting method to try, like creating child from good performing individuals,
and slowly remove individuals that showed decreasing performance. I thought about just giving a million or 2, good test suites instead of game playing.

Something like create individual engines by giving
setoption name passer value 120
setoption name mobility_percent 80
...

Then another individual with different or random values.

Feed the test suite, return the static eval or qsearch score,
the engine that gives the minimum eval error will be the first in the ranking.

Keep a record of all those individual, as after the test those
good performers will be put to a serious game tests.