예제 #1
0
def get_actual(guess):
	"""
	Returns the object the human player was thinking of
	"""

	global _objects
	yn = interface.ask("My guess is %s. Was I right? " % guess.name)

	if yn == "yes":
		obj_name = guess.name
		obj_id = guess.id
	else:
		if robot():
			obj_name = robot().ask_object()

		else:
			print "\nObject names:\n"
			for j in range(len(_objects)):
				print _objects[j].name
			obj_name = raw_input("\nWhat was your object? Remember to type it exactly as you saw above. ")
		while True:
			check = False
			for i in range(len(_objects)):
				if _objects[i].name == obj_name:
					check = True
					obj_id = _objects[i].id
					break
			if check == True:
				break
			obj_name = raw_input("It seems as though you mistyped. Please try typing the name of your object again. ")
	return obj_id, obj_name
예제 #2
0
def get_actual(guess):
	"""
	Returns the object the human player was thinking of
	"""

	global _objects
	answer = interface.ask("My guess is %s. Was I right? " % guess.name)

	if answer:
		obj_name = guess.name
		obj_id = guess.id
	else:
		if robot():
			obj_name = robot().ask_object()

		else:
			print "\nObject names:\n"
			for j in range(len(_objects)):
				print _objects[j].name
			obj_name = raw_input("\nWhat was your object? Remember to type it exactly as you saw above. ")
		while True:
			check = False
			for i in range(len(_objects)):
				if _objects[i].name == obj_name:
					check = True
					obj_id = _objects[i].id
					break
			if check == True:
				break
			obj_name = raw_input("It seems as though you mistyped. Please try typing the name of your object again. ")

	return obj_id, obj_name
예제 #3
0
	def playGame(self, number_of_objects):
		"""
		Play the game
		"""

		log.info('Playing game %d', self.id)

		start = time.time()

		# Generate all image probabilities at once since this takes a little while
		if config.args.imagemodels:
			Pi = models.gen_image_probabilities(self, number_of_objects)
		else:
			#we use -1 to say that we'll skip that tag, so this skips all tags for all objects
			Pi = [[-1] * 289] * 17

		# Initialize game stats to empty
		NoOfQuestions = 0
		round_wins = 0
		round_losses = 0
		avg_win = 0
		avg_lose = 0
		question_answers = {}
		questions_asked = {}
		objlist = objects.get_all()
		# For each object in the set
		count = 0

		for i in objlist:
			if config.args.notsimulated:
				for j in range(len(objlist)):
					print objlist[j].name
				interface.say("Choose an object. Don't tell me! ")
				time.sleep(2.5)
			#TODO: ask if the person is ready???

			result, number_of_questions, answers, askedQuestions = i.playObject(self, Pi, number_of_objects)
			log.info("Game %d, object %d complete, updating stats", self.id, i.id)

			#averages are calculated later (in main) using these sums
			if result == 0: # Loss
				round_losses += 1
				avg_lose += number_of_questions
			else: # Win
				round_wins += 1
				avg_win += number_of_questions
			NoOfQuestions += number_of_questions

			# Save questions and answers for later
			questions_asked[i.id] = askedQuestions
			question_answers[i.id] = answers

			count += 1

			if config.args.notsimulated == True:
				quit = interface.ask("Would you like to quit this game early? \nThere are %d rounds left. " % (17 - count))
				if quit == "yes":
					break
		# Save results
		self._record(round_wins, round_losses, NoOfQuestions, count)

		end = time.time()
		log.info("Game %d complete (Took %ds)", self.id, int(end - start))

		return round_wins, round_losses, NoOfQuestions, avg_win, avg_lose, question_answers, questions_asked
