Example #1
0
def run_thread(agents, players, map_, visualize=False):
    ''' set up and run sc2_env loop '''
    # agents = [AGENT,]
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name=map_,
                    step_mul=8,
                    visualize=visualize,
                    players=players,
                    agent_interface_format=sc2_env.
                    parse_agent_interface_format(
                        feature_screen=32,
                        feature_minimap=32,
                        action_space=None,  # what to do about this later?
                        use_feature_units=True),
                    game_steps_per_episode=0) as env:

                # agents = [agent_cls() for agent_cls in agent_classes]
                env = available_actions_printer.AvailableActionsPrinter(
                    env)  # what this do?
                run_loop.run_loop(agents, env, max_frames=0, max_episodes=0)

    except KeyboardInterrupt:
        pass
Example #2
0
def main(unused_argvs):
    map_name = "MoveToBeacon"
    map_inst = maps.get(map_name)

    players = []

    players.append(sc2_env.Agent(sc2_env.Race["random"], "TestAgent"))

    with sc2_env.SC2Env(
            map_name=map_name,
            players=players,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=84,
                feature_minimap=64,
                rgb_screen=None,
                rgb_minimap=None,
                action_space=None,
                use_feature_units=True,
                use_raw_units=True),
            step_mul=8,
            game_steps_per_episode=None,
            disable_fog=False,
            visualize=False) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        for function in env.action_spec()[0].functions:
            print(function)
        print(env.action_spec()[0].types)
        print(env.observation_spec()[0])
Example #3
0
def run_thread(agent, players, visualize):
    with sc2_env.SC2Env(
            map_name=FLAGS.map,
            step_mul=FLAGS.step_mul,
            visualize=visualize,
            players=players,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=FLAGS.screen_resolution,
                feature_minimap=FLAGS.minimap_resolution,
                use_feature_units=True)) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        acc_score = 0

        for recorder, is_done in run_loop(agent,
                                          env,
                                          FLAGS.screen_resolution,
                                          max_episodes=FLAGS.max_episodes):
            obs = recorder[-1].observation
            score = obs["score_cumulative"][0]

            print('Your score is ' + str(score) + '!')

            acc_score += score

        print("Average score is", acc_score, "/", FLAGS.max_episodes, '=',
              acc_score / FLAGS.max_episodes)
def run_thread(agent_cls, map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        feature_screen_size=FLAGS.screen_resolution,
                        feature_minimap_size=FLAGS.minimap_resolution,
                        visualize=visualize,
                        use_feature_units=True) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()

        agent_name = FLAGS.agent_file

        # set the path to save the models and graphs
        path = 'models/' + agent_name

        # restore the model only if u have the previously trained a model
        #agent.dqn.load_model(path)

        # run the steps
        run_loop.run_loop([agent], env, FLAGS.max_agent_steps)

        # save the model
        agent.dqn.save_model(path, 1)

        # plot cost and reward
        agent.dqn.plot_cost(path, save=True)
        agent.plot_reward(path, save=True)
        agent.plot_player_hp(path, save=True)
        agent.plot_enemy_hp(path, save=True)

        if FLAGS.save_replay:
            env.save_replay(agent_cls.__name__)
Example #5
0
def run_thread(map_name, visualize):
  
      # feature_screen_size=(FLAGS.screen_resolution, FLAGS.screen_resolution),
      # feature_minimap_size=(FLAGS.minimap_resolution, FLAGS.minimap_resolution),
  with sc2_env.SC2Env(
      map_name=map_name,
      agent_interface_format=sc2_env.parse_agent_interface_format(
          feature_screen=FLAGS.feature_screen_size,
          feature_minimap=FLAGS.feature_minimap_size,
          rgb_screen=FLAGS.rgb_screen_size,
          rgb_minimap=FLAGS.rgb_minimap_size,
          action_space=FLAGS.action_space,
          use_feature_units=FLAGS.use_feature_units),
      agent_race=FLAGS.agent_race,
      bot_race=FLAGS.bot_race,
      difficulty=FLAGS.difficulty,
      step_mul=FLAGS.step_mul,
      game_steps_per_episode=FLAGS.game_steps_per_episode,
      visualize=visualize) as env:
    env = available_actions_printer.AvailableActionsPrinter(env)
    agent = Agent()
    # run_loop([agent], env, FLAGS.max_agent_steps)
    agent.train(env, FLAGS.train)
    if FLAGS.save_replay:
      env.save_replay(Agent.__name__)
