chess-vector-engine 0.5.1

Open source chess engine with hybrid vector-based position analysis, advanced tactical search, and NNUE neural network evaluation
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Strategic Motif Recognition System
//!
//! This module implements a curated database of strategic chess patterns that provide
//! master-level positional understanding. Instead of storing millions of positions,
//! we focus on ~10K strategic motifs that capture the essence of positional play.

use chess::{Board, Piece, Square};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A strategic chess motif representing a meaningful positional pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrategicMotif {
    /// Unique identifier for this motif
    pub id: u64,
    /// Pattern hash for fast matching
    pub pattern_hash: u64,
    /// Type of strategic pattern
    pub motif_type: MotifType,
    /// Strategic evaluation adjustment (-2.0 to +2.0)
    pub evaluation: f32,
    /// When this pattern is most relevant
    pub context: StrategicContext,
    /// Confidence in this pattern (0.0 to 1.0)
    pub confidence: f32,
    /// Source game references where this pattern was successful
    pub master_games: Vec<GameReference>,
    /// Human-readable description
    pub description: String,
}

/// Types of strategic motifs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MotifType {
    /// Pawn structure patterns
    PawnStructure(PawnPattern),
    /// Piece coordination and placement
    PieceCoordination(CoordinationPattern),
    /// King safety configurations
    KingSafety(SafetyPattern),
    /// Initiative and tempo patterns
    Initiative(InitiativePattern),
    /// Endgame-specific patterns
    Endgame(EndgamePattern),
    /// Opening-specific strategic ideas
    Opening(OpeningPattern),
}

/// Pawn structure patterns that affect strategic evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PawnPattern {
    /// Isolated pawn weaknesses
    IsolatedPawn {
        file_index: u8,
        is_white: bool,
        weakness_value: f32,
    },
    /// Doubled pawn formations
    DoubledPawns {
        file_index: u8,
        is_white: bool,
        count: u8,
    },
    /// Passed pawn advantages
    PassedPawn {
        square_index: u8,
        is_white: bool,
        advancement: f32,
    },
    /// Pawn chains and support structures
    PawnChain {
        base_square_index: u8,
        length: u8,
        is_white: bool,
    },
    /// Hanging pawns (abreast, unsupported)
    HangingPawns {
        file1_index: u8,
        file2_index: u8,
        is_white: bool,
    },
    /// Backward pawn weaknesses
    BackwardPawn { square_index: u8, is_white: bool },
    /// Pawn majority patterns
    PawnMajority {
        kingside: bool,
        is_white: bool,
        advantage: f32,
    },
}

/// Piece coordination patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CoordinationPattern {
    /// Knight outpost on strong squares
    KnightOutpost {
        square_index: u8,
        is_white: bool,
        strength: f32,
    },
    /// Bishop pair advantages in open positions
    BishopPair { is_white: bool, open_diagonals: u8 },
    /// Rook lift patterns for attack
    RookLift {
        from_rank_index: u8,
        to_rank_index: u8,
        is_white: bool,
    },
    /// Queen and knight coordination
    QueenKnightAttack {
        target_area: KingArea,
        is_white: bool,
    },
    /// Piece sacrifices for positional advantage
    PositionalSacrifice { piece_type: u8, compensation: f32 }, // 0=Pawn, 1=Knight, 2=Bishop, 3=Rook, 4=Queen, 5=King
    /// Piece activity vs opponent passivity
    ActivityAdvantage {
        is_white: bool,
        activity_differential: f32,
    },
}

/// King safety patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SafetyPattern {
    /// Castling structure integrity
    CastlingStructure {
        side: CastlingSide,
        is_white: bool,
        safety_value: f32,
    },
    /// Pawn shield configurations
    PawnShield {
        king_square_index: u8,
        shield_pattern: u8,
    },
    /// King exposure levels
    KingExposure {
        square_index: u8,
        is_white: bool,
        danger_level: f32,
    },
    /// Opposite-side castling attack patterns
    OppositeCastling {
        attacker_is_white: bool,
        attack_potential: f32,
    },
}

