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
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
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.
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
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 * ----------------------------------------------------------------------
37 #define ALTERNATIVE_TC
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.
46 static unsigned long next = 1;
53 return ((unsigned int) (next >> 16) & 0xFFFF);
59 gsrand(unsigned int seed)
69 /* adjust number of moves remaining in gamein games */
76 /* Don't do anything until you have enough numbers. */
77 if (GameCnt < (MINGAMEIN * 2))
80 /* Calculate average time in sec for last MINGAMEIN moves. */
81 for (i = 0; i < MINGAMEIN; i++)
83 tcompsum += timecomp[i];
87 topsum /= (100 * MINGAMEIN);
88 tcompsum /= (100 * MINGAMEIN);
90 /* If I have less time than opponent add another move. */
91 me = TimeControl.clock[computer] / 100;
92 him = TimeControl.clock[opponent] / 100;
97 if (((him - me) > 60) || ((me < him) && (me < 120)))
100 /* If I am losing more time with each move add another. */
101 /* If (!((me - him) > 60) && tcompsum > topsum) increment++; */
103 if (tcompsum > topsum)
107 else if ((TimeControl.moves[computer] < MINMOVES) && !increment)
109 /* ... but don't let moves go below MINMOVES. */
112 else if ((me > him) && (tcompsum < topsum))
114 /* If I am doing really well use more time per move. */
118 /* If not fischer clock be careful about time. */
119 /* CHECKME: what's a fischer clock? */
121 if ((TCadd == 0) && (increment > 0))
124 if ((me == 0) && (increment > 0))
127 TimeControl.moves[computer] += increment;
133 * Set ResponseTime, TCcount, and TCleft.
136 void SetResponseTime(short side)
138 #ifdef ALTERNATIVE_TC
139 int DetermineTCcount = true;
145 if (TimeControl.moves[side] < 1)
146 TimeControl.moves[side] = 1;
148 /* special case time per move specified */
151 ResponseTime = TimeControl.clock[side] - 100;
156 /* calculate avg time per move remaining */
157 if (TimeControl.clock[side] <= 0)
160 TCleft = (long)MINRESPONSETIME / MAXTCCOUNTX;
164 short rtf = in_opening_stage ? 8 : 2;
165 short tcq = in_opening_stage ? 2 : 4;
166 int moves = TimeControl.moves[side];
168 if(!xboard) /* no pre-add of increment in XBoard mode */
169 TimeControl.clock[side] += TCadd;
170 if(TCflag == 2 && TCadd == 0) /* sudden death */
171 moves = (moves < 30 ? 30 : moves);
172 ResponseTime = (TimeControl.clock[side])
174 TCleft = (long)ResponseTime / tcq;
175 ResponseTime += TCadd / 2;
178 if (TimeControl.moves[side] < 5)
180 TCcount = MAXTCCOUNTX - 6 - TimeControl.moves[side];
185 DetermineTCcount = false;
189 if (ResponseTime < MINRESPONSETIME)
191 ResponseTime = MINRESPONSETIME;
192 TCcount = MAXTCCOUNTX - 10;
197 DetermineTCcount = false;
200 if (!hard_time_limit && (ResponseTime < 2 * MINRESPONSETIME))
202 TCcount = MAXTCCOUNTX - 10;
207 DetermineTCcount = false;
213 ResponseTime = MaxResponseTime;
214 ElapsedTime(COMPUTE_AND_INIT_MODE);
217 if (DetermineTCcount)
222 = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
225 if (AllowedCounts <= 0)
226 TCcount = MAXTCCOUNTX;
227 else if (AllowedCounts > MAXTCCOUNTX)
230 TCcount = MAXTCCOUNTX - AllowedCounts;
234 TCcount = MAXTCCOUNTX;
238 if (ResponseTime < MINRESPONSETIME)
239 ResponseTime = MINRESPONSETIME;
247 if (TimeControl.moves[side] < 1)
248 TimeControl.moves[side] = 1;
250 /* special case time per move specified */
253 ResponseTime = TimeControl.clock[side] - 100;
258 /* calculate avg time per move remaining */
259 int moves = TimeControl.moves[side];
261 if(!xboard) /* no pre-add of increment in XBoard mode */
262 TimeControl.clock[side] += TCadd;
264 if(TCflag == 2 && TCadd == 0) /* sudden death */
265 moves = (moves < 30 ? 30 : moves);
266 ResponseTime = (TimeControl.clock[side])
268 TCleft = (int) ResponseTime / 3;
269 ResponseTime += TCadd / 2;
271 if (TimeControl.moves[side] < 5)
272 TCcount = MAXTCCOUNTX - 10;
275 if (ResponseTime < 101)
278 TCcount = MAXTCCOUNTX - 10;
280 else if (ResponseTime < 200)
282 TCcount = MAXTCCOUNTX - 10;
287 ResponseTime = MaxResponseTime;
289 ElapsedTime(COMPUTE_AND_INIT_MODE);
294 TCcount = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
297 if (TCcount > MAXTCCOUNTX)
300 TCcount = MAXTCCOUNTX - TCcount;
304 TCcount = MAXTCCOUNTX;
307 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);
308 assert(TCcount <= MAXTCCOUNTX);
314 CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
316 if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
319 else if (TCflag && (Sdepth > (MINDEPTH - 1)) && (TCcount < MAXTCCOUNTR))
321 if (killr0[1] != PrVar[1] /* || Killr0[2] != PrVar[2] */)
327 if ((TCcount < MAXTCCOUNTR)
328 && (abs(score - globalscore) / Sdepth) > ZDELTA)
335 if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
338 ElapsedTime(COMPUTE_MODE);
340 if (root->flags & exact)
342 /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
344 #if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
345 else if (!(Sdepth < MINDEPTH)
347 && ((4 * et) > (2*ResponseTime + ExtraTime)))
350 else if (!(Sdepth < MINDEPTH)
352 && ((int)(1.93913099l * (pow((double)et, 1.12446928l)))
353 > (ResponseTime + ExtraTime)))
358 dsp->ShowMessage("timeout");
363 * Determine the time that has passed since the search was started. If the
364 * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
365 * to true which will terminate the search.
366 * iop = COMPUTE_MODE calculate et, bump ETnodes
367 * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
371 ElapsedTime(ElapsedTime_mode iop)
374 #ifdef HAVE_GETTIMEOFDAY
380 #ifdef HAVE_GETTIMEOFDAY
381 gettimeofday(&tv, NULL);
382 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
384 et = ((current_time = time((long *) 0)) - time0) * 100;
387 #ifdef INTERRUPT_TEST
388 if (iop == INIT_INTERRUPT_MODE)
390 itime0 = current_time;
392 else if (iop == COMPUTE_INTERRUPT_MODE)
394 it = current_time - itime0;
399 #ifdef HAVE_GETTIMEOFDAY
400 et = current_time - time0;
402 ETnodes = NodeCnt + znodes;
406 #ifdef INTERRUPT_TEST
407 printf("elapsed time %ld not positive\n", et);
412 if (iop == COMPUTE_AND_INIT_MODE)
414 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
417 time0 = current_time;
420 #ifdef QUIETBACKGROUND
433 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
434 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
435 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
439 TimeControl.moves[black] = TimeControl.moves[white] = 0;
440 TimeControl.clock[black] = TimeControl.clock[white] = 0;
443 flag.onemove = (TCmoves == 1);
445 ElapsedTime(COMPUTE_AND_INIT_MODE);
449 RenewTimeControl(int side, int TCadd)
451 if (flag.gamein || TCadd)
455 else if (TimeControl.moves[side] == 0)
461 TCmoves = XCmoves[XCmore];
462 TCminutes = XCminutes[XCmore];
463 TCseconds = XCseconds[XCmore];