Move SetTimeControl into tctrl.c.
[gnushogi.git] / gnushogi / tcontrl.c
1 /*
2  * FILE: tcontrl.c
3  *
4  * ----------------------------------------------------------------------
5  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
8  *
9  * GNU SHOGI is based on GNU CHESS
10  *
11  * Copyright (c) 1988, 1989, 1990 John Stanback
12  * Copyright (c) 1992 Free Software Foundation
13  *
14  * This file is part of GNU SHOGI.
15  *
16  * GNU Shogi is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the
18  * Free Software Foundation; either version 3 of the License,
19  * or (at your option) any later version.
20  *
21  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with GNU Shogi; see the file COPYING. If not, see
28  * <http://www.gnu.org/licenses/>.
29  * ----------------------------------------------------------------------
30  *
31  */
32
33
34 #include "gnushogi.h"
35 #include <math.h>
36
37 #define ALTERNATIVE_TC
38
39
40 /*
41  * In a networked enviroment gnushogi might be compiled on different hosts
42  * with different random number generators; that is not acceptable if they
43  * are going to share the same transposition table.
44  */
45
46 static unsigned long next = 1;
47
48 unsigned int
49 urand(void)
50 {
51     next *= 1103515245;
52     next += 12345;
53     return ((unsigned int) (next >> 16) & 0xFFFF);
54 }
55
56
57
58 void
59 gsrand(unsigned int seed)
60 {
61     next = seed;
62 }
63
64
65
66 void
67 TimeCalc()
68 {
69     /* adjust number of moves remaining in gamein games */
70     int increment = 0;
71     int topsum = 0;
72     int tcompsum = 0;
73     int me, him;
74     int i;
75
76     /* Don't do anything until you have enough numbers. */
77     if (GameCnt < (MINGAMEIN * 2))
78         return;
79
80     /* Calculate average time in sec for last MINGAMEIN moves. */
81     for (i = 0; i < MINGAMEIN; i++)
82     {
83         tcompsum += timecomp[i];
84         topsum += timeopp[i];
85     }
86
87     topsum   /= (100 * MINGAMEIN);
88     tcompsum /= (100 * MINGAMEIN);
89
90     /* If I have less time than opponent add another move. */
91     me  = TimeControl.clock[computer] / 100;
92     him = TimeControl.clock[opponent] / 100;
93
94     if (me < him)
95         increment += 2;
96
97     if (((him - me) > 60) || ((me < him) && (me < 120)))
98         increment++;
99
100     /* If I am losing more time with each move add another. */
101     /* If (!((me - him) > 60) && tcompsum > topsum) increment++; */
102
103     if (tcompsum > topsum)
104     {
105         increment += 2;
106     }
107     else if ((TimeControl.moves[computer] < MINMOVES) && !increment)
108     {
109         /* ... but don't let moves go below MINMOVES. */
110         increment++;
111     }
112     else if ((me > him) && (tcompsum < topsum))
113     {
114         /* If I am doing really well use more time per move. */
115         increment = -1;
116     }
117
118     /* If not fischer clock be careful about time. */
119     /* CHECKME: what's a fischer clock? */
120
121     if ((TCadd == 0) && (increment > 0))
122         increment += 2;
123
124     if ((me == 0) && (increment > 0))
125         increment += 2;
126
127     TimeControl.moves[computer] += increment;
128 }
129
130
131
132 /*
133  * Set ResponseTime, TCcount, and TCleft.
134  */
135
136 void SetResponseTime(short side)
137 {
138 #ifdef ALTERNATIVE_TC
139     int DetermineTCcount = true;
140
141     if (TCflag)
142     {
143         TCcount = 0;
144
145         if (TimeControl.moves[side] < 1)
146             TimeControl.moves[side] = 1;
147
148         /* special case time per move specified */
149         if (flag.onemove)
150         {
151             ResponseTime = TimeControl.clock[side] - 100;
152             TCleft = 0;
153         }
154         else
155         {
156             /* calculate avg time per move remaining */
157             if (TimeControl.clock[side] <= 0)
158             {
159                 ResponseTime = 0;
160                 TCleft = (long)MINRESPONSETIME / MAXTCCOUNTX;
161             }
162             else
163             {
164                 short rtf = in_opening_stage ? 8 : 2;
165                 short tcq = in_opening_stage ? 2 : 4;
166
167                 TimeControl.clock[side] += TCadd;
168                 ResponseTime = (TimeControl.clock[side])
169                     / (((TimeControl.moves[side]) * rtf) + 1);
170                 TCleft = (long)ResponseTime / tcq;
171                 ResponseTime += TCadd / 2;
172             }
173
174             if (TimeControl.moves[side] < 5)
175             {
176                 TCcount = MAXTCCOUNTX - 10;
177
178                 if (TCcount < 0)
179                     TCcount = 0;
180
181                 DetermineTCcount = false;
182             }
183         }
184
185         if (ResponseTime < MINRESPONSETIME)
186         {
187             ResponseTime = MINRESPONSETIME;
188             TCcount = MAXTCCOUNTX - 10;
189
190             if (TCcount < 0)
191                 TCcount = 0;
192
193             DetermineTCcount = false;
194         }
195
196         if (!hard_time_limit && (ResponseTime < 2 * MINRESPONSETIME))
197         {
198             TCcount = MAXTCCOUNTX - 10;
199
200             if (TCcount < 0)
201                 TCcount = 0;
202
203             DetermineTCcount = false;
204         }
205     }
206     else
207     {
208         TCleft = 0;
209         ResponseTime = MaxResponseTime;
210         ElapsedTime(COMPUTE_AND_INIT_MODE);
211     }
212
213     if (DetermineTCcount)
214     {
215         if (TCleft )
216         {
217             int AllowedCounts
218                 = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
219                 / TCleft;
220
221             if (AllowedCounts <= 0)
222                 TCcount = MAXTCCOUNTX;
223             else if (AllowedCounts > MAXTCCOUNTX)
224                 TCcount = 0;
225             else
226                 TCcount = MAXTCCOUNTX - AllowedCounts;
227         }
228         else
229         {
230             TCcount = MAXTCCOUNTX;
231         }
232     }
233
234     if (ResponseTime < MINRESPONSETIME)
235         ResponseTime = MINRESPONSETIME;
236
237 #else
238
239     if (TCflag)
240     {
241         TCcount = 0;
242
243         if (TimeControl.moves[side] < 1)
244             TimeControl.moves[side] = 1;
245
246         /* special case time per move specified */
247         if (flag.onemove)
248         {
249             ResponseTime = TimeControl.clock[side] - 100;
250             TCleft = 0;
251         }
252         else
253         {
254             /* calculate avg time per move remaining */
255             TimeControl.clock[side] += TCadd;
256
257             ResponseTime = (TimeControl.clock[side])
258                 / (((TimeControl.moves[side]) * 2) + 1);
259             TCleft = (int) ResponseTime / 3;
260             ResponseTime += TCadd / 2;
261
262             if (TimeControl.moves[side] < 5)
263                 TCcount = MAXTCCOUNTX - 10;
264         }
265
266         if (ResponseTime < 101)
267         {
268             ResponseTime = 100;
269             TCcount = MAXTCCOUNTX - 10;
270         }
271         else if (ResponseTime < 200)
272         {
273             TCcount = MAXTCCOUNTX - 10;
274         }
275     }
276     else
277     {
278         ResponseTime = MaxResponseTime;
279         TCleft = 0;
280         ElapsedTime(COMPUTE_AND_INIT_MODE);
281     }
282
283     if (TCleft)
284     {
285         TCcount = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
286             / TCleft;
287
288         if (TCcount > MAXTCCOUNTX)
289             TCcount = 0;
290         else
291             TCcount = MAXTCCOUNTX - TCcount;
292     }
293     else
294     {
295         TCcount = MAXTCCOUNTX;
296     }
297 #endif
298
299     assert(TCcount <= MAXTCCOUNTX);
300 }
301
302
303
304 void
305 CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
306 {
307     if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
308         flag.timeout = true;
309
310     else if (TCflag && (Sdepth > (MINDEPTH - 1)) && (TCcount < MAXTCCOUNTR))
311     {
312         if (killr0[1] != PrVar[1] /* || Killr0[2] != PrVar[2] */)
313         {
314             TCcount++;
315             ExtraTime += TCleft;
316         }
317
318         if ((TCcount < MAXTCCOUNTR)
319             && (abs(score - globalscore) / Sdepth) > ZDELTA)
320         {
321             TCcount++;
322             ExtraTime += TCleft;
323         }
324     }
325
326     if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
327         ExtraTime = 0;
328
329     ElapsedTime(COMPUTE_MODE);
330
331     if (root->flags & exact)
332         flag.timeout = true;
333     /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
334      */
335 #if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
336     else if (!(Sdepth < MINDEPTH)
337              && TCflag
338              && ((4 * et) > (2*ResponseTime + ExtraTime)))
339         flag.timeout = true;
340 #else
341     else if (!(Sdepth < MINDEPTH)
342              && TCflag
343              && ((int)(1.93913099l * (pow((double)et, 1.12446928l)))
344                  > (ResponseTime + ExtraTime)))
345         flag.timeout = true;
346 #endif
347
348     if (flag.timeout)
349         dsp->ShowMessage("timeout");
350 }
351
352
353 /*
354  * Determine the time that has passed since the search was started. If the
355  * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
356  * to true which will terminate the search.
357  * iop = COMPUTE_MODE calculate et, bump ETnodes
358  * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
359  *     set reference time
360  */
361 void
362 ElapsedTime(ElapsedTime_mode iop)
363 {
364     long current_time;
365 #ifdef HAVE_GETTIMEOFDAY
366     struct timeval tv;
367 #endif
368
369     dsp->PollForInput();
370
371 #ifdef HAVE_GETTIMEOFDAY
372     gettimeofday(&tv, NULL);
373     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
374 #else
375     et = ((current_time = time((long *) 0)) - time0) * 100;
376 #endif
377
378 #ifdef INTERRUPT_TEST
379     if (iop == INIT_INTERRUPT_MODE)
380     {
381         itime0 = current_time;
382     }
383     else if (iop == COMPUTE_INTERRUPT_MODE)
384     {
385         it = current_time - itime0;
386     }
387     else
388 #endif
389     {
390 #ifdef HAVE_GETTIMEOFDAY
391         et = current_time - time0;
392 #endif
393         ETnodes = NodeCnt + znodes;
394
395         if (et < 0)
396         {
397 #ifdef INTERRUPT_TEST
398             printf("elapsed time %ld not positive\n", et);
399 #endif
400             et = 0;
401         }
402
403         if (iop == COMPUTE_AND_INIT_MODE)
404         {
405             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
406                 flag.timeout = true;
407
408             time0 = current_time;
409         }
410
411 #ifdef QUIETBACKGROUND
412         if (!background)
413 #endif
414             dsp->UpdateClocks();
415     }
416 }
417
418
419 void
420 SetTimeControl(void)
421 {
422     if (TCflag)
423     {
424         TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
425         TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
426         TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
427     }
428     else
429     {
430         TimeControl.moves[black] = TimeControl.moves[white] = 0;
431         TimeControl.clock[black] = TimeControl.clock[white] = 0;
432     }
433
434     flag.onemove = (TCmoves == 1);
435     et = 0;
436     ElapsedTime(COMPUTE_AND_INIT_MODE);
437 }