I just wanted to put it out there, I think it would be a fantastic learning tool if there was an option for OGS to move the stones around for counting at the end of a game, the same as is done on a real Go board.
The purpose being to simulate the traditional counting method, as a cultural artifact, as a learning aid, and because it looks cool. I think it would be a great resource to see how counting actually works and make it easier for people to transition to in-person Go boards.
Accurately capturing the traditional method in an algorithm is tricky, since it’s a matter of style. Personally I feel that Japanese-style counting would be most clear, but I’m not a programmer!
I don’t think it is an “AI”, just an algorithm for going around the board picking up stones, putting them down, and making areas in each surrounded territory. I do not enjoy programming in my free time, but people in discord seemed to like the idea so I just wanted to put it out there.
From my limited knowledge, I would say something like:
Pick up all the dead stones
Divide each distinct group that surrounds territory
Set it up as a constrained optimization problem to create separate zones that are as square/rectangular as possible
I am not sure how the movement of stones would work, that seems like the hard part to program. Maybe the first step is a problem to draw lines where the minimum territorial borders are, and then the next step is to move stones around.
The “AI” might simply solve an optimisation problem, as follows.
Step 0. Start with a go position after both players have passed. Remove the dead stones.
Step 1. Mark every intersection of the board with one of the following five labels:
White stone, Black stone, White territory, Black territory, or Dame.
If using Japanese rules, if there is a seki on the board, then all intersections involved in the seki must be marked as Dame as well, including the stones and eyes.
Step 2. A “solution” of the optimisation problems consists in changing the labels of any number of intersections, with the following rules:
A black stone or territory cannot become a white stone or territory, nor vice versa;
The number of black stones that become black territories plus the number of stones captured by white must be equal to the number of black territories that become black stones;
The number of white stones that become white territories plus the number of stones captured by black must be equal to the number of white territories that become white stones;
These rules guarantee that the score of the game is unaffected.
Then, the optimal solution is the solution that
maximises the “rectangularness” of the territories, while at the same time
Minimising the number of changed labels is easy to formulate. Maximising the rectangularness is the harder part.
“Maximising the rectangularness” might be reworded as “minimising the perimeter”. Now we just need to find a good definition for perimeter of a territory.
We can define “perimeter” as follows:
Every intersection contributes between 0 and 4 to the perimeter of a territory.
An intersection which does not belong to a territory contributes 0 to the perimeter of that territory.
An intersection which belongs to a territory contributes 1 for each adjacent intersection which does not belong to that territory.
An intersection which belongs to a territory also contributes 1 for each adjacent side of the board.
A territory is a rectangle if and only if it is convex. So we can add penalties for every concavity in a territory. A concavity is an intersection which is not in the territory, but is adjacent to at least two intersections which are in the same territory.