/// Initiative and tempo patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InitiativePattern {
    /// Space advantage in center or wings
    SpaceAdvantage {
        area: BoardArea,
        is_white: bool,
        space_value: f32,
    },
    /// Tempo advantages in development
    DevelopmentLead { is_white: bool, tempo_count: u8 },
    /// Pressure point creation
    PressurePoints {
        square_indices: Vec<u8>,
        is_white: bool,
    },
    /// Strategic pawn breaks
    PawnBreak {
        break_square_index: u8,
        is_white: bool,
        timing: f32,
    },
}

/// Endgame-specific patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EndgamePattern {
    /// Good vs bad bishop evaluations
    BishopEndgame { is_white: bool, bishop_quality: f32 },
    /// Rook endgame principles
    RookEndgame {
        pattern: RookPattern,
        advantage: f32,
    },
    /// King activity in endgames
    KingActivity {
        square_index: u8,
        is_white: bool,
        activity: f32,
    },
    /// Pawn endgame races
    PawnRace { is_white: bool, race_advantage: f32 },
}

/// Opening-specific strategic patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OpeningPattern {
    /// Central control strategies
    CentralControl {
        square_indices: Vec<u8>,
        is_white: bool,
        control_value: f32,
    },
    /// Development principles
    DevelopmentPrinciple {
        piece_type: u8,
        target_square_index: u8,
        value: f32,
    },
    /// Opening pawn breaks
    OpeningBreak {
        break_move: String,
        is_white: bool,
        strategic_value: f32,
    },
}

/// Context for when a motif is most relevant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrategicContext {
    /// Game phase where this motif matters most
    pub game_phase: GamePhase,
    /// Material balance context
    pub material_context: MaterialContext,
    /// Minimum ply for relevance
    pub min_ply: u16,
    /// Maximum ply for relevance (None = always relevant)
    pub max_ply: Option<u16>,
}

/// Game phases for contextual relevance
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum GamePhase {
    Opening,
    Middlegame,
    Endgame,
    Any,
}

/// Material context for pattern relevance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MaterialContext {
    /// Equal material
    Equal,
    /// Material advantage for white (true) or black (false)
    Advantage(bool),
    /// Material disadvantage requires compensation for white (true) or black (false)
    Compensation(bool),
    /// Any material situation
    Any,
}