Example #6
0
def run_thread(agent, player, map_name, visualize):
  with sc2_env.SC2Env(
      map_name=map_name,
      players=[player],
      agent_interface_format=sc2_env.parse_agent_interface_format(
          feature_screen=FLAGS.screen_resolution,
          feature_minimap=FLAGS.minimap_resolution),
      step_mul=FLAGS.step_mul,
      visualize=visualize) as env:
    env = available_actions_printer.AvailableActionsPrinter(env)

    replay_buffer = []
    mean_score = 0
    count = 0
    for recorder, is_done in run_loop([agent], env, FLAGS.max_agent_steps):
      if FLAGS.training:
        replay_buffer.append(recorder)
        if is_done:
          global COUNTER
          COUNTER += 1
          agent.update(replay_buffer)
          replay_buffer = []
          if COUNTER % FLAGS.snapshot_episode == 1:
            agent.save_model(SNAPSHOT, COUNTER)
          if COUNTER >= FLAGS.max_episode:
            break
      elif is_done:
        count += 1
        obs = recorder[-1].observation
        score = obs["score_cumulative"][0]
        print('Your score is ' + str(score) + '!')
        mean_score = mean_score + score
        if count == FLAGS.mean_episodes:
          print("your mean score in 100 episode is ", mean_score // 100)
          break
Example #7
0
def main(argv):

    with sc2_env.SC2Env(map_name='FindAndDefeatZerglings',
                        step_mul=8,
                        game_steps_per_episode=8,
                        screen_size_px=(84, 84),
                        minimap_size_px=(64, 64),
                        visualize=True) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        timestep = env.reset()[0]
        minimap = timestep.observation['minimap']
        screen = timestep.observation['screen'][_PLAYER_RELATIVE]
        player_relative = minimap[_PLAYER_RELATIVE]
        player_relative_color = colorize(
            transform.resize(player_relative, (256, 256),
                             order=0,
                             preserve_range=True))
        number_of_enemies = count_enemies(player_relative)
        min_distance = min_distance_to_enemy(screen)
        units = count_units(timestep)
        print("Number of units in screen: " + str(units))
        print("Number of enemies in minimap: " + str(number_of_enemies))
        print("Minimum distance to enemy: " + str(min_distance))
        print("Proportion visible: {}%".format(
            100 * proportion_visible_onscreen(timestep)))
        pic = Image.fromarray(player_relative_color)
        pic.show()
        input("Press any key to continue")
Example #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)
        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__)
Example #9
0
def run_thread(agent_classes, players, map_name, visualize):
  """Run one thread worth of the environment with agents."""
  with sc2_env.SC2Env(
      map_name=map_name,
      players=players,
      agent_interface_format=sc2_env.parse_agent_interface_format(
            #feature_screen=128,
            #feature_minimap=128,
          feature_screen=FLAGS.feature_screen_size,
          feature_minimap=FLAGS.feature_minimap_size,
          rgb_screen=FLAGS.rgb_screen_size,
          rgb_minimap=FLAGS.rgb_minimap_size,
          action_space=FLAGS.action_space,
          use_feature_units=True,
          use_camera_position=True),
      step_mul=FLAGS.step_mul,
      game_steps_per_episode=FLAGS.game_steps_per_episode,
      disable_fog=FLAGS.disable_fog,
      visualize=visualize      
    ) as env:


    env = available_actions_printer.AvailableActionsPrinter(env)
    agents = [agent_cls() for agent_cls in agent_classes]
    run_loop.run_loop(agents, env, FLAGS.max_agent_steps, FLAGS.max_episodes)
    if FLAGS.save_replay:
      env.save_replay(agent_classes[0].__name__)
