def choose_question(initial_questions, objects_values, asked_questions, how_many=10): '''Returns a question with the lowest entropy.''' if initial_questions: question = initial_questions.pop(0) else: sorted_objects_values = sorted_objects_values = sort_objects_values(objects_values) if len(sorted_objects_values) <= how_many: ### possibly some proportion of the objects in the database max = len(sorted_objects_values) else: max = how_many most_likely_objects = sorted_objects_values[:max] objects = [object[1] for object in most_likely_objects] questions = model.get_questions() best_question_entropy = abs(float('inf')) best_question = None for question in questions: # loop through all the questions if not(question.id in asked_questions): # if we have not already asked it, condider it question_entropy = entropy(objects, question) if question_entropy <= best_question_entropy: best_question_entropy = question_entropy best_question = question question = best_question return question
def GET(self, object_id): '''Renders a page with all of the questions and values for a specified object_id so that it can be retrained manually.''' object = model.get_object_by_id(object_id) questions = model.get_questions() data = model.get_data_dictionary() if object: return render.retrain(object, list(questions), data) else: raise web.seeother('/') # returns to admin page
def load_initial_questions(): '''Loads questions we always want to ask as well as some random ones so that we can learn more about the objects.''' initial_questions = [] initial_questions.append(model.get_question_by_id(1)) # is character real questions = list(model.get_questions()) # converts from webpy's IterBetter to a list for i in range(2): # up to 2 initial random questions q = random.choice(questions) if not(q in initial_questions) and not(q.id in [1,6]): # real/man initial_questions.append(q) initial_questions.append(model.get_question_by_id(6)) # is the character a man return initial_questions
def choose_question (objects_values, asked_questions): bestCanidates = get_nearby_objects(objects_values, 2) object1 = bestCanidates[0].id object2 = bestCanidates[1].id questions = model.get_questions() biggest_difference = [0, 0] #[Question id, difference] for question in questions: if not(question.id in asked_questions): objectOneAnswer = model.get_value(object1, question.id) objectTwoAnswer = model.get_value(object2, question.id) difference = abs(objectOneAnswer-objectTwoAnswer) if difference > biggest_difference[1]: biggest_difference = [question.id, difference] return model.get_question_by_id(biggest_difference[0])
def GET(self): '''Lists all of the questions so that selected questions can be deleted.''' questions = model.get_questions() return render.delete_question(questions)
new_object_id = model.add_object(name) ### adds to database and trains learn(asked_questions, new_object_id) return new_object_id def learn(asked_questions, object_id): '''Updates the data for the correct object based on information in asked_questions. Also updates times played for the object and stores the playlog.''' for question in asked_questions: current_weight = model.get_value(object_id, question) if not(current_weight): current_weight = 0 new_weight = current_weight + asked_questions[question] model.update_data(object_id, question, new_weight) model.update_times_played(object_id) model.record_playlog(object_id, asked_questions, True) if __name__ == '__main__': ##### Tests entropy! ##### objects = model.get_objects() objects = [object.id for object in objects] objects = tuple(objects) questions = model.get_questions() for question in questions: print question.id print 'DAN:', simple_entropy(objects, question) print 'ANDY:', entropy(objects, question)