File: tcontrl.c

package info (click to toggle)
gnushogi 1.5~git20140725-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,900 kB
  • sloc: ansic: 12,731; sh: 4,104; makefile: 128
file content (372 lines) | stat: -rw-r--r-- 9,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * FILE: tcontrl.c
 *
 * ----------------------------------------------------------------------
 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
 *
 * GNU SHOGI is based on GNU CHESS
 *
 * Copyright (c) 1988, 1989, 1990 John Stanback
 * Copyright (c) 1992 Free Software Foundation
 *
 * This file is part of GNU SHOGI.
 *
 * GNU Shogi is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNU Shogi; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------
 *
 */


#include "gnushogi.h"
#include <math.h>


/*
 * In a networked enviroment gnushogi might be compiled on different hosts
 * with different random number generators; that is not acceptable if they
 * are going to share the same transposition table.
 */

static unsigned long next = 1;

unsigned int
urand(void)
{
    next *= 1103515245;
    next += 12345;
    return ((unsigned int) (next >> 16) & 0xFFFF);
}



void
gsrand(unsigned int seed)
{
    next = seed;
}



void
TimeCalc()
{
    /* adjust number of moves remaining in gamein games */
    int increment = 0;
    int topsum = 0;
    int tcompsum = 0;
    int me, him;
    int i;

    /* Don't do anything until you have enough numbers. */
    if (GameCnt < (MINGAMEIN * 2))
        return;

    /* Calculate average time in sec for last MINGAMEIN moves. */
    for (i = 0; i < MINGAMEIN; i++)
    {
        tcompsum += timecomp[i];
        topsum += timeopp[i];
    }

    topsum   /= (100 * MINGAMEIN);
    tcompsum /= (100 * MINGAMEIN);

    /* If I have less time than opponent add another move. */
    me  = TimeControl.clock[computer] / 100;
    him = TimeControl.clock[opponent] / 100;

    if (me < him)
        increment += 2;

    if (((him - me) > 60) || ((me < him) && (me < 120)))
        increment++;

    /* If I am losing more time with each move add another. */
    /* If (!((me - him) > 60) && tcompsum > topsum) increment++; */

    if (tcompsum > topsum)
    {
        increment += 2;
    }
    else if ((TimeControl.moves[computer] < MINMOVES) && !increment)
    {
        /* ... but don't let moves go below MINMOVES. */
        increment++;
    }
    else if ((me > him) && (tcompsum < topsum))
    {
        /* If I am doing really well use more time per move. */
        increment = -1;
    }

    /* If not fischer clock be careful about time. */
    /* CHECKME: what's a fischer clock? */

    if ((TCadd == 0) && (increment > 0))
        increment += 2;

    if ((me == 0) && (increment > 0))
        increment += 2;

    TimeControl.moves[computer] += increment;
}



/*
 * Set ResponseTime, TCcount, and TCleft.
 */

void SetResponseTime(short side)
{
    int DetermineTCcount = true;

    if (TCflag)
    {
        TCcount = 0;

        if (TimeControl.moves[side] < 1)
            TimeControl.moves[side] = 1;

        /* special case time per move specified */
        if (flag.onemove)
        {
            ResponseTime = TimeControl.clock[side] - 100;
            TCleft = 0;
        }
        else
        {
            /* calculate avg time per move remaining */
            if (TimeControl.clock[side] <= 0)
            {
                ResponseTime = 0;
                TCleft = (long)MINRESPONSETIME / MAXTCCOUNTX;
            }
            else
            {
                short rtf = in_opening_stage ? 8 : 2;
                short tcq = in_opening_stage ? 2 : 4;

                TimeControl.clock[side] += TCadd;
                ResponseTime = (TimeControl.clock[side])
                    / (((TimeControl.moves[side]) * rtf) + 1);
                TCleft = (long)ResponseTime / tcq;
                ResponseTime += TCadd / 2;
            }

            if (TimeControl.moves[side] < 5)
            {
                TCcount = MAXTCCOUNTX - 10;

                if (TCcount < 0)
                    TCcount = 0;

                DetermineTCcount = false;
            }
        }

        if (ResponseTime < MINRESPONSETIME)
        {
            ResponseTime = MINRESPONSETIME;
            TCcount = MAXTCCOUNTX - 10;

            if (TCcount < 0)
                TCcount = 0;

            DetermineTCcount = false;
        }

        if (!hard_time_limit && (ResponseTime < 2 * MINRESPONSETIME))
        {
            TCcount = MAXTCCOUNTX - 10;

            if (TCcount < 0)
                TCcount = 0;

            DetermineTCcount = false;
        }
    }
    else
    {
        TCleft = 0;
        ResponseTime = MaxResponseTime;
        ElapsedTime(COMPUTE_AND_INIT_MODE);
    }

    if (DetermineTCcount)
    {
        if (TCleft )
        {
            int AllowedCounts
                = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
                / TCleft;

            if (AllowedCounts <= 0)
                TCcount = MAXTCCOUNTX;
            else if (AllowedCounts > MAXTCCOUNTX)
                TCcount = 0;
            else
                TCcount = MAXTCCOUNTX - AllowedCounts;
        }
        else
        {
            TCcount = MAXTCCOUNTX;
        }
    }

    if (ResponseTime < MINRESPONSETIME)
        ResponseTime = MINRESPONSETIME;

    assert(TCcount <= MAXTCCOUNTX);
}



