gyges_engine 1.1.0

A powerful Gygès engine.
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Main strutures, and concepts related to searching.
//! 

pub mod evaluation;

use core::f64;
use std::cmp::Ordering;
use std::sync::mpsc::Receiver;
use std::time::Instant;

use movegen::GenMoveCount;
use movegen::GenMoves;
use movegen::GenNone;
use movegen::GenResult;
use movegen::GenThreatCount;
use movegen::MoveGen;
use movegen::NoQuit;
use movegen::QuitOnThreat;
use rayon::prelude::*;

use gyges::board::*;
use gyges::moves::*;
use gyges::moves::move_list::*;
use gyges::tools::tt::*;
use gyges::core::*;

use crate::search::evaluation::*;
use crate::consts::*;
use crate::ugi;

// Constants for move ordering.
pub const LOSE_MOVE_SCORE: f64 = -1000000.0;
pub const TT_MOVE_SCORE: f64 = 1000000.0;

/// Structure that holds all needed information to perform a search, and conatains all of the main searching functions.
pub struct Searcher {
    pub current_ply: i8,

    pub completed_searchs: Vec<SearchData>,
    pub search_data: SearchData,
    pub search_stats: SearchStats,
    pub root_moves: RootMoveList,

    pub options: SearchOptions,
    pub stop_in: Receiver<bool>,
    pub stop: bool,

    pub mg: MoveGen

}

impl Searcher {
    /// Creates a new searcher.
    pub fn new(stop_in: Receiver<bool>, options: SearchOptions) -> Searcher {
        Searcher {
            current_ply: 0,
            
            completed_searchs: vec![],
            search_data: SearchData::new(1),
            search_stats: SearchStats::new(),
            root_moves: RootMoveList::new(),

            options, 
            stop_in,
            stop: false,

            mg: MoveGen::default()

        }

    }

    // Checks to see if the engine should stop the search.
    pub fn check_stop(&mut self) {
        // Check if the stop signal has been sent.
        if self.stop_in.try_recv().is_ok() {
            self.stop = true;

        }

        // Check if the max time has been reached.
        if let Some(maxtime) = self.options.maxtime {
            if self.search_stats.start_time.elapsed().as_secs_f64() >= maxtime {
                self.stop = true;

            }

        }

    }

    /// Updates the search data based on the collected data.
    pub fn update_search_data(&mut self) {
        // Stats
        self.search_stats.search_time = self.search_stats.start_time.elapsed().as_secs_f64();
        self.search_stats.nps = (self.search_stats.nodes as f64 / self.search_stats.search_time) as usize;
        
        // Results
        self.search_data.pv = get_pv(&mut self.options.board.clone());
        self.search_data.best_move = self.search_data.pv.get(0).unwrap_or(&RootMove::new_null()).clone();

        if self.search_data.best_move.score == f64::INFINITY {
            self.search_data.game_over = true;
            self.search_data.winner = 1;

        } else if self.search_data.best_move.score == f64::NEG_INFINITY {
            self.search_data.game_over = true;
            self.search_data.winner = 2;

        }

        // Prune the root moves list.
        self.root_moves.sort();
        self.root_moves.moves = self.root_moves.moves.iter().filter(|mv| mv.score != f64::NEG_INFINITY).cloned().collect();

    } 

    
    /// Iterative deepening search.
    pub fn iterative_deepening_search(&mut self) {
        // Setup search
        self.stop = false;

        let board = &mut self.options.board.clone();

        for mv in unsafe { self.mg.gen::<GenMoves, NoQuit>(board, Player::One).move_list.moves(board) } {
            if mv.is_win() {
                self.search_data.best_move = RootMove::new(mv, f64::INFINITY, 1, 0);
                self.completed_searchs.push(self.search_data.clone());

                let best_search_data = self.completed_searchs.last().unwrap().clone();
                ugi::info_output(best_search_data.clone(), self.search_stats.clone());
                ugi::best_move_output(best_search_data);
                return;

            }

        }

        self.root_moves = RootMoveList::new();
        self.setup_rootmoves(board);

        self.search_stats.start_time = Instant::now();

        // Start iterative deepening
        self.current_ply = 3;
        'iterative_deepening: while !self.search_data.game_over {
            self.search_data = SearchData::new(self.current_ply);

            let (mut alpha, mut beta) = if self.completed_searchs.len() > 0 {
                let prev_score = self.completed_searchs.last().unwrap().clone().best_move.score;
                (prev_score - 1000.0, prev_score + 1000.0)

            } else {
                (f64::NEG_INFINITY, f64::INFINITY)

            };

            'aspiration_windows: loop {
                let score = self.search(board, alpha, beta, Player::One, self.current_ply);
                
                if self.stop || score == f64::INFINITY || score == f64::NEG_INFINITY {
                    break 'aspiration_windows;
        
                }

                if score <= alpha {
                    alpha -= 1000.0;

                } else if score >= beta {
                    beta += 1000.0;

                } else {
                    break 'aspiration_windows;

                }

            }
                
