示例#1
0
def main(unused_argv):
    """Run an agent."""
    stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace
    stopwatch.sw.trace = FLAGS.trace

    maps.get(FLAGS.map)  # Assert the map exists.
    players = []
    agents = []
    bot_difficulty = difficulties[FLAGS.difficulty]
    if FLAGS.agent1 == 'Bot':
        players.append(sc2_env.Bot(races['Z'], bot_difficulty))
    else:
        players.append(sc2_env.Agent(races[FLAGS.agent1_race]))
        agents.append(get_agent(FLAGS.agent1, FLAGS.agent1_config))
    if FLAGS.agent2 is None:
        pass
    elif FLAGS.agent2 == 'Bot':
        players.append(sc2_env.Bot(races['Z'], bot_difficulty))
    else:
        players.append(sc2_env.Agent(races[FLAGS.agent2_race]))
        agents.append(get_agent(FLAGS.agent2, FLAGS.agent2_config))

    run_thread(players, agents, FLAGS.map, FLAGS.render)

    if FLAGS.profile:
        print(stopwatch.sw)
def main(unused_argv):
    agent = ZergAgent()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Simple64",
                    players=[
                        sc2_env.Agent(
                            sc2_env.Race.zerg),  # First player: Our Agent
                        sc2_env.Bot(sc2_env.Race.random,
                                    sc2_env.Difficulty.very_easy)
                    ],  # Second player: A predefined bot
                    agent_interface_format=features.
                    AgentInterfaceFormat(  # Screen and minimap setup
                        feature_dimensions=features.Dimensions(screen=84,
                                                               minimap=64),
                        use_feature_units=True),  # Enable feature units
                    step_mul=
                    16,  # Number of steps before the agent choose an action
                    game_steps_per_episode=0,  # Length of the game
                    visualize=True) as env:

                # Feed the agent and receive actions until the game ends
                agent.setup(env.observation_spec(), env.action_spec())
                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)
    except KeyboardInterrupt:
        pass
def main(unused_argv):
    max_episodes = 10
    episode = 0
    try:
        with sc2_env.SC2Env(
                map_name="UnitSpawnerMarine",
                players=[sc2_env.Agent(sc2_env.Race.terran),
                         sc2_env.Bot(sc2_env.Race.random,
                                     sc2_env.Difficulty.very_easy)],
                agent_interface_format=features.AgentInterfaceFormat(
                    feature_dimensions=features.Dimensions(screen=84, minimap=64),
                    use_feature_units=True, ),
                step_mul=5,
                game_steps_per_episode=0,
                visualize=True) as env:
            while episode < max_episodes:
                episode += 1
                print("Episode "+str(episode)+"/"+str(max_episodes))
                agent = TestAttack()
                agent.setup(env.observation_spec(), env.action_spec())

                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#4
0
    def __init__(
        self,
        map_name='Simple64',
        players=None,
        player_race = 'terran',
        enemy_race = 'random',
        difficulty = 'very_easy',
        render=False,
        reset_done=True,
        spatial_dim=16,
        step_mul=16,
        game_steps_per_ep=0,
        obs_features=None,
        realtime=False,
    ):
        super().__init__(map_name, render, reset_done)

        FLAGS = flags.FLAGS
        FLAGS(sys.argv)

        self.step_mul = step_mul
        self.game_steps_per_ep = game_steps_per_ep
        self.spatial_dim = spatial_dim
        self.player_race = get_sc2_race(player_race)
        self.enemy_race = get_sc2_race(enemy_race)
        self.difficulty = get_sc2_difficulty(difficulty)
        self.players = [sc2_env.Agent(self.player_race), sc2_env.Bot(self.enemy_race, self.difficulty)]
        self.realtime = realtime
        self.done = False

        self.start()
