source code to read the experience.bin file

Code, algorithms, languages, construction...
Post Reply
User avatar
deeds
Posts: 652
Joined: Wed Oct 20, 2021 9:24 pm
Location: France
Contact:

source code to read the experience.bin file

Post by deeds » Fri Oct 29, 2021 5:10 pm

Image

Code: Select all

Module modMain
    Public processus As System.Diagnostics.Process
    Public entree As System.IO.StreamWriter
    Public sortie As System.IO.StreamReader

    Sub Main()
        Dim i As Integer, keyUCI As String, chaine As String, keyBIN As String
        
        chargerMoteur("E:\JEUX\ARENA CHESS 3.5.1\Engines\BrainLearn\20T BrainLearn 13.1 x64 BMI2.exe")
        keyUCI = uciKEY(entree, sortie, "") 'clé normale

        keyBIN = ""
        For i = 0 To 15 Step 2
            chaine = keyUCI.Substring(i, 2)
            If Len(chaine) = 1 Then
                chaine = "0" & chaine
            End If
            keyBIN = chaine & keyBIN
        Next
        keyUCI = keyBIN 'clé inversée
        keyBIN = ""

        dechargerMoteur()

        Console.WriteLine(brainlearn_expListe("E:\JEUX\ARENA CHESS 3.5.1\Engines\BrainLearn\experience.bin", keyUCI))

        Console.WriteLine("press enter to quit")
        Console.ReadLine()

    End Sub

    Public Function brainlearn_expListe(sourceBIN As String, keyUCI As String) As String
        Dim lectureBIN As IO.FileStream, tabTampon(23) As Byte, chaineExperience As String, compteur As Integer
        Dim coup As String, profondeur As Integer, scoreCP As String, performance As Integer, scoreMAT As String
        Dim chaine As String, keyBIN As String

        lectureBIN = New IO.FileStream(sourceBIN, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        chaineExperience = ""
        compteur = 1
        Do
            lectureBIN.Read(tabTampon, 0, 24) 'clé inversée
            keyBIN = ""
            For i = 0 To 7
                chaine = Hex(tabTampon(i))
                If Len(chaine) = 1 Then
                    chaine = "0" & chaine
                End If
                keyBIN = keyBIN & chaine
            Next

            If keyUCI = keyBIN Then
                'inversed key (8 octets) | Depth |          | score       | inv. move |       | Performance |
                'h0 h1 h2 h3 h4 h5 h6 h7 | h8    | h9 hA hB | hC hD hE hF | h0 h1     | h2 h3 | h4          | h5 h6 h7 (24 octets)
                '00 01 02 03 04 05 06 07   08      09 10 11   12 13 14 15   16 17       18 19   20            21 22 23 tabTampon
                '-----------------------------------------------------------------------------------------------------
                'fb 59 2f 56 d4 01 8f 8f | 1e    | 00 00 00 | 3f 00 00 00 | 1c 03     | 00 00 | 64          | 00 00 00
                'decimal                   30                 0000003f      031c                100%
                '										      63/208        001 100 011 100
                '										      +0.30         2   e   4   e
                '                                                           e2e4

                coup = hexa(tabTampon(17)) & hexa(tabTampon(16))
                '031c => 001 100 011 100 => e2e4
                coup = binToMove(hexadecimalToBinaire(coup))

                profondeur = tabTampon(8)

                scoreCP = hexa(tabTampon(15)) & hexa(tabTampon(14)) & hexa(tabTampon(13)) & hexa(tabTampon(12))
                scoreMAT = scoreCP
                'score positif
                If tabTampon(15) = 0 And tabTampon(14) = 0 Then
                    If hexa(tabTampon(13)) = "7D" And hexa(tabTampon(12)) = "02" Then
                        scoreCP = "<empty>"
                    ElseIf hexa(tabTampon(13)) = "7C" Then
                        scoreMAT = "+M" & Format((32001 - Convert.ToInt64(scoreMAT, 16)) / 2, "0")
                        scoreCP = ""
                    Else
                        scoreCP = Format(Convert.ToInt64(scoreCP, 16) / 208, "+0.00")
                        scoreCP = Replace(scoreCP, "+0,00", " 0.00")
                        scoreCP = Replace(scoreCP, ",", ".")
                    End If
                ElseIf tabTampon(15) = 255 And tabTampon(14) = 255 Then
                    If hexa(tabTampon(13)) = "83" Then
                        scoreMAT = "-M" & Format((32000 - (4294967296 - Convert.ToInt64(scoreMAT, 16))) / 2, "0")
                        scoreCP = ""
                    Else
                        scoreCP = Format((Convert.ToInt64(scoreCP, 16) - 4294967295) / 208, "0.00")
                        scoreCP = Replace(scoreCP, ",", ".")
                    End If
                Else
                    MsgBox("scoreCP = " & scoreCP, MsgBoxStyle.Exclamation, "en travaux")
                End If

                performance = tabTampon(20)

                If scoreCP <> "" Then
                    chaineExperience = chaineExperience & compteur & " : " & coup & ", depth: " & profondeur & ", eval: " & scoreCP & ", count: 1, quality: " & performance & vbCrLf
                Else
                    chaineExperience = chaineExperience & compteur & " : " & coup & ", depth: " & profondeur & ", eval: " & scoreMAT & ", count: 1, quality: " & performance & vbCrLf
                End If
                '1 : e2e4 , depth: 30, eval: cp 30 , count: 1    , quality: 100      

                compteur = compteur + 1
            End If

        Loop While lectureBIN.Position < lectureBIN.Length

        lectureBIN.Close()

        Return chaineExperience

    End Function

    Public Function hexa(valeur As Integer) As String
        Dim chaine As String

        chaine = Hex(valeur)
        If Len(chaine) = 1 Then
            chaine = "0" & chaine
        End If
        Return chaine

    End Function

    Public Function binToMove(chaineBIN As String) As String
        Dim move As String

        '0 111 110 000 111 001
        '  Q   7   a   8   b

        move = ""

        Select Case gauche(droite(chaineBIN, 9), 3) 'a
            Case "000"
                move = move & "a"

            Case "001"
                move = move & "b"

            Case "010"
                move = move & "c"

            Case "011"
                move = move & "d"

            Case "100"
                move = move & "e"

            Case "101"
                move = move & "f"

            Case "110"
                move = move & "g"

            Case "111"
                move = move & "h"

        End Select

        Select Case gauche(droite(chaineBIN, 12), 3) '7
            Case "000"
                move = move & "1"

            Case "001"
                move = move & "2"

            Case "010"
                move = move & "3"

            Case "011"
                move = move & "4"

            Case "100"
                move = move & "5"

            Case "101"
                move = move & "6"

            Case "110"
                move = move & "7"

            Case "111"
                move = move & "8"

        End Select

        Select Case droite(chaineBIN, 3) 'b
            Case "000"
                move = move & "a"

            Case "001"
                move = move & "b"

            Case "010"
                move = move & "c"

            Case "011"
                move = move & "d"

            Case "100"
                move = move & "e"

            Case "101"
                move = move & "f"

            Case "110"
                move = move & "g"

            Case "111"
                move = move & "h"

        End Select

        Select Case gauche(droite(chaineBIN, 6), 3) '8
            Case "000"
                move = move & "1"

            Case "001"
                move = move & "2"

            Case "010"
                move = move & "3"

            Case "011"
                move = move & "4"

            Case "100"
                move = move & "5"

            Case "101"
                move = move & "6"

            Case "110"
                move = move & "7"

            Case "111"
                move = move & "8"

        End Select


        Select Case droite(gauche(chaineBIN, 4), 3) 'Q
            Case "001"
                move = move & "N"

            Case "101"
                move = move & "B"

            Case "110"
                move = move & "R"

            Case "111"
                move = move & "Q"

        End Select

        Return move
    End Function

    Public Sub chargerMoteur(chemin As String)
        Dim chaine As String

        processus = New System.Diagnostics.Process()

        processus.StartInfo.RedirectStandardOutput = True
        processus.StartInfo.UseShellExecute = False
        processus.StartInfo.RedirectStandardInput = True
        processus.StartInfo.CreateNoWindow = True
        processus.StartInfo.WorkingDirectory = My.Application.Info.DirectoryPath
        processus.StartInfo.FileName = chemin
        processus.Start()
        processus.PriorityClass = 64 '64 (idle), 16384 (below normal), 32 (normal), 32768 (above normal), 128 (high), 256 (realtime)

        entree = processus.StandardInput
        sortie = processus.StandardOutput

        entree.WriteLine("uci")
        chaine = ""
        While InStr(chaine, "uciok") = 0
            chaine = sortie.ReadLine
            Threading.Thread.Sleep(1)
        End While

        'options communes
        entree.WriteLine("setoption name Read only learning value true")
        entree.WriteLine("setoption name Use NNUE value false")
        entree.WriteLine("setoption name EvalFile value <empty>")
        entree.WriteLine("setoption name SyzygyPath value <empty>")

        entree.WriteLine("isready")
        chaine = ""
        While InStr(chaine, "readyok") = 0
            chaine = sortie.ReadLine
            Threading.Thread.Sleep(1)
        End While
    End Sub

    Public Sub dechargerMoteur()
        entree.Close()
        sortie.Close()
        processus.Close()

        entree = Nothing
        sortie = Nothing
        processus = Nothing
    End Sub

End Module

Code: Select all

Public Function uciKEY(entreeKEY As System.IO.StreamWriter, sortieKEY As System.IO.StreamReader, movesUCI As String, Optional startpos As String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") As String
        Dim key As String

        key = startpos

        If movesUCI <> "" Then
            entreeKEY.WriteLine("position fen " & startpos & " moves " & movesUCI)
        Else
            entreeKEY.WriteLine("position fen " & startpos)
        End If

        entreeKEY.WriteLine("d")

        key = ""
        While InStr(key, "Key: ", CompareMethod.Text) = 0
            key = sortieKEY.ReadLine
        End While

        Return Replace(key, "Key: ", "")
    End Function

    Public Function droite(texte As String, longueur As Integer) As String
        If longueur > 0 Then
            Return VB.Right(texte, longueur)
        Else
            Return ""
        End If
    End Function

    Public Function gauche(texte As String, longueur As Integer) As String
        If longueur > 0 Then
            Return VB.Left(texte, longueur)
        Else
            Return ""
        End If
    End Function


Post Reply