            if self.stop {
                break 'iterative_deepening;
    
            }   

            self.update_search_data();
            ugi::info_output(self.search_data.clone(), self.search_stats.clone());
            
            self.completed_searchs.push(self.search_data.clone());
           
            self.current_ply += 2;

            if let Some(maxply) = self.options.maxply {
                if self.current_ply >= maxply {
                    break 'iterative_deepening;
    
                }
    
            }

        }

        let best_search_data = self.completed_searchs.last().unwrap().clone();
        ugi::info_output(best_search_data.clone(), self.search_stats.clone());
        ugi::best_move_output(best_search_data);

    }

    /// Main search function.
    fn search(&mut self, board: &mut BoardState, mut alpha: f64, mut beta: f64, player: Player, ply: i8) -> f64 {
        let is_root = ply == self.search_data.ply;
        let is_leaf = ply == 0;
        let board_hash = board.hash();

        // Check if the search should stop.
        if self.stop {
            return 0.0;
    
        } else if self.search_stats.nodes % 1000 == 0 {
            self.check_stop();

        }

        // Generate the Raw move list for this node.
        let data: GenResult = unsafe { self.mg.gen::<GenMoves, QuitOnThreat>(board, player) };
        let (has_threat, mut move_list) = (data.threat, data.move_list);
    
        // If there is the threat for the current player return INF, because that move would eventualy be picked as best.
        if has_threat {
            return f64::INFINITY;
            
        }

        self.search_stats.nodes += 1;

        // Base case, if the node is a leaf node, return the evaluation.
        if is_leaf {
            let eval = get_evalulation(board, &mut self.mg) * player.eval_multiplier();
            return eval;

        }

        // Handle Transposition Table
        let mut tt_move: Option<Move> = None;
        let (valid, entry) = unsafe { tt().probe(board_hash) };
        if valid && entry.depth >= ply {
            tt_move = Some(entry.bestmove);

            match entry.bound {
                NodeBound::ExactValue => {
                    return entry.score

                },
                NodeBound::LowerBound => {
                    alpha = entry.score

                },
                NodeBound::UpperBound => {
                    beta = entry.score
                
                }

            }

            if alpha >= beta {
                return entry.score;

            }

        }

        // Use previous ply search to order the moves, otherwise generate and order them.
        let current_player_moves: Vec<Move> = if is_root {
            self.root_moves.clone().into()
            
        } else {
            let moves = move_list.moves(board);
            self.order_moves(moves, board, player, tt_move)

        };

        // If there are no valid moves, return negative infinity.
        if current_player_moves.len() == 0 {
            return f64::NEG_INFINITY;

        }
        
        // Loop through valid moves and search them.
        let mut best_move = Move::new_null();
        let mut best_score: f64 = f64::NEG_INFINITY;
        for (i, mv) in current_player_moves.iter().enumerate() {
            let mut new_board = board.make_move(mv);

            // Principal Variation Search
            let score: f64 = if i < 5 {
                -self.search(&mut new_board, -beta, -alpha, player.other(), ply - 1) // Full search

            } else {
                let mut score = -self.search(&mut new_board, -alpha - 1.0, -alpha, player.other(), ply - 1); // Null window search
                if score > alpha && score < beta { 
                    score = -self.search(&mut new_board, -beta, -alpha, player.other(), ply - 1);

                }

                score

            };

            // Update the score of the rootnode.
            if is_root {
                self.root_moves.update_move(*mv, score, self.current_ply);

            }

            if score > best_score {
                best_score = score;
                best_move = *mv;

            }
            if best_score > alpha {
                alpha = best_score;

            }

            if alpha >= beta {
                break;

            }

        }

        if !self.stop {
            let node_bound: NodeBound = if best_score >= beta {
                NodeBound::LowerBound
    
            } else if best_score <= alpha  {
                NodeBound::UpperBound
    
            } else {
                NodeBound::ExactValue
    
            };
            
            let new_entry = Entry::new(board_hash, best_score, ply, best_move, node_bound);
            unsafe { tt().insert(new_entry) };

        }
        
        best_score

    }

    /// Orders a list of moves.
    pub fn order_moves(&mut self, moves: Vec<Move>, board: &mut BoardState, player: Player, tt_move: Option<Move>) -> Vec<Move> {
        // For every move calculate a value to sort it by.
        let mut moves_to_sort: Vec<(Move, f64)> = moves.into_par_iter().filter_map(|mv| {
            let mut sort_val: f64 = 0.0;
            let mut new_board = board.make_move(&mv);

            THREAD_LOCAL_MOVEGEN.with(|movegen| {
                let mut movegen = movegen.borrow_mut();

                let data = unsafe { movegen.gen::<GenMoveCount, QuitOnThreat>(&mut new_board, player.other()) };
                if data.threat {
                    return None;

                }
                let opp_movecount = data.move_count;

                // If the move is the TT sort it first.
                if tt_move.is_some() && mv == tt_move.unwrap() {
                    return Some((mv, TT_MOVE_SCORE));

                }

                // If the move has a threat then increase the sort value.
                let data = unsafe { movegen.gen::<GenNone, QuitOnThreat>(&mut new_board, player) };
                if data.threat {
                    sort_val += 1000.0;

                }
                
                sort_val -= opp_movecount as f64;

                Some((mv, sort_val))

            })

        }).collect();

        // Sort the moves
        moves_to_sort.sort_by(|a, b| {
            if a.1 > b.1 {
                Ordering::Less

            } else if a.1 < b.1 {
                Ordering::Greater

            } else {
                Ordering::Equal

            }

        });
        
        return moves_to_sort.into_iter().map(|(mv, _)| mv).collect();

    }

    /// Setups up the RootMoveList from a [BoardState].
    /// 
    /// Generates all moves, sorts them, and calculates the number of threats that they each have.
    /// 
    pub fn setup_rootmoves(&mut self, board: &mut BoardState) {
        let moves = unsafe { self.mg.gen::<GenMoves, NoQuit>(board, Player::One).move_list.moves(board) };
        let ordered: Vec<Move> = self.order_moves(moves, board, Player::One, None);
        
        let root_moves: Vec<RootMove> = ordered.iter().map( |mv| {
            let mut new_board = board.make_move(mv);
            let threats: usize = unsafe { self.mg.gen::<GenThreatCount, NoQuit>(&mut new_board, Player::One).threat_count };

            RootMove::new(*mv, 0.0, 0, threats)

        }).collect();

        let mut rootmove_list = RootMoveList::new();
        rootmove_list.moves = root_moves;

        self.root_moves = rootmove_list;

    }

}


