# Using Stockfish to identify ideal squares

How can an engine be used to answer positional questions?

One of my favourite exercises in chess is the ideal square exercise. This exercise involves looking at a piece and imagining which square it would be best placed on, without initially worrying about how to get it there.
It's a simple exercise, but it deepens your understanding of a given position and can point to possible strategic plans in the position.
However, determining exactly where a piece is best placed can be challenging. When one comes up with an answer, there is no easy way to check if it’s correct, so you would need to consult a stronger player, which isn't always feasible.
Since this exercise is quite simple, I decided to try and write a program that identifies the ideal square for a given piece using Stockfish.

### Naive Approach

Firstly, I decided to emulate the human approach for finding the ideal square: removing a piece from its square and looking at possible new squares.
So I took a position and removed a piece from a given square. Then I placed the piece on every possible empty square. I evaluated the initial and the resulting positions and compared the change in evaluation. The ideal square is chosen to be the square where the corresponding position hast the highest evaluation.
I changed the move to the other side after I placed the piece on the new square so that one side doesn't move twice in a row. This should prevent some problems where one square gets favoured due to tactical reasons.
Still, it's clear that this approach is a bit too naive since a piece can be strong on a square for tactical reasons. The following position from the game Larsen-Korchnoi, 1969 nicely illustrates the problem with this method:

https://lichess.org/study/DEfXX4Ra/YZzW34UW#0

In the game, Larsen rerouted the f3-knight to d5 and it's clear that this is the best square for the knight.
However, my program had other ideas. It wanted to move the knight to c6 since Black can't prevent material loss, even if it is their move.
So this problem needs to be addressed before looking at more positions.

### Counting Material

There are many possible ways to try and fix this issue, but for now, I decided to look at the material. I counted the material in the starting position and at the end of Stockfish's PV after moving the piece to the new square. Then I compared the material balances and disregarded any squares where one side wins more than a pawn.
Making this change fixed the example from above and the program now gives the d5-square as the ideal square for the knight on f3.
The program certainly isn’t perfect yet, but good enough to take a look at more positions.

## Results

In order to get a feeling for the advantages and drawbacks of the program, I looked at many different examples taken from the excellent book Mastering Chess Strategy by Johan Hellsten.
The first position is from the game Benko-Najdorf, 1963:

https://lichess.org/study/DEfXX4Ra/l24W4MYi#0

What is the best square for the knight on c3?
The program came up with f5 and Benko agreed. The game continued with 19.Rdh1 Ng6 20.Nd1 Rc8 21.Ne3 Rc7 22.Nf5
Knights are the typical example when thinking about ideal squares but I wanted to make sure that the program gives reasonable answers for different piece types.
So I looked for the ideal square for the c4-bishop in the following position taken from the game Beliavsky-Romanishin, 1978:
https://lichess.org/study/DEfXX4Ra/E1SS2NCB#0

My program came up with g2 as the ideal square and this is clearly the case. In the game, there followed 14.Bf1 Rac8 15.Bg2.
Finally, I also wanted to looked at examples involving rooks, so I searched for the ideal square of the a1-rook in the following position from the game Agrest-Brynell, 2005:
https://lichess.org/study/DEfXX4Ra/CbV7Wb8l#0

Agrest played 15.Ra3! in the game with the idea to lift the rook to the kingside. My program agreed, giving h3 as the ideal square and g3 as the second best.
These examples are all very promising and I'm very happy with the results overall, but there a still positions where the program gives bad answers and so there are things that could be improved in the future.

## Possible Improvements

### Unrealistic Squares

The biggest issue with this naive approach is that a piece can be placed on literally any square (not regarding the material constraint mentioned before). This can lead to some interesting suggestions, as in the game Gamrekeli-Petrosian, 1944:

https://lichess.org/study/DEfXX4Ra/rRQqGp37#0

In the game, a young Petrosian played 21...Ra6 followed by 22...Rga8 to prepare for the opening of the a-file.
My program has the brilliant idea to move the rook to b3. Of course, it would be much stronger on b3, but it can never get to that square, so it shouldn't be considered. The other top answers are e3, c2, and c3 which are also completely unrealistic.
But when only considering the realistic squares, the best squares are a7 and a6, so one would need a better way to determine which squares can’t be realistically reached and exclude them from the consideration.

### Pawn Moves

The pawn structure plays a big role in determining how active a piece is, so it'd be very interesting to have a program that takes possible pawn moves into account. The following example from the game Timman-Ikonnikov, 2009 illustrates this well:

https://lichess.org/study/DEfXX4Ra/Zg08HLwJ#0

White's bishop on c1 currently doesn't look very good, but if the long diagonal opens up, it would be amazing on b2. So Timman played 14.c4! to prevent Black from playing 14...c4 and fixing the white pawn on c3.
Since the bishop currently can't go anywhere, my program suggests that it should be left on c1, which basically means that White should do something differently on that move, since placing the bishop on a different square would give Black the move.
Allowing White to move the bishop to another square and keep the right to move also doesn't work too well. In that case, Stockfish wants to move the bishop from c1 to b4 and play 1.Bxa5 Bc8 2.Bb4. As I had expected, allowing one side to move a piece to any square and keeping the right to move is just too much.

### King Position

I'm also unsure if this approach can be used to find the best place for the king. My main concern is that it's often best to castle which means that the rook and king have to move at the same time. But if there are still pieces on their starting squares, they also have to move before one can castle, so one needs to find a square for them before castling. Hence, a similar approach would lead to moving many pieces and the resulting position will probably be very different from the starting position.

## Future Ideas

I think that using ideal squares can be a helpful tool in exploring other positional topics.
One refinement of finding the ideal square would be to quantify how good a piece is on a given square. This would help to compare the strength of a piece on two different squares. Moreover, it would also show which piece should occupy an outpost, if multiple pieces could move to that square.
A slightly different, but closely related positional theme is the concept of the worst placed piece. I think that being able to quantify how strong a piece on a given square is, would allow one to pretty easily determine the worst placed piece in a position.
I'd be very interested to hear what you think about these ideas and my method for finding the ideal square for a piece.

If you enjoyed the post, please check out my Substack for post about similar topics.