示例#5
0
def main(unused_argv):
  """Run an agent."""
  stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace
  stopwatch.sw.trace = FLAGS.trace

  map_inst = maps.get(FLAGS.map)

  agent_classes = []
  players = []

  agent_module, agent_name = FLAGS.agent.rsplit(".", 1)
  agent_cls = getattr(importlib.import_module(agent_module), agent_name)
  agent_classes.append(agent_cls)
  players.append(sc2_env.Agent(sc2_env.Race[FLAGS.agent_race],
                               FLAGS.agent_name or agent_name))

  if map_inst.players >= 2:
    if FLAGS.agent2 == "Bot":
      players.append(sc2_env.Bot(sc2_env.Race[FLAGS.agent2_race],
                                 sc2_env.Difficulty[FLAGS.difficulty]))
    else:
      agent_module, agent_name = FLAGS.agent2.rsplit(".", 1)
      agent_cls = getattr(importlib.import_module(agent_module), agent_name)
      agent_classes.append(agent_cls)
      players.append(sc2_env.Agent(sc2_env.Race[FLAGS.agent2_race],
                                   FLAGS.agent2_name or agent_name))

  # threads = []
  # for _ in range(FLAGS.parallel - 1):
  #   t = threading.Thread(target=run_thread,
  #                        args=(agent_classes, players, FLAGS.map, False))
  #   threads.append(t)
  #   t.start()

  run_thread(agent_classes, players, FLAGS.map, FLAGS.render)
示例#6
0
def default_macro_env_maker(kwargs):
    '''
    :param kwargs: map_name, players, ... almost same as SC2Env
    :return: env_maker
    '''
    assert kwargs.get('map_name') is not None

    screen_sz = kwargs.pop('screen_size', 64)
    minimap_sz = kwargs.pop('minimap_size', 64)
    difficulty = kwargs.pop('difficulty', sc2_env.Difficulty.very_easy)
    assert screen_sz == minimap_sz
    if 'agent_interface_format' not in kwargs:
        kwargs['agent_interface_format'] = sc2_env.AgentInterfaceFormat(
            use_feature_units=True,
            use_raw_units=True,
            use_unit_counts=True,
            feature_dimensions=sc2_env.Dimensions(screen=(screen_sz,
                                                          screen_sz),
                                                  minimap=(minimap_sz,
                                                           minimap_sz)))

    if 'players' not in kwargs:
        kwargs['players'] = [
            sc2_env.Agent(sc2_env.Race.protoss),
            sc2_env.Bot(sc2_env.Race.terran, difficulty)
        ]
    if 'game_steps_per_episode' not in kwargs:
        kwargs['game_steps_per_episode'] = 16000
    if 'visualize' not in kwargs:
        kwargs['visualize'] = False
    if 'step_mul' not in kwargs:
        kwargs['step_mul'] = 4
    return MacroEnv(**kwargs)
def main(unused_argv):
    agent = ProtossAgent()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Catalyst",
                    players=[
                        sc2_env.Agent(sc2_env.Race.protoss),
                        sc2_env.Bot(sc2_env.Race.random,
                                    sc2_env.Difficulty.easy)
                    ],
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(screen=84,
                                                               minimap=64),
                        use_feature_units=True),
                    step_mul=16,
                    game_steps_per_episode=0,
                    visualize=True,
                    save_replay_episodes=1,
                    replay_dir='E:\Program Files (x86)\StarCraft II\Replays'
            ) as env:

                agent.setup(env.observation_spec(), env.action_spec())
                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#8
