Пример #1
0
def main(args):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.InteractiveSession(config=config)

    import lf2gym
    from lf2gym import WebDriver, Character
    env = lf2gym.make(startServer=True,
                      driverType=WebDriver.Chrome,
                      characters=[Character.Firen, Character[args.player]],
                      versusPlayer=True)
    env.start_recording()

    num_actions = env.action_space.n
    global_network = Worker(-1,
                            num_actions,
                            1e-4,
                            sess,
                            AIchar=env.characters[0])

    saver = tf.train.Saver()
    latest_ckpt = tf.train.latest_checkpoint(args.loadpath)
    saver.restore(sess, latest_ckpt)
    print("restore from", latest_ckpt)

    global_network.test(env)
Пример #2
0
        from brain.fbrain import DQN
    elif args.mode == 'mix':
        from brain.mbrain import DQN
    else:
        print("Error mode! Use picture instead.")
        from brain.pbrain import DQN

    # game setup
    AGENT = 'Firen'
    OPPOENENT = 'Dennis'

    # env setup
    env = lf2gym.make(
        startServer=True,
        wrap='skip4',
        driverType=lf2gym.WebDriver.PhantomJS,
        characters=[lf2gym.Character[AGENT], lf2gym.Character[OPPOENENT]],
        difficulty=lf2gym.Difficulty.Crusher,
        debug=True,
        action_options=['Basic', 'AJD'])

    options = env.get_reset_options()
    print('Original reset options: %s' % options)
    options['hp_full'] = 100
    options['mp_start'] = 250
    print('Custom reset options: %s' % options)

    # initial obervation number
    mode = args.mode
    if mode == 'picture':
        state_n = env.observation_space.n
    elif mode == 'feature':
Пример #3
0
    t = time()
    print('%s: %f seconds.' % (msg, t - s))
    s = t


# Add import path and import the lf2gym
import os, sys
sys.path.append(os.path.abspath('..'))

# Import lf2gym
import lf2gym
# Make an env
env = lf2gym.make(
    startServer=True,
    wrap='skip4',
    driverType=lf2gym.WebDriver.PhantomJS,
    characters=[lf2gym.Character[AGENT], lf2gym.Character[OPPOENENT]],
    difficulty=lf2gym.Difficulty.Dumbass,
    debug=True)

# Set the reset options
options = env.get_reset_options()
print('Original reset options: %s' % options)
options['hp_full'] = 100
options['mp_start'] = 250
print('Custom reset options: %s' % options)

# Reset the env
env.reset(options)

