Example #1
0
def move():
    data = request.json
    board = Board(data["board"])
    brain = Brain(data["you"]["id"], board)

    decision = brain.get_decision()
    print(decision)
    return api.move_response(decision)
Example #2
0
 def __init__(self, dna="#", num_acts=3):
     self.dna = dna
     self.dna_cost = C.COST_HEAD * C.COST_FACTOR
     self.body = self.build_body()
     self.num_actions = num_acts
     self.craneum = Brain()
     self.perception_shape = None
     self.energy = C.INITIAL_ENERGY
     self.day_reward = 0
     self.overall_reward = 0
 def think_should_have_correct_premise_values(self):
   brain = Brain(["a"], [[[0.1]],[[0.1]],[[0.1]]])
   with self.assertRaises(TypeError):
     brain.think(0.1)
   with self.assertRaises(TypeError):
     brain.think(["a"])
   with self.assertRaises(TypeError):
     brain.think([0.1,"a"])
   with self.assertRaises(ValueError):
     brain.think([0.1,0.2])
Example #4
0
def move():
    data = request.json
    print('Move data:')
    pprint(data)
    board = Board(data["board"])
    brain = Brain(data["you"]["id"], board)

    decision = brain.get_decision()
    print("Turn: ", data["turn"], "; snake_id: ", data["you"]["id"],
          "; made the decision to go ", decision)
    return api.move_response(decision)
 def __init__(self, mic, profile, iot_client):
     """
     初始化
     :param mic:
     :param persona:
     :param profile:
     :param iot_client:
     """
     self._logger = logging.getLogger()
     self.text_mode = False
     self.mic = mic
     self.profile = profile
     self._logger.debug(mic)
     self.brain = Brain(mic, profile, iot_client)
     self._is_server_listen_thread = self.mic.is_server_listen_thread
 def test_get_plugins(self):
     """
     获取插件
     :return:
     """
     plugins = Brain.get_plugins()
     seg_list = jieba.cut("帮我把窗帘打开", cut_all=False)
     print(list(seg_list))
Example #7
0
 def get_brain(self, snakes, foods, width):
     board = Board({
         "snakes": snakes,
         "food": foods,
         "width": width,
         "height": width,
     })
     return Brain(board.snakes[0].id, board)
Example #8
0
    def __init__(self):
        self.screen_size = (500, 300)
        self.FPS = 60
        self.fps_clock = pygame.time.Clock()

        self.world = World(self.screen_size)  # generate the world
        self.world.generate()  # populate the world

        self.collider = Collider(self.world.objects, self.world.walls,
                                 self.screen_size)

        # print(self.world.objects)

        self.win_surf = pygame.display.set_mode(self.screen_size, 0, 32)
        pygame.display.set_caption('Ant-1!')

        self.renderer = Renderer(self.win_surf, self.world, self.fps_clock)

        self.brain = Brain()
        self.reward_total = 0
        self.reward_previous = 0
Example #9
0
  def learnAndTestPredictions(self, input_frames):
    """Teach brain with input_frames and make sure predicted frames match."""
    b = Brain(num_layers=2, neurons_in_leaf_layer=256)

    # Empty 2D array is always first predicted frame because we have nothing to learnAndTestPredictions on.
    empty_frame = numpy.zeros(
      (b.layers[0].height, b.layers[0].width), dtype=numpy.int32)
    predicted_frames = []
    input_frames = map(lambda flat_arr: numpy.array(flat_arr), input_frames)
    actual_layer_frames = [[] for _ in range(b.num_layers)]
    predicted_layer_frames = [[] for _ in range(b.num_layers)]

    def record():
      if self.AUTOMATED_TEST:
        return

      for layer_index, layer in enumerate(b.layers):
        actual_layer_frames[layer_index].append(
          b.layers[layer_index].state().tolist())

        predicted_layer_frames[layer_index].append(
          b.layers[layer_index].predict().tolist())

    # Learn the sequence.
    for frame in input_frames:
      b.perceive(frame, learn=True)
      record()

    # Let some time pass so brain doesn't predict replay.
    for i in range(Neuron.MAX_HISTORY):
      b.perceive(empty_frame, learn=False)
      record()

    for index, frame in enumerate(input_frames):
      # Now that we've learned, test predictions.
      b.perceive(frame, learn=False)
      prediction = b.predict().tolist() # Bottom layer prediction.
      record()
      #      print index
      #      print frame
      #      print numpy.array(prediction)
      predicted_frames.append(prediction)
      if self.AUTOMATED_TEST and prediction != input_frames[index + 1]:
        expected_prediction = numpy.array(input_frames[index + 1])
        actual_prediction = numpy.array(prediction)
        self.fail('Frame: ' + str(index + 1) + ' doesn\'t match prediction.\n\n' +
                  'Predicted:\n' + str(actual_prediction) + '\n\n' +
                  'Got:\n' + str(expected_prediction))
    src.util.writeFrames(actual_layer_frames, 'actual', self.curr_test_name)
    src.util.writeFrames(predicted_frames, 'predicted', self.curr_test_name)