0
def run_thread(map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        players=[
                            sc2_env.Agent(sc2_env.Race.protoss),
                            sc2_env.Bot(sc2_env.Race.protoss,
                                        sc2_env.Difficulty.very_easy)
                        ],
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        agent_interface_format=features.AgentInterfaceFormat(
                            feature_dimensions=features.Dimensions(
                                screen=FLAGS.screen_resolution,
                                minimap=FLAGS.minimap_resolution)),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        if not FLAGS.save_file:
            save_name = f'./data/{FLAGS.map}/{FLAGS.max_episodes}eps_{FLAGS.agent}'
        else:
            save_name = FLAGS.save_file
        if FLAGS.load_checkpoint:
            agent = agent_selector(FLAGS.agent,
                                   save_name=save_name,
                                   load_name=FLAGS.load_file)
            # agent = Agent(save_name=FLAGS.load_file)
            agent.load_model_checkpoint(load_params=FLAGS.load_params)
        else:
            # save_name = f'./data/{FLAGS.map}/{FLAGS.max_episodes}eps_{FLAGS.agent}'
            agent = agent_selector(FLAGS.agent, save_name=save_name)
            # agent = Agent(save_name=save_name)
        if FLAGS.train:
            agent.train(env, FLAGS.train, max_episodes=FLAGS.max_episodes)
        else:
            agent.evaluate(env)
def main(unused_argv):
    agent = TerranRLAgentWithRawActsAndRawObs()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Simple64",
                    players=[sc2_env.Agent(sc2_env.Race.terran),
                             sc2_env.Bot(sc2_env.Race.terran,
                                         sc2_env.Difficulty.easy)],
                    agent_interface_format=features.AgentInterfaceFormat(
                        action_space=actions.ActionSpace.RAW,
                        use_raw_units=True,
                        raw_resolution=64,
                    ),
                    step_mul=8,
                    disable_fog=True,
                    visualize=False
            ) as env:
                # run_loop.run_loop([agent], env, max_episodes=1000)
                agent.setup(env.observation_spec(), env.action_spec())

                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)
    except KeyboardInterrupt:
        pass
def main(_):
    agent = SparseZergAgent()
    try:
        with sc2_env.SC2Env(
                map_name="Simple64",
                players=[
                    sc2_env.Agent(sc2_env.Race.zerg),
                    sc2_env.Bot(sc2_env.Race.random,
                                sc2_env.Difficulty.very_easy)
                ],
                agent_interface_format=features.AgentInterfaceFormat(
                    feature_dimensions=features.Dimensions(
                        screen=Constants.SCREEN_SIZE,
                        minimap=Constants.MINIMAP_SIZE),
                    use_feature_units=True,
                    hide_specific_actions=False,
                    use_raw_units=True,
                    use_unit_counts=True),
                step_mul=16,
                game_steps_per_episode=0,
                visualize=False) as env:
            run_loop.run_loop([agent], env)

    except KeyboardInterrupt:
        pass
示例#11
0
文件: main.py 项目: munsy/overmind
def main(unused_argv):
    agent = zerg()
    map = 'AbyssalReef'
    dims = features.Dimensions(screen=84, minimap=64)
    agent_iface_fmt = features.AgentInterfaceFormat(feature_dimensions=dims,
                                                    use_feature_units=True)
    player_setup = [
        sc2_env.Agent(sc2_env.Race.zerg),
        sc2_env.Bot(sc2_env.Race.protoss, sc2_env.Difficulty.very_easy)
    ]
    try:
        while True:
            with sc2_env.SC2Env(map_name=map,
                                players=player_setup,
                                agent_interface_format=agent_iface_fmt,
                                step_mul=16,
                                game_steps_per_episode=0,
                                visualize=False) as env:
                agent.setup(env.observation_spec(), env.action_spec())
                timesteps = env.reset()
                agent.reset()
                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#12
0
def main(unused_argv):
    agent = ZergAgent()
    try:
        while True:
            # Choose environment, player, features
            with sc2_env.SC2Env(
                    map_name="AbyssalReef",
                    players=[
                        sc2_env.Agent(sc2_env.Race.zerg),
                        sc2_env.Bot(sc2_env.Race.random,
                                    sc2_env.Difficulty.very_easy)
                    ],
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(screen=84,
                                                               minimap=64),
                        use_feature_units=True),
                    # 16 step_mul is similar to 150 APM
                    step_mul=16,
                    game_steps_per_episode=0,
                    visualize=True) as env:

                agent.setup(env.observation_spec(), env.action_spec())

                timesteps = env.reset()
                agent.reset()
                # Looping, feed detail in agent, receive action then repeat till game end
                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#13
0
    def initialize(self):
        self.agents = SmartAgent()
        self.env = sc2_env.SC2Env(
            map_name=self.map_name,
            players=[
                sc2_env.Agent(sc2_env.Race.terran),
                sc2_env.Bot(sc2_env.Race.terran, sc2_env.Difficulty.very_easy),
            ],
            agent_interface_format=features.AgentInterfaceFormat(
                action_space=actions.ActionSpace.RAW,
                use_raw_units=True,
                raw_resolution=self.resolution,
            ),
            step_mul=self.step_mul,
            disable_fog=True,
        )

        observation_spec = self.env.observation_spec()
        action_spec = self.env.action_spec()
        # for agent, obs_spec, act_spec in zip(self.agents, observation_spec, action_spec):
        self.agents.setup(observation_spec, action_spec)

        self.terminal_state = False
        self.timesteps = self.env.reset()
        self.agents.reset()
