Another quick simple visualization of search trees

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
j.t.
Posts: 262
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: Another quick simple visualization of search trees

Post by j.t. »

Quick little update, I made the process a bit simpler and fixed an issue that could happen in engines that use multiple root searches per iteration (e.g. aspiration windows).

------------------------------------------------------------------

What you need to do first is to add a line to the search of the engine, that prints out the height of each node that gets entered (I only did regular search, but could probably also work with quiesce). So like this:

Code: Select all

int alphaBeta(int alpha, const int beta, const int depth, const int height) {
   std::cout << height << std::endl; // <----- print out height
   if (depth <= 0) return quiesce(alpha, beta);
   for (all moves)  {
      // important, height must be increased by exactly one with each recursive call
      score = -alphaBeta(-beta, -alpha, depth - 1, height + 1);
      ...
   }
   ...
}
then run your engine to some fixed depth.
Copy the whole output (can include UCI chattering) and paste it into a text file called "myTreeNumbers.txt".

Then run the following Python program:

Code: Select all

from treelib import Node, Tree

filename = "myTreeNumbers.txt"

myNumbers = None
with open(filename, 'r') as file:
    for line in file:
        line = line[0:-1]
        if line.isdigit():
            number = int(line)
            if number == 0:
                myNumbers = []
            myNumbers.append(number)

tree = Tree()
root = tree.create_node(tag = 0)

index = 0

print("myNumbers:", len(myNumbers))

def addNode(height, parent):
    global index
    assert myNumbers[index] == height

    while myNumbers[index] >= height:
        if myNumbers[index] == height:
            current = tree.create_node(tag=height, parent=parent)
            print("Created node:", index, height)
            index += 1
        if index >= len(myNumbers):
            print("Returning node:", index)
            return
        if myNumbers[index] > height:
            addNode(height + 1, current)
        if index >= len(myNumbers):
            print("Returning node:", index)
            return
    
    assert myNumbers[index] < height

addNode(0, root)

tree.to_graphviz(filename="myTree.dot", shape="point", graph="graph")
Then run the following script:

Code: Select all

#!/bin/bash

dot -Tpng myTree.dot -o myTree.png
twopi -Tpng myTree.dot -o myTreeRadial.png
Now you should have two images of a search tree visualization: "myTree.png" and "myTreeRadial.png".
This below is an example image (you probably need to zoom in a little to see anything):
Image
glav
Posts: 50
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Another quick simple visualization of search trees

Post by glav »

Nice. Could you please post some of your "myTreeNumbers.txt" files so that we cam experiment with the graphic part, without having to change the evaluation function.
Thanks in advance.
User avatar
j.t.
Posts: 262
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: Another quick simple visualization of search trees

Post by j.t. »

glav wrote: Sat Aug 10, 2024 11:22 am Nice. Could you please post some of your "myTreeNumbers.txt" files so that we cam experiment with the graphic part, without having to change the evaluation function.
Thanks in advance.
You can find the files on the search_tree_image branch of the Nalwald repo. If you want I can also create an executable of Nalwald so that you can get the output for any position you want.
glav
Posts: 50
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Another quick simple visualization of search trees

Post by glav »

You can find the files on the search_tree_image branch of the Nalwald repo. If you want I can also create an executable of Nalwald so that you can get the output for any position you want.
[/quote]

Thank you, Appreciated. Yes, it would be great if you could also post an executable of Nalwald so that we can get the output for any position we want,
I was also wondering if your plotting method could also apply to a minimaxed epd file, starting from a given position. For istance, starting from the following position:
[d]rnbq1rk1/ppp1ppbp/3p1np1/6B1/2PPP3/2N2P2/PP4PP/R2QKBNR b KQ - ce -49; acd 26; acs 9; pv c5 d5;

I would like to plot the tree of the enclosed epd file, possibly making evident the moves according to their centipawn evaluation.

https://www.dropbox.com/scl/fi/8u8c9jqw ... smcvu&dl=0

Would that be possible? Thanks in advance.
User avatar
j.t.
Posts: 262
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: Another quick simple visualization of search trees

Post by j.t. »

That sounds more like this tool might be more suited to your needs.

In any case, here you can download the Nalwald version that prints out the text that needs to be put into a "myTreeNumbers.txt" file: Nalwald_search_tree_image.exe
glav
Posts: 50
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Another quick simple visualization of search trees

Post by glav »

j.t. wrote: Mon Aug 12, 2024 12:37 am That sounds more like this tool might be more suited to your needs.

In any case, here you can download the Nalwald version that prints out the text that needs to be put into a "myTreeNumbers.txt" file: Nalwald_search_tree_image.exe
Sounds interesting, Thanks for pointing out this resource. Thanks also for the 'special' Nalwad version.