Example #10
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)
Example #11
0
def run_thread(agent_cls, map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        feature_screen_size=FLAGS.screen_resolution,
                        feature_minimap_size=FLAGS.minimap_resolution,
                        visualize=visualize,
                        use_feature_units=True) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()

        # set the path to save the models and graphs
        path1 = 'models/'
        path2 = 'graphs/'
        # restore the model only if u have the previously trained a model
        if LOAD_MODEL:
            agent.ddpg.load_model(path1)

        # run the steps
        run_loop.run_loop([agent], env, FLAGS.max_agent_steps)

        # save the model
        if SAVE_MODEL:
            agent.ddpg.save_model(path1, 1)

        # plot cost and reward
        agent.ddpg.plot_cost(path2, save=SAVE_PIC)
        agent.ddpg.plot_reward(path2, save=SAVE_PIC)
        agent.plot_hp(path2, save=SAVE_PIC)

        if FLAGS.save_replay:
            env.save_replay(agent_cls.__name__)
Example #12
0
def run_thread(agent, map_name, visualize):
    """Run one thread worth of the environment with agents."""
    with sc2_env.SC2Env(
            map_name=map_name,
            #players=players,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=FLAGS.feature_screen_size,
                feature_minimap=FLAGS.feature_minimap_size,
                rgb_screen=FLAGS.rgb_screen_size,
                rgb_minimap=FLAGS.rgb_minimap_size,
                action_space=FLAGS.action_space,
                use_feature_units=FLAGS.use_feature_units),
            step_mul=FLAGS.step_mul,
            game_steps_per_episode=FLAGS.game_steps_per_episode,
            disable_fog=FLAGS.disable_fog,
            visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        # Only for a single player!
        replay_buffer = []
        score_list = []

        for recorder, is_done in my_run_loop([agent], env, 10000):
            if FLAGS.training:
                replay_buffer.append(recorder)
                if is_done:
                    counter = 0
                    with threading.Lock():
                        global COUNTER
                        COUNTER += 1
                        counter = COUNTER
                    obs = recorder[-1].observation
                    score = obs["score_cumulative"][0]
                    score_list.append(score)

                    with open('test.csv', 'w', encoding='utf-8',
                              newline='') as files:
                        writer = csv.writer(files, delimiter=',')
                        for i in range(len(score_list)):
                            writer.writerow([i, score_list[i]])

                    # Learning rate schedule
                    learning_rate = FLAGS.learning_rate * (
                        1 - 0.9 * counter / FLAGS.max_steps)
                    agent.update(replay_buffer, FLAGS.discount, learning_rate,
                                 counter)
                    replay_buffer = []
                    print("현재 counter : ", counter)
                    if counter % 249 == 1:
                        agent.save_model("./snapshot/", counter)
                    if counter >= FLAGS.max_steps:
                        break
            elif is_done:
                obs = recorder[-1].observation
                score = obs["score_cumulative"][0]
                print('Your score is ' + str(score) + '!')
        if FLAGS.save_replay:
            env.save_replay(agent.name)
