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 * ----------------------------------------------------------------------
39 * In a networked enviroment gnushogi might be compiled on different hosts
40 * with different random number generators; that is not acceptable if they
41 * are going to share the same transposition table.
44 static unsigned long next
= 1;
51 return ((unsigned int) (next
>> 16) & 0xFFFF);
57 gsrand(unsigned int seed
)
67 /* adjust number of moves remaining in gamein games */
74 /* Don't do anything until you have enough numbers. */
75 if (GameCnt
< (MINGAMEIN
* 2))
78 /* Calculate average time in sec for last MINGAMEIN moves. */
79 for (i
= 0; i
< MINGAMEIN
; i
++)
81 tcompsum
+= timecomp
[i
];
85 topsum
/= (100 * MINGAMEIN
);
86 tcompsum
/= (100 * MINGAMEIN
);
88 /* If I have less time than opponent add another move. */
89 me
= TimeControl
.clock
[computer
] / 100;
90 him
= TimeControl
.clock
[opponent
] / 100;
95 if (((him
- me
) > 60) || ((me
< him
) && (me
< 120)))
98 /* If I am losing more time with each move add another. */
99 /* If (!((me - him) > 60) && tcompsum > topsum) increment++; */
101 if (tcompsum
> topsum
)
105 else if ((TimeControl
.moves
[computer
] < MINMOVES
) && !increment
)
107 /* ... but don't let moves go below MINMOVES. */
110 else if ((me
> him
) && (tcompsum
< topsum
))
112 /* If I am doing really well use more time per move. */
116 /* If not fischer clock be careful about time. */
117 /* CHECKME: what's a fischer clock? */
119 if ((TCadd
== 0) && (increment
> 0))
122 if ((me
== 0) && (increment
> 0))
125 TimeControl
.moves
[computer
] += increment
;
131 * Set ResponseTime, TCcount, and TCleft.
134 void SetResponseTime(short side
)
136 bool DetermineTCcount
= true;
142 if (TimeControl
.moves
[side
] < 1)
143 TimeControl
.moves
[side
] = 1;
145 /* special case time per move specified */
148 ResponseTime
= TimeControl
.clock
[side
] - 100;
153 /* calculate avg time per move remaining */
154 if (TimeControl
.clock
[side
] <= 0)
157 TCleft
= (long)MINRESPONSETIME
/ MAXTCCOUNTX
;
161 short rtf
= in_opening_stage
? 8 : 2;
162 short tcq
= in_opening_stage
? 2 : 4;
164 TimeControl
.clock
[side
] += TCadd
;
165 ResponseTime
= (TimeControl
.clock
[side
])
166 / (((TimeControl
.moves
[side
]) * rtf
) + 1);
167 TCleft
= (long)ResponseTime
/ tcq
;
168 ResponseTime
+= TCadd
/ 2;
171 if (TimeControl
.moves
[side
] < 5)
173 TCcount
= MAXTCCOUNTX
- 6 - TimeControl
.moves
[side
];
178 DetermineTCcount
= false;
182 if (ResponseTime
< MINRESPONSETIME
)
184 ResponseTime
= MINRESPONSETIME
;
185 TCcount
= MAXTCCOUNTX
- 10;
190 DetermineTCcount
= false;
193 if (!hard_time_limit
&& (ResponseTime
< 2 * MINRESPONSETIME
))
195 TCcount
= MAXTCCOUNTX
- 10;
200 DetermineTCcount
= false;
206 ResponseTime
= MaxResponseTime
;
207 ElapsedTime(COMPUTE_AND_INIT_MODE
);
210 if (DetermineTCcount
)
215 = ((int)((TimeControl
.clock
[side
] - ResponseTime
)) / 2)
218 if (AllowedCounts
<= 0)
219 TCcount
= MAXTCCOUNTX
;
220 else if (AllowedCounts
> MAXTCCOUNTX
)
223 TCcount
= MAXTCCOUNTX
- AllowedCounts
;
227 TCcount
= MAXTCCOUNTX
;
231 if (ResponseTime
< MINRESPONSETIME
)
232 ResponseTime
= MINRESPONSETIME
;
234 assert(TCcount
<= MAXTCCOUNTX
);
240 CheckForTimeout(int score
, int globalscore
, int Jscore
, int zwndw
)
242 if (flag
.musttimeout
|| (Sdepth
>= MaxSearchDepth
))
245 else if (TCflag
&& (Sdepth
> (MINDEPTH
- 1)) && (TCcount
< MAXTCCOUNTR
))
247 if (killr0
[1] != PrVar
[1] /* || Killr0[2] != PrVar[2] */)
253 if ((TCcount
< MAXTCCOUNTR
)
254 && (abs(score
- globalscore
) / Sdepth
) > ZDELTA
)
261 if ((score
> (Jscore
- zwndw
)) && (score
> (Tree
[1].score
+ 250)))
264 ElapsedTime(COMPUTE_MODE
);
266 if (root
->flags
& exact
)
268 /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
270 #if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
271 else if (!(Sdepth
< MINDEPTH
)
273 && ((4 * et
) > (2*ResponseTime
+ ExtraTime
)))
276 else if (!(Sdepth
< MINDEPTH
)
278 && ((int)(1.93913099l * (pow((double)et
, 1.12446928l)))
279 > (ResponseTime
+ ExtraTime
)))
284 dsp
->ShowMessage("timeout");
289 * Determine the time that has passed since the search was started. If the
290 * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
291 * to true which will terminate the search.
292 * iop = COMPUTE_MODE calculate et, bump ETnodes
293 * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
297 ElapsedTime(ElapsedTime_mode iop
)
300 #ifdef HAVE_GETTIMEOFDAY
306 #ifdef HAVE_GETTIMEOFDAY
307 gettimeofday(&tv
, NULL
);
308 current_time
= tv
.tv_sec
*100 + (tv
.tv_usec
/10000);
310 et
= ((current_time
= time((long *) 0)) - time0
) * 100;
313 #ifdef INTERRUPT_TEST
314 if (iop
== INIT_INTERRUPT_MODE
)
316 itime0
= current_time
;
318 else if (iop
== COMPUTE_INTERRUPT_MODE
)
320 it
= current_time
- itime0
;
325 #ifdef HAVE_GETTIMEOFDAY
326 et
= current_time
- time0
;
328 ETnodes
= NodeCnt
+ znodes
;
332 #ifdef INTERRUPT_TEST
333 printf("elapsed time %ld not positive\n", et
);
338 if (iop
== COMPUTE_AND_INIT_MODE
)
340 if ((et
> (ResponseTime
+ ExtraTime
)) && (Sdepth
> MINDEPTH
))
343 time0
= current_time
;
346 #ifdef QUIETBACKGROUND
359 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = TCmoves
;
360 TimeControl
.clock
[black
] += 6000L * TCminutes
+ TCseconds
* 100;
361 TimeControl
.clock
[white
] += 6000L * TCminutes
+ TCseconds
* 100;
365 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = 0;
366 TimeControl
.clock
[black
] = TimeControl
.clock
[white
] = 0;
369 flag
.onemove
= (TCmoves
== 1);
371 ElapsedTime(COMPUTE_AND_INIT_MODE
);