Exemple #1
0
def on_message(message):
    if message.channel.name != 'infocom':
        return

    if message.content.startswith('!play'):
        parts = message.content.split(' ')
        if len(parts) >= 2:
            t = tp.TextPlayer(parts[1])
            if message.channel.id in sessions:
                try:
                    sessions[message.channel.id]['tp'].quit()
                except:
                    sessions.pop(message.channel.id, None)
            start_info = t.run()

            if not t.game_loaded_properly:
                yield from client.send_message(message.channel, "game failed to start")
                return

            sessions[message.channel.id] = { 'tp': t }
            yield from send_text_with_reactions(message.channel, start_info)
        else:
            games = [os.path.basename(x) for x in glob.glob('./textplayer/games/*.z*')]
            games.sort()
            yield from client.send_message(message.channel, "need a game name: " + ', '.join(games))

    if message.content.startswith('.'):
        if not message.channel.id in sessions:
            yield from client.send_message(message.channel, "no game started, start one with !play (name)")
            return
 
        yield from send_command(message.channel, message.content[1:])
Exemple #2
0
def evaluate(path):
    t = tp.TextPlayer(sys.argv[1], directory)
    t.run()
    n = len(path)
    score = 0
    first = 0
    i = 0
    for i, value in enumerate(path):
        try:
            t.execute_command(value)
            got_score = t.get_score()
            if got_score is None:
                break
            new_score, possible_score = got_score
            if new_score != score:
                first = i
                score = new_score
            print '\r{0}: {1}%, score: {2} / {3}'.format(
                filename, (i + 1) * 100 / n, score, possible_score),
        except IOError:
            break
    try:
        t.quit()
    except:
        pass
    return score, i
Exemple #3
0
def train_agent(game_chosen,game,run,pop_size,generations):
	"""	
	function to run the game

	   Args:
			game_chosen (str): name of the game
			game(str): folder name
			run(int): folder name
			pop_size: population size for the SNES algorithm
			generations: number of generations for the SNES algorithm
	"""

	# start the desired game file
	textPlayer = tP.TextPlayer(game_chosen)

	# initialize a list to keep track of the game scores
	track_scores = []

	# set last score to zero
	last_score = 0

	# initialize the agent
	agent = Neuroagent_agent.NeuroAgent()

	# return the original weights of the neural network structure in the neuroevolution
	initial_weights = agent.agent_return_weights()

	state = textPlayer.run()
	last_state = state

	# pass variables to SNES
	snes = SNES(initial_weights, 1, pop_size)

	# start the timer
	start = timer()


	# iterate through number of generations
	for i in range(0, generations):
		asked = snes.ask()
		told = []
		j = 0
		for asked_j in asked:
			# use SNES to set the weights of the NN
			last_state = state
			action = agent.take_action(state,asked_j)
			state = textPlayer.execute_command(action)
			print('{0} >>> {1}'.format(action,state))
			print('This is Population No. {0} of Generation no. {1}'.format(j,i))
			if textPlayer.get_score() != None:
				score, possible_score = textPlayer.get_score()
				# if previous state is equal to current state, then agent reward gets deducted by value of -0.2 otherwise reward multiplies by a factor of * 1.2
				reward = score - last_score
				if last_state == state:
					agent_reward = reward - 0.2
				else:
					agent_reward = reward * 1.2
				told.append(agent_reward)
				last_score = score
				agent.update(reward)
				accumulated_reward = agent.get_total_points_earned()
				print  ('Your overall score is {0} and you gained reward of {1} in the last action and agent reward of {2}'.format(accumulated_reward,reward,agent_reward))
				track_scores.append((state,j,i,action,agent_reward,reward,accumulated_reward))
			else:
				#in case the game cant retrieve the score from the game engine, set the score for that generation to 0
				score = 0
				# if previous state is equal to current state, then agent reward gets deducted by value of -0.2 otherwise reward multiplies by a factor of * 1.2
				reward = score - last_score
				if last_state == state:
					agent_reward = reward - 0.2
				else:
					agent_reward = reward * 1.2
				told.append(agent_reward)
				last_score = score
				agent.update(reward)
				accumulated_reward = agent.get_total_points_earned()
				print  ('Your overall score is {0} and you gained reward of {1} in the last action and agent reward of {2}'.format(accumulated_reward,reward,agent_reward))
				track_scores.append((state,j,i,action,agent_reward,reward,accumulated_reward))
			j += 1
		snes.tell(asked,told)
		save(i,track_scores,game,run)
		tokens, labels = agent.agent_return_models()
		save_tokens_labels_words_seen(i, tokens, labels,game,run)

	# at end of training
	# pass the final weights to the neural network structure
	snes_centre_weights = snes.center
	agent.pass_snes_centre_weight(snes_centre_weights)

	word_seen = agent.agent_return_word_seen()

	# save the scores
	with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/track_scores_'+game+'_pop50gen500.pkl', 'wb') as f:
		pickle.dump(track_scores, f)

	# save the words seen in the game
	with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/words_seen_'+game+'_pop50gen500.pkl', 'wb') as h:
		pickle.dump(word_seen, h)
	with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/test_word_seen_vectors_'+game+'_pop50gen500.pkl', 'wb') as i:
		pickle.dump(tokens, i)
	with open('./completed_runs/'+game+'/finalruns/run_'+str(run)+'/test_word_seen_vocab_'+game+'_pop50gen500.pkl', 'wb') as k:
		pickle.dump(labels, k)

	# end the timer
	end = timer()
	print('The time taken to run the traning is {0}'.format(end - start))
	textPlayer.quit()
        command_output = t.execute_command('go west')
        print command_output
        command_output = t.execute_command('go east')
        print command_output
        command_output = t.execute_command('go east')
        print command_output
        command_output = t.execute_command('go east')
        print command_output
        command_output = t.execute_command('look')
        print command_output
        game_num += 1