示例#14
0
def main(argv):
    logging.info("Starting local game...")

    maxSecond = 300
    max_steps = 16 / STEP_MUL * 1.4 * maxSecond  #adjusting for 1.4 for "faster" game speed setting
    max_episodes = 1

    players = []
    agents = []

    players.append(sc2_env.Agent(RACE))
    agents.append(ZergMacroAgent(max_steps))

    players.append(
        sc2_env.Bot(sc2_env.Race.random, sc2_env.Difficulty.very_easy))

    with sc2_env.SC2Env(
            map_name="AbyssalReef",  #"Simple64",
            players=players,
            visualize=False,
            step_mul=STEP_MUL,
            agent_interface_format=AGENT_INTERFACE_FORMAT) as env:

        # hack to override maxEpisodes as it doesn't seem to work
        # TODO: there is probably an unreported exception in the try catch at: https://github.com/deepmind/pysc2/blob/7b7afd7eeae985e6498855ac368c865ed9d527fb/pysc2/env/run_loop.py
        total_episodes = 0
        while not max_episodes or total_episodes < max_episodes:
            total_episodes += 1
            run_loop.run_loop(agents, env, max_steps, 1)
示例#15
0
def main(unused_argv):
    agent = ZergAgent()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Simple64",
                    players=[
                        sc2_env.Agent(sc2_env.Race.zerg),
                        sc2_env.Bot(sc2_env.Race.random,
                                    sc2_env.Difficulty.very_easy)
                    ],
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(screen=84,
                                                               minimap=64),
                        use_feature_units=True),
                    step_mul=16,
                    game_steps_per_episode=0,
                    visualize=True) as env:

                agent.setup(env.observation_spec(), env.action_spec())

                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#16
0
def main(unused_argv):
  agent = TerranAgent()                                                                 #Start agent
  try:
    while True:                                                                       #Run function indefinitely
      with sc2_env.SC2Env(                                                            #Setup environment

          map_name="Simple64",                                                        
#AbyssalReef
          players=[sc2_env.Agent(sc2_env.Race.terran),                                  #Player 1 is agent, player 2 is a very easy bot
                   sc2_env.Bot(sc2_env.Race.random,                                   #It's possible to change 'Bot' to 'Agent' for another agent
                               sc2_env.Difficulty.medium)],          

          agent_interface_format=features.AgentInterfaceFormat(
              feature_dimensions=features.Dimensions(screen=84, minimap=64),          #Screen and minimap resolutions
              use_feature_units=True),                                                #Feature units list

          step_mul=16,
          game_steps_per_episode=0,                    
          visualize=True) as env:

        agent.setup(env.observation_spec(), env.action_spec())
        
        timesteps = env.reset()
        agent.reset()
        
        while True:
          step_actions = [agent.step(timesteps[0])] 
          if timesteps[0].last():
            break
          timesteps = env.step(step_actions)
      
  except KeyboardInterrupt:
    pass