Example #13
0
def run_thread(agent_classes, players, map_name, visualize):
  """Run one thread worth of the environment with agents."""
  SNAPSHOT = FLAGS.snapshot_path+FLAGS.map+'/'+FLAGS.net
  LOG = FLAGS.log_path+FLAGS.map+'/'+FLAGS.net
  if not os.path.exists(LOG):
    os.makedirs(LOG)
  with sc2_env.SC2Env(
      map_name=map_name,
      players=players,
      agent_interface_format=sc2_env.parse_agent_interface_format(
          feature_screen=FLAGS.feature_screen_size,
          feature_minimap=FLAGS.feature_minimap_size,
          rgb_screen=FLAGS.rgb_screen_size,
          rgb_minimap=FLAGS.rgb_minimap_size,
          action_space=FLAGS.action_space,
          use_feature_units=FLAGS.use_feature_units),
      step_mul=FLAGS.step_mul,
      game_steps_per_episode=FLAGS.game_steps_per_episode,
      disable_fog=FLAGS.disable_fog,
      visualize=False) as env:
    
    env = available_actions_printer.AvailableActionsPrinter(env)
    # wouldnt work for agent vs bot
    agents = [agent_cls(int(FLAGS.feature_minimap_size.x),int(FLAGS.feature_screen_size.x),LOG) for agent_cls in agent_classes]
    # run_loop.run_loop(agents, env, FLAGS.max_agent_steps, FLAGS.max_episodes)
    replay_buffer = []
    for recorder, is_done in run_loop.run_loop(agents, env, FLAGS.max_agent_steps, FLAGS.max_episodes):
      if FLAGS.training:
        
        replay_buffer.append(recorder)
        if is_done:
          counter = 0
          # with LOCK:
          global COUNTER
          COUNTER += 1
          counter = COUNTER
          # Learning rate schedule
          learning_rate = FLAGS.learning_rate * (1 - 0.9 * counter / FLAGS.max_steps)
          # print(replay_buffer)
          agents[0].update(replay_buffer, FLAGS.discount, learning_rate, counter)
          if counter % FLAGS.snapshot_step == 1:
            agents[0].save_model()
          replay_buffer = []
          # if counter % FLAGS.snapshot_step == 1:
          #   agents[0].save_model(SNAPSHOT, counter)
          if counter >= FLAGS.max_steps:
            break
      elif is_done:
        obs = recorder[-1].observation
        score = obs["score_cumulative"][0]
        print('Your score is '+str(score)+'!')
    if FLAGS.save_replay:
      env.save_replay(agent_classes[0].__name__)
Example #14
0
def make_env(config):
    with sc2_env.SC2Env(
            map_name=config.map,
            agent_race=config.agent_race,
            bot_race=config.bot_race,
            difficulty=config.difficulty,
            step_mul=config.step_mul,
            screen_size_px=(config.screen_resolution, config.screen_resolution),
            minimap_size_px=(config.minimap_resolution, config.minimap_resolution),
            save_replay_episodes=config.save_replay_frequency,
            replay_dir=config.save_dir,
            visualize=config.render) as env:
        return available_actions_printer.AvailableActionsPrinter(env)
Example #15
0
def run_thread(agent, map_name, agent_id=0):
    ''' set up and run sc2_env loop '''
    try:
        while True:
            with sc2_env.SC2Env(
                    map_name=FLAGS.map_name,
                    step_mul=FLAGS.step_mul,
                    visualize=FLAGS.render,
                    players=PLAYERS,
                    agent_interface_format=features.AgentInterfaceFormat(
                        feature_dimensions=features.Dimensions(
                            screen=FLAGS.screen_res,
                            minimap=FLAGS.minimap_res),
                        use_feature_units=True),
                    game_steps_per_episode=0) as env:
                env = available_actions_printer.AvailableActionsPrinter(env)

                # Only for a single player!
                # snapshot_dir = SNAPSHOT+str(id)  # if i need later
                replay_buffer = []
                for recorder, is_done in run_loop([agent], env,
                                                  MAX_AGENT_STEPS):
                    if FLAGS.training:
                        replay_buffer.append(recorder)
                        if is_done:
                            counter = 0
                            with LOCK:
                                global COUNTER
                                COUNTER += 1
                                counter = COUNTER
                            # Learning rate schedule
                            learning_rate = FLAGS.learning_rate * (
                                1 - 0.9 * counter / FLAGS.max_steps)
                            agent.update(replay_buffer, FLAGS.gamma,
                                         learning_rate, counter)
                            replay_buffer = []
                            if counter % FLAGS.snapshot_step == 1:
                                agent.save_model(SNAPSHOT, counter)
                            if counter >= FLAGS.max_steps:
                                break
                    elif is_done:
                        obs = recorder[-1].observation
                        score = obs["score_cumulative"][0]
                        print('Your score is ' + str(score) +
                              '!')  # does this ever print?
                if FLAGS.save_replay:
                    env.save_replay(agent.name)

    except KeyboardInterrupt:
        pass