예제 #4
0
def ask(question_id, object_we_play, game, answers, pO, Pi, objects,
        number_of_objects, answer_data):
    """
	Asks a question and updates probabilities based on the answer
	"""

    # gets probability that an answer will be yes for each of the 7 possible t-values of question/answer combos
    probabilityD = get_tval()

    # gets the correct question for the given tag
    # This is where we'll update the code to include newly-generated questions; right now the code just pulls pre-generated questions from a database.
    question_tag = tags.get(question_id)
    questions = tags.get_questions()
    question = questions[question_id - 1]

    # in not simulated game, get answer from user
    if config.args.notsimulated:
        answer = interface.ask(question + " ")

    # in simulated game, get answer from answer bank in database
    else:
        answer = answer_data[game.id - 1][object_we_play.id - 1][question_id -
                                                                 1]
        print question, answer

    answers.append(answer)

    multipliers = []

    for objectID in range(17):  # for all known objects
        # count the number of that object's descriptions that contain the tag used to formulate that question
        T = get_t(objectID + 1, question_id, number_of_objects)

        # number of yes answers to this question/object combo
        yes_answers = objects[objectID][question_id - 1][0]

        # total number of answers given for this question/object combo
        total_answers = objects[objectID][question_id - 1][1]

        # TODO: see if Dr. Nielsen has any suggestions for improving the use of the
        # image models since they reduce the accuracy at the moment

        if answer:
            K = probabilityD[T] + (yes_answers + 1) / (total_answers + 2.0)
            # if Pi is -1 then it means we're skipping the image models for that tag
            if Pi[0][question_id - 1] == -1:
                multiplier = K / 2
                multipliers.append(multiplier)
            else:
                multiplier = (K + Pi[objectID][question_id - 1]) / 3
                multipliers.append(multiplier)
        else:
            K = (1 - probabilityD[T]) + (total_answers - yes_answers +
                                         1) / (total_answers + 2.0)
            if Pi[0][question_id - 1] == -1:
                multiplier = K / 2
            else:
                multiplier = (K + 1 - Pi[objectID][question_id - 1]) / 3

        pO[objectID] *= multiplier

    for objectID in range(
            17, number_of_objects
    ):  # TODO: for all new objects/unknown tags for a new object
        # this assigns a probability to unknown objects based on the idea that
        # an object is about as likely as the rest of the objects to have the quality
        # of a certain tag - i.e. many objects are balls, but not many objects are polka dotted
        # TODO: make more sophisticated - some tags are opposites, so if we know an opposite then we
        # can take that into consideration when finding the multiplier
        multiplier = np.mean(multipliers)
        pO[objectID] *= multiplier

    # Normalize the probabilities so that all object probabilities will sum to 1
    pO /= np.sum(pO)

    # Save the questions to each answer and the updated probabilities
    with open("example.txt", "a") as myfile:
        myfile.write(str(question_tag) + " -> " + str(answer) + " \n")
        myfile.write(str(pO) + "\n")

    return pO, answers
예제 #5
0
파일: game.py 프로젝트: natalieparde/i-spy
	def playGame(self, number_of_objects):
		"""
		Play the game
		"""

		log.info('Playing game %d', self.id)

		start = time.time()

		# Generate all image probabilities at once since this takes a little while
		if config.args.imagemodels:
			Pi = models.gen_image_probabilities(self, number_of_objects)
		else:
			#we use -1 to say that we'll skip that tag, so this skips all tags for all objects
			Pi = [[-1] * 289] * 17

		# Initialize game stats to empty
		NoOfQuestions = 0
		round_wins = 0
		round_losses = 0
		avg_win = 0
		avg_lose = 0
		question_answers = {}
		questions_asked = {}
		objlist = objects.get_all()
		count = 0

		# quit variable passed to main that tells if user wants to quit
		quit = False

		# For each object in the set
		for i in objlist:
			if robot():
				robot().initGazeCounts()

			if config.args.notsimulated:
				for j in range(len(objlist)):
					print objlist[j].name
				interface.say("Choose an object. Don't tell me! ")
				time.sleep(2.5)
			#TODO: ask if the person is ready???

			result, number_of_questions, answers, askedQuestions = i.playObject(self, Pi, number_of_objects)
			log.info("Game %d, object %d complete, updating stats", self.id, i.id)

			#averages are calculated later (in main) using these sums
			if result == 0: # Loss
				round_losses += 1
				avg_lose += number_of_questions
			else: # Win
				round_wins += 1
				avg_win += number_of_questions
			NoOfQuestions += number_of_questions

			# Save questions and answers for later
			questions_asked[i.id] = askedQuestions
			question_answers[i.id] = answers

			count += 1

			if config.args.notsimulated == True:
				quit = not interface.ask("Do you want to play again?")
				# quit = interface.ask("Would you like to quit this game early? \nThere are %d rounds left. " % (17 - count))
				if quit:
					break

		# Save results
		self._record(round_wins, round_losses, NoOfQuestions, count)

		end = time.time()
		log.info("Game %d complete (Took %ds)", self.id, int(end - start))

		return round_wins, round_losses, NoOfQuestions, avg_win, avg_lose, question_answers, questions_asked, quit
