bug-gnubg
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inquiry on Integrating GNU Backgammon for Analysis via RESTful API


From: Philippe Michel
Subject: Re: Inquiry on Integrating GNU Backgammon for Analysis via RESTful API
Date: Mon, 1 Apr 2024 14:24:37 +0200

On Wed, Mar 27, 2024 at 03:00:06PM +0000, David Reay wrote:

> I hope this message finds you well. I am currently developing a RESTful API
> using Python, specifically with frameworks such as FastAPI/Flask, and I am
> keen on integrating GNU Backgammon (gnubg) into this project. The primary
> function I aim to achieve is to send a match and position ID to gnubg, have
> it load the match, analyze the position, and if it is gnubg's turn, to also
> receive the recommended move.
> 
> The end goal is to facilitate this interaction through JSON for both the
> requests and responses, ensuring seamless integration within a Python
> environment. This setup is intended to serve a broader application that
> leverages gnubg's capabilities for analysis and decision-making in
> backgammon matches.
> 
> Could you please provide guidance or point me towards documentation on how
> to achieve the following using gnubg?
> 
> 1. Sending a match and position ID to gnubg, preferably in a JSON format,
> for it to load and analyze the match.
> 2. Receiving gnubg's analysis and recommended move in a JSON response,
> which is crucial for the integration with my Python-based API.
> 3. Any examples or best practices for integrating gnubg with Python,
> especially in a serverless architecture or a containerized environment.

I'm not familiar with cient-server programming and json encoding in 
python, but gnubg includes an embedded python interpreter, so anything 
that works with regular python should (with Linux where the system 
python libraries are used) or could (with Windows where a limited set of 
modules are included in gnubg's installer and you may need to add some 
missing ones).

For instance the following short examples (picked from searches, I can't 
explain how they work exactly or how much oversimplified they are but 
someone more familiar with what you want to do should be able to) can be 
launched from gnubg. Or more probably the server part from gnubg and the 
client part from regular python.

server.py:

import socketserver
import json

class MyTCPHandler(socketserver.StreamRequestHandler):

    def handle(self):
        self.data = self.rfile.readline().strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)

        j = json.loads(self.data)

        self.wfile.write(str(j).format().encode('utf-8'))
 
if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
 
    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
        server.serve_forever()


client.py:

import socket
import sys
import json

HOST, PORT = "localhost", 9999
data = json.dumps(sys.argv[1:])

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

    received = str(sock.recv(1024), "utf-8")

print("Sent:     {}".format(data))
print("Received: {}".format(received))


To do something useful, the server part's handler can use the following 
functions:

gnubg.setgnubgid() to set the position
and then either
gnubg.findbestmove() to find the best checker play
or
gnubg.cfevaluate() to evaluate a cube decision


>>> gnubg.setgnubgid("cC/kAVDatg0iAA:QYkVAAAAAAAA")
Setting GNUbg ID cC/kAVDatg0iAA:QYkVAAAAAAAA
>>> gnubg.findbestmove()
(8, 5, 7, 2)
>>> mt = gnubg.findbestmove()
>>> gnubg.movetupletostring(mt, gnubg.board())
'8/5 7/2'

(the latter needs gnubg.board() to distinguish 8/5 from 8/5* for instance...)

or

>>> gnubg.setgnubgid("4HPwA0AzTvABMA:cAkAAAAAAAAA")
Setting GNUbg ID 4HPwA0AzTvABMA:cAkAAAAAAAAA
>>> gnubg.cfevaluate()
(0.5438268780708313, 0.5438268780708313, 0.5045644044876099, 1.0, 2, 'No 
double, take')

The result tuple is:
estimated cubeful equity after the best decision
ND equity
D/T equity
D/P equity
index of the best decision (from the cubedecision enum in eval.h)
description of the best decision



reply via email to

[Prev in Thread] Current Thread [Next in Thread]