Example #16
0
def run_thread(agent_cls, map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=FLAGS.agent_race,
                        bot_race=FLAGS.bot_race,
                        difficulty=FLAGS.difficulty,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        screen_size_px=(FLAGS.screen_resolution,
                                        FLAGS.screen_resolution),
                        minimap_size_px=(FLAGS.minimap_resolution,
                                         FLAGS.minimap_resolution),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls
        run_loop.run_loop([agent], env, FLAGS.max_agent_steps)
Example #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=8,
            game_steps_per_episode=0,
            agent_interface_format=features.AgentInterfaceFormat(
                feature_dimensions=features.Dimensions(screen=32, minimap=32)),
            visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = BattleAgentScripted(map_name)
        agent.run_loop(env)
Example #18
0
def run_thread(agent, map_name, visualize):
    players = []
    agent_module, agent_name = FLAGS.agent.rsplit(".", 1)
    players.append(
        sc2_env.Agent(sc2_env.Race[FLAGS.agent_race], FLAGS.agent_name
                      or agent_name))

    with sc2_env.SC2Env(
            map_name=map_name,
            players=players,
            step_mul=FLAGS.step_mul,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=FLAGS.feature_minimap_size,
                feature_minimap=FLAGS.feature_minimap_size,
                rgb_screen=FLAGS.rgb_screen_size,
                rgb_minimap=FLAGS.rgb_minimap_size,
                action_space=FLAGS.action_space,
                use_feature_units=FLAGS.use_feature_units),
            visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        # Only for a single player!
        replay_buffer = []
        for recorder, is_done in run_loop([agent], env, MAX_AGENT_STEPS):
            if FLAGS.training:
                replay_buffer.append(recorder)
                if is_done:
                    counter = 0
                    with LOCK:
                        global COUNTER
                        COUNTER += 1
                        counter = COUNTER
                    # Learning rate schedule
                    learning_rate = FLAGS.learning_rate * (
                        1 - 0.9 * counter / FLAGS.max_steps)
                    agent.update(replay_buffer, FLAGS.discount, learning_rate,
                                 counter)
                    replay_buffer = []
                    if counter % FLAGS.snapshot_step == 1:
                        agent.save_model(SNAPSHOT, counter)
                    if counter >= FLAGS.max_steps:
                        break
            elif is_done:
                obs = recorder[-1].observation
                score = obs["score_cumulative"][0]
                print('Your score is ' + str(score) + '!')
        if FLAGS.save_replay:
            env.save_replay(agent.name)
Example #19
0
def run_thread(agent, map_name, visualize):
    with sc2_env.SC2Env(
            map_name=map_name,
            players=
        [
            sc2_env.Agent(sc2_env.Race.terran),
            #sc2_env.Agent(sc2_env.Race.zerg)
            #sc2_env.Bot(race=sc2_env.Race.zerg, difficulty=sc2_env.Difficulty.very_easy)
        ],
            agent_interface_format=features.AgentInterfaceFormat(
                feature_dimensions=features.Dimensions(screen=64, minimap=64),
                use_feature_units=True),
            step_mul=FLAGS.step_mul,
            visualize=False) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        # Only for a single player!
        agent.set_action_spec(env.action_spec())
        replay_buffer = []
        #zerg_agent = ZergAgent()
        for recorder, is_done in run_loop([
                agent,
        ], env, MAX_AGENT_STEPS):
            if FLAGS.training:
                replay_buffer.append(recorder)
                if is_done:
                    counter = 0
                    with LOCK:
                        global COUNTER
                        COUNTER += 1
                        counter = COUNTER
                    # Learning rate schedule
                    learning_rate = FLAGS.learning_rate * (
                        1 - 0.9 * counter / FLAGS.max_steps)
                    agent.update(replay_buffer, FLAGS.discount, learning_rate,
                                 counter)
                    replay_buffer = []
                    if counter % FLAGS.snapshot_step == 1:
                        agent.save_model(SNAPSHOT, counter)
                        print('saving model {}'.format(counter))
                    if counter >= FLAGS.max_steps:
                        break
            elif is_done:
                obs = recorder[-1].observation
                score = obs["score_cumulative"][0]
                print('Your score is ' + str(score) + '!')
        if FLAGS.save_replay:
            env.save_replay(agent.name)
Example #20
0
def run_thread(agent_cls, map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=RACE,
                        bot_race=OPPONENT_RACE,
                        difficulty=DIFFICULTY,
                        step_mul=8,
                        game_steps_per_episode=0,
                        screen_size_px=(84, 84),
                        minimap_size_px=(64, 64),
                        visualize=visualize,
                        camera_width_world_units=128) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()
        run_loop.run_loop([agent], env, 2500)
        if True:
            env.save_replay(agent_cls.__name__)
Example #21
0
def run_thread(agent, map_name, visualize):
  with sc2_env.SC2Env(
    map_name=map_name,
    agent_race=FLAGS.agent_race,
    bot_race=FLAGS.bot_race,
    difficulty=FLAGS.difficulty,
    step_mul=FLAGS.step_mul,
    screen_size_px=(FLAGS.screen_resolution, FLAGS.screen_resolution),
    minimap_size_px=(FLAGS.minimap_resolution, FLAGS.minimap_resolution),
    visualize=visualize) as env:
    env = available_actions_printer.AvailableActionsPrinter(env)
    #TODO don't discard replay_buffer, sample (20) from (2000)
    pc_buffer = deque()
    if FLAGS.training:
      for recorder, is_done in random_run_loop( env, REPLAY_BUFFER_SIZE)
        pc_buffer.append(recorder)
        if is_done:
          break
        pc_buffer[-1][3][0].last() = True
    # Only for a single player!
    env.reset()
    replay_buffer = []
    for recorder, is_done in run_loop([agent], env, MAX_AGENT_STEPS):
        pc_buffer.append(recorder)
        pc_buffer. popleft()

        replay_buffer.append(recorder)
        if is_done:
          counter = 0
          with LOCK:
            global COUNTER
            COUNTER += 1
            counter = COUNTER
          # Learning rate schedule
          learning_rate = FLAGS.learning_rate * (1 - 0.9 * counter / FLAGS.max_steps)
          agent.update(replay_buffer, FLAGS.discount, pc_buffer, learning_rate, counter)
          replay_buffer = []
          if counter % FLAGS.snapshot_step == 1:
            agent.save_model(SNAPSHOT, counter)
          if counter >= FLAGS.max_steps:
            break
      elif is_done:
        obs = recorder[-1].observation
        score = obs["score_cumulative"][0]
        print('Your score is '+str(score)+'!')
    if FLAGS.save_replay:
      env.save_replay(agent.name)
Example #22
0
def run_thread(agent, players, map_name, visualize):
    """Run one thread worth of the environment with agents."""
    with sc2_env.SC2Env(
            map_name=map_name,
            players=players,
            agent_interface_format=sc2_env.parse_agent_interface_format(
                feature_screen=FLAGS.feature_screen_size,
                feature_minimap=FLAGS.feature_minimap_size,
                rgb_screen=FLAGS.rgb_screen_size,
                rgb_minimap=FLAGS.rgb_minimap_size,
                action_space=None,
                use_feature_units=FLAGS.use_feature_units,
                use_raw_units=FLAGS.use_raw_units),
            step_mul=FLAGS.step_mul,
            game_steps_per_episode=FLAGS.game_steps_per_episode,
            visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        replay_buffer = []
        for recorder, is_done in run_loop([agent], env, MAX_AGENT_STEPS):
            if FLAGS.training:
                replay_buffer.append(recorder)
                if is_done:
                    counter = 0
                    with LOCK:
                        global COUNTER
                        COUNTER += 1
                        counter = COUNTER
                    # Learning rate schedule
                    learning_rate = FLAGS.learning_rate * (
                        1 - 0.9 * counter / FLAGS.max_steps)
                    agent.update(replay_buffer, FLAGS.discount, learning_rate,
                                 counter)
                    replay_buffer = []
                    if counter % FLAGS.output_step == 1:
                        agent.save_model(OUTPUT, counter)
                    if counter >= FLAGS.max_steps:
                        break
                    obs = recorder[-1].observation
                    score = obs["score_cumulative"][0]
                    print('Your score is ' + str(score) + '!')
            elif is_done:
                obs = recorder[-1].observation
                score = obs["score_cumulative"][0]
                print('Your score is ' + str(score) + '!')
        if FLAGS.save_replay:
            env.save_replay(agent.name)
Example #23
0
def run_thread(agent, map_name, visualize):
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=FLAGS.agent_race,
                        bot_race=FLAGS.bot_race,
                        difficulty=FLAGS.difficulty,
                        step_mul=FLAGS.step_mul,
                        screen_size_px=(FLAGS.screen_resolution,
                                        FLAGS.screen_resolution),
                        minimap_size_px=(FLAGS.minimap_resolution,
                                         FLAGS.minimap_resolution),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)

        # Only for a single player!
        replay_buffer = []
        for recorder, is_done in run_loop([agent], env, MAX_AGENT_STEPS):
            if FLAGS.training:
                replay_buffer.append(recorder)
                if is_done:
                    counter = 0
                    with LOCK:
                        global COUNTER
                        COUNTER += 1
                        counter = COUNTER
                    # Learning rate schedule
                    learning_rate = FLAGS.learning_rate * (
                        1 - 0.9 * counter / FLAGS.max_steps)
                    agent.update(replay_buffer, FLAGS.discount, learning_rate,
                                 counter)
                    replay_buffer = []
                    if counter % FLAGS.snapshot_step == 1:
                        agent.save_model(SNAPSHOT, counter)
                    if counter >= FLAGS.max_steps:
                        break
                    if counter % 5 == 0:
                        obs = recorder[-1].observation
                        score = obs["score_cumulative"][0]
                        f = open('scorelog', 'a')
                        f.write(str(counter) + ' ' + str(score) + '\n')
                        f.close()

            elif is_done:
                obs = recorder[-1].observation
                score = obs["score_cumulative"][0]
                print('Your score is ' + str(score) + '!')
        if FLAGS.save_replay:
            env.save_replay(agent.name)
Example #24
0
def main(unused_argv):
    """Run an agent."""
    maps.get(MAP)

    with sc2_env.SC2Env(map_name=MAP,
                        agent_race=RACE,
                        bot_race=OPPONENT_RACE,
                        difficulty=DIFFICULTY,
                        step_mul=8,
                        game_steps_per_episode=0,
                        screen_size_px=(84, 84),
                        minimap_size_px=(64, 64),
                        visualize=True,
                        camera_width_world_units=128) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = DefeatZergling()  #random_agent.RandomAgent()
        run_loop.run_loop([agent], env, 2500)
Example #25
0
def run_thread(agent_cls, map_name, args):
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=args["agent_race"],
                        bot_race=args["bot_race"],
                        difficulty=args["difficulty"],
                        step_mul=args["step_mul"],
                        game_steps_per_episode=args["game_steps_per_episode"],
                        screen_size_px=(args["screen_resolution"],
                                        args["screen_resolution"]),
                        minimap_size_px=(args["minimap_resolution"],
                                         args["minimap_resolution"]),
                        visualize=True) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()
        run_loop.run_loop([agent], env, args["max_agent_steps"])
        if args["save_replay"]:
            env.save_replay(agent_cls.__name__)
