File size: 3,333 Bytes
63a8db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f4808d
63a8db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f4808d
 
 
 
 
 
 
 
63a8db2
6f4808d
 
 
 
 
63a8db2
6f4808d
63a8db2
 
 
 
 
 
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
/**
 * Trigo Tree Agent - AI agent using tree attention for efficient move evaluation
 *
 * Uses evaluation mode ONNX model to score all valid moves in parallel.
 * Organizes moves as a prefix tree where branches with same head token are merged.
 */
import { ModelInferencer } from "./modelInferencer";
import { TrigoGame } from "./trigo/game";
import type { Move } from "./trigo/types";
export interface ScoredMove {
    move: Move;
    score: number;
    notation: string;
}
export declare class TrigoTreeAgent {
    private inferencer;
    private readonly START_TOKEN;
    constructor(inferencer: ModelInferencer);
    /**
     * Convert Stone type to player string
     */
    private stoneToPlayer;
    /**
     * Encode a position to TGN notation (3 characters for 5×5×5 board)
     */
    private positionToTGN;
    /**
     * Convert string to byte tokens (ASCII encoding)
     */
    private stringToTokens;
    /**
     * Build prefix tree from token arrays using recursive merging
     * Merges branches with the same token at EVERY level
     *
     * Algorithm:
     * 1. Group sequences by their first token
     * 2. For each group:
     *    - Create one node for the shared first token
     *    - Extract remaining tokens (residues)
     *    - Recursively build subtree from residues
     * 3. Combine all subtrees and build attention mask
     *
     * Example for ["aa", "ab", "ba", "bb"]:
     *   Level 1: Group by first token → 'a': ["a","b"], 'b': ["a","b"]
     *   Level 2: Within 'a' group, build subtree for ["a","b"]
     *            Within 'b' group, build subtree for ["a","b"]
     *   Result: Two branches, each with properly merged second-level nodes
     *
     * @param tokenArrays - Array of token arrays
     * @returns Flattened token array (length m), mask matrix (m×m), and move-to-position mapping
     */
    private buildPrefixTree;
    /**
     * Build tree structure for all valid moves
     * Returns prefix tokens and tree structure for batch evaluation
     */
    private buildMoveTree;
    /**
     * Get tree structure for visualization (public method)
     */
    getTreeStructure(game: TrigoGame, moves: Move[]): {
        evaluatedIds: number[];
        mask: number[];
        parent: Array<number | null>;
        moveData: Array<{
            move: Move;
            notation: string;
            leafPos: number;
        }>;
    };
    /**
     * Select move using tree attention with optional temperature sampling
     * @param game Current game state
     * @param temperature Sampling temperature (0 = greedy, higher = more random)
     * @returns Selected move (position or Pass if no valid positions)
     */
    selectMove(game: TrigoGame, temperature?: number): Promise<Move>;
    /**
     * Select best move using tree attention (greedy, temperature=0)
     * Evaluates all valid moves in a single inference call
     * Pass is excluded from model prediction - returned directly if no positions available
     */
    selectBestMove(game: TrigoGame): Promise<Move>;
    /**
     * Sample a move from scored moves using temperature
     */
    private sampleMove;
    /**
     * Score all moves using tree attention (batch evaluation)
     */
    scoreMoves(game: TrigoGame, moves: Move[]): Promise<ScoredMove[]>;
}
//# sourceMappingURL=trigoTreeAgent.d.ts.map