4e1da41d8c2a69a2eaa02d0e7471ec475db19c54
[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                 if(!xboard) /* no pre-add of increment in XBoard mode */
168                     TimeControl.clock[side] += TCadd;
169                 ResponseTime = (TimeControl.clock[side])
170                     / (((TimeControl.moves[side]) * rtf) + 1);
171                 TCleft = (long)ResponseTime / tcq;
172                 ResponseTime += TCadd / 2;
173             }
174
175             if (TimeControl.moves[side] < 5)
176             {
177                 TCcount = MAXTCCOUNTX - 10;
178
179                 if (TCcount < 0)
180                     TCcount = 0;
181
182                 DetermineTCcount = false;
183             }
184         }
185
186         if (ResponseTime < MINRESPONSETIME)
187         {
188             ResponseTime = MINRESPONSETIME;
189             TCcount = MAXTCCOUNTX - 10;
190
191             if (TCcount < 0)
192                 TCcount = 0;
193
194             DetermineTCcount = false;
195         }
196
197         if (!hard_time_limit && (ResponseTime < 2 * MINRESPONSETIME))
198         {
199             TCcount = MAXTCCOUNTX - 10;
200
201             if (TCcount < 0)
202                 TCcount = 0;
203
204             DetermineTCcount = false;
205         }
206     }
207     else
208     {
209         TCleft = 0;
210         ResponseTime = MaxResponseTime;
211         ElapsedTime(COMPUTE_AND_INIT_MODE);
212     }
213
214     if (DetermineTCcount)
215     {
216         if (TCleft )
217         {
218             int AllowedCounts
219                 = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
220                 / TCleft;
221
222             if (AllowedCounts <= 0)
223                 TCcount = MAXTCCOUNTX;
224             else if (AllowedCounts > MAXTCCOUNTX)
225                 TCcount = 0;
226             else
227                 TCcount = MAXTCCOUNTX - AllowedCounts;
228         }
229         else
230         {
231             TCcount = MAXTCCOUNTX;
232         }
233     }
234
235     if (ResponseTime < MINRESPONSETIME)
236         ResponseTime = MINRESPONSETIME;
237
238 #else
239
240     if (TCflag)
241     {
242         TCcount = 0;
243
244         if (TimeControl.moves[side] < 1)
245             TimeControl.moves[side] = 1;
246
247         /* special case time per move specified */
248         if (flag.onemove)
249         {
250             ResponseTime = TimeControl.clock[side] - 100;
251             TCleft = 0;
252         }
253         else
254         {
255             /* calculate avg time per move remaining */
256
257             if(!xboard) /* no pre-add of increment in XBoard mode */
258                 TimeControl.clock[side] += TCadd;
259
260             ResponseTime = (TimeControl.clock[side])
261                 / (((TimeControl.moves[side]) * 2) + 1);
262             TCleft = (int) ResponseTime / 3;
263             ResponseTime += TCadd / 2;
264
265             if (TimeControl.moves[side] < 5)
266                 TCcount = MAXTCCOUNTX - 10;
267         }
268
269         if (ResponseTime < 101)
270         {
271             ResponseTime = 100;
272             TCcount = MAXTCCOUNTX - 10;
273         }
274         else if (ResponseTime < 200)
275         {
276             TCcount = MAXTCCOUNTX - 10;
277         }
278     }
279     else
280     {
281         ResponseTime = MaxResponseTime;
282         TCleft = 0;
283         ElapsedTime(COMPUTE_AND_INIT_MODE);
284     }
285
286     if (TCleft)
287     {
288         TCcount = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
289             / TCleft;
290
291         if (TCcount > MAXTCCOUNTX)
292             TCcount = 0;
293         else
294             TCcount = MAXTCCOUNTX - TCcount;
295     }
296     else
297     {
298         TCcount = MAXTCCOUNTX;
299     }
300 #endif
301 printf("# %2d. moves=%d,%d time=%d,%d ResponseTime=%d+%d\n",GameCnt,TimeControl.moves[computer],TimeControl.moves[opponent],TimeControl.clock[computer],TimeControl.clock[opponent],ResponseTime,TCleft);
302     assert(TCcount <= MAXTCCOUNTX);
303 }
304
305
306
307 void
308 CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
309 {
310     if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
311         flag.timeout = true;
312
313     else if (TCflag && (Sdepth > (MINDEPTH - 1)) && (TCcount < MAXTCCOUNTR))
314     {
315         if (killr0[1] != PrVar[1] /* || Killr0[2] != PrVar[2] */)
316         {
317             TCcount++;
318             ExtraTime += TCleft;
319         }
320
321         if ((TCcount < MAXTCCOUNTR)
322             && (abs(score - globalscore) / Sdepth) > ZDELTA)
323         {
324             TCcount++;
325             ExtraTime += TCleft;
326         }
327     }
328
329     if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
330         ExtraTime = 0;
331
332     ElapsedTime(COMPUTE_MODE);
333
334     if (root->flags & exact)
335         flag.timeout = true;
336     /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
337      */
338 #if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
339     else if (!(Sdepth < MINDEPTH)
340              && TCflag
341              && ((4 * et) > (2*ResponseTime + ExtraTime)))
342         flag.timeout = true;
343 #else
344     else if (!(Sdepth < MINDEPTH)
345              && TCflag
346              && ((int)(1.93913099l * (pow((double)et, 1.12446928l)))
347                  > (ResponseTime + ExtraTime)))
348         flag.timeout = true;
349 #endif
350
351     if (flag.timeout)
352         dsp->ShowMessage("timeout");
353 }
354
355
356 /*
357  * Determine the time that has passed since the search was started. If the
358  * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
359  * to true which will terminate the search.
360  * iop = COMPUTE_MODE calculate et, bump ETnodes
361  * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
362  *     set reference time
363  */
364 void
365 ElapsedTime(ElapsedTime_mode iop)
366 {
367     long current_time;
368 #ifdef HAVE_GETTIMEOFDAY
369     struct timeval tv;
370 #endif
371
372     dsp->PollForInput();
373
374 #ifdef HAVE_GETTIMEOFDAY
375     gettimeofday(&tv, NULL);
376     current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
377 #else
378     et = ((current_time = time((long *) 0)) - time0) * 100;
379 #endif
380
381 #ifdef INTERRUPT_TEST
382     if (iop == INIT_INTERRUPT_MODE)
383     {
384         itime0 = current_time;
385     }
386     else if (iop == COMPUTE_INTERRUPT_MODE)
387     {
388         it = current_time - itime0;
389     }
390     else
391 #endif
392     {
393 #ifdef HAVE_GETTIMEOFDAY
394         et = current_time - time0;
395 #endif
396         ETnodes = NodeCnt + znodes;
397
398         if (et < 0)
399         {
400 #ifdef INTERRUPT_TEST
401             printf("elapsed time %ld not positive\n", et);
402 #endif
403             et = 0;
404         }
405
406         if (iop == COMPUTE_AND_INIT_MODE)
407         {
408             if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
409                 flag.timeout = true;
410
411             time0 = current_time;
412         }
413
414 #ifdef QUIETBACKGROUND
415         if (!background)
416 #endif
417             dsp->UpdateClocks();
418     }
419 }
420
421
422 void
423 SetTimeControl(void)
424 {
425     if (TCflag)
426     {
427         TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
428         TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
429         TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
430     }
431     else
432     {
433         TimeControl.moves[black] = TimeControl.moves[white] = 0;
434         TimeControl.clock[black] = TimeControl.clock[white] = 0;
435     }
436
437     flag.onemove = (TCmoves == 1);
438     et = 0;
439     ElapsedTime(COMPUTE_AND_INIT_MODE);
440 }
441
442 void
443 RenewTimeControl(int side, int TCadd)
444 {
445     if (flag.gamein || TCadd)
446     {
447         TimeCalc();
448     }
449     else if (TimeControl.moves[side] == 0)
450     {
451         if (XC)
452         {
453             if (XCmore < XC)
454             {
455                 TCmoves   = XCmoves[XCmore];
456                 TCminutes = XCminutes[XCmore];
457                 TCseconds = XCseconds[XCmore];
458                 XCmore++;
459             }
460         }
461
462         SetTimeControl();
463     }
464 }