Example #10
0
 def load_anatomy(self, path):
     try:
         temp_anatomy = Brain(path)
     except IOError:
         return path + " does not exist"
     except:
         return path + " could not be opened. It might be corrupted"
     if len(temp_anatomy.shape) != 3:
         return "The data has " + str(len(
             temp_anatomy.shape)) + " dimensions instead of 3"
     else:
         self.anatomy = temp_anatomy
         return None
Example #11
0
 def load_sequence(self, path):
     """ Load the sequence from the path. Returns an error message if something went wrong, otherwise None """
     try:
         temp_brain = Brain(path)
     except IOError:
         return path + " does not exist"
     except:
         return path + " could not be opened. It might be corrupted"
     if len(temp_brain.shape) != 4:
         return "The data has " + str(len(
             temp_brain.shape)) + " dimensions instead of 4"
     elif self.mask and self.mask.shape != temp_brain.shape[0:3]:
         return "The EPI sequence is not the same size as the mask"
     elif self.stimuli and self.stimuli.data[-1, 0] > temp_brain.images:
         return "The EPI sequence is too short compared to the times in the stimuli file"
     else:
         self.brain = temp_brain
         return None
Example #12
0
    # RECEIVING 1ST MAP (MAP)
    commande4 = get_command(sock)
    if commande4 != "MAP":
        raise ValueError("Erreur protocole: attendu MAP (cote client)")
    else:
        map_infos = get_map(sock)
        print("Received first map! : ", map_infos, "\n")

    # Initialize map with initial coords and map
    new_map = Map(vampires=[], werewolves=[], humans=[], size_x=m, size_y=n)
    new_map.initialize_map(map_infos)
    team = new_map.find_grp(initial_x, initial_y)


    # Initialize
    brain = Brain(new_map, team[1])

    while True:
        commande5 = get_command(sock)
        if commande5 not in ["UPD", "END"]:
            raise ValueError("Erreur protocole: mauvaise commande reçue.")

        elif commande5 == "END":
            break

        elif commande5 == "UPD":
            # get updates
            numbers = number_of_changes(sock)
            changes = get_changes(sock, numbers)
            if len(changes) > 0:
                new_map.update_map(changes)
