Skip to content

Commit

Permalink
Merge branch 'release-1.1.0' into main
Browse files Browse the repository at this point in the history
New features: Black AI, AI vs AI proposal
2 modes: Devmode (Human vs Human, White AI, Black AI, AI vs AI), Game (White AI, Black AI)
New chess mechanism: pieces movements are based on Python-Chess module: "legal_move"
New displays:
- when there is an outcome (White or Black win or Draw) + the reason called "Termination".
- "New Game" button to start a new game.
- when AI inferes wrong movement, "None" movement and when a source square is empty.
New checking if the given IP_Address is valid + if remote server is running.
What missing is the Null movement (for Human and AI).
  • Loading branch information
MikeHatchi committed Oct 20, 2023
2 parents e6c0c43 + 6c6e0da commit f36e6e0
Show file tree
Hide file tree
Showing 36 changed files with 727 additions and 900 deletions.
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ FHE.Chess is about an application that let you play Chess against an AI opponent
<br>

## Acknowledgement
**This repo contains large files: training dataset, mlir docs.**<br>
**Repo is about 4 Go. Real repo is 8 Go, with private and public keys for FHE**
**This repo contains large files: training dataset.**<br>

- FHE.Chess project answers to [Zama.ai](https://www.zama.ai)'s bounty and shows how to use Zama's [Concrete-ML](https://docs.zama.ai/concrete-ml/) open-source library,
- AI plays as "white" only,
- The FHE.Chess app can be run under 3 modes:
Expand All @@ -17,20 +17,14 @@ FHE.Chess is about an application that let you play Chess against an AI opponent
- deepfhe: encrypted context thanks to FHE. Very slow inferences (because model complexity + hardware)
- AI mainly starts its moves the same way, after several white & black moves its moves are relevant,
- Several minor bugs remain within the [Chess_app](docs/Chess_app/Chess_app.md) itself, such as:
- false positive authorized en-passant moves,
- false positive authorized move of kings even if the targeted (aka destination) square conducts to check,
Because of the dual chess developments: one from scratch, one from python-chess, the latter crashes the Chess app when a move is not "legal".
- Do not download the followed large files directories:
- ```server_cloud/mlir_source.txt```
- ```server_cloud/mlir_target.txt```
- ```server_cloud/client/source```
- ```server_cloud/client/target```
- ```server_cloud/server/model```
- potential false positive authorized en-passant moves,
- because of the dual chess developments: one from scratch, one from python-chess, the latter call potential "illegal" moves (this does not lead to crash the app.).
- To download [wb_2000_300.csv](server_cloud/data/wb_2000_300.csv) training data, **[```git lfs```](https://git-lfs.com) is required**.

<br>

## How to
[Project flow](docs/Project_Flow.md) is the **IMPORTANT** document and represents your Ariadne's wire to install, run or rebuilt FHE.Chess project.<br>
[Project flow](docs/Project_Flow.md) is the :o: **IMPORTANT** :o: document and represents your Ariadne's wire to install, run or rebuilt FHE.Chess project.<br>
As it contains details, explanations and links to other detailed docs, follow the inner steps it explains.
<br>

Expand Down Expand Up @@ -71,11 +65,19 @@ Then, run:
**1st remote terminal**: ```$ python3 server/server_all.py -i (or --inference) "clear" or "simfhe" or "deepfhe"```<br>
!! Wait until the server is connected !! (waiting time:```"clear"``` and ```"deepfhe"``` < several seconds, ```"simfhe"``` between 2 and 7 mins)<br>

**2nd local terminal**: ```$ python3 client_local/chess_env/main.py --server IP_address --port PORT```<br>
NB: default ```--port``` is 3389 which is ok on GCI and AWS.
**2nd local terminal**: ```$ python3 client_local/chess_env/main.py --server "IP_address" --port PORT```<br>
NB:
- ```--server```: **Required option** and it enables "White AI" and "Black AI" modes,
If you have mistyped your IP_Address or if you forgot to run your remote server, please answer to the prompt displayed by your Local Terminal.
- ```--port```: **Facultative** if your value is the default value:```3389```. This is the ok firewall on GCI and AWS.
<br>


There is a "developer mode" called ```--devmode```. **Facultative** if you are not interesting in:
- "Human vs Human" game,
- "AI vs AI"game.<br><br>
Its default value: ```False```.<br>
To activate it, run ```$ python3 client_local/chess_env/main.py --devmode True --server "IP_address" --port PORT```
<br>

## Reset and kill
- to reset the game: press r,
Expand All @@ -88,16 +90,26 @@ NB: default ```--port``` is 3389 which is ok on GCI and AWS.

2 main modes:

- WhiteAI (AI as white vs Human as black)<br>
- **Game** <br>
<br>
White or Black AI (vs Human as Black or White)<br>
3 modes:
- clear
- simfhe
- deepfhe
- White H (Human as white vs Human as black)
<br>
<div align="center"><img src="./images/screen_zama_vrona_chess.png" style="width:'50%'"/></div>
<div align="center"><img src="./images/W_B_AI_menu.png" style="width:'50%'"/></div>
<br>

- **Developer Mode**<br>
<br>
2 more choices added to the Game mode:<br>
- Human vs Human
- AI vs AI (here to watch different type of behavior, you can play with the top best answers of each models. In your remote machine, then into [server_all.py](server_cloud/server/server_all.py), change the parameters 5 and 3 of <br>```python reply = inference.predict(data, 5, 3)```. Recall that 5 is the 5 top answers of Source model. And for each of them, Target model keep the 3 top answers).

<br>
<div align="center"><img src="./images/Devmode_menu.png" style="width:'50%'"/></div>
<br>

[^1]: if needed, main steps to create ssh connection with GCI on Linux/Mac:
- recall your ```USERNAME``` from GCI and think about a ```KEY_FILENAME```,
Expand Down
54 changes: 52 additions & 2 deletions client_local/chess_env/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
sqsize = sp_width // cb_cols #size of squares


"""Python-Chess lib. uses alphanumeric coordinates (e2 equivalent to col 1 - row 2) but also
"""
Python-Chess lib. uses alphanumeric coordinates (e2 equivalent to col 1 - row 2) but also
bitboard logic: an array of square table location within chessboard.
the move e2e4 becomes 8 to 24.
"""
Expand All @@ -29,4 +30,53 @@
[16,17,18,19,20,21,22,23],
[8,9,10,11,12,13,14,15],
[0,1,2,3,4,5,6,7],
])
])

"""
Flipped bitboard to understand black point of view.
flip_bitboard = np.flip(bitboard)
[[ 7 6 5 4 3 2 1 0]
[15 14 13 12 11 10 9 8]
[23 22 21 20 19 18 17 16]
[31 30 29 28 27 26 25 24]
[39 38 37 36 35 34 33 32]
[47 46 45 44 43 42 41 40]
[55 54 53 52 51 50 49 48]
[63 62 61 60 59 58 57 56]]
"""

"""
Hash tables:
- dict of square_alphanum (square: alphanumeric),
- dict of alphanum_square (alphanumeric: square).
col = ["a", "b", "c", "d", "e", "f", "g", "h"]
row = [1,2,3,4,5,6,7,8]
bitalpha = []
square_alphanum = {}
for ic in np.ndenumerate(bitboard):
square_alphanum[ic[1]] = "%s%s" %(col[ic[0][1]],row[7-ic[0][0]])
square_alphanum = {56: 'a8', 57: 'b8', 58: 'c8', 59: 'd8', 60: 'e8', 61: 'f8', 62: 'g8', 63: 'h8',
48: 'a7', 49: 'b7', 50: 'c7', 51: 'd7', 52: 'e7', 53: 'f7', 54: 'g7', 55: 'h7',
40: 'a6', 41: 'b6', 42: 'c6', 43: 'd6', 44: 'e6', 45: 'f6', 46: 'g6', 47: 'h6',
32: 'a5', 33: 'b5', 34: 'c5', 35: 'd5', 36: 'e5', 37: 'f5', 38: 'g5', 39: 'h5',
24: 'a4', 25: 'b4', 26: 'c4', 27: 'd4', 28: 'e4', 29: 'f4', 30: 'g4', 31: 'h4',
16: 'a3', 17: 'b3', 18: 'c3', 19: 'd3', 20: 'e3', 21: 'f3', 22: 'g3', 23: 'h3',
8: 'a2', 9: 'b2', 10: 'c2', 11: 'd2', 12: 'e2', 13: 'f2', 14: 'g2', 15: 'h2',
0: 'a1', 1: 'b1', 2: 'c1', 3: 'd1', 4: 'e1', 5: 'f1', 6: 'g1', 7: 'h1',}
# alphanum_square = {v: k for k, v in square_alphanum.items()}
"""
alphanum_square = {'a8': 56, 'b8': 57, 'c8': 58, 'd8': 59, 'e8': 60, 'f8': 61, 'g8': 62, 'h8': 63,
'a7': 48, 'b7': 49, 'c7': 50, 'd7': 51, 'e7': 52, 'f7': 53, 'g7': 54, 'h7': 55,
'a6': 40, 'b6': 41, 'c6': 42, 'd6': 43, 'e6': 44, 'f6': 45, 'g6': 46, 'h6': 47,
'a5': 32, 'b5': 33, 'c5': 34, 'd5': 35, 'e5': 36, 'f5': 37, 'g5': 38, 'h5': 39,
'a4': 24, 'b4': 25, 'c4': 26, 'd4': 27, 'e4': 28, 'f4': 29, 'g4': 30, 'h4': 31,
'a3': 16, 'b3': 17, 'c3': 18, 'd3': 19, 'e3': 20, 'f3': 21, 'g3': 22, 'h3': 23,
'a2': 8, 'b2': 9, 'c2': 10, 'd2': 11, 'e2': 12, 'f2': 13, 'g2': 14, 'h2': 15,
'a1': 0, 'b1': 1, 'c1': 2, 'd1': 3, 'e1': 4, 'f1': 5, 'g1': 6, 'h1': 7,}
Loading

0 comments on commit f36e6e0

Please sign in to comment.