Time management

Discussion of chess software programming and technical issues.

Moderator: Ras

eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Time management

Post by eligolf »

Hi everyone,

Progress is continuing slowly but surely on my Python chess engine. When testing a few different time controls I found that the time management could probably be a bit better than it currently is. Right now it is very basic for the case of giving blitz input (minutes, seconds, increment):

Code: Select all

        # If time control is available, flag that we play with time control and set timing
        if tot_time != 0:
            timeset = 1

            # Divide time equally between how many moves we should do
            tot_time /= movestogo

            # Lag compensation 100 ms
            tot_time -= 0.1

            # Add time and increment to get what time we should stop at
            stoptime = starttime + tot_time + self.theme.inc


So basically it divides the time it has by how many moves that the game should be (set to 50 as default), and then add increment.

I have been looking at e.g. https://www.chessprogramming.org/Time_Management which explains some concepts to think about. Before starting to fiddling around on my own, is there a "standard" approach to use when it comes to blitz time management? Or should I just experiement and see what works best for some given test time controls (3-0, 3-2, 5-0, 5-5 etc)?
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Time management

Post by hgm »

It is best to allow the engine some leeway in deciding on how long it is gonna take for a move. Aborting the search in the middle of a depth iteration usually makes most effort on that iteration going to waste. So you would have benefited by not starting iterations you will surely not be able to finish, so that you can put the saved time to better use in the remainder of the game. That also means you can afford to think longer than the total time divided by the expected number of moves on other occasions, without getting into time trouble.

Even if you do this, iterations sometimes take much longer than could be expected, so that you cannot finish them anyway. Usually this happens when the engine suddenly detects that the move it was planning to play is no good, and that it should better swicth move if it doesn't want to lose. In that case enforcing strict time would be very detrimental; allowing the engine to avoid the disaster is about the best usage of time you could wish for. So in case of a score drop compared to the previous iteration you should award generous extra time. You can then abort the search in the root as soon as you have found a move that is about as good as the previous iteration, and play that one.
makc2299
Posts: 7
Joined: Thu Sep 15, 2022 3:50 pm
Full name: Maksim Yukhimets

Re: Time management

Post by makc2299 »

Hello, sorry for a possibly stupid question, but I'm really trying to understand how this TC improvement works.This isn't the first time I've come across asuggestion like this.
hgm wrote: Sun Jan 31, 2021 7:12 pm So in case of a score drop compared to the previous iteration you should award generous extra time. You can then abort the search in the root as soon as you have found a move that is about as good as the previous iteration, and play that one.
For example, my search gives me the following output:

info score cp 1895 depth 1 nodes 82 time 0 pv g7h8q
info score cp 1790 depth 2 nodes 592 time 1 pv g7h8q d8h4
info score cp 1790 depth 3 nodes 2483 time 3 pv g7h8q d8h4 e1d2
info score cp 1790 depth 4 nodes 13687 time 24 pv g7h8q d8h4 e1d2 h4d4
info score cp 1790 depth 5 nodes 70813 time 72 pv g7h8q d8h4 e1d2 h4d4 c2c3
info score cp 1790 depth 6 nodes 306224 time 452 pv g7h8q d8h4 e1d2 h4d4 c2c3 d4d5
info score cp 1805 depth 7 nodes 1818125 time 947 pv g7h8q d8h4 e1d2 h4d4 c2c3 d4f4 e2e3
info score cp 1795 depth 8 nodes 8863760 time 8214 pv g7h8q d8h4 e1d2 h4h6 e2e3 e5d4 d2e1 d4e3

My last iteration returns 1795 score which less than previous. Does this mean that I needed to allocate extra time to look for another iteration in the hope that it would give me a score higher or the same as 1805. If so, how long should I allow the search and also won't this situation happen too often? Maybe in my case it's because my evaluation function is too rudimentary or something but I'm afraid that by allocating extra time often I will go beyond TC.
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Time management

Post by hgm »

No, that is not what I meant. You sow scores at the end of each iteration, and then no extra time is needed to finish the iteration. What I was talking about is whether you should abort the iteration before it is finished, or not.

For one, a drop from 1805 to 1795 is negligible. And you already found that score at d=8 on the first move, as the PV did not change. So if halfway the d=8 iteration you would have reached your nominal thinking time, there would be no reason at all to finish that iteration. (Provided the search of the first move has already completed, so the bestScore at that point is 1795.) It is very unlikely you would find anything better, so very likely that you would just waste time to no effect.