示例#17
0
def run_thread(map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        players=[
                            sc2_env.Agent(sc2_env.Race.protoss),
                            sc2_env.Bot(sc2_env.Race.protoss,
                                        sc2_env.Difficulty.very_easy)
                        ],
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        agent_interface_format=features.AgentInterfaceFormat(
                            feature_dimensions=features.Dimensions(
                                screen=FLAGS.screen_resolution,
                                minimap=FLAGS.minimap_resolution)),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = Agent(
            save_name=
            f'./data/{FLAGS.map}/{FLAGS.max_episodes}eps_{FLAGS.screen_resolution}res'
        )
        # run_loop([agent], env, FLAGS.max_agent_steps)
        # agent.train(env, FLAGS.train)
        # agent.train(env, FLAGS.train,  max_episodes=FLAGS.max_episodes)
        agent.evaluate(env)
        if FLAGS.save_replay:
            env.save_replay(Agent.__name__)
示例#18
0
def main(unused_argv):
    agent = ArcaneSwarm()
    try:
        while True:
            with sc2_env.SC2Env(
                map_name="AbyssalReef",
                players=[sc2_env.Agent(sc2_env.Race.zerg), sc2_env.Bot(sc2_env.Race.random,
            sc2_env.Difficulty.very_easy)],
            agent_interface_format=features.AgentInterfaceFormat(
feature_dimensions=features.Dimensions(screen=84, minimap=64),use_feature_units=True), # Size of screen and minimap that the agent see

            visualize=True
            ) as env:
                agent.setup(env.observation_spec(), env.action_spec())
                timesteps = env.reset()
                agent.reset()
                
                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)


    except KeyboardInterrupt:
        pass
示例#19
0
def main(unused_argv):
    agent1 = myAgent()

    try:
        with sc2_env.SC2Env(
                map_name="Flat96",
                players=[
                    sc2_env.Agent(sc2_env.Race.terran),
                    sc2_env.Bot(sc2_env.Race.protoss,
                                sc2_env.Difficulty.very_easy)
                ],
                agent_interface_format=features.AgentInterfaceFormat(
                    feature_dimensions=features.Dimensions(
                        screen=config.MAP_SIZE, minimap=config.MAP_SIZE),
                    camera_width_world_units=config.MAP_SIZE * 1,
                    action_space=actions.ActionSpace.RAW,
                    use_raw_units=True,
                    raw_resolution=config.MAP_SIZE,
                    use_unit_counts=True),
                score_index=-1,
                step_mul=32,
                disable_fog=False,
                visualize=True,
                realtime=False) as env:
            run_loop.run_loop([agent1], env)

    except KeyboardInterrupt:
        pass
示例#20
0
def main(unused_argv):
    agent1 = myAgent()

    try:
        with sc2_env.SC2Env(
                map_name="Simple96",
                players=[
                    sc2_env.Agent(sc2_env.Race.terran),
                    sc2_env.Bot(sc2_env.Race.protoss,
                                sc2_env.Difficulty.very_easy)
                ],
                agent_interface_format=features.AgentInterfaceFormat(
                    feature_dimensions=features.Dimensions(
                        screen=macro_operation.mapSzie,
                        minimap=macro_operation.mapSzie),
                    action_space=actions.ActionSpace.RAW,
                    use_raw_units=True,
                    raw_resolution=macro_operation.mapSzie,
                    use_unit_counts=True),
                step_mul=8,
                disable_fog=False,
                visualize=False,
                realtime=False) as env:
            run_loop.run_loop([agent1], env)

    except KeyboardInterrupt:
        pass
示例#21
0
def main(unused_argv):
  #agent = ZergAgent()
  agent = SmartAgent()
  #agent = TestAgent()
  try:
    while True:
      with sc2_env.SC2Env(
        map_name="Simple64",
        players=[sc2_env.Agent(sc2_env.Race.zerg), # our Bot
                #sc2_env.Agent(sc2_env.Race.zerg)],
                sc2_env.Bot(sc2_env.Race.protoss,sc2_env.Difficulty.very_easy)], # Blizzard bot
        agent_interface_format=features.AgentInterfaceFormat( # Interface parameters
            feature_dimensions=features.Dimensions(screen=64, minimap=64),use_feature_units=True),
        step_mul=1, # steps before our Agent take a decision 8=300APM, 16=150
        game_steps_per_episode=0, #run game as long as necessary
        visualize=True) as env:
        agent.setup(env.observation_spec(), env.action_spec())

        timesteps = env.reset()
        agent.reset()

        while True: 
          step_actions = [agent.step(timesteps[0])]
          if timesteps[0].last():
            break
          timesteps = env.step(step_actions)

  except KeyboardInterrupt:
    pass