class Conversation:
    """
    交谈
    """
    def __init__(self, mic, profile, iot_client):
        """
        初始化
        :param mic:
        :param persona:
        :param profile:
        :param iot_client:
        """
        self._logger = logging.getLogger()
        self.text_mode = False
        self.mic = mic
        self.profile = profile
        self._logger.debug(mic)
        self.brain = Brain(mic, profile, iot_client)
        self._is_server_listen_thread = self.mic.is_server_listen_thread

    def is_proper_time(self):
        """
        是否是适当的交谈时间
        :return:
        """
        return True

    def handle_forever(self):
        """
        持续处理
        :return:
        """
        if self._is_server_listen_thread:  # 监听服务器端从MQTT发送来的消息

            def on_message(client, userdata, msg):
                self._logger.info("从服务器监听到MQTT消息: " + msg.topic + " message" +
                                  str(msg.payload))
                logger.send_conversation_log(
                    iot_client=self.mic.iot_client,
                    mic='server',
                    content=msg.payload.decode('utf8'),
                    speaker='user')
                if 'mic_text_from_server' in msg.topic:
                    self.send_to_brain(
                        msg.payload.decode('utf8'))  # 订阅到的消息发送到大脑处理

            self._logger.info('进入server listen handle循环')
            self.mic.iot_client.mqttc.on_message = on_message

        else:
            while True:
                # Print notifications until empty  处理邮件等通知
                # notifications = self.notifier.getAllNotifications()
                # for notif in notifications:
                #     self._logger.info("Received notification: '%s'", str(notif))

                self._logger.debug("Started listening for keyword")
                threshold, transcribed = self.mic.passive_listen()
                self._logger.debug("Stopped listening for keyword")

                if not transcribed or not threshold:
                    self._logger.info("Nothing has been said or transcribed.")
                    continue

                self._logger.debug(
                    "Started to listen actively with threshold: %r", threshold)
                input_content = self.mic.active_listen()
                self._logger.debug(
                    "Stopped to listen actively with threshold: %r", threshold)
                self.send_to_brain(input_content)

    def send_to_brain(self, input_content):
        if input_content:
            self.brain.query(input_content)
        else:
            self.mic.say("")
  def think_should_return_output(self):
    brain = Brain(["a"], [[[0.1]],[[0.1]],[[0.1]]])
    value = brain.think([0.1])
    self.assertTrue(type(value) is float)
    self.assertTrue(value < 1)
    self.assertTrue(value > 0)

    brain = Brain(["a"], [[[1.0]],[[0.5]],[[1.0]]])
    value = brain.think([1.0])
    self.assertTrue(round(value,2) == 0.35)
    
    brain = Brain(["a"], [[[0.5],[0.5]],[[0.5,0.5],[0.5,0.5]],[[0.5,0.5]]])
    value = brain.think([1.0])
    self.assertTrue(round(value,2) == 0.41)
    
    # Only one input but 2 weights for inputs
    brain = Brain(["a"], [[[0.5,0.5]]])
    with self.assertRaises(ValueError):
      value = brain.think([1.0])

    # One level network with 4 inputs
    values = [
      [list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4))],
      [list(float(random.uniform(0,1)) for x in range(4))]
    ]
    brain = Brain(["a","b","c","d"], values)
    value = brain.think(list(float(random.uniform(0,1)) for x in range(4)))
      
    # Two level network with 4 inputs
    values = [
      [list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4))],
      [list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4)), list(float(random.uniform(0,1)) for x in range(4))],
      [list(float(random.uniform(0,1)) for x in range(4))]
    ]
    brain = Brain(["a","b","c","d"], values)
    value = brain.think(list(float(random.uniform(0,1)) for x in range(4)))
    print(value)
      
