-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiedrapapeltijera.py
139 lines (104 loc) · 3.58 KB
/
piedrapapeltijera.py
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
137
138
139
from random import choice
from sklearn.neural_network import MLPClassifier
import pickle
options = ["piedra", "tijeras", "papel"]
result = 0
def search_winner(p1, p2):
if p1 == p2:
result = 0
elif p1 == "piedra" and p2 == "tijeras":
result = 1
elif p1 == "piedra" and p2 == "papel":
result = 2
elif p1 == "tijeras" and p2 == "piedra":
result = 2
elif p1 == "tijeras" and p2 == "papel":
result = 1
elif p1 == "papel" and p2 == "piedra":
result = 1
elif p1 == "papel" and p2 == "tijeras":
result = 2
return result
print("probando funcion de ganador",search_winner("papel", "tijeras"))
testear = [
["piedra", "piedra", 0],
["piedra", "tijeras", 1],
["piedra", "papel", 2]
]
for partida in testear:
print("player1: %s player2: %s Winner: %s Validation: %s" % (
partida[0], partida[1], search_winner(partida[0], partida[1]), partida[2]
))
def get_choice():
return choice(options)
for i in range(10):
player1 = get_choice()
player2 = get_choice()
print("player 1: %s player 2: %s Winner is: %s " % (
player1, player2, search_winner(player1, player2)
))
# pasamos los elementos de las opciones a binario.
def str_to_list(option):
if option=="piedra":
res = [1,0,0]
elif option=="tijeras":
res = [0,1,0]
else:
res = [0,0,1]
return res
data_X = list(map(str_to_list, ["piedra", "tijeras", "papel"]))
data_y = list(map(str_to_list, ["papel", "piedra", "tijeras"]))
print(data_X)
print(data_y)
clf = MLPClassifier(verbose=False, warm_start=True)
model = clf.fit([data_X[0]], [data_y[0]])
print("nuestro modelo: ",model)
def play_and_learn(iters=10, debug=False):
score = {"win": 0, "loose": 0}
data_X = []
data_y = []
for i in range(iters):
player1 = get_choice()
predict = model.predict_proba([str_to_list(player1)])[0]
if predict[0] >= 0.95:
player2 = options[0]
elif predict[1] >= 0.95:
player2 = options[1]
elif predict[2] >= 0.95:
player2 = options[2]
else:
player2 = get_choice()
if debug==True:
print("Player1: %s Player2 (modelo): %s --> %s" % (player1, predict, player2))
winner = search_winner(player1, player2)
if debug==True:
print("Comprobamos: p1 VS p2: %s" % winner)
if winner==2:
data_X.append(str_to_list(player1))
data_y.append(str_to_list(player2))
score["win"]+=1
else:
score["loose"]+=1
return score, data_X, data_y
score, data_X, data_y = play_and_learn(1, debug=True)
print(data_X)
print(data_y)
print("Score: %s %s %%" % (score, (score["win"]*100/(score["win"]+score["loose"]))))
if len(data_X):
model = model.partial_fit(data_X, data_y)
i = 0
historic_pct = []
while True:
i+=1
score, data_X, data_y = play_and_learn(1000, debug=False)
pct = (score["win"]*100/(score["win"]+score["loose"]))
historic_pct.append(pct)
print("Iter: %s - score: %s %s %%" % (i, score, pct))
if len(data_X):
model = model.partial_fit(data_X, data_y)
if sum(historic_pct[-9:])==900:
break
print(model.predict_proba([str_to_list("piedra")]))
# guarda el modelo.
filename = 'piedrapapeltijera_model.sav'
pickle.dump(model, open(filename, 'wb'))