-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (141 loc) · 5.68 KB
/
main.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
191
192
193
194
195
196
import numpy as np
import random
import game
def add_target(asked_questions, target_objects, weights, questions, question_features, asked_questions_data, answer_target_data):
new_target_name = input('Ingrese nombre : ').strip()
target_object_id = check_target(new_target_name, target_objects)
features = {}
features['ingredients'] = input('Ingredientes: ').split(',')
features['flavor'] = input('Sabor: ').split(',')
print('Tipo: ')
types = ['entrada', 'sopa', 'segundo', 'postre', 'aperitivo', 'otros']
for i in range(len(types)):
print('[%d] %s' %(i, types[i]))
options = input('Opcion(es): ').split(',')
features['target_type'] = [ types[int(option)] for option in options]
features['country'] = input('Origen o Pais: ').split(',')
features['other'] = input('Otra caracteristica: ').split(',')
weights = game.add_features(features, questions, question_features, weights, asked_questions)
if target_object_id is not None:
game.learn(asked_questions, target_object_id, weights, asked_questions_data, answer_target_data)
else:
weights = game.learn_new_target(asked_questions, new_target_name, target_objects, weights, asked_questions_data, answer_target_data)
return weights
def check_target(name, target_objects):
similar = []
for target_object_id in range(len(target_objects)):
value = target_objects[target_object_id]
if name in value or value in name:
similar.append((target_object_id, value))
if len(similar) == 0:
return None
print('Nombres similares:')
for i in range(len(similar)):
value = similar[i][1]
print('[%d] %s' %(i, value))
answer = input('Es alguno de estos?(y/n):').strip()
if answer == 'n':
return None
option = int(input('Opcion:').strip())
return similar[option][0]
def train(filepath, weights, asked_questions_data, answer_target_data):
m = []
with open(filepath, 'r') as file:
for line in file:
aux = line.split()
m.append(aux)
#print(m)
for j in range(len(m[0])):
asked = {}
for i in range(len(m)):
asked[i] = int(m[i][j])
print(asked)
#game.learn(asked, j, weights, asked_questions_data, answer_target_data)
def main():
#game.get_data()
asked_questions_data = game.load_asked_questions_data()
answer_target_data = game.load_answer_target_data()
#game.init()
question_features = game.load_question_features()
questions = game.load_questions()
target_objects = game.load_target_objects()
weights = game.load_weights()
# for i in range(9):
# train('data_2.in', weights, asked_questions_data, answer_target_data)
# with open('test_2', 'a') as file:
# file.write('Targets: \n')
# for i in range(len(target_objects)):
# file.write('[%d] %s \n' %(i,target_objects[i]) )
# file.write('Questions: \n')
# for i in range(len(questions)):
# file.write('[%d] %s \n' %(i, questions[i]))
# print(asked_questions_data)
# print(answer_target_data)
# for i in range(len(target_objects)):
# print('[%d]%s: %d' %(i, target_objects[i], answer_target_data.count(i) ) )
while(True):
print('Targets: ')
print(target_objects)
print('Features: ')
print(question_features)
print('Questions: ')
print(questions)
print('Weights:')
print(weights)
asked_questions = {}
initial_questions = game.load_initial_questions(questions)
rank_target_objects = {}
for target_object_id in range(len(target_objects)):
rank_target_objects[target_object_id] = 0
question_number = 0
question_count = 0
n = len(target_objects)
r = random.randint(0, n - 1)
print( 'try: %s', target_objects[r])
while(True):
tmp_weights = weights.copy()
question_id = game.choose_next_question(rank_target_objects, target_objects ,tmp_weights, asked_questions, questions, initial_questions)
if( question_id == -1):
choosen = game.guess(rank_target_objects, target_objects)
if(choosen == None):
weights = add_target(asked_questions, target_objects, weights, questions, question_features, asked_questions_data, answer_target_data)
break
print( 'Estas pensando en: ' + choosen['name'])
success = input('Es correcto (y/n) : ').strip()
if(success == 'y'):
game.learn(asked_questions, choosen['id'], weights, asked_questions_data, answer_target_data)
else:
weights = add_target(asked_questions, target_objects, weights, questions, question_features, asked_questions_data, answer_target_data)
break
elif( question_number >= 5 and game.check_finish(rank_target_objects, target_objects) ):
choosen = game.guess(rank_target_objects, target_objects)
if(choosen == None):
weights = add_target(asked_questions, target_objects, weights, questions, question_features, asked_questions_data, answer_target_data)
break
print( 'Estas pensando en: ' + choosen['name'])
success = input('Es correcto (y/n) : ').strip()
if(success == 'y'):
game.learn(asked_questions, choosen['id'], weights, asked_questions_data, answer_target_data)
break
else:
rank_target_objects[choosen['id']] = -float('inf')
continuar = input('continuar:')
if continuar == 'n':
weights = add_target(asked_questions, target_objects, weights, questions, question_features, asked_questions_data, answer_target_data)
break
print('asked questions:')
print(asked_questions)
print('question_id: %d' %(question_id) )
print('rank:')
game.print_top(rank_target_objects, target_objects)
question_number += 1
print('Pregunta ' + str(question_number) + ': '+ questions[question_id] + '?' )
answer = int(input().strip())
game.update_local_weights(question_id, answer, asked_questions, tmp_weights, rank_target_objects)
if answer != 0:
question_count += 1
answer = input('Seguir jugando (y/n): ')
if answer == 'n':
break
if __name__ == '__main__' :
main()