示例#22
0
def main(unused_argv):
    agent1 = myAgent()


    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Flat96",
                    players=[sc2_env.Agent(race=sc2_env.Race.terran, name='agent1'),
                             sc2_env.Bot(sc2_env.Race.protoss,
                                         sc2_env.Difficulty.very_easy)],

                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(screen=macro_operation.screenSize,
                                                               minimap=macro_operation.minimapSize),
                        action_space=actions.ActionSpace.RAW,
                        camera_width_world_units=macro_operation.screenSize,
                        use_unit_counts=True,
                        use_raw_units=True,
                        raw_resolution=macro_operation.screenSize,

                    ),

                    step_mul=8,
                    game_steps_per_episode=0,
                    realtime=False,
                    visualize=True,

            ) as env:
                run_loop.run_loop([agent1], env)

    except KeyboardInterrupt:
        pass
def main(unused):
    agent = ZergAgent()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="AbyssalReef",
                    players=[sc2_env.Agent(sc2_env.Race.zerg),
                             sc2_env.Bot(sc2_env.Race.random,
                                         sc2_env.Difficulty.very_easy)],
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(screen=84, minimap=64),
                        use_feature_units=True),
                    step_mul=16,  # the default value is 8, this is also suggested value from the paper
                    game_steps_per_episode=0,  # makes the game for as long as necessary
                    visualize=True) as env:

                agent.setup(env.observation_spec(), env.action_spec())

                time_steps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(time_steps[0])]
                    if time_steps[0].last():
                        break
                    time_steps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
def main():
    agent = honoursAgent()
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name="Simple64",
                    players=[
                        sc2_env.Agent(sc2_env.Race.zerg),
                        sc2_env.Bot(sc2_env.Race.random,
                                    sc2_env.Difficulty.very_easy)
                    ],
                    agent_interface_format=features.AgentInterfaceFormat(
                        action_space=actions.ActionSpace.RAW,
                        use_raw_units=True,
                        raw_resolution=64),
                    step_mul=16,
                    game_steps_per_episode=15000,
            ) as env:

                agent.setup(env.observation_spec(), env.action_spec())

                timesteps = env.reset()
                agent.reset()

                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last():
                        break
                    timesteps = env.step(step_actions)

    except KeyboardInterrupt:
        pass
示例#25
0
  def test_observation_matches_obs_spec(self):
    with sc2_env.SC2Env(
        map_name="Simple64",
        players=[sc2_env.Agent(sc2_env.Race.random),
                 sc2_env.Bot(sc2_env.Race.random, sc2_env.Difficulty.easy)],
        agent_interface_format=sc2_env.AgentInterfaceFormat(
            feature_dimensions=sc2_env.Dimensions(
                screen=(84, 87),
                minimap=(64, 67)))) as env:

      multiplayer_obs_spec = env.observation_spec()
      self.assertIsInstance(multiplayer_obs_spec, tuple)
      self.assertLen(multiplayer_obs_spec, 1)
      obs_spec = multiplayer_obs_spec[0]

      multiplayer_action_spec = env.action_spec()
      self.assertIsInstance(multiplayer_action_spec, tuple)
      self.assertLen(multiplayer_action_spec, 1)
      action_spec = multiplayer_action_spec[0]

      agent = random_agent.RandomAgent()
      agent.setup(obs_spec, action_spec)

      multiplayer_obs = env.reset()
      agent.reset()
      for _ in range(100):
        self.assertIsInstance(multiplayer_obs, tuple)
        self.assertLen(multiplayer_obs, 1)
        raw_obs = multiplayer_obs[0]
        obs = raw_obs.observation
        self.check_observation_matches_spec(obs, obs_spec)

        act = agent.step(raw_obs)
        multiplayer_act = (act,)
        multiplayer_obs = env.step(multiplayer_act)
