def __init__(self, args): self.n_actions = args.n_actions self.n_agents = args.n_agents self.state_shape = args.state_shape self.obs_shape = args.obs_shape alg = args.alg if alg.find('reinforce') > -1: self.policy = Reinforce(args) elif alg.find('coma') > -1: self.policy = COMA(args) elif alg.find('central_v') > -1: self.policy = CentralV(args) else: raise Exception("No such algorithm") self.args = args print('Init CommAgents')
def __init__(self, args): self.n_actions = args.n_actions self.n_agents = args.n_agents self.state_shape = args.state_shape self.obs_shape = args.obs_shape if args.alg == 'vdn': from policy.vdn import VDN self.policy = VDN(args) elif args.alg == 'iql': from policy.iql import IQL self.policy = IQL(args) elif args.alg == 'qmix': from policy.qmix import QMIX self.policy = QMIX(args) elif args.alg == 'coma': from policy.coma import COMA self.policy = COMA(args) elif args.alg == 'qtran_alt': from policy.qtran_alt import QtranAlt self.policy = QtranAlt(args) elif args.alg == 'qtran_base': from policy.qtran_base import QtranBase self.policy = QtranBase(args) elif args.alg == 'maven': from policy.maven import MAVEN self.policy = MAVEN(args) elif args.alg == 'central_v': from policy.central_v import CentralV self.policy = CentralV(args) elif args.alg == 'reinforce': from policy.reinforce import Reinforce self.policy = Reinforce(args) else: raise Exception("No such algorithm") self.args = args
def __init__(self, args): self.n_actions = args.n_actions self.n_agents = args.n_agents self.state_shape = args.state_shape self.obs_shape = args.obs_shape if args.alg == 'vdn': self.policy = VDN(args) elif args.alg == 'qmix': self.policy = QMIX(args) elif args.alg == 'coma': self.policy = COMA(args) elif args.alg == 'qtran_alt': self.policy = QtranAlt(args) elif args.alg == 'qtran_base': self.policy = QtranBase(args) elif args.alg == 'maven': self.policy = MAVEN(args) elif args.alg == 'central_v': self.policy = CentralV(args) elif args.alg == 'reinforce': self.policy = Reinforce(args) else: raise Exception("No such algorithm") self.args = args print('Init Agents')
def __init__(self, args): self.n_actions = args.n_actions self.n_agents = args.n_agents * 2 self.state_shape = args.state_shape self.obs_shape = args.obs_shape self.idact_shape = args.id_dim + args.n_actions self.search_actions = np.eye(args.n_actions) self.search_ids = np.zeros(self.n_agents) if args.alg == 'vdn': self.policy = VDN(args) elif args.alg == 'qmix': self.policy = QMIX(args) elif args.alg == 'ours': self.policy = OURS(args) elif args.alg == 'coma': self.policy = COMA(args) elif args.alg == 'qtran_alt': self.policy = QtranAlt(args) elif args.alg == 'qtran_base': self.policy = QtranBase(args) elif args.alg == 'maven': self.policy = MAVEN(args) elif args.alg == 'central_v': self.policy = CentralV(args) elif args.alg == 'reinforce': self.policy = Reinforce(args) else: raise Exception("No such algorithm") if args.use_fixed_model: args_goal_a = get_common_args() args_goal_a.load_model = True args_goal_a = get_mixer_args(args_goal_a) args_goal_a.learn = False args_goal_a.epsilon = 0 # 1 args_goal_a.min_epsilon = 0 args_goal_a.map = 'battle' args_goal_a.n_actions = args.n_actions args_goal_a.episode_limit = args.episode_limit args_goal_a.n_agents = args.n_agents args_goal_a.state_shape = args.state_shape args_goal_a.feature_shape = args.feature_shape args_goal_a.view_shape = args.view_shape args_goal_a.obs_shape = args.obs_shape args_goal_a.real_view_shape = args.real_view_shape args_goal_a.load_num = args.load_num args_goal_a.use_ja = False args_goal_a.mlp_hidden_dim = [512, 512] self.fixed_policy = VDN_F(args_goal_a) self.args = args print('Init Agents')
class CommAgents: def __init__(self, args): self.n_actions = args.n_actions self.n_agents = args.n_agents self.state_shape = args.state_shape self.obs_shape = args.obs_shape alg = args.alg if alg.find('reinforce') > -1: self.policy = Reinforce(args) elif alg.find('coma') > -1: self.policy = COMA(args) elif alg.find('central_v') > -1: self.policy = CentralV(args) else: raise Exception("No such algorithm") self.args = args print('Init CommAgents') # 根据weights得到概率,然后再根据epsilon选动作 def choose_action(self, weights, avail_actions, epsilon, evaluate=False): weights = weights.unsqueeze(0) avail_actions = torch.tensor(avail_actions, dtype=torch.float32).unsqueeze(0) action_num = avail_actions.sum(dim=1, keepdim=True).float().repeat( 1, avail_actions.shape[-1]) # 可以选择的动作的个数 # 先将Actor网络的输出通过softmax转换成概率分布 prob = torch.nn.functional.softmax(weights, dim=-1) # 在训练的时候给概率分布添加噪音 prob = ((1 - epsilon) * prob + torch.ones_like(prob) * epsilon / action_num) prob[avail_actions == 0] = 0.0 # 不能执行的动作概率为0 """ 不能执行的动作概率为0之后,prob中的概率和不为1,这里不需要进行正则化,因为torch.distributions.Categorical 会将其进行正则化。要注意在训练的过程中没有用到Categorical,所以训练时取执行的动作对应的概率需要再正则化。 """ if epsilon == 0 and evaluate: # 测试时直接选最大的 action = torch.argmax(prob) else: action = Categorical(prob).sample().long() return action def get_action_weights(self, obs, last_action): obs = torch.tensor(obs, dtype=torch.float32) last_action = torch.tensor(last_action, dtype=torch.float32) inputs = list() inputs.append(obs) # 给obs添加上一个动作、agent编号 if self.args.last_action: inputs.append(last_action) if self.args.reuse_network: inputs.append(torch.eye(self.args.n_agents)) inputs = torch.cat([x for x in inputs], dim=1) if self.args.cuda: inputs = inputs.cuda() self.policy.eval_hidden = self.policy.eval_hidden.cuda() weights, self.policy.eval_hidden = self.policy.eval_rnn( inputs, self.policy.eval_hidden) weights = weights.reshape(self.args.n_agents, self.args.n_actions) return weights.cpu() def _get_max_episode_len(self, batch): terminated = batch['terminated'] episode_num = terminated.shape[0] max_episode_len = 0 for episode_idx in range(episode_num): for transition_idx in range(self.args.episode_limit): if terminated[episode_idx, transition_idx, 0] == 1: if transition_idx + 1 >= max_episode_len: max_episode_len = transition_idx + 1 break return max_episode_len def train(self, batch, train_step, epsilon=None): # coma在训练时也需要epsilon计算动作的执行概率 # 每次学习时,各个episode的长度不一样,因此取其中最长的episode作为所有episode的长度 max_episode_len = self._get_max_episode_len(batch) for key in batch.keys(): batch[key] = batch[key][:, :max_episode_len] self.policy.learn(batch, max_episode_len, train_step, epsilon) if train_step > 0 and train_step % self.args.save_cycle == 0: self.policy.save_model(train_step)