def loadGame(param_dir):
    gs = dict()
    with open(param_dir, 'rb') as f:
        shimon_hero_params = pickle.load(f)
    for paramater in shimon_hero_params:
        gs[paramater] = shimon_hero_params[paramater]
    game = sh.Game(gs)
    return game
Esempio n. 2
0
EXPLORE = 3000000.  # frames over which to anneal epsilon
FINAL_EPSILON = 0.0001  # final value of epsilon
INITIAL_EPSILON = 0.1  # starting value of epsilon
REPLAY_MEMORY = 50000  # number of previous transitions to remember
BATCH = 32  # size of minibatch
FRAME_PER_ACTION = 3  # This controls how many frames to wait before deciding on an action. If F_P_A = 1, then Shimon
# chooses a new action every tick, which causes erratic movements with no exploration middle spaces

# 1NN controlling 2 arms means you need to enumerate all the controls
action_dict = {0: -1, 1: 0, 2: 1}

img_rows, img_cols = 80, 80  # All images are downsampled to 80 x 80
img_channels = 4  # Stack 4 frames to infer movement

# Initialize instance of Shimon Hero game
game = sh.Game()  # Instantiate Shimon Hero game
timestr = time.strftime(
    "%m-%d_%H-%M-%S")  # save the current time to name the model
model_prefix = "2A1NN_" + timestr  # The prefix used to identify the model and time training was created

# Save the shimon_hero paramters corresponding to the model
shimon_hero_param = game.get_settings()
if SAVE_MODEL:
    param_path = "../saved_models/" + model_prefix + "_param.p"
    with open(param_path, 'wb') as f:
        pickle.dump(shimon_hero_param, f, pickle.HIGHEST_PROTOCOL)


def buildmodel():
    # To make a model with shared weights, need to use Keras Functional API, not Sequential API
    # Build the model using the same specifications as the DeepMind paper
    pass  # print (time.time() - last_time)


fr = 40.  # frame rate
s_per_frame = (1. / fr)

gs = dict()
with open('../saved_models/model_03-15_22-24-56_param.p', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    shimon_hero_params = pickle.load(f)

for paramater in shimon_hero_params:
    gs[paramater] = shimon_hero_params[paramater]

game = sh.Game(gs)
print(game.get_settings())

game_over = False
key_action = [0, 0, 0, 0]
last_time = time.time()
while not game_over:
    # type: (object) -> object
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            #print(event.key)
            if event.key == 49:  # 1 key down
                key_action[0] = -1
            elif event.key == 50:  # 2 key down
                key_action[0] = 1
            elif event.key == 113:  # q key down
import sched
import time

from shimon_hero import shimon_hero as sh

s = sched.scheduler(time.time, time.sleep)


def print_time(last_time):
    pass  # print (time.time() - last_time)


fr = 40.  # frame rate
s_per_frame = (1. / fr)

game = sh.Game()
print(game.get_settings())

game_over = False
key_action = [0, 0, 0, 0]
last_time = time.time()
while not game_over:
    # type: (object) -> object
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            #print(event.key)
            if event.key == 49:  # 1 key down
                key_action[0] = -1
            elif event.key == 50:  # 2 key down
                key_action[0] = 1
            elif event.key == 113:  # q key down
def main():
    model_dir, param_dir = loadModelPrefix()
    model = loadModel(model_dir)  # load model weights
    #game = loadGame(param_dir)  # load corresponding settings in shimon when model was trained
    game = sh.Game()
    playGame(model, game)
from shimon_hero import shimon_hero as sh

# Initialize instance of Shimon Hero game
game = sh.Game(human_playing=False)
NUM_ACTIONS = 4
FRAME_PER_ACTION = 1
ACTION_HOLD = 6
import numpy as np
import random
a_t = np.zeros(NUM_ACTIONS)
a_t[0] = 1

t = 0
while True:
    '''
    if t % ACTION_HOLD == 0:  # We need to give a chance for the agent to "hold" the z key over more than 1 frame
        print "--- NEW ACTION ---"
        a_t = np.zeros(NUM_ACTIONS)
        action_index = random.randrange(NUM_ACTIONS)
        a_t[action_index] = 1  # set action command with correct one-hot encoding
    '''

    if t % FRAME_PER_ACTION == 0:  # We need to give a chance for the agent to "hold" the z key over more than 1 frame
        #print "--- NEW ACTION ---"
        a_t = np.zeros(NUM_ACTIONS)
        action_index = random.randrange(NUM_ACTIONS)
        a_t[action_index] = 1  # set action command with correct one-hot encoding

    #print a_t
    image_data, r_t, game_over = game.next_action(a_t)
    print("r_t: ", r_t)