Example #15
0
class Trilobit:
    def __init__(self, dna="#", num_acts=3):
        self.dna = dna
        self.dna_cost = C.COST_HEAD * C.COST_FACTOR
        self.body = self.build_body()
        self.num_actions = num_acts
        self.craneum = Brain()
        self.perception_shape = None
        self.energy = C.INITIAL_ENERGY
        self.day_reward = 0
        self.overall_reward = 0

    def is_dead(self):
        return self.energy <= 0

    def get_perception_shape(self):
        if self.perception_shape is None:
            eyes_pos = np.argwhere(self.body == C.EYES)
            head_pos = np.argwhere(self.body == C.HEAD)
            inputs = [((0, 0), (5, 5))]
            for r, c in eyes_pos:
                print("Warning, multiple inputs not supported yet")
            self.perception_shape = inputs
            return inputs
        return self.perception_shape

    def set_perception(self, perception):
        """
        Set the model inputs to the current perception of the agent.
        :param perception: The array with shape self.perception_shape representing the current perception
        :return: None
        """
        self.craneum.init_perception(perception[0])

    def reset_state(self):
        """
        Reset the agents state to have full energy and zero reward.
        :return: None
        """
        self.energy = C.INITIAL_ENERGY
        self.day_reward = 0

    def act(self):
        a_index = self.craneum.decision()
        action_const = C.ACTIONS[a_index]
        if action_const == C.ROTATE:
            self.energy -= 0.5 * self.dna_cost
        elif action_const == C.BACKWARD:
            self.energy -= 0.75 * self.dna_cost
        return action_const

    def react(self, reward):
        """
        Agent reacts to the action it just took. Its energy is updated and the reward imprinted in its memory.
        :param reward: The reward for the action taken, can be positive or negative.
        :return: False if the agent is dead as a result. True otherwise.
        """
        self.energy += reward
        self.energy -= self.dna_cost
        self.craneum.remember_reward(reward)
        self.day_reward += reward
        if self.energy < 0:
            return False
        return True

    # TODO: remove if unneeded.
    # def passive_react(self, reward):
    #     """
    #     When a passive agent is intersected by a moving agent, the resulting reward (or punishment) is imprinted into
    #     its memory.
    #     :param reward:
    #     :return:
    #     """
    #     # print(f"{self.dna} got intersected!")
    #     self.energy += reward
    #     self.craneum.remember_reward(reward)

    def dream(self):
        self.overall_reward = 0.05 * self.day_reward + (
            1 - 0.05) * self.overall_reward  # todo: why negative?
        self.craneum.dream()

    def grow_gene(self, idx_dna, cell_dict, gene_pos):
        idx_dna += 1
        if idx_dna >= len(self.dna):
            return idx_dna
        gene = self.dna[idx_dna]
        if C.CELL_DICT[gene] == C.EMPTY or (gene_pos in cell_dict):
            return idx_dna
        cell_dict[gene_pos] = gene
        if C.CELL_DICT[gene] == C.EYES or C.CELL_DICT[gene] == C.FEED:
            return idx_dna
        for i in range(4):
            ori = C.ORIENTATIONS[i]
            child_gene_pos = (gene_pos[0] + ori[0], gene_pos[1] + ori[1])
            idx_dna = self.grow_gene(idx_dna, cell_dict, child_gene_pos)
        return idx_dna

    def build_body(self):
        gene_pos = (0, 0)
        cell_dict = {}
        self.grow_gene(-1, cell_dict, gene_pos)
        return self.array_body(cell_dict)

    def array_body(self, cell_dict):
        max_r = max([k[0] for k in cell_dict.keys()])
        min_r = min([k[0] for k in cell_dict.keys()])
        max_c = max([k[1] for k in cell_dict.keys()])
        min_c = min([k[1] for k in cell_dict.keys()])
        rows = max_r - min_r + 1
        cols = max_c - min_c + 1
        body = np.zeros((rows, cols), dtype=np.int8)
        for k, v in cell_dict.items():
            r = k[0] - min_r
            c = k[1] - min_c
            body[r, c] = C.CELL_DICT[v]
        for i, cell_type in enumerate(C.CELLS):
            n_cells = np.count_nonzero(body == cell_type)
            self.dna_cost += n_cells * C.CELLS_COST[i] * C.COST_FACTOR
        return body
Example #16
0
 def test_get_voxel_size(self):
     ref = Brain('src/tests/test-data/brain.nii')
     self.assertEqual((1.0, 1.0, 1.0, 1.0), ref.get_voxel_size())
Example #17
0
 def test_brain(self, mock_nib):
     Brain('src/tests/test-data/brain.nii')
     mock_nib.load.assert_called_with('src/tests/test-data/brain.nii')
