Ejemplo n.º 1
0
    def __init__(self, config=config):

        # Reproduce training results
        np.random.seed(42)
        tf.random.set_seed(42)

        self._config = config
        current_dir = os.getcwd()
        model_save_path = current_dir + '.\Model'

        # Apply env wrappers
        env = gym.make(config['Environment Name'])

        env = preprocess.FrameSkip(env, 4)
        env = preprocess.GrayScaleImage(env,
                                        height=84,
                                        width=84,
                                        grayscale=True)
        env = preprocess.FrameStack(env, 4)

        NUM_STATE = env.observation_space.shape
        NUM_ACTIONS = env.env.action_space.n
        ACTION_SPACE = env.env.action_space

        network_params = (NUM_STATE, 1.0, NUM_ACTIONS, ACTION_SPACE)
        self.Test_Model = None
        if self._config['CNN type'] == 'large':
            self.Test_Model = AC_Model_Large(NUM_STATE,
                                             NUM_ACTIONS,
                                             self._config,
                                             is_training=False)
        else:
            self.Test_Model = AC_Model_Small(NUM_STATE,
                                             NUM_ACTIONS,
                                             self._config,
                                             is_training=False)

        # Load model if exists
        if not os.path.exists(model_save_path):
            os.makedirs(model_save_path)
        else:
            try:
                if (os.path.exists(model_save_path + "\checkpoint")):

                    self.Test_Model.load_model_weights()
                    print("Model restored.")
                    print("Now running environment...")
                    self._run(10000, env, self.Test_Model, True)

                else:
                    raise ("There is no model available")
            except:
                print("ERROR: There was an issue running the model!")
                raise
Ejemplo n.º 2
0
    def __init__(self,
                 render=False,
                 eps_start=EPS_START,
                 eps_end=EPS_STOP,
                 eps_steps=EPS_STEPS):
        threading.Thread.__init__(self)
        self.render = render

        # Make the super mario gym environment and apply wrappers
        self.env = gym.make(ENV)
        self.env = BinarySpaceToDiscreteSpaceEnv(self.env, SIMPLE_MOVEMENT)
        self.env = preprocess.GrayScaleImage(self.env,
                                             height=HIGHT,
                                             width=WIDTH,
                                             grayscale=True)
        # self.env = wrappers.Monitor(self.env, "./Super_Mario_AI/videos", force = True, write_upon_reset=True)
        self.agent = Agent(TEMPERATURE)
Ejemplo n.º 3
0
###### Main ######
##################

### Here is a list of the available environments ###

# SuperMarioBros-v0, SuperMarioBros-v1, SuperMarioBros-v2,
# SuperMarioBros-v3, SuperMarioBrosNoFrameskip-v0, SuperMarioBrosNoFrameskip-v1,
# SuperMarioBrosNoFrameskip-v2, SuperMarioBrosNoFrameskip-v3, SuperMarioBros2-v0,
# SuperMarioBros2-v1, SuperMarioBros2NoFrameskip-v0, SuperMarioBros2NoFrameskip-v1

# Get and build our test environment
env = gym_super_mario_bros.make('SuperMarioBros-v0')

# env = BinarySpaceToDiscreteSpaceEnv(env, COMPLEX_MOVEMENT)
env = BinarySpaceToDiscreteSpaceEnv(env, SIMPLE_MOVEMENT)
env = preprocess.GrayScaleImage(env, height=128, width=128, grayscale=True)
env = wrappers.Monitor(env,
                       "./Super_Mario_AI/videos",
                       force=True,
                       write_upon_reset=True)
number_actions = env.action_space.n

# Construct/Load a model
cnn = CNN(number_actions)
optimizer = optim.Adam(cnn.parameters(), lr=0.001)
loss = nn.MSELoss()
last_epoch = 1

# If there is a previous save
if os.path.exists("./Super_Mario_AI/Model/model.pth"):
    checkpoint = torch.load("./Super_Mario_AI/Model/model.pth")
Ejemplo n.º 4
0
    def __init__(self, config):

        self._config = config

        def get_available_gpus():
            local_device_protos = device_lib.list_local_devices()
            return [
                x.name for x in local_device_protos if x.device_type == 'GPU'
            ]

        print("GPU Available: ", tf.test.is_gpu_available())

        # GPU configuration
        gpus = tf.config.experimental.list_physical_devices('GPU')
        tf.config.threading.set_inter_op_parallelism_threads(0)
        tf.config.threading.set_intra_op_parallelism_threads(0)

        if gpus:

            try:

                # Currently, memory growth needs to be the same across GPUs
                for gpu in gpus:
                    tf.config.experimental.set_memory_growth(gpu, True)

            except RuntimeError as e:
                print(e)

        env_names = []
        for _ in range(self._config['Number of worker threads']):
            env_names.append(self._config['Environment Name'])

        # Configuration
        # current_dir = os.getcwd()
        self._model_save_path = '.\Model'
        self._video_save_path = '.\Videos'
        self.record = True

        # Make the super mario gym environments and apply wrappers
        self._envs = []
        collector = Collector()
        collector.set_dimensions(
            ["CMA", "EMA", "SMA", "LENGTH", "LOSS", 'TOTAL_EPISODE_REWARDS'])
        self._plot = AsynchronousPlot(collector, live=False)

        # Apply env wrappers
        counter = 0
        for env_name in env_names:
            env = gym.make(env_name)

            if env_name == 'SuperMarioBros-v0':
                env = JoypadSpace(env, COMPLEX_MOVEMENT)

            # Load wrapper class
            env = Stats(env, collector)
            if self._config['Wrapper class'] != '':
                env = env_wrapper_import(self._config['Wrapper class'], env)

            env = Monitor(env,
                          env.observation_space.shape,
                          savePath=self._video_save_path,
                          record=self.record)

            env = preprocess.GrayScaleImage(
                env, height=84, width=84, grayscale=self._config['Grayscale'])
            env = preprocess.FrameStack(env, 4)

            self._envs.append(env)

        self.NUM_STATE = self._envs[0].observation_space.shape
        self.NUM_ACTIONS = self._envs[0].env.action_space.n
        self.ACTION_SPACE = self._envs[0].env.action_space

        if not os.path.exists(self._video_save_path):
            os.makedirs(self._video_save_path)

        if not os.path.exists(self._model_save_path):
            os.makedirs(self._model_save_path)

        if not os.path.exists('.\stats'):
            os.makedirs('.\stats')