'''
# For running commands on a single game

t = tp.TextPlayer('zork1.z5')
start_info = t.run()
print start_info
command_output = t.execute_command('open mailbox')
print command_output
command_output = t.execute_command('read leaflet')
print command_output
command_output = t.execute_command('go south')
print command_output
command_output = t.execute_command('go east')
print command_output
command_output = t.execute_command('open window')
print command_output
command_output = t.execute_command('enter window')
print command_output
command_output = t.execute_command('take bottle')
Exemple #5
0
import sys, os, time
import textplayer.textPlayer as tp

if len(sys.argv) < 2:
    print 'Needs more parameters. Try \'python autoplay.py zork1.z5\'.'
    print 'Available games include: ',
    game_directory = os.listdir('textplayer/games')
    for game in sorted(game_directory):
        print game,
    sys.exit()
else:
    print 'Running ' + sys.argv[1]

current_game_file = sys.argv[1]
t = tp.TextPlayer(current_game_file)

current_game_text = t.run()
print current_game_text
current_command = raw_input('>')

while current_command != 'exit':
    current_game_text = t.execute_command(current_command)
    print current_game_text
    if t.get_score() != None:
        score, possible_score = t.get_score()
        print 'Current score is: ', score
    current_command = raw_input('>')
Exemple #6
0
#		if textPlayer.get_score() != None:
#			score, __ = textPlayer.get_score()
#			reward = score - last_score
#			last_score = score
#			print  ('Your overall score is {0} and you gained reward of {1} in the last action'.format(score,reward))
#		action = raw_input('>> ')

#	textPlayer.quit()

#def train_agent(game_chosen):

#hp = hpy()  # type: object
#before = hp.heap()

# start desired game file
textPlayer = tP.TextPlayer(game_chosen)

# defining the NN structure for neurovolui
#model = Sequential()
#model.add(Dense(100,input_shape=(100,)))
#model.add(Dense(100))
#model.compile(loss="mse", optimizer="adam")
#nnw = NNWeightHelper(model)
#weights = nnw.get_weights()

#hp.setrelheap()
track_scores = []
last_score = 0
# defining population size
pop_size = 10
# defining generation size
Exemple #7
0
                ("(%s/%s) " % (i + 1, len(messages)) + messages[i]),
                visibility='unlisted')
            print('sent message %s with id %s' % (i + 1, info['id']))
    else:
        info = api.status_post(message, visibility='unlisted')
        print('sent toot with id %s' % info['id'])

    return info


if __name__ == '__main__':
    client = Mastodon(config.client_id,
                      config.client_secret,
                      config.access_token,
                      api_base_url=config.base_url)
    t = tp.TextPlayer(config.game)
    latest = sendToot(t.run(), client)
    tootTime = arrow.get(latest['created_at'])
    toWait = config.time_gap
    while True:
        time.sleep(15)
        replies = filter(
            (lambda x: x['type'] == 'mention' and
             (arrow.get(x['created_at']) - tootTime).seconds <= toWait),
            client.notifications(
            ))  # Find mentions sent in the last `toWait` seconds
        options = [
            naughty.replace(
                bs(i['status']['content'],
                   'html.parser').p.text.split('@' + config.name + ' ')[1])
            for i in replies
    tokens = nltk.word_tokenize(desc)
    tagged = nltk.pos_tag(tokens)
    nouns = [word.lower() for (word, tag) in tagged if 'NN' in tag]
    freq = list({(descriptions.frequency(word), word) for word in nouns})
    freq.sort()
    freq = freq[:min(len(freq), k)]
    return freq


def get_command():
    print '\n>>',
    return raw_input()


directory = 'textplayer/games/' if len(sys.argv) < 3 else sys.argv[2]
t = tp.TextPlayer('zork1.z5' if len(sys.argv) < 2 else sys.argv[1], directory)
start_info = t.run()
print start_info
while True:
    command = get_command()
    if command == 'look':
        desc = look()
        print desc
        #att = dict (attention.compute_weights(desc))
        #for l in least_common(desc, 20):
        #    print l[0], l[1], (att[l[1]] if l[1] in att else 1.5) ** 3
    else:
        command_output = t.execute_command(command)
        print command_output
        positive = classifier.is_response_positive(command_output)
        print 'Positive' if positive else 'Negative'
Exemple #9
0
def run(params, filename, directory, steps=2000, quiet=False):

    try:
        for k, v in params.items():
            if type(v) == np.ndarray:
                params[k] = v[0]

        if not directory.endswith('/'):
            directory += '/'

        import json
        with open('params.json', 'w') as out:
            json.dump(params, out)

        scores = open(path + 'scores', 'a')
        score = 0
        max_score = 0
        possible_score = 0
        step_taken, tmp_score = 0, 0

        t = tp.TextPlayer(filename, directory)
        start_info = t.run()

        from golovinAgent import GolovinAgent

        agent = GolovinAgent(t, start_info, params)

        if start_info is None:
            print 'start_info is None'
            t.quit()
            exit(0)
        logger.open_log(path, filename)
        noneCount = 0
        for i in range(steps):
            command, command_type, response, additional = agent.makeAction()
            step_taken += 1
            if not quiet:
                print 'map size:', len(agent.map.edges)
                print agent.desc
                print agent.inv.text
                print command
                print response
            logger.log('description', agent.desc)
            logger.log('inventory', agent.inv.text)
            logger.log(command_type, command)
            logger.log('response', response)

            # tscore = t.get_score()
            # if tscore is not None:
            #     (score, possible_score) = tscore
            #     if score > max_score:
            #         agent.save_path()
            #     max_score = max(max_score, score)
            #     if not quiet:
            #         print("Score:", score, "Possible score:", possible_score)

            #     logger.log('score', str(score) + ' ' + str(possible_score))
            #     noneCount = 0
            # else:
            #     noneCount += 1
            #     if noneCount > 10:
            #         break
            print("--- ", command, response)
            if "*** The End ***" in response:
                tmp_score = 1
                break
            elif "*** You lost! ***" in response:
                tmp_score = -1
                break

            if quiet:
                print '\r{0}: {1}%, score: {2} / {3}'.format(
                    filename, (i + 1) * 100 / steps, score, possible_score),

        if not quiet:
            agent.map.update()
            agent.map.print_all()
            for _ in range(5):
                agent.map.update()
                print 'number of places:', len(agent.map.edges)
            for k, v in agent.places.items():
                print k
                print 'score:', v.score()

        if score != max_score:
            agent.run_best_path()
            tscore = t.get_score()
            if tscore is not None:
                (score, possible_score) = tscore
                max_score = max(max_score, score)
                if not quiet:
                    print("Score:", score, "Possible score:", possible_score)

        t.quit()

    except IOError:
        if not quiet:
            print "IOError"
        pass

    scores.write("{3} {0} (max {2}) / {1} (step {4}, score {5})\n".format(
        score, possible_score, max_score, filename, step_taken, tmp_score))
    if quiet:
        print '\r{0}: finished, score: {1} / {3}, max score: {2} / {3} (step {4}, score {5})'.format(
            filename, score, max_score, possible_score, step_taken, tmp_score)
    else:
        print 'final score:', score
        print 'max score:', max_score

    return (score + max_score) / 2.0