Ejemplo n.º 1
0
    def train_attacker(
            config: ClientConfig) -> Union[ExperimentResult, ExperimentResult]:
        """
        Trains an attacker agent in the environment

        :param config: Training configuration
        :return: trainresult, evalresult
        """
        env: IdsGameEnv = None
        env = gym.make(config.env_name,
                       idsgame_config=config.idsgame_config,
                       save_dir=config.output_dir + "/results/data/" +
                       str(config.random_seed),
                       initial_state_path=config.initial_state_path)
        if config.title is not None:
            env.idsgame_config.render_config.title = config.title
        attacker: TrainAgent = None
        if config.attacker_type == AgentType.TABULAR_Q_AGENT.value:
            attacker = TabularQAgent(env, config.q_agent_config)
        elif config.attacker_type == AgentType.DQN_AGENT.value:
            attacker = DQNAgent(env, config.q_agent_config)
        elif config.attacker_type == AgentType.REINFORCE_AGENT.value:
            attacker = ReinforceAgent(env, config.pg_agent_config)
        elif config.attacker_type == AgentType.ACTOR_CRITIC_AGENT.value:
            attacker = ActorCriticAgent(env, config.pg_agent_config)
        elif config.attacker_type == AgentType.BAYES_ACTOR_CRITIC_AGENT.value:
            attacker = BayesActorCriticAgent(env, config.pg_agent_config)
        elif config.attacker_type == AgentType.PPO_AGENT.value:
            attacker = PPOAgent(env, config.pg_agent_config)
        elif config.attacker_type == AgentType.PPO_OPENAI_AGENT.value:
            wrapper_env = BaselineEnvWrapper(
                config.env_name,
                idsgame_config=config.idsgame_config,
                save_dir=config.output_dir + "/results/data/" +
                str(config.random_seed),
                initial_state_path=config.initial_state_path,
                pg_agent_config=config.pg_agent_config)
            if config.title is not None:
                wrapper_env.idsgame_env.idsgame_config.render_config.title = config.title
            attacker = OpenAiPPOAgent(wrapper_env, config.pg_agent_config)
        else:
            raise AssertionError(
                "Attacker train agent type not recognized: {}".format(
                    config.attacker_type))
        attacker.train()
        train_result = attacker.train_result
        eval_result = attacker.eval_result
        return train_result, eval_result
Ejemplo n.º 2
0
        lstm_core=False,
        lstm_hidden_dim=32,
        multi_channel_obs=False,
        channel_1_dim=32,
        channel_1_layers=2,
        channel_1_input_dim=16,
        channel_2_dim=32,
        channel_2_layers=2,
        channel_2_input_dim=16,
        channel_3_dim=32,
        channel_3_layers=2,
        channel_3_input_dim=4,
        channel_4_dim=32,
        channel_4_layers=2,
        channel_4_input_dim=4,
        mini_batch_size=64,
        ar_policy=False,
        attacker_node_input_dim=((4 + 2) * 4),
        attacker_at_net_input_dim=(4 + 2),
        attacker_at_net_output_dim=(4 + 1),
        attacker_node_net_output_dim=4)

    env_name = args.env_name
    env = gym.make(env_name,
                   save_dir=default_output_dir() + "/results/data/" +
                   args.experiment_id)
    attacker_agent = ReinforceAgent(env=env, config=pg_agent_config)
    attacker_agent.train()
    train_result = attacker_agent.train_result
    eval_result = attacker_agent.eval_result