Chess, Patterns, Linux and Anki

October 10, 2018

Patterns and Chess

Recently my interest in chess surged again and I started (once again) studying about chess. There are a lot (and I mean a lot!) books, tutorials, videos that promise you “the perfect training program” and other such golden promises. If we set aside the fact that if you haven’t played and studied chess seriously until the age of 18 the odds that you will become a GM are very slim, there’s still the question of what is the “best training method” to achieve your full potential. Even though my current way of studying is based on the “Russian methodology” (studying the Yusupov book series), my interests about how the human brain works and about the field of patterns in general, pushed me to search about pattern recognition in chess. Apparently there have been studies [1, 2, 3] that show that grandmasters are most of the times able to “recall” decisions from their brains. These recalls may be from previous games they ’ve played/studied or can even be patterns based on them. By saying patterns here we mean a board setup that ressembles in its’ core elements a position that has already been studied/played. By recalling the pattern, one can be able to make quicker decisions on how to move.

It’s not only memory

A very important clarification is needed here: Simply recalling a pattern doesn’t guarantee you a good move. You will have to be able to recognise the key elements and reason about whether this pattern fits or changes have to be made to push towards a position that will be a good pattern match for which you know the solution. So it’s not only about remembering, but also applying reason and thinking on the position. Chess is not only a game of calculation either. It’s a game that combines, strategy, tactics, patterns, and calculation giving as a result a complex and intriguing board game. So hopefully none here is committing hubris thinking that chess is simply a memorization game. But at the same time we realize that a certain kind of memory must also be obtained somehow: a memory for patterns. There are books that focus on specific chess patterns like Techniques of Positional Play by Broznik and the books by Arthur Van De Oudeweetering Improve/Train Your Chess Pattern Recognition. Ok so we know that we can study patterns. How can we remember them? And here comes the real debate! There are people who say that by studying the same tactics again and again, you can build the pattern memory we were talking about before. Most famous ones are Michael de la Maza with his book Rapid Chess Improvement who talks about the Seven Circles (like Dante’s hell :P) and the more recent one The Woodpecker Method by Axel Smith & Hans Tikkanen. Their common belief is that training the same themes again and again, will help you build an unconscious pattern recognition skill.

SRS

Of course these ideas have provoked many arguments against such “simplistic way of thinking about the game of chess”. My understanding though is that these arguments focus mainly on the false promise that simply by remembering patterns you can be a great player. I haven’t read De La Maza’s book, but I’ve read the preface and the guidelines in the “Woodpecker book” and I’ve seen no such promises. Instead, what the authors tell us is that their focus is on building the pattern recognition mechanism but that this does not guarantee good chess play. It is supplementary. So there’s no “secret key” to become a GM. Instead what we see here is a flavor of the well known Spaced repetition system. We know that somehow the Spacing Effect works. We get to know it from early age when we realize the hard way, that studying the whole book for the first time a couple of days before the exams will not help us at all not matter how hard we try to cram all that information on our brain.

SRS with computers

Ok so we don’t believe that pattern memory will make us “GM in X days” but we want to train our pattern memory because we know that it will be of some help in our play (especially in terms of time at least). How do we train it? Fortunately for us, there are programs that focus on the Spaced Repetition System (SRS). These are usually in terms of small card decks that we are shown from time to time to make use of the SRS. Such programs are Mnemosyne and Anki. Ok so how do these programs can help us on our chess related goal?

I will focus here on Anki which has a plugin that allows to visualize a FEN record as a chess board setup (unfortunately this plugin is not available on the android app version but there is a capability as option in settings). This means that we can have a couple of positions in FEN record as a quizz and on the other side of the card, the solution with algebraic notation. Many books noawadays come with a .pgn version of every diagram, so that means we have the FEN positions and the solutions. How can we built decks quickly without having to copy paste every FEN position with their solution?

A practical problem

I recently obtained a couple of pgn files of the book Practical Chess Exercises. The pgn files were in the following format:

[Event "1 An Overriding Factor"]
[Site "?"]
[Date "1.??.??"]
[Round "1"]
[White "You"]
[Black "#97"]
[Result "*"]
[SetUp "1"]
[FEN "1rb2rk1/p1q1ppbp/n2p3B/3n1P2/1ppP4/3B1N2/PPPQN1PP/K2R3R w - - 0 1"]
[PlyCount "1"]

White to move *