void
CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
{
    if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
        flag.timeout = true;

    else if (TCflag && (Sdepth > (MINDEPTH - 1)) && (TCcount < MAXTCCOUNTR))
    {
        if (killr0[1] != PrVar[1] /* || Killr0[2] != PrVar[2] */)
        {
            TCcount++;
            ExtraTime += TCleft;
        }

        if ((TCcount < MAXTCCOUNTR)
            && (abs(score - globalscore) / Sdepth) > ZDELTA)
        {
            TCcount++;
            ExtraTime += TCleft;
        }
    }

    if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
        ExtraTime = 0;

    ElapsedTime(COMPUTE_MODE);

    if (root->flags & exact)
        flag.timeout = true;
    /*else if (Tree[1].score < -SCORE_LIMIT) flag.timeout = true;
     */
#if defined OLDTIME || !defined HAVE_GETTIMEOFDAY
    else if (!(Sdepth < MINDEPTH)
             && TCflag
             && ((4 * et) > (2*ResponseTime + ExtraTime)))
        flag.timeout = true;
#else
    else if (!(Sdepth < MINDEPTH)
             && TCflag
             && ((int)(1.93913099l * (pow((double)et, 1.12446928l)))
                 > (ResponseTime + ExtraTime)))
        flag.timeout = true;
#endif

    if (flag.timeout)
        dsp->ShowMessage("timeout");
}


/*
 * Determine the time that has passed since the search was started. If the
 * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
 * to true which will terminate the search.
 * iop = COMPUTE_MODE calculate et, bump ETnodes
 * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
 *     set reference time
 */
void
ElapsedTime(ElapsedTime_mode iop)
{
    long current_time;
#ifdef HAVE_GETTIMEOFDAY
    struct timeval tv;
#endif

    dsp->PollForInput();

#ifdef HAVE_GETTIMEOFDAY
    gettimeofday(&tv, NULL);
    current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
#else
    et = ((current_time = time((long *) 0)) - time0) * 100;
#endif

#ifdef INTERRUPT_TEST
    if (iop == INIT_INTERRUPT_MODE)
    {
        itime0 = current_time;
    }
    else if (iop == COMPUTE_INTERRUPT_MODE)
    {
        it = current_time - itime0;
    }
    else
#endif
    {
#ifdef HAVE_GETTIMEOFDAY
        et = current_time - time0;
#endif
        ETnodes = NodeCnt + znodes;

        if (et < 0)
        {
#ifdef INTERRUPT_TEST
            printf("elapsed time %ld not positive\n", et);
#endif
            et = 0;
        }

        if (iop == COMPUTE_AND_INIT_MODE)
        {
            if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
                flag.timeout = true;

            time0 = current_time;
        }

#ifdef QUIETBACKGROUND
        if (!background)
#endif
            dsp->UpdateClocks();
    }
}


void
SetTimeControl(void)
{
    if (TCflag)
    {
        TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
        TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
        TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
    }
    else
    {
        TimeControl.moves[black] = TimeControl.moves[white] = 0;
        TimeControl.clock[black] = TimeControl.clock[white] = 0;
    }

    flag.onemove = (TCmoves == 1);
    et = 0;
    ElapsedTime(COMPUTE_AND_INIT_MODE);
}