forked from LennyGonzales/R5B10-IA_RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
136 lines (108 loc) · 3.79 KB
/
graph.py
File metadata and controls
136 lines (108 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 5 21:26:45 2023
@author: dadag
"""
import itertools
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
def compter_symboles(grille):
nombre_X, nombre_O = map(lambda s: sum(l.count(s) for l in grille), ['X', 'O'])
return nombre_X + nombre_O
def isGoodGrille(grille):
nombre_X, nombre_O = map(lambda s: sum(l.count(s) for l in grille), ['X', 'O'])
if (nombre_X + 1 == nombre_O or nombre_X == nombre_O + 1):
return True
if (nombre_X == nombre_O):
return True
if (nombre_X == nombre_O == 0):
return True
winO = isWin(grille, 'X')
winX = isWin(grille, 'O')
if (winO != winX):
return True
return False
def whoPlay(grille):
nombre_X, nombre_O = map(lambda s: sum(l.count(s) for l in grille), ['X', 'O'])
if (nombre_X < nombre_O):
return 'X'
else:
return 'O'
def isWin(state, item):
for row in state:
if (row[0] == item and row[1] == item and row[2] == item):
return True
# vertical
for i in range(3):
if (state[0][i] == item and state[1][i] == item and state[2][i] == item):
return True
# diagonal
if (state[0][0] == item and state[1][1] == item and state[2][2] == item):
return True
if (state[2][0] == item and state[1][1] == item and state[0][2] == item):
return True
def whoWin(state):
# horizontal
for row in state:
if (row[0]!= ' ' and row[0] == row[1] == row[2]):
return True
# vertical
for i in range(3):
if (state[0][i]!= ' ' and state[0][i] == state[1][i] == state[2][i]):
return True
# diagonal
if (state[0][0]!= ' ' and state[0][0] == state[1][1] == state[2][2]):
return True
if (state[2][0]!= ' ' and state[2][0] == state[1][1] == state[0][2]):
return True
return False
def combinations(board):
emptyCellsArray = emptyCells(board)
arrayOfNextStateId = []
boardTemplate = np.copy(board)
actualBoardClass = np.copy(board)
for i in range(len(emptyCellsArray)):
actualBoardClass = addItem(emptyCellsArray[i], whoPlay(board), board)
arrayOfNextStateId.append(list(allCombinations.keys())[list(allCombinations.values()).index(actualBoardClass)])
actualBoardClass = boardTemplate
return arrayOfNextStateId
def addItem(position, item, board):
x = int(position[0])
y = int(position[1])
board[x][y] = item
return board
def emptyCells(board):
emptyCells = np.array([])
for x in range(len(board)):
for y in range (len(board[0])):
if board[x][y] == ' ':
emptyCells = np.append(emptyCells,str(x)+str(y))
return emptyCells
player = ['X', 'O', ' ']
states_dict = {}
all_possible_states = [[list(i[0:3]), list(i[3:6]), list(i[6:9])] for i in itertools.product(player, repeat=9)]
n_states = len(all_possible_states)
allCombinations = {}
for state in range (n_states):
allCombinations[state] = all_possible_states[state]
file = np.loadtxt('trained_state_values_X.txt', dtype=np.float64)
graph = {}
for idx, state in enumerate(all_possible_states):
if( not isGoodGrille(state)):
continue
states_dict[idx] = state
if(compter_symboles(state) < 5 and not (whoWin(state))): # On ne vérifie pas ces next states
continue
listNextStates = combinations(state)
dictNextStates = {}
for nextState in listNextStates:
dictNextStates[nextState] = {'weight':file[nextState]}
graph[idx] = dictNextStates
G = nx.DiGraph(graph)
pos=nx.spring_layout(G, k=0.15)
nx.draw(G, with_labels=True,pos=pos)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
plt.show()
print("--- fin ---")