Ejemplo n.º 1
0
 def do_setup(self, ants):
     print("In mybot1 setup")
     antLog.write_log(
         "*************************************setup**************************************",
         botMark)
     t = threading.Thread(target=self.start_client, args=(port_remote, ))
     t.start()
Ejemplo n.º 2
0
    def do_turn(self, ants):
        # loop through all my ants and try to give them orders
        # the ant_loc is an ant location tuple in (row, col) form
        antLog.write_log("turn %d" % self.test_num, botMark)
        self.queue.empty()

        # generate state for entire map
        # map_state = [-2, -1]  # only for test
        # map_state.append(ants.map)
        # # self.client.sendall(pickle.dumps(map_state))
        #
        # # send Ants loc to Network
        # ants_loc = [-1, -2]
        # for ant_loc in ants.my_ants():
        #     ants_loc.append(ant_loc)
        # self.client.sendall(pickle.dumps(ants_loc))
        arraySend = [-2, -1]
        arraySend.append(ants.map)
        arraySend.append(-1)
        arraySend.append(-2)
        for ant_loc in ants.my_ants():
            arraySend.append(ant_loc)
        self.client.sendall(pickle.dumps(arraySend))

        #  wait for actions
        self.test_num += 1
        data_arr = self.queue.get(timeout=5)
        antLog.write_log(str(data_arr), botMark)
        # waite to receive action
        for index in range(len(ants.my_ants())):

            ant_loc = data_arr[index * 2]
            action = choose_action(data_arr[index * 2 + 1])
            if action is None:
                continue
            new_loc = ants.destination(ant_loc, action)
            if ants.passable(new_loc):
                # an order is the location of a current ant and a direction
                ants.issue_order((ant_loc, action))

            # check if we still have time left to calculate more orders
            if ants.time_remaining() < 10:
                break
Ejemplo n.º 3
0
    def reset(self):
        command = ''
        self.stepNum = 1
        self.DONE = False
        self.state_queue.empty()
        self.ants_loc_queue.empty()
        if self.Env_name == 'W_1':
            command = Start_play_command % ('MyBot_1.py', ('ant_log_' + self.Env_name))
            t = threading.Thread(target=self.start_server, args=(PORT1,))
            t.start()
            print("Bot1 reStart")
        elif self.Env_name == 'W_2':
            command = Start_play_command % ('MyBot_2.py', ('ant_log_' + self.Env_name))
            t = threading.Thread(target=self.start_server, args=(PORT2,))
            t.start()
            print("Bot2 reStart")
        elif self.Env_name == 'W_3':
            command = Start_play_command % ('MyBot_3.py', ('ant_log_' + self.Env_name))
            t = threading.Thread(target=self.start_server, args=(PORT3,))
            t.start()
            print("Bot3 reStart")
        elif self.Env_name == 'W_4':
            command = Start_play_command % ('MyBot_4.py', ('ant_log_' + self.Env_name))
            t = threading.Thread(target=self.start_server, args=(PORT4,))
            t.start()
            print("Bot4 reStart")
        os.popen(command)
        tmp_state = []
        tmp_ants = []
        try:
            tmp_state = self.state_queue.get(block=True, timeout=5)
            tmp_state = np.array(tmp_state)
            antLog.write_log('receive state ', self.Env_name)
            tmp_ants = self.ants_loc_queue.get(block=True, timeout=6)
            antLog.write_log('receive ants ', self.Env_name)
        except Exception as err:
            self.connection.close
            antLog.write_log('receive exception in reset', self.Env_name)
            self.DONE = True

        return tmp_state, tmp_ants, self.DONE
Ejemplo n.º 4
0
    def step(self, actions):

        next_state = None
        next_ants = None
        antLog.write_log('send to Ant %s' % str(actions), self.Env_name)
        self.connection.sendall(pickle.dumps(actions, protocol=2))
        try:
            next_state = self.state_queue.get(timeout=5)
            next_state = np.array(next_state)
            antLog.write_log('receive state ', self.Env_name)
            next_ants = self.ants_loc_queue.get(timeout=1)
            antLog.write_log('receive Ants ', self.Env_name)
        except Exception as err:
            self.DONE = True
        # print("next_ants = ", next_ants)
        if not self.DONE:
            reward = len(next_ants) / self.stepNum
            self.stepNum += 1
        else:
            reward = 0
        # send action to ant
        return next_state, next_ants, reward, self.DONE