Exemplo n.º 1
0
class Snake:
    def __init__(self):
        self.reset()
        self.brain = Brain()
        self.experience_buffer = []

    # Adds a SARS datapoint to the temporary memory.
    def remember(self, state, action, reward, next_state, final_state):
        self.experience_buffer.append(
            (state, action, reward, next_state, final_state))

    def percept_state(self, state):
        q_values = self.brain.predict(state)
        action = np.argmax(q_values)
        return action

    def reset(self):

        self.direction = random.choice(ACTIONS[1:])
        self.head_position = np.copy(START_POS)
        self.body_position = np.vstack(
            [self.head_position - i * self.direction for i in range(1, 5)])
        self.growing = False

    def grow(self):
        self.growing = True

    def die(self):

        for experience in self.experience_buffer:
            self.brain.add_memory(experience)

        self.experience_buffer = []

        self.brain.learn()

    def turn(self, action):
        if action == 0:  #Do nothing -action
            return
        else:
            direction = ACTIONS[action]

            if not np.all(direction == -self.direction):
                self.direction = direction

    def update(self, action):
        self.turn(action)

        if self.growing:
            self.body_position = np.append([self.head_position],
                                           self.body_position,
                                           axis=0)
            self.growing = False
        else:
            self.body_position[1:, :] = self.body_position[:-1, :]
            self.body_position[0, :] = self.head_position

        self.head_position += self.direction
Exemplo n.º 2
0
class Juna(TestBot):
    def __init__(self, channel, nickname, server, port=6667):
        self.brain = Brain()
        self.message_que = []
        self.my_que = []
        self.topic_que = []
        TestBot.__init__(self, channel, nickname, server, port)

    def on_pubmsg(self, c, e):
        incoming_message = unicode(e.arguments()[0], 'utf-8')
        sentence = self.brain.learn(incoming_message)
        if sentence: self.message_que.append(incoming_message)
        if len(self.message_que) > 10: self.message_que.pop(0)
        self.speak(c)
        """
        print '---pubq---'
        for pubq in self.message_que:print pubq
        print '---myq---'
        for myq in self.my_que:print myq
        """
        return

    def speak(self, c):
        """Speak something!
        """
        final_output = self.brain.getMessageString(self.message_que,
                                                   self.my_que, self.topic_que)
        if final_output:
            c.privmsg(self.channel, final_output[0].encode('utf-8'))
            message_que = final_output[1]
            my_que = final_output[2]
            topic_que = final_output[3]
Exemplo n.º 3
0
        # We should have 3 for occurence where second word = 名前
        word = Word.byAppeared_name('名前')
        hits = Markov.select(Markov.q.second_wordID == word.id)
        check = list(hits)[0]
        self.assertEquals(check.occurence, 3)

    def testMarkovLexDuplicates(self):
        # We should only have 3 instance where second word = 名前
        word = Word.byAppeared_name('名前')
        hits = MarkovLex.select(MarkovLex.q.second_lexID == word.main_type.id)
        check = list(hits)
        self.assertEquals(len(check), 3)

    def testMarkovLexOccurence(self):
        # We should have 3 for occurence where second word = 名前
        word = Word.byAppeared_name('名前')
        hits = MarkovLex.select(MarkovLex.q.second_lexID == word.main_type.id)
        check = list(hits)[0]
        self.assertEquals(check.occurence, 3)


if __name__ == '__main__':
    brain = Brain()
    phrase = [u'私の名前は中野です', u'私の名前は中野です', u'私の名前は中野です']
    for p in range(len(phrase)):
        #We learn the phrase 3 times
        print 'pase:%d' % p
        brain.learn(phrase[p])
    unittest.main()

Exemplo n.º 4
0
    while not is_done:
        activated_inputs = []
        if observation[2] > 0:
            activated_inputs.append(1)
        else:
            activated_inputs.append(0)

        if observation[3] > 0:
            activated_inputs.append(3)
        else:
            activated_inputs.append(2)

        output = brain.think(activated_inputs)
        if output is None:
            action = 0
        elif output == 0:
            action = 1

        observation, reward, is_done, info = env.step(action)
        if is_done:
            reward = -0.2
        else:
            reward = 0.01
        brain.learn(reward)
        env.render()
        steps += 1

    brain.terminate_episode()

    print(steps)