예제 #6
0
파일: questions.py 프로젝트: HiLT-ISpy/ispy
def ask(question_id, object_we_play, game, answers, pO, Pi, objects, number_of_objects, answer_data):
	"""
	Asks a question and updates probabilities based on the answer
	"""

	# gets probability that an answer will be yes for each of the 7 possible t-values of question/answer combos
	probabilityD = get_tval()

	# gets the correct question for the given tag
	question_tag = tags.get(question_id)
	questions = tags.get_questions()
	question = questions[question_id - 1]

	# in not simulated game, get answer from user
	if config.args.notsimulated:
		answer = interface.ask(question + " ")

	# in simulated game, get answer from answer bank in database
	else:
		answer = answer_data[game.id-1][object_we_play.id-1][question_id-1]
		print question, answer

	answers.append(answer)

	multipliers = []

	for objectID in range(17): # for all known objects
		# count the number of that object's descriptions that contain the tag used to formulate that question
		T = get_t(objectID+1, question_id, number_of_objects)

		# number of yes answers to this question/object combo
		yes_answers = objects[objectID][question_id-1][0]

		# total number of answers given for this question/object combo
		total_answers = objects[objectID][question_id-1][1]

		# TODO: see if Dr. Nielsen has any suggestions for improving the use of the
		# image models since they reduce the accuracy at the moment

		if answer:
			K = probabilityD[T] + (yes_answers + 1)/(total_answers + 2.0)
			# if Pi is -1 then it means we're skipping the image models for that tag
			if Pi[0][question_id-1] == -1:
				multiplier = K / 2
				multipliers.append(multiplier)
			else:
				multiplier = (K + Pi[objectID][question_id-1]) / 3
				multipliers.append(multiplier)
		else:
			K = (1 - probabilityD[T]) + (total_answers - yes_answers + 1)/(total_answers + 2.0)
			if Pi[0][question_id-1] == -1:
				multiplier = K / 2
			else:
				multiplier = (K + 1 - Pi[objectID][question_id-1]) / 3

		pO[objectID] *= multiplier

	for objectID in range(17,number_of_objects): # TODO: for all new objects/unknown tags for a new object
		# this assigns a probability to unknown objects based on the idea that
		# an object is about as likely as the rest of the objects to have the quality
		# of a certain tag - i.e. many objects are balls, but not many objects are polka dotted
		# TODO: make more sophisticated - some tags are opposites, so if we know an opposite then we
		# can take that into consideration when finding the multiplier
		multiplier = np.mean(multipliers)
		pO[objectID] *= multiplier

	# Normalize the probabilities so that all object probabilities will sum to 1
	pO /= np.sum(pO)

	# Save the questions to each answer and the updated probabilities
	with open("example.txt", "a") as myfile:
		myfile.write(str(question_tag) + " -> " + str(answer)  + " \n")
		myfile.write(str(pO) + "\n")

	return pO, answers