示例#26
0
def main(unused_argv):
    #env = gym.make("SC2GYMENV-v0")
    #env.settings['map_name'] = 'ScoutSimple64'

    rs = FLAGS.random_seed
    if FLAGS.random_seed is None:
        rs = int((time.time() % 1) * 1000000)

    logger.configure(dir=FLAGS.train_log_dir, format_strs=['log'])

    players = []
    players.append(
        sc2_env.Bot(races[FLAGS.bot_race], difficulties[FLAGS.difficulty]))
    players.append(sc2_env.Agent(races[FLAGS.agent_race]))

    screen_res = (int(FLAGS.screen_ratio * FLAGS.screen_resolution) // 4 * 4,
                  FLAGS.screen_resolution)
    if FLAGS.agent_interface_format == 'feature':
        agent_interface_format = sc2_env.AgentInterfaceFormat(
            feature_dimensions=sc2_env.Dimensions(
                screen=screen_res, minimap=FLAGS.minimap_resolution))
    elif FLAGS.agent_interface_format == 'rgb':
        agent_interface_format = sc2_env.AgentInterfaceFormat(
            rgb_dimensions=sc2_env.Dimensions(
                screen=screen_res, minimap=FLAGS.minimap_resolution))
    else:
        raise NotImplementedError

    env = ZergScoutEnv(
        map_name=FLAGS.map,
        players=players,
        step_mul=FLAGS.step_mul,
        random_seed=rs,
        game_steps_per_episode=FLAGS.max_step,
        agent_interface_format=agent_interface_format,
        score_index=-1,  # this indicates the outcome is reward
        disable_fog=FLAGS.disable_fog,
        visualize=FLAGS.render)

    env = make(FLAGS.wrapper, env)

    network = model(FLAGS.wrapper)  #deepq.models.mlp([64, 32])

    print('params, lr={} bf={} ef={} ef_eps={}'.format(FLAGS.param_lr,
                                                       FLAGS.param_bf,
                                                       FLAGS.param_ef,
                                                       FLAGS.param_efps))

    act = deepq.learn(env,
                      q_func=network,
                      lr=FLAGS.param_lr,
                      max_timesteps=100000,
                      buffer_size=FLAGS.param_bf,
                      exploration_fraction=FLAGS.param_ef,
                      exploration_final_eps=FLAGS.param_efps,
                      checkpoint_path=FLAGS.checkpoint_path,
                      checkpoint_freq=FLAGS.checkpoint_freq,
                      print_freq=10,
                      callback=callback)
示例#27
0
def main(unused_argv):
    """Run an agent."""
    step_mul = FLAGS.step_mul
    players = 2

    screen_res = (int(FLAGS.screen_ratio * FLAGS.screen_resolution) // 4 * 4,
                  FLAGS.screen_resolution)
    agent_interface_format = None
    if FLAGS.agent_interface_format == 'feature':
        agent_interface_format = sc2_env.AgentInterfaceFormat(
            feature_dimensions=sc2_env.Dimensions(
                screen=screen_res, minimap=FLAGS.minimap_resolution))
    elif FLAGS.agent_interface_format == 'rgb':
        agent_interface_format = sc2_env.AgentInterfaceFormat(
            rgb_dimensions=sc2_env.Dimensions(
                screen=screen_res, minimap=FLAGS.minimap_resolution))
    else:
        raise NotImplementedError
    players = [
        sc2_env.Agent(sc2_env.Race.zerg),
        sc2_env.Agent(sc2_env.Race.zerg)
    ]
    agents = []
    bot_difficulty = get_difficulty(FLAGS.difficulty)
    if FLAGS.player1 == 'Bot':
        players[0] = sc2_env.Bot(sc2_env.Race.zerg, bot_difficulty)
    else:
        agents.append(get_agent(FLAGS.player1))
    if FLAGS.player2 == 'Bot':
        players[1] = sc2_env.Bot(sc2_env.Race.zerg, bot_difficulty)
    else:
        agents.append(get_agent(FLAGS.player2))
    with sc2_env.SC2Env(map_name=FLAGS.map,
                        visualize=FLAGS.visualize,
                        players=players,
                        step_mul=step_mul,
                        game_steps_per_episode=FLAGS.max_steps_per_episode *
                        step_mul,
                        agent_interface_format=agent_interface_format,
                        disable_fog=FLAGS.disable_fog) as env:
        run_loop(agents,
                 env,
                 max_frames=0,
                 max_episodes=FLAGS.episodes,
                 sleep_time_per_step=FLAGS.sleep_time_per_step,
                 merge_units_info=FLAGS.merge_units_info)