Example #18
0
class Game():
    def __init__(self):
        self.screen_size = (500, 300)
        self.FPS = 60
        self.fps_clock = pygame.time.Clock()

        self.world = World(self.screen_size)  # generate the world
        self.world.generate()  # populate the world

        self.collider = Collider(self.world.objects, self.world.walls,
                                 self.screen_size)

        # print(self.world.objects)

        self.win_surf = pygame.display.set_mode(self.screen_size, 0, 32)
        pygame.display.set_caption('Ant-1!')

        self.renderer = Renderer(self.win_surf, self.world, self.fps_clock)

        self.brain = Brain()
        self.reward_total = 0
        self.reward_previous = 0

    def run(self, max_frames=3e+2):

        # -------------------------------- to re-initialize world
        self.FPS = 60
        self.fps_clock = pygame.time.Clock()

        self.world = World(self.screen_size)  # generate the world
        self.world.generate()  # populate the world

        self.collider = Collider(self.world.objects, self.world.walls,
                                 self.screen_size)

        # print(self.world.objects)

        self.win_surf = pygame.display.set_mode(self.screen_size, 0, 32)
        pygame.display.set_caption('Ant-1!')

        self.renderer = Renderer(self.win_surf, self.world, self.fps_clock)

        self.reward_total = 0
        self.reward_previous = 0
        # ----------------------------------------

        frame_counter = 0
        eps_start = .99
        eps_end = .3
        eps_steps = 1e4

        phero = 'pheromones'
        rendering = True

        while frame_counter < max_frames + 1:
            if frame_counter % 500 == 0:
                delta_reward = self.reward_total - self.reward_previous
                print("frame #{} | total reward: {} | Delta: {}".format(
                    frame_counter, self.reward_total, delta_reward))
                self.reward_previous = self.reward_total

            # main game loop
            dt = self.fps_clock.tick(self.FPS)
            # dt = self.fps_clock.tick()
            frame_counter += 1
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.terminate()
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.terminate()
                    elif event.key == K_r:
                        rendering = not rendering
                    # elif event.key == K_p:
                    # self.pause_game()
                    elif event.key == K_LEFT:
                        if phero == 'pherofoods':
                            phero = 'pheromones'
                            print('Displaying pheromones')
                        else:
                            phero = 'pherofoods'
                            print('Displaying pherofoods')
                    elif event.key == K_RIGHT:
                        if phero == 'pherofoods':
                            phero = 'pheromones'
                            print('Displaying pheromones')
                        else:
                            phero = 'pherofoods'
                            print('Displaying pherofoods')
                #     elif event.key == K_SPACE:
                #         print('space')

            if frame_counter >= eps_steps:
                epsilon = eps_end
            else:
                epsilon = eps_start + frame_counter * (eps_end -
                                                       eps_start) / eps_steps

            for ant in self.world.ants:
                ant.update_epsilon(epsilon)
                ant.reinitialize_r()
                ant.update_state(self.world.get_state(ant))
                # print(ant.s)
                p = self.brain.predict_p(ant.s)[0]
                ant.update_p(p)

                (x, y) = ant.pos
                if x < 0:
                    x = 0
                elif x > self.screen_size[0]:
                    x = self.screen_size[0]

                if y < 0:
                    y = 0
                elif y > self.screen_size[1]:
                    y = self.screen_size[1]

                ant.pos = (x, y)

                # ant.pos[0] = np.clip(ant.pos[0], 0, self.screen_size[0])
                # ant.pos[1] = np.clip(ant.pos[1], 0, self.screen_size[1])

            # self.world.update(dt * 1)
            self.world.update(60)
            self.collider.update()
            if rendering:
                self.renderer.render(phero)
                # if frame_counter % 100 == 0:
                #     self.renderer.render(phero)

            for ant in self.world.ants:
                ant.update_state_(self.world.get_state(ant))

                s, a, r, s_ = ant.get_info()
                self.reward_total += r
                a_cats = np.zeros(NUM_ACTIONS)
                a_cats[a] = 1
                self.brain.train_push(s, a_cats, r, s_)

            self.brain.optimize()

    def run_episodes(self, num_episodes=1, num_frames=1e4):
        for episode in range(num_episodes):
            print('-' * 50)
            print('Episode #{}'.format(episode))
            self.run(num_frames)

    def terminate(self):
        """Shut all down"""
        pygame.quit()
        sys.exit()