Had your first move g7h8q suddenly returned a score of 1000, however, you are apparently running into some deeply hidden tactics, and it is not so inconceivable that you might find a better move immune to that, which will score far higher (e.g. promote on another square, or on a later move in the PV). So than you should try to continue the iteration until you find something better. The previous iterations did not exclude at all that you have alternatives that would score, say, 1700 at low depth already. It would be a pity if you overlooked those.
makc2299
Posts: 7
Joined: Thu Sep 15, 2022 3:50 pm
Full name: Maksim Yukhimets

Re: Time management

Post by makc2299 »

hgm wrote: Mon Feb 27, 2023 10:42 pm No, that is not what I meant. You sow scores at the end of each iteration, and then no extra time is needed to finish the iteration. What I was talking about is whether you should abort the iteration before it is finished, or not.

For one, a drop from 1805 to 1795 is negligible. And you already found that score at d=8 on the first move, as the PV did not change. So if halfway the d=8 iteration you would have reached your nominal thinking time, there would be no reason at all to finish that iteration. (Provided the search of the first move has already completed, so the bestScore at that point is 1795.) It is very unlikely you would find anything better, so very likely that you would just waste time to no effect.

Had your first move g7h8q suddenly returned a score of 1000, however, you are apparently running into some deeply hidden tactics, and it is not so inconceivable that you might find a better move immune to that, which will score far higher (e.g. promote on another square, or on a later move in the PV). So than you should try to continue the iteration until you find something better. The previous iterations did not exclude at all that you have alternatives that would score, say, 1700 at low depth already. It would be a pity if you overlooked those.
I understand, thanks for the answer.
makc2299
Posts: 7
Joined: Thu Sep 15, 2022 3:50 pm
Full name: Maksim Yukhimets

Re: Time management

Post by makc2299 »

hgm wrote: Mon Feb 27, 2023 10:42 pm And you already found that score at d=8 on the first move, as the PV did not change
Ok but what if at the moment when my time is up at the d=8 I still haven't searched g7h8q and my PV is different. For example my move ordering didn't put g7h8q on first place and i only complete search for previous moves at the root. So i have to keep searching until g7h8q score and then compare with g7h8q score (best move) from previous iteration ? Or i can compare with PV score i have at that moment and if it's better allocate extra time to complete the iteration.
JVMerlino
Posts: 1396
Joined: Wed Mar 08, 2006 10:15 pm
Location: San Francisco, California

Re: Time management

Post by JVMerlino »

makc2299 wrote: Tue Feb 28, 2023 6:04 pm
hgm wrote: Mon Feb 27, 2023 10:42 pm And you already found that score at d=8 on the first move, as the PV did not change
Ok but what if at the moment when my time is up at the d=8 I still haven't searched g7h8q and my PV is different. For example my move ordering didn't put g7h8q on first place and i only complete search for previous moves at the root. So i have to keep searching until g7h8q score and then compare with g7h8q score (best move) from previous iteration ? Or i can compare with PV score i have at that moment and if it's better allocate extra time to complete the iteration.
You should always search the best move from the previous iteration first. If you're not doing that already, you should see a huge improvement in "time-to-depth".
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Time management

Post by hgm »

Indeed, if you do not search the best move first, there is no need to worry about time management. Fix that first.
makc2299
Posts: 7
Joined: Thu Sep 15, 2022 3:50 pm
Full name: Maksim Yukhimets

Re: Time management

Post by makc2299 »

hgm wrote: Mon Feb 27, 2023 10:42 pm For one, a drop from 1805 to 1795 is negligible.
What i should consider as a negligible change ? For example if my first move droped from 30 to 10 cp it is alright but > 100 cp no.
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Time management

Post by hgm »

Exactly. If the score drop suggests you will actually lose material, there is reason to worry, as gaining material is not easy, and you probably won't get it back. A little bit of positional score doesn't mean much. Perhaps the last move in the PV gained a few PST points by moving a piece to a good square. Often you can the chase that piece away by attacking it with a lower-valued piece on the next ply, that was just outside the horizon. But when the last move of the PV captured one of your Pawns, you know you are not going to recapture it in the first move beyond the horizon. Because if that were possible, QS would already have seen it.