def evaluation_function(self, state): def cross_product(a, b): return a[0] * b[0] + a[1] * b[1] vpos_offense_agents = [] vpos_defense_agents = [] for i in range(state.n_agent): if i < state.n_agent / 2: vpos_offense_agents.append(state.get_agent_virtual_pos(i)) else: vpos_defense_agents.append(state.get_agent_virtual_pos(i)) vpos_basket = state.get_basket_virtual_pos() ret = 0 # if offense agent far from basket for i in range(len(vpos_offense_agents)): real_three_point = 6.75 if state.position.real_distance( vpos_offense_agents[i], vpos_basket) > real_three_point + 1: ret -= 10000 """ # if offense close to basket for agent_id in range(int(state.n_agent / 2)): ret += -2 * (state.basket_distance(agent_id)) """ # if offense has space for agent_id in range(int(state.n_agent / 2)): if state.is_open(agent_id): ret += 1000 return ret
def refine_possible_moves(self, state, moves): """remove those move that would result two agent run into same place""" new_moves = [] for move in moves: ok = True # can't two agent screen for same agent same_screen_agent = set() same_place = set() for agent_id_1 in move["screen"]: agent_id_2 = move["screen"][agent_id_1] if agent_id_2 in same_screen_agent: ok = False same_screen_agent.add(agent_id_2) # can't two agent be at same place, except screen_one for agent_id in range(int(state.n_agent / 2)): if agent_id in move["screen"]: continue if agent_id not in move["go"]: vpos = state.get_agent_virtual_pos(agent_id) else: vpos = move["go"][agent_id] if str(vpos) in same_place: ok = False same_place.add(str(vpos)) if ok: new_moves.append(move) return new_moves
def get_step_3_moves(self, state, move, white_list, pass_list): ret = [] further_search = False # casee 1: find one agent to go for agent_id_1 in white_list: if agent_id_1 in pass_list: continue # case 1.1 agent_id_1 choose to pass this round new_move = copy.deepcopy(move) new_white_list = copy.deepcopy(white_list) new_pass_list = copy.deepcopy(pass_list) new_pass_list.add(agent_id_1) ret.extend( self.get_step_3_moves(state, new_move, new_white_list, new_pass_list)) further_search = True # case 1.2 agent_id_1 choose to run v1 = state.get_agent_virtual_pos(agent_id_1) for v2 in state.stand_place_link[str(v1)]: new_move = copy.deepcopy(move) new_move["go"][agent_id_1] = v2 new_white_list = copy.deepcopy(white_list) new_white_list.remove(agent_id_1) new_pass_list = copy.deepcopy(pass_list) ret.extend( self.get_step_3_moves(state, new_move, new_white_list, new_pass_list)) further_search = True break if further_search: return ret else: new_move = copy.deepcopy(move) return [new_move]
def get_step_1_moves(self, state): """ Returns ------- ret: list of dict list of possible move """ ret = [] move = {} move["pass"] = {} move["screen"] = {} move["go"] = {} # build white list that could move freely white_list = set() for agent_id in range(int(state.n_agent / 2)): if agent_id == state.ball_agent_id: continue if agent_id in state.screen_one: continue white_list.add(agent_id) # case 1: not pass new_move = copy.deepcopy(move) new_white_list = copy.deepcopy(white_list) ret.extend( self.get_step_2_moves(state, new_move, new_white_list, set())) # case 2: find if someone could pass ball_agent_id = state.ball_agent_id v1 = state.get_agent_virtual_pos(ball_agent_id) for agent_id in white_list: if agent_id in state.run_one: # screen_one need to run continue v2 = state.get_agent_virtual_pos(agent_id) if v2 in state.stand_place_link[str(v1)]: new_move = copy.deepcopy(move) new_move["pass"][ball_agent_id] = agent_id new_white_list = copy.deepcopy(white_list) new_white_list.add(ball_agent_id) new_white_list.remove(agent_id) ret.extend( self.get_step_2_moves(state, new_move, new_white_list, set())) return ret
def next_move(self, agent_id, state): vpos_basket = state.get_basket_virtual_pos() vpos_offense = state.get_agent_virtual_pos(agent_id - int(state.n_agent / 2)) vpos_defense = state.get_agent_virtual_pos(agent_id) path_offence = state.get_shortest_path(vpos_offense, vpos_basket) try: path_defense = state.get_shortest_path(vpos_defense, path_offence[1]) except: path_defense = state.get_shortest_path(vpos_defense, path_offence[0]) try: dx = path_defense[1][0] - vpos_defense[0] dy = path_defense[1][1] - vpos_defense[1] except: dx = 0 dy = 0 return [dx, dy]
def get_reward(self, state, move): log_p = 0 ball_agent_id = state.ball_agent_id if len(move["pass"]) > 0: for x in move["pass"]: # only one x ball_agent_id = move["pass"][x] v1 = state.get_agent_virtual_pos(ball_agent_id) for agent_id in move["go"]: v2 = move["go"][agent_id] if v2 in state.stand_place_link[str(v1)]: if agent_id in state.run_one: log_p += math.log(self.p_screen) else: log_p += math.log(self.p_unscreen) return log_p
def get_successor_state(self, state, move): new_state = copy.copy(state) new_state.my_deep_copy() """ self.agents = copy.deepcopy(self.agents) self.screen_one = copy.deepcopy(self.screen_one) self.run_one = copy.deepcopy(self.run_one) """ if len(move["pass"]) > 0: for x in move["pass"]: # only one x new_state.ball_agent_id = move["pass"][x] new_state.screen_one = [] new_state.run_one = [] for agent_id_1, agent_id_2 in move["screen"].items(): new_state.screen_one.append(agent_id_1) new_state.run_one.append(agent_id_2) vpos = state.get_agent_virtual_pos(agent_id_2) new_state.move_agent_to(agent_id_1, vpos) for agent_id_1, vpos in move["go"].items(): new_state.move_agent_to(agent_id_1, vpos) new_state.log_p += self.get_reward(state, move) return new_state
def get_step_2_moves(self, state, move, white_list, pass_list): ret = [] further_search = False # case 1: find one agent to screen for agent_id_1 in white_list: if agent_id_1 in pass_list: continue # case 1.1 agent_id_1 choose to pass this round new_move = copy.deepcopy(move) new_white_list = copy.deepcopy(white_list) new_pass_list = copy.deepcopy(pass_list) new_pass_list.add(agent_id_1) ret.extend( self.get_step_2_moves(state, new_move, new_white_list, new_pass_list)) further_search = True # case 1.2 agent_id_1 screen for someone for agent_id_2 in white_list: if agent_id_2 == agent_id_1: continue v1 = state.get_agent_virtual_pos(agent_id_1) v2 = state.get_agent_virtual_pos(agent_id_2) if v2 not in state.stand_place_link[str(v1)]: continue new_move = copy.deepcopy(move) new_move["screen"][agent_id_1] = agent_id_2 new_white_list = copy.deepcopy(white_list) new_white_list.remove(agent_id_1) new_white_list.remove(agent_id_2) new_pass_list = copy.deepcopy(pass_list) ret.extend( self.get_step_2_moves(state, new_move, new_white_list, new_pass_list)) further_search = True # state.screen_one could also be agent_id_2 for agent_id_2 in state.screen_one: if agent_id_2 in white_list: # already process above continue if agent_id_2 == agent_id_1: continue v1 = state.get_agent_virtual_pos(agent_id_1) v2 = state.get_agent_virtual_pos(agent_id_2) if v2 not in state.stand_place_link[str(v1)]: continue print(state.screen_one) print(state.run_one) print(new_move) print(new_white_list) print(new_pass_list) new_move = copy.deepcopy(move) new_move["screen"][agent_id_1] = agent_id_2 new_white_list = copy.deepcopy(white_list) new_white_list.remove(agent_id_1) new_pass_list = copy.deepcopy(pass_list) ret.extend( self.get_step_2_moves(state, new_move, new_white_list, new_pass_list)) further_search = True break if further_search: return ret else: new_move = copy.deepcopy(move) new_white_list = copy.deepcopy(white_list) return self.get_step_3_moves(state, new_move, new_white_list, set())