# Start to record the screen
def main(args):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.intra_op_parallelism_threads = 8
    config.inter_op_parallelism_threads = 8
    sess = tf.InteractiveSession(config=config)

    if args.train:
        workers = []
        import lf2gym
        from lf2gym import Difficulty

        # url = 'http://127.0.0.1:' + str(args.port)
        envs = [
            lf2gym.make(startServer=True,
                        difficulty=Difficulty.Challengar,
                        port=args.port + i) for i in range(args.thread)
        ]
        # [env.reduce_action_space(13) for env in envs]
        try:
            num_actions = envs[0].action_space.n
            global_network = Worker(-1,
                                    num_actions,
                                    args.learning_rate,
                                    sess,
                                    AIchar=envs[0].characters[0])

            for i in range(args.thread):
                workers.append(
                    Worker(i,
                           num_actions,
                           args.learning_rate,
                           sess,
                           glob_net=global_network,
                           save_path=args.savepath,
                           AIchar=envs[i].characters[0]))
            saver = tf.train.Saver(max_to_keep=35)

            if args.retrain:
                latest_ckpt = tf.train.latest_checkpoint(args.loadpath)
                saver.restore(sess, latest_ckpt)
                print("restore from", latest_ckpt)
            else:
                sess.run(tf.global_variables_initializer())

            if not os.path.exists(args.savepath):
                os.mkdir(args.savepath)

            path = args.savepath + 'output_thread.log'
            t_writer = open(path, 'w')
            t_writer.write(args.comment)
            t_writer.write('\nstart training a3c, thread 1 log...\n')
            actor_learner_threads = [
                threading.Thread(target=workers[i].train,
                                 args=(envs[i], args.checkpoint_interval,
                                       saver, t_writer))
                for i in range(args.thread)
            ]
            for t in actor_learner_threads:
                t.start()
            for t in actor_learner_threads:
                t.join()

        except KeyboardInterrupt:
            print("W: interrupt received, stopping...")

        finally:
            [env.close() for env in envs]

    if args.test:
        import lf2gym  # difficulty='CRUSHER 1.0'
        from lf2gym import Character, Difficulty
        # env = lf2gym.make(startServer=False, autoStart=True, rewardList=['hp'], difficulty=Difficulty.Challengar, wrap='skip4' )
        env = lf2gym.make(startServer=True,
                          difficulty=Difficulty.Challengar,
                          port=args.port,
                          action_options=['Basic', 'AJD', 'No Combos'])
        env.start_recording()
        num_actions = env.action_space.n
        global_network = Worker(-1,
                                num_actions,
                                args.learning_rate,
                                sess,
                                AIchar=env.characters[0])

        saver = tf.train.Saver()

        latest_ckpt = tf.train.latest_checkpoint(args.loadpath)
        saver.restore(sess, latest_ckpt)
        print("restore from", latest_ckpt)

        global_network.test(env, render=False)

    if args.test_chrome:
        import lf2gym
        from lf2gym import WebDriver, Character, Difficulty
        env = lf2gym.make(startServer=False,
                          driverType=WebDriver.Chrome,
                          canvasSize=(600, 800),
                          characters=[Character.Firen, Character.Freeze],
                          versusPlayer=False,
                          difficulty=Difficulty.Crusher)
        # env.reduce_action_space(13)

        num_actions = env.action_space.n
        global_network = Worker(-1,
                                num_actions,
                                args.learning_rate,
                                sess,
                                AIchar=env.characters[0])

        saver = tf.train.Saver()
        latest_ckpt = tf.train.latest_checkpoint(args.loadpath)
        saver.restore(sess, latest_ckpt)
        print("restore from", latest_ckpt)

        global_network.test(env, render=False)
Пример #5
0
parser.add_argument("--interval", default=0.2, help="your enemy")
args = parser.parse_args()

# Add import path and import the lf2gym
import os, sys

sys.path.append(os.path.abspath('..'))

# Import lf2gym
import lf2gym
from time import sleep
# Make an env
env = lf2gym.make(startServer=True,
                  driverType=lf2gym.WebDriver.Chrome,
                  characters=[
                      lf2gym.Character[args.opponent],
                      lf2gym.Character[args.player]
                  ],
                  versusPlayer=True)
# Reset the env
env.reset()

# Game starts
done = False
while not done:
    # Sample a random action
    action = env.action_space.sample()
    # Take the chosen action
    _, reward, done, _ = env.step(action)
    print('Enemy took action %d.' % action)
    sleep(args.interval)
Пример #6
0
         player1_info.get('vy'), player1_info.get('vz'),
         player2_info.get('hp'), player2_info.get('mp'), player2_info.get('x'),
         player2_info.get('y'), player2_info.get('z'), player2_info.get('vx'),
         player2_info.get('vy'), player2_info.get('vz')))
    return player_info


if __name__ == "__main__":
    # initialize gym environment and the agent
    env = lf2gym.make(
        startServer=True,
        driverType=lf2gym.WebDriver.Chrome,
        background=lf2gym.Background.HK_Coliseum,
        characters=[args.player, args.opponent],  #[Me/AI, bot]
        difficulty=lf2gym.Difficulty.Crusher,
        action_options=[
            'Basic', 'AJD', 'Full Combos'
        ],  # ['Basic', 'AJD', 'Full Combos']   # defines action space
        headless=HEADLESS,
        rewardList=['hp'],
        versusPlayer=False
    )  # versusPlayer= False means Agent is playing against bot instedad of user

    env.start_recording()

    state_size_x = env.observation_space.n[0]
    state_size_y = env.observation_space.n[1]
    print("state_size: (" + str(state_size_x) + ", " + str(state_size_y) + ")")
    action_size = int(env.action_space.n)
    print("action_size: " + str(action_size))