/// Supporting types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CastlingSide {
    Kingside,
    Queenside,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BoardArea {
    Center,
    Kingside,
    Queenside,
    Rank(u8), // rank index 0-7
    File(u8), // file index 0-7
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum KingArea {
    Kingside,
    Queenside,
    Center,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RookPattern {
    ActiveRook,
    PassiveRook,
    RookBehindPasser,
    SeventhRank,
}

/// Reference to a master game where this motif appeared
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameReference {
    /// Game identifier (e.g., Lichess game ID)
    pub game_id: String,
    /// Player names
    pub white: String,
    pub black: String,
    /// Game result
    pub result: String,
    /// Ply where this motif was relevant
    pub ply: u16,
    /// Rating of players (for confidence weighting)
    pub rating: Option<u16>,
}

/// Match result when finding motifs in a position
#[derive(Debug, Clone)]
pub struct MotifMatch {
    /// The matched motif
    pub motif: StrategicMotif,
    /// Relevance score (0.0 to 1.0)
    pub relevance: f32,
    /// Specific squares involved in this match
    pub matching_squares: Vec<Square>,
}

/// Fast strategic database for real-time pattern matching
pub struct StrategicDatabase {
    /// All strategic motifs indexed by pattern hash
    motifs: HashMap<u64, StrategicMotif>,
    /// Pattern matchers for different motif types
    pawn_matcher: PawnPatternMatcher,
    piece_matcher: PiecePatternMatcher,
    king_matcher: KingPatternMatcher,
    /// Cache for recent evaluations
    evaluation_cache: lru::LruCache<u64, f32>,
    /// Statistics
    total_motifs: usize,
}

impl StrategicDatabase {
    /// Create new strategic database
    pub fn new() -> Self {
        Self {
            motifs: HashMap::new(),
            pawn_matcher: PawnPatternMatcher::new(),
            piece_matcher: PiecePatternMatcher::new(),
            king_matcher: KingPatternMatcher::new(),
            evaluation_cache: lru::LruCache::new(std::num::NonZeroUsize::new(10000).unwrap()),
            total_motifs: 0,
        }
    }

    /// Load strategic database from binary file (ultra-fast)
    pub fn load_from_binary<P: AsRef<std::path::Path>>(
        path: P,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        use std::fs::File;
        use std::io::BufReader;

        let file = File::open(path)?;
        let reader = BufReader::new(file);
        let motifs: Vec<StrategicMotif> = bincode::deserialize_from(reader)?;

        let mut database = Self::new();
        for motif in motifs {
            database.add_motif(motif);
        }

        println!("📚 Loaded {} strategic motifs", database.total_motifs);
        Ok(database)
    }

    /// Save strategic database to binary file
    pub fn save_to_binary<P: AsRef<std::path::Path>>(
        &self,
        path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        use std::fs::File;
        use std::io::BufWriter;

        let motifs: Vec<&StrategicMotif> = self.motifs.values().collect();
        let file = File::create(path)?;
        let writer = BufWriter::new(file);
        bincode::serialize_into(writer, &motifs)?;

        Ok(())
    }

    /// Add a motif to the database
    pub fn add_motif(&mut self, motif: StrategicMotif) {
        // Index the motif for fast retrieval
        self.motifs.insert(motif.pattern_hash, motif.clone());

        // Add to specialized pattern matchers
        match &motif.motif_type {
            MotifType::PawnStructure(_) => self.pawn_matcher.add_pattern(&motif),
            MotifType::PieceCoordination(_) => self.piece_matcher.add_pattern(&motif),
            MotifType::KingSafety(_) => self.king_matcher.add_pattern(&motif),
            _ => {} // Other types use general pattern matching
        }

        self.total_motifs += 1;
    }

    /// Find strategic motifs in a position
    pub fn find_motifs(&mut self, board: &Board) -> Vec<MotifMatch> {
        let board_hash = board.get_hash();

        // Check cache first
        if let Some(&_cached_eval) = self.evaluation_cache.get(&board_hash) {
            // Return empty matches but signal cached evaluation available
            return vec![];
        }

        let mut matches = Vec::new();

        // Find pawn structure motifs
        matches.extend(self.pawn_matcher.find_matches(board));

        // Find piece coordination motifs
        matches.extend(self.piece_matcher.find_matches(board));

        // Find king safety motifs
        matches.extend(self.king_matcher.find_matches(board));

        // Cache the result
        let total_eval = self.evaluate_motifs(&matches);
        self.evaluation_cache.put(board_hash, total_eval);

        matches
    }

    /// Evaluate a collection of motif matches
    pub fn evaluate_motifs(&self, matches: &[MotifMatch]) -> f32 {
        if matches.is_empty() {
            return 0.0;
        }

        let weighted_sum: f32 = matches
            .iter()
            .map(|m| m.motif.evaluation * m.relevance * m.motif.confidence)
            .sum();

        let total_weight: f32 = matches
            .iter()
            .map(|m| m.relevance * m.motif.confidence)
            .sum();

        if total_weight > 0.0 {
            weighted_sum / total_weight
        } else {
            0.0
        }
    }

    /// Get strategic evaluation for a position
    pub fn get_strategic_evaluation(&mut self, board: &Board) -> f32 {
        let matches = self.find_motifs(board);
        self.evaluate_motifs(&matches)
    }

    /// Get database statistics
    pub fn stats(&self) -> StrategicDatabaseStats {
        StrategicDatabaseStats {
            total_motifs: self.total_motifs,
            cache_size: self.evaluation_cache.len(),
            cache_hit_rate: 0.0, // TODO: implement cache hit tracking
        }
    }
}

/// Statistics for the strategic database
#[derive(Debug)]
pub struct StrategicDatabaseStats {
    pub total_motifs: usize,
    pub cache_size: usize,
    pub cache_hit_rate: f32,
}

/// Pattern matcher for pawn structures
pub struct PawnPatternMatcher {
    patterns: Vec<StrategicMotif>,
}

impl PawnPatternMatcher {
    pub fn new() -> Self {
        Self {
            patterns: Vec::new(),
        }
    }

    pub fn add_pattern(&mut self, motif: &StrategicMotif) {
        self.patterns.push(motif.clone());
    }

    pub fn find_matches(&self, board: &Board) -> Vec<MotifMatch> {
        let mut matches = Vec::new();

        // Analyze pawn structure for each pattern
        for pattern in &self.patterns {
            if let Some(motif_match) = self.match_pawn_pattern(board, pattern) {
                matches.push(motif_match);
            }
        }

        matches
    }

    fn match_pawn_pattern(&self, _board: &Board, _motif: &StrategicMotif) -> Option<MotifMatch> {
        // Implementation will analyze specific pawn patterns
        // For now, return None (to be implemented)
        None
    }
}

/// Pattern matcher for piece coordination
pub struct PiecePatternMatcher {
    patterns: Vec<StrategicMotif>,
}

impl PiecePatternMatcher {
    pub fn new() -> Self {
        Self {
            patterns: Vec::new(),
        }
    }

    pub fn add_pattern(&mut self, motif: &StrategicMotif) {
        self.patterns.push(motif.clone());
    }

    pub fn find_matches(&self, _board: &Board) -> Vec<MotifMatch> {
        // Implementation for piece pattern matching
        Vec::new()
    }
}

/// Pattern matcher for king safety
pub struct KingPatternMatcher {
    patterns: Vec<StrategicMotif>,
}

impl KingPatternMatcher {
    pub fn new() -> Self {
        Self {
            patterns: Vec::new(),
        }
    }

    pub fn add_pattern(&mut self, motif: &StrategicMotif) {
        self.patterns.push(motif.clone());
    }

    pub fn find_matches(&self, _board: &Board) -> Vec<MotifMatch> {
        // Implementation for king safety pattern matching
        Vec::new()
    }
}

/// Utility functions for pattern analysis
pub mod pattern_utils {
    use super::*;

    /// Generate pattern hash for a chess position focusing on strategic elements
    pub fn generate_pattern_hash(board: &Board) -> u64 {
        // Use a hash that focuses on positional features rather than exact piece placement
        // This allows similar positions to match even with minor piece differences

        let mut hash = 0u64;

        // Hash pawn structure (most important for strategic patterns)
        hash ^= hash_pawn_structure(board);

        // Hash piece placement patterns
        hash ^= hash_piece_patterns(board);

        // Hash king positions
        hash ^= hash_king_positions(board);

        hash
    }

    fn hash_pawn_structure(_board: &Board) -> u64 {
        // Implementation for hashing pawn structure
        0
    }

    fn hash_piece_patterns(_board: &Board) -> u64 {
        // Implementation for hashing piece patterns
        0
    }

    fn hash_king_positions(_board: &Board) -> u64 {
        // Implementation for hashing king positions
        0
    }

    /// Determine game phase based on material
    pub fn determine_game_phase(board: &Board) -> GamePhase {
        let material_count = count_material(board);

        if material_count > 60 {
            GamePhase::Opening
        } else if material_count > 20 {
            GamePhase::Middlegame
        } else {
            GamePhase::Endgame
        }
    }

    fn count_material(board: &Board) -> u32 {
        // Simple material counting (can be improved)
        let mut total = 0u32;

        // Count all pieces except kings and pawns for phase determination
        for square in chess::ALL_SQUARES {
            if let Some(piece) = board.piece_on(square) {
                match piece {
                    Piece::Queen => total += 9,
                    Piece::Rook => total += 5,
                    Piece::Bishop => total += 3,
                    Piece::Knight => total += 3,
                    _ => {}
                }
            }
        }

        total
    }
}