示例#28
0
def main(unused_args):
    try:
        for i in range(n_games):
            print(f'Game {i+1}')

            with sc2_env.SC2Env(
                    map_name=map_name,
                    players=[
                        sc2_env.Agent(getattr(sc2_env.Race, agent.race_name)),
                        sc2_env.Bot(getattr(sc2_env.Race, op_race),
                                    getattr(sc2_env.Difficulty, op_difficulty),
                                    getattr(sc2_env.BotBuild, op_build))
                    ],
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(
                            screen=RESOLUTION, minimap=RESOLUTION),
                        raw_resolution=RESOLUTION,
                        use_feature_units=True,
                        use_raw_units=True,
                        use_raw_actions=agent.raw_interface,
                        show_cloaked=True,
                        show_burrowed_shadows=True,
                        show_placeholders=True,
                        add_cargo_to_units=True,
                        crop_to_playable_area=True,
                        raw_crop_to_playable_area=True,
                        send_observation_proto=True),
                    step_mul=STEP_MUL,
                    score_index=features.ScoreCumulative.score,
                    visualize=visualize,
                    realtime=realtime,
                    save_replay_episodes=save_replay_episodes,
                    replay_dir='{}\\data\\replays'.format(
                        os.path.abspath(os.getcwd()))) as env:

                agent.setup(env.observation_spec(), env.action_spec())
                timesteps = env.reset()
                agent.reset()
                while True:
                    step_actions = [agent.step(timesteps[0])]
                    if timesteps[0].last() or time and agent.game_step * STEP_MUL > time_iter \
                                            or agent.episode_length is not None \
                                            and agent.game_step * STEP_MUL > agent.episode_length:
                        break
                    timesteps = env.step(step_actions)

            if time:
                break

            if isinstance(agent, SmartZerg):
                agent.net.save(r'data/net.h5')
                rewards = np.array([x for sub in agent.rewards for x in sub])
                np.save(r'data/rewards.npy', rewards)

    except KeyboardInterrupt:
        agent.log.close()

    return agent
示例#29
0
def main(unused_argv):
    agent = SimpleAgent()

    map_name = "CollectMineralShards" # "CollectMineralShards" "Simple64" MoveToBeacon

    if  map_name == "Simple64":
        players = [sc2_env.Agent(sc2_env.Race.terran),
                    sc2_env.Bot(sc2_env.Race.random,
                                sc2_env.Difficulty.very_easy)
                ]
示例#30
0
def main(_):
    players = [
        sc2_env.Agent(sc2_env.Race.zerg),
        sc2_env.Bot(sc2_env.Race.zerg, FLAGS.difficulty)
    ]
    env = SC2BaseEnv(
        players=players,
        agent_interface='feature',
        map_name='KairosJunction',
        max_steps_per_episode=48000,
        screen_resolution=168,
        screen_ratio=0.905,
        step_mul=1,
        version=FLAGS.version,
        replay_dir=FLAGS.replay_dir,
        save_replay_episodes=1,
        use_pysc2_feature=False,
        minimap_resolution=(152, 168),
    )
    interface_cls = import_module_or_data(FLAGS.interface)
    interface_config = read_config_dict(FLAGS.interface_config)
    interface = interface_cls(**interface_config)
    env = EnvIntWrapper(env, [interface])
    obs = env.reset()
    print(env.observation_space.spaces)
    policy = import_module_or_data(FLAGS.policy)
    policy_config = read_config_dict(FLAGS.policy_config)
    agent = PGAgent2(policy,
                     env.observation_space.spaces[0],
                     env.action_space.spaces[0],
                     policy_config=policy_config)
    model_path = FLAGS.model
    model = joblib.load(model_path)
    agent.load_model(model.model)
    agent.reset(obs[0])

    episodes = FLAGS.episodes
    iter = 0
    sum_rwd = []
    while True:
        while True:
            if obs[0] is not None:
                act = [agent.step(obs[0])]
            else:
                act = [[]]
            obs, rwd, done, info = env.step(act)
            if done:
                print(rwd)
                sum_rwd.append(rwd[0])
                break
        iter += 1
        if iter >= episodes:
            print(sum_rwd)
            break
        obs = env.reset()