-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.py
executable file
·191 lines (159 loc) · 5.55 KB
/
rps.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc. Unless you have purchased from
# Numenta, Inc. a separate commercial license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""A simple client to create a CLA model to play Rock/Paper/Scissors."""
import sys
import random
import logging
from nupic.frameworks.opf.modelfactory import ModelFactory
from nupic.data.inference_shifter import InferenceShifter
import description
R = 1
P = 5
S = 10
# uncomment for repeatable testing data
#random.seed("NuPIC")
# number of records to train on
TRAINING = 3
#-----------------------------------------------------------------------------
# nupic functions
#-----------------------------------------------------------------------------
def createModel():
return ModelFactory.create(description.config)
def get_throw(val):
global R
global P
global S
if val < R+((P-R)/2):
# Predicting Rock, so throw Paper to beat it
return P
elif val < P+((S-P)/2):
# Predicting Paper, so throw Scissors
return S
else:
# Predicting Scissors, so throw Rock
return R
def get_print_val(val):
global R
global P
global S
if val == R:
return 'R'
elif val == P:
return 'P'
elif val == S:
return 'S'
else:
return '?'
def print_result(text, arrow, choice, choice_cla, win, lose, draw):
c1 = get_print_val(choice)
c2 = get_print_val(choice_cla)
print c1, arrow, c2, " - ", text, " - Record:", win, lose, draw
def runGame():
global R
global P
global S
global TRAINING
win = 0
lose = 0
draw = 0
choicelist = [R,P,S]
model = createModel()
model.enableInference({'predictionSteps': [1], 'predictedField': 'choice', 'numRecords': 1000})
inf_shift = InferenceShifter();
# Train initial values
for i in range(0, TRAINING):
for c in choicelist:
record = {'choice': c}
result = inf_shift.shift(model.run(record))
# Train on random choices
#for i in range(0, TRAINING):
# c = random.choice(choicelist)
# record = {'choice': c}
# result = inf_shift.shift(model.run(record))
# - Get the initial predicted value
#inferred = result.inferences['multiStepPredictions'][1]
#predicted = sorted(inferred.items(), key=lambda x: x[1])[-1][0]
predicted = 0.0
total_probability = 0.0
for key, value in result.inferences['multiStepPredictions'][1].iteritems():
predicted += float(key) * float(value)
total_probability += float(value)
predicted = predicted / total_probability
choice_cla = get_throw(predicted)
# - Start playing the game
print
print "Play 'Rock, Paper, Scissors' with the CLA."
print
print "Enter 1 or 'r' for Rock, 2 or 'p' for Paper, and 3 or 's' for Scissors ('q' to quit):"
choice = raw_input()
while choice != 'q':
# Interpret the user's choice
if choice == '1' or choice == 'r':
choice = R
elif choice == '2' or choice == 'p':
choice = P
elif choice == '3' or choice == 's':
choice = S
else:
print "Unknown value, try again."
choice = raw_input()
continue
# Compare the two choices
if choice == choice_cla:
draw = draw + 1
print_result("It's a draw", " == ", choice, choice_cla, win, lose, draw)
elif choice == R and choice_cla == P:
lose = lose + 1
print_result("You lose! ", " ==>", choice, choice_cla, win, lose, draw)
elif choice == R and choice_cla == S:
win = win + 1
print_result("You win! ", "<== ", choice, choice_cla, win, lose, draw)
elif choice == P and choice_cla == S:
lose = lose + 1
print_result("You lose! ", " ==>", choice, choice_cla, win, lose, draw)
elif choice == P and choice_cla == R:
win = win + 1
print_result("You win! ", "<== ", choice, choice_cla, win, lose, draw)
elif choice == S and choice_cla == R:
lose = lose + 1
print_result("You lose! ", " ==>", choice, choice_cla, win, lose, draw)
elif choice == S and choice_cla == P:
win = win + 1
print_result("You win! ", "<== ", choice, choice_cla, win, lose, draw)
else:
print_result("ERROR!!! ", ">--<", choice, choice_cla, win, lose, draw)
# Feed their choice to the model and get the next predicted value
record = {'choice': choice}
result = inf_shift.shift(model.run(record))
#inferred = result.inferences['multiStepPredictions'][1]
#predicted = sorted(inferred.items(), key=lambda x: x[1])[-1][0]
predicted = 0.0
total_probability = 0.0
for key, value in result.inferences['multiStepPredictions'][1].iteritems():
predicted += float(key) * float(value)
total_probability += float(value)
predicted = predicted / total_probability
choice_cla = get_throw(predicted)
choice = raw_input()
if __name__ == "__main__":
runGame()
print "Thanks for playing!"