/// Gets the principle variation from the transposition table.
pub fn get_pv(board: &mut BoardState) -> Vec<RootMove> {
    let mut pv: Vec<RootMove> = vec![];

    let mut current_board = board.clone();
    loop {
        let (valid, entry) = unsafe { tt().probe(current_board.hash()) };
        if valid {
            let current_move = entry.bestmove;
            current_board = current_board.make_move(&current_move);

            pv.push(RootMove::new(current_move, entry.score, 0, 0));

        } else {
            return pv;

        }
        
    }

}


/// Structure that holds all of the results from a specific search ply.
#[derive(Debug, Clone)]
pub struct SearchData {
    pub best_move: RootMove,
    pub pv: Vec<RootMove>,
    pub ply: i8,

    pub game_over: bool,
    pub winner: usize,

}

impl SearchData {
    pub fn new(ply: i8) -> SearchData {
        SearchData {
            best_move: RootMove::new_null(),
            pv: vec![],
            ply,

            game_over: false,
            winner: 0,

        }

    }

}

/// Structure that holds all statistics about a search.
#[derive(Debug, Clone)]
pub struct SearchStats {
    pub nodes: usize,
    pub nps: usize,

    pub start_time: Instant,
    pub search_time: f64

}

impl SearchStats {
    pub fn new() -> SearchStats {
        SearchStats {
            nodes: 0,
            nps: 0,

            start_time: Instant::now(),
            search_time: 0.0

        }

    }

}


/// Holds all of the settings for a spsific search.
#[derive(Clone)]
pub struct SearchOptions {
    pub board: BoardState,
    pub maxply: Option<i8>,
    pub maxtime: Option<f64>,

}

impl SearchOptions {
    pub fn new() -> SearchOptions {
        SearchOptions {
            board: BoardState::from(STARTING_BOARD),   
            maxply: Option::None,
            maxtime: Option::None,

        }

    }

}

impl Default for SearchOptions {
    fn default() -> Self {
        Self::new()

    }

}