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;
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;
175 if (TimeControl.moves[side] < 5)
177 TCcount = MAXTCCOUNTX - 10;
182 DetermineTCcount = false;
186 if (ResponseTime < MINRESPONSETIME)
188 ResponseTime = MINRESPONSETIME;
189 TCcount = MAXTCCOUNTX - 10;
194 DetermineTCcount = false;
197 if (!hard_time_limit && (ResponseTime < 2 * MINRESPONSETIME))
199 TCcount = MAXTCCOUNTX - 10;
204 DetermineTCcount = false;
210 ResponseTime = MaxResponseTime;
211 ElapsedTime(COMPUTE_AND_INIT_MODE);
214 if (DetermineTCcount)
219 = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
222 if (AllowedCounts <= 0)
223 TCcount = MAXTCCOUNTX;
224 else if (AllowedCounts > MAXTCCOUNTX)
227 TCcount = MAXTCCOUNTX - AllowedCounts;
231 TCcount = MAXTCCOUNTX;
235 if (ResponseTime < MINRESPONSETIME)
236 ResponseTime = MINRESPONSETIME;
244 if (TimeControl.moves[side] < 1)
245 TimeControl.moves[side] = 1;
247 /* special case time per move specified */
250 ResponseTime = TimeControl.clock[side] - 100;
255 /* calculate avg time per move remaining */
257 if(!xboard) /* no pre-add of increment in XBoard mode */
258 TimeControl.clock[side] += TCadd;
260 ResponseTime = (TimeControl.clock[side])
261 / (((TimeControl.moves[side]) * 2) + 1);
262 TCleft = (int) ResponseTime / 3;
263 ResponseTime += TCadd / 2;
265 if (TimeControl.moves[side] < 5)
266 TCcount = MAXTCCOUNTX - 10;
269 if (ResponseTime < 101)
272 TCcount = MAXTCCOUNTX - 10;
274 else if (ResponseTime < 200)
276 TCcount = MAXTCCOUNTX - 10;
281 ResponseTime = MaxResponseTime;
283 ElapsedTime(COMPUTE_AND_INIT_MODE);
288 TCcount = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
291 if (TCcount > MAXTCCOUNTX)
294 TCcount = MAXTCCOUNTX - TCcount;
298 TCcount = MAXTCCOUNTX;
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);
308 CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
310 if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
313 else if (TCflag && (Sdepth > (MINDEPTH - 1)) && (TCcount < MAXTCCOUNTR))
315 if (killr0[1] != PrVar[1] /* || Killr0[2] != PrVar[2] */)
321 if ((TCcount < MAXTCCOUNTR)
322 && (abs(score - globalscore) / Sdepth) > ZDELTA)
329 if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
332 ElapsedTime(COMPUTE_MODE);
334 if (root->flags & exact)
336 /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
338 #if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
339 else if (!(Sdepth < MINDEPTH)
341 && ((4 * et) > (2*ResponseTime + ExtraTime)))
344 else if (!(Sdepth < MINDEPTH)
346 && ((int)(1.93913099l * (pow((double)et, 1.12446928l)))
347 > (ResponseTime + ExtraTime)))
352 dsp->ShowMessage("timeout");
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,
365 ElapsedTime(ElapsedTime_mode iop)
368 #ifdef HAVE_GETTIMEOFDAY
374 #ifdef HAVE_GETTIMEOFDAY
375 gettimeofday(&tv, NULL);
376 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
378 et = ((current_time = time((long *) 0)) - time0) * 100;
381 #ifdef INTERRUPT_TEST
382 if (iop == INIT_INTERRUPT_MODE)
384 itime0 = current_time;
386 else if (iop == COMPUTE_INTERRUPT_MODE)
388 it = current_time - itime0;
393 #ifdef HAVE_GETTIMEOFDAY
394 et = current_time - time0;
396 ETnodes = NodeCnt + znodes;
400 #ifdef INTERRUPT_TEST
401 printf("elapsed time %ld not positive\n", et);
406 if (iop == COMPUTE_AND_INIT_MODE)
408 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
411 time0 = current_time;
414 #ifdef QUIETBACKGROUND
427 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
428 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
429 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
433 TimeControl.moves[black] = TimeControl.moves[white] = 0;
434 TimeControl.clock[black] = TimeControl.clock[white] = 0;
437 flag.onemove = (TCmoves == 1);
439 ElapsedTime(COMPUTE_AND_INIT_MODE);
443 RenewTimeControl(int side, int TCadd)
445 if (flag.gamein || TCadd)
449 else if (TimeControl.moves[side] == 0)
455 TCmoves = XCmoves[XCmore];
456 TCminutes = XCminutes[XCmore];
457 TCseconds = XCseconds[XCmore];