[Event "1 An Overriding Factor"]
[Site "?"]
[Date "1.??.??"]
[Round "1"]
[White "You"]
[Black "#97"]
[Result "*"]
[SetUp "1"]
[FEN "1rb2rk1/p1q1ppbp/n2p3B/3n1P2/1ppP4/3B1N2/PPPQN1PP/K2R3R w - - 0 1"]
[PlyCount "1"]

Don't worry about the attacked piece 1. Qg5 and it's mate next move. *

What we see here? The same position 2 times: One that shows the board and says whose turn is to play (which is also noted in the FEN as w), and another that shows the same board setup and has the solution in algebraic notation. So my next question was “how can I build a csv that will be imported from Anki to build a deck?”. Anki can import a csv as a deck as long as there are two columns: the first one corresponds to the front card side (the challenge) and the second to the back card side (the solution). So I had to find a way to automate this, and skip the tedious edit/copy/paste part.

Linux to the rescue

I’m using Linux as my operating system which provides the bash language. So instead of writing a program in Python I thought that with bash I might be able to build a fast chain of commands to solve my problem. And I did! This is how I did it:

Let’s start with the solutions part
awk '/\[PlyCount/{flag=1 ++n; next}  /\[Event/{flag=0} flag && n%2==0' PCE_Difficulty_Level_1.pgn | awk ' /^$/ { print "\n"; } /./ { printf("%s ", $0); } END { print ""; } ' | sed  '/^$/d' > solutions.txt

I wanted to keep the annotations but because I had the same board position twice, I wanted only the even results (like the example above).

The first part in the above line sets an expression to find, a flag to keep only the text between Plycount and Event and a counter. If the counter is even (n%2==0) then we print the line (more info how the flag works can be found here).

The second part, prints a new line before each result, then the result text, and then for every result it replaces the newlines with spaces (remember we want the annotated solution as a one-liner).

The thirt part is the simplest: Because, as we see in the example above, the annotation contains empty lines, that means they are part of our result. So the sed '/^$/d' finds the lines that are empty and deletes them. We then redirect our results to a text named solutions.txt. We will put these lines in the second column of our .csv file

Now the board setup

We want to keep the FEN position setup. This is easy:

awk '/\[FEN.*/' PCE_Difficulty_Level_1.pgn 

awk fetches us the lines that contain the FEN part. We know that we have double results, and we know that obviously each board setup is unique. So we filter the results with uniq:

awk '/\[FEN.*/' PCE_Difficulty_Level_1.pgn | uniq

Now for the final part. Reading the Anki plugin documentation, we see that the plugin recognises the following expression:

[fen]1rb2rk1/p1q1ppbp/n2p3B/3n1P2/1ppP4/3B1N2/PPPQN1PP/K2R3R w - - 0 1[/fen]

So how do we convert

[FEN "1rb2rk1/p1q1ppbp/n2p3B/3n1P2/1ppP4/3B1N2/PPPQN1PP/K2R3R w - - 0 1"]

to

 [fen]1rb2rk1/p1q1ppbp/n2p3B/3n1P2/1ppP4/3B1N2/PPPQN1PP/K2R3R w - - 0 1[/fen]

?

Easy:

sed -E "s/(\[FEN \")(.*)(\")/\[fen\]\2\[\/fen/g"

Using sed in “extended regular expression” mode, we create 3 groups. First group is the [FEN " match, second group is everything after, and third group are the quotation marks " (we keep the trailing ]). So reading the plugin documentation, we see that we want to keep the second group and surround it with [fen] and [/fen]. This is exactly what the second part of sed does (\[fen\]\2\[\/fen). So the complete expression that will craft us the FEN positions in a format that the plugin understands and store them in a text file named fen.txt is:

awk '/\[FEN.*/' PCE_Difficulty_Level_1.pgn | uniq | sed -E "s/(\[FEN \")(.*)(\")/\[fen\]\2\[\/fen/g" > fen.txt

END

So there you have it! Now we can craft the csv with our two columns in a way that Anki can import, and store as a deck! First csv column is crafted using

awk '/\[FEN.*/' PCE_Difficulty_Level_1.pgn | uniq | sed -E "s/(\[FEN \")(.*)(\")/\[fen\]\2\[\/fen/g" > fen.txt

and second csv column is crafted using

awk '/\[PlyCount/{flag=1 ++n; next}  /\[Event/{flag=0} flag && n%2==0' PCE_Difficulty_Level_1.pgn | awk ' /^$/ { print "\n"; } /./ { printf("%s ", $0); } END { print ""; } ' | sed  '/^$/d' > solutions.txt

Happy studying!


comments powered by Disqus