src_code/controllers/basic_controller_interactive.py [94:181]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def _build_inputs(self, batch, t):
        # Assumes homogenous agents with flat observations.
        # Other MACs might want to e.g. delegate building inputs to each agent
        bs = batch.batch_size
        inputs = []
        inputs.append(batch["obs"][:, t])  # b1av
        if self.args.obs_last_action:
            if t == 0:
                inputs.append(th.zeros_like(batch["actions_onehot"][:, t]))
            else:
                inputs.append(batch["actions_onehot"][:, t-1])
        if self.args.obs_agent_id:
            inputs.append(th.eye(self.n_agents, device=batch.device).unsqueeze(0).expand(bs, -1, -1))

        inputs = th.cat([x.reshape(bs*self.n_agents, -1) for x in inputs], dim=1)
        return inputs

    def _build_alone_inputs(self, batch, t):
        # Assumes homogenous agents with flat observations.
        # Other MACs might want to e.g. delegate building inputs to each agent
        bs = batch.batch_size
        inputs_alone = []
        inputs_alone.append(batch["obs_alone"][:, t])  # b1av
        if self.args.obs_last_action:
            if t == 0:
                inputs_alone.append(th.zeros_like(batch["actions_onehot"][:, t]))
            else:
                inputs_alone.append(batch["actions_onehot"][:, t-1])
        if self.args.obs_agent_id:
            inputs_alone.append(th.eye(1, device=batch.device).expand(self.n_agents, -1).unsqueeze(0).expand(bs, -1, -1))

        inputs_alone = th.cat([x.reshape(bs*self.n_agents, -1) for x in inputs_alone], dim=1)
        return inputs_alone

    def _get_input_shape(self, scheme):
        input_shape = scheme["obs"]["vshape"]
        if self.args.obs_last_action:
            input_shape += scheme["actions_onehot"]["vshape"][0]
        if self.args.obs_agent_id:
            input_shape += self.n_agents

        return input_shape

    def _get_input_alone_shape(self, scheme):
        input_alone_shape = scheme["obs_alone"]["vshape"]
        if self.args.obs_last_action:
            input_alone_shape += scheme["actions_onehot"]["vshape"][0]
        if self.args.obs_agent_id:
            input_alone_shape += 1

        return input_alone_shape

    def focus_fire_rate(self, chosen_actions, batch, t):
        self.test_total += 1
        n_actions_no_attack = 6
        #Compute focus fire rate
        target_id = th.clamp(chosen_actions - n_actions_no_attack, min=-1)
        max_id = self.args.n_actions - n_actions_no_attack
        num_agents_attack = []
        for i in range(max_id):
            num_agents_attack.append(th.sum(target_id == i).item())
        #Compute average distance
        inputs = batch["obs"][:, t]
        bs = batch.batch_size
        individual_feats_size = (self.input_shape-self.n_agents-self.input_alone_shape+1) // (self.n_agents - 1) - 4
        all_feats_size = individual_feats_size + 4
        n_enemies = (self.input_shape-individual_feats_size-self.n_agents-self.args.n_actions-all_feats_size*(self.n_agents-1)) // all_feats_size
        enemy_ally_feats = inputs[:, :, -individual_feats_size-all_feats_size*(self.n_agents-1+n_enemies):-individual_feats_size]\
                            .reshape(inputs.shape[0], inputs.shape[1], self.n_agents-1+n_enemies, -1)
        #Compute enemy
        e_shootable = (enemy_ally_feats[:, :, :n_enemies, 0] > 0).long()
        e_visible = (enemy_ally_feats[:, :, :n_enemies, 1] > 0).long()
        e_distance = enemy_ally_feats[:, :, :n_enemies, 1]
        e_average_distance = th.sum(e_distance, dim=1)/(th.sum(e_visible, dim=1) + 1e-6)
        #Compute ally
        #Compute enemy
        a_visible = (enemy_ally_feats[:, :, :n_enemies, 0] > 0).long()
        a_distance = enemy_ally_feats[:, :, :n_enemies, 1] * a_visible
        a_average_distance = th.sum(a_distance, dim=1)/(th.sum(a_visible, dim=1) + 1e-6)

        for num_attack in num_agents_attack:
            self.avg_num_agents_attack[num_attack] += 1
        self.avg_ally_distance += a_average_distance.mean().item()

        th.set_printoptions(precision=2)
        print("focus fire rate: ", self.avg_num_agents_attack/self.test_total)
        print("focus fire rate mean: ", self.avg_num_agents_attack[2:].sum()/self.test_total)
        print("average distance between agents: ", "%.2f" % (self.avg_ally_distance/self.test_total))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