Example #26
0
def pysc2_run_thread(agent_cls, map_name, visualize):
    """Original version of run_thread used for most agents, from pysc2.bin.agent"""
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=FLAGS.agent_race,
                        bot_race=FLAGS.bot_race,
                        difficulty=FLAGS.difficulty,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        screen_size_px=(FLAGS.screen_resolution,
                                        FLAGS.screen_resolution),
                        minimap_size_px=(FLAGS.minimap_resolution,
                                         FLAGS.minimap_resolution),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()
        pysc2_run_loop.run_loop([agent], env, FLAGS.max_agent_steps)
        if FLAGS.save_replay:
            env.save_replay(agent_cls.__name__)
Example #27
0
def run_thread(agent_cls, map_name, visualize, id, params, lock, session,
               graph, optimizer):
    with sc2_env.SC2Env(map_name=map_name,
                        agent_race=FLAGS.agent_race,
                        bot_race=FLAGS.bot_race,
                        difficulty=FLAGS.difficulty,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        screen_size_px=(FLAGS.screen_resolution,
                                        FLAGS.screen_resolution),
                        minimap_size_px=(FLAGS.minimap_resolution,
                                         FLAGS.minimap_resolution),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls(id, params, lock, session, graph, optimizer)
        run_loop.run_loop([agent], env, FLAGS.max_agent_steps)
        if FLAGS.save_replay:
            env.save_replay(agent_cls.__name__)
Example #28
0
def create_environment():
    game = sc2_env.SC2Env(
        map_name=FLAGS.map,
        step_mul=FLAGS.step_mul,
        agent_interface_format=AgentInterfaceFormat(
            feature_dimensions=Dimensions(
                (FLAGS.screen_resolution, FLAGS.screen_resolution),
                (FLAGS.minimap_resolution, FLAGS.minimap_resolution)),
            action_space=ActionSpace.FEATURES,
            camera_width_world_units=FLAGS.camera_width),
        visualize=FLAGS.render)

    # Here we create an hot encoded version of our actions (5 possible actions)
    # possible_actions = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]...]
    possible_actions = np.identity(len(actions.FUNCTIONS), dtype=int).tolist()
    env = available_actions_printer.AvailableActionsPrinter(game)

    return env, possible_actions
Example #29
0
def run_thread(agent_classes, players, map_name, visualize):
    with sc2_marl_env.SC2MarlEnv(
            map_name=map_name,
            players=players,
            step_mul=FLAGS.step_mul,
            game_steps_per_episode=FLAGS.game_steps_per_episode,
            feature_screen_size=FLAGS.feature_screen_size,
            feature_minimap_size=FLAGS.feature_minimap_size,
            rgb_screen_size=FLAGS.rgb_screen_size,
            rgb_minimap_size=FLAGS.rgb_minimap_size,
            action_space=(FLAGS.action_space
                          and sc2_marl_env.ActionSpace[FLAGS.action_space]),
            use_feature_units=FLAGS.use_feature_units,
            visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agents = [agent_cls() for agent_cls in agent_classes]
        run_loop.run_loop(agents, env, FLAGS.max_agent_steps)
        if FLAGS.save_replay:
            env.save_replay(agent_classes[0].__name__)
Example #30
0
def run_thread(currentWorker, max_episode_length, gamma, master_network, sess,
               coord, saver, agent_cls, map_name, visualize):
    with sc2_env.SC2Env(map_name,
                        agent_race=FLAGS.agent_race,
                        bot_race=FLAGS.bot_race,
                        difficulty=FLAGS.difficulty,
                        step_mul=FLAGS.step_mul,
                        game_steps_per_episode=FLAGS.game_steps_per_episode,
                        screen_size_px=(FLAGS.screen_resolution,
                                        FLAGS.screen_resolution),
                        minimap_size_px=(FLAGS.minimap_resolution,
                                         FLAGS.minimap_resolution),
                        visualize=visualize) as env:
        env = available_actions_printer.AvailableActionsPrinter(env)
        agent = agent_cls()
        currentWorker.work([agent], env, max_episode_length, gamma,
                           master_network, sess, coord, saver,
                           FLAGS.max_agent_steps)
        if FLAGS.save_replay:
            env.save_replay(agent_cls.__name__)