src_code/controllers/basic_controller_interactive.py [329:416]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def _build_inputs(self, batch, t):
        # Assumes homogenous agents with flat observations.
        # Other MACs might want to e.g. delegate building inputs to each agent
        bs = batch.batch_size
        inputs = []
        inputs.append(batch["obs"][:, t])  # b1av
        if self.args.obs_last_action:
            if t == 0:
                inputs.append(th.zeros_like(batch["actions_onehot"][:, t]))
            else:
                inputs.append(batch["actions_onehot"][:, t-1])
        if self.args.obs_agent_id:
            inputs.append(th.eye(self.n_agents, device=batch.device).unsqueeze(0).expand(bs, -1, -1))

        inputs = th.cat([x.reshape(bs*self.n_agents, -1) for x in inputs], dim=1)
        return inputs

    def _build_alone_inputs(self, batch, t):
        # Assumes homogenous agents with flat observations.
        # Other MACs might want to e.g. delegate building inputs to each agent
        bs = batch.batch_size
        inputs_alone = []
        inputs_alone.append(batch["obs_alone"][:, t])  # b1av
        if self.args.obs_last_action:
            if t == 0:
                inputs_alone.append(th.zeros_like(batch["actions_onehot"][:, t]))
            else:
                inputs_alone.append(batch["actions_onehot"][:, t-1])
        if self.args.obs_agent_id:
            inputs_alone.append(th.eye(1, device=batch.device).expand(self.n_agents, -1).unsqueeze(0).expand(bs, -1, -1))

        inputs_alone = th.cat([x.reshape(bs*self.n_agents, -1) for x in inputs_alone], dim=1)
        return inputs_alone

    def _get_input_shape(self, scheme):
        input_shape = scheme["obs"]["vshape"]
        if self.args.obs_last_action:
            input_shape += scheme["actions_onehot"]["vshape"][0]
        if self.args.obs_agent_id:
            input_shape += self.n_agents

        return input_shape

    def _get_input_alone_shape(self, scheme):
        input_alone_shape = scheme["obs_alone"]["vshape"]
        if self.args.obs_last_action:
            input_alone_shape += scheme["actions_onehot"]["vshape"][0]
        if self.args.obs_agent_id:
            input_alone_shape += 1

        return input_alone_shape

    def focus_fire_rate(self, chosen_actions, batch, t):
        self.test_total += 1
        n_actions_no_attack = 6
        #Compute focus fire rate
        target_id = th.clamp(chosen_actions - n_actions_no_attack, min=-1)
        max_id = self.args.n_actions - n_actions_no_attack
        num_agents_attack = []
        for i in range(max_id):
            num_agents_attack.append(th.sum(target_id == i).item())
        #Compute average distance
        inputs = batch["obs"][:, t]
        bs = batch.batch_size
        individual_feats_size = (self.input_shape-self.n_agents-self.input_alone_shape+1) // (self.n_agents - 1) - 4
        all_feats_size = individual_feats_size + 4
        n_enemies = (self.input_shape-individual_feats_size-self.n_agents-self.args.n_actions-all_feats_size*(self.n_agents-1)) // all_feats_size
        enemy_ally_feats = inputs[:, :, -individual_feats_size-all_feats_size*(self.n_agents-1+n_enemies):-individual_feats_size]\
                            .reshape(inputs.shape[0], inputs.shape[1], self.n_agents-1+n_enemies, -1)
        #Compute enemy
        e_shootable = (enemy_ally_feats[:, :, :n_enemies, 0] > 0).long()
        e_visible = (enemy_ally_feats[:, :, :n_enemies, 1] > 0).long()
        e_distance = enemy_ally_feats[:, :, :n_enemies, 1]
        e_average_distance = th.sum(e_distance, dim=1)/(th.sum(e_visible, dim=1) + 1e-6)
        #Compute ally
        #Compute enemy
        a_visible = (enemy_ally_feats[:, :, :n_enemies, 0] > 0).long()
        a_distance = enemy_ally_feats[:, :, :n_enemies, 1] * a_visible
        a_average_distance = th.sum(a_distance, dim=1)/(th.sum(a_visible, dim=1) + 1e-6)

        for num_attack in num_agents_attack:
            self.avg_num_agents_attack[num_attack] += 1
        self.avg_ally_distance += a_average_distance.mean().item()

        th.set_printoptions(precision=2)
        print("focus fire rate: ", self.avg_num_agents_attack/self.test_total)
        print("focus fire rate mean: ", self.avg_num_agents_attack[2:].sum()/self.test_total)
        print("average distance between agents: ", "%.2f" % (self.avg_ally_distance/self.test_total))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



