Commit db4aae29 authored by william_guss's avatar william_guss Committed by shivam_khandelwal
Browse files

Update test.py

parent c93005e4
# Simple env test.
import json
import select
import time
......@@ -6,14 +6,18 @@ import logging
import os
import threading
from typing import Callable
import aicrowd_helper
import gym
import minerl
import abc
import numpy as np
import coloredlogs
coloredlogs.install(logging.DEBUG)
# All the evaluations will be evaluated on MineRLObtainDiamond-v0 environment
# All the evaluations will be evaluated on MineRLObtainDiamondVectorObf-v0 environment
MINERL_GYM_ENV = os.getenv('MINERL_GYM_ENV', 'MineRLObtainDiamondVectorObf-v0')
MINERL_MAX_EVALUATION_EPISODES = int(os.getenv('MINERL_MAX_EVALUATION_EPISODES', 5))
......@@ -23,63 +27,164 @@ EVALUATION_THREAD_COUNT = os.getenv('EPISODES_EVALUATION_THREAD_COUNT', 4)
EVALUATION_EPISODES_PROCESSED = 0
EVALUATION_EPISODES_PROCESSED_LOCK = threading.Lock()
class EpisodeDone(Exception):
pass
class Episode(gym.Env):
"""A class for a single episode.
"""
def __init__(self, env):
self.env = env
self.action_space = env.action_space
self.observation_space = env.observation_space
self._done = True
def reset(self):
if not self._done:
return self.env.reset()
def step(self, action):
r,s,d,i = self.env.step(action)
if d:
self._done = True
raise EpisodeDone()
else:
return r,s,d,i
# DO NOT CHANGE THIS CLASS, THIS IS THE BASE CLASS FOR YOUR AGENT.
class MineRLAgentBase(abc.ABC):
"""
To compete in the competition, you are required to implement a
SUBCLASS to this class.
YOUR SUBMISSION WILL FAIL IF:
* Rename this class
* You do not implement a subclass to this class
This class enables the evaluator to run your agent in parallel,
so you should load your model only once in the 'load_agent' method.
"""
class MineRLInference:
@abc.abstractmethod
def load_agent(self):
"""
This method is called at the beginning of the evaluation.
You should load your model and do any preprocessing here.
THIS METHOD IS ONLY CALLED ONCE AT THE BEGINNING OF THE EVALUATION.
DO NOT LOAD YOUR MODEL ANYWHERE ELSE.
"""
raise NotImplementedError()
@abc.abstractmethod
def run_agent_on_episode(self, single_episode_env : Episode):
"""This method runs your agent on a SINGLE episode.
You should just implement the standard environment interaction loop here:
obs = env.reset()
while not done:
env.step(self.agent.act(obs))
...
NOTE: This method will be called in PARALLEL during evaluation.
So, only store state in LOCAL variables.
For example, if using an LSTM, don't store the hidden state in the class
but as a local variable to the method.
Args:
env (gym.Env): The env your agent should interact with.
"""
raise NotImplementedError()
#######################
# YOUR CODE GOES HERE #
#######################
class MineRLMatrixAgent(MineRLAgentBase):
"""
Random agent inference, implement this class for testing/inference phase.
An example random agent.
Note, you MUST subclass MineRLAgentBase.
"""
def __init__(self):
# Sample code for illustration, add your code below to run in test phase.
# Load trained model from train/ directory, any preprocessing required, etc.
pass
def load_agent(self):
"""In this example we make a random matrix which
we will use to multiply the state by to produce an action!
This is where you could load a neural network.
"""
# Some helpful constants from the environment.
flat_video_obs_size = 64*64
obs_size = 64
ac_size = 64
self.matrix = np.random.random(size=(ac_size, flat_video_obs_size + obs_size))*2 -1
self.flatten_obs = lambda obs: np.concatenate(obs['pov'].flatten()/255.0, obs['vector'].flatten())
self.act = lambda flat_obs: np.clip(self.matrix.dot(flat_obs), -1,1)
def inference(self):
# Implement per-episodee inference code
obs = self.env.reset()
def run_agent_on_episode(self, single_episode_env : Episode):
"""Runs the agent on a SINGLE episode.
Args:
single_episode_env (Episode): The episode on which to run the agent.
"""
obs = single_episode_env.reset()
done = False
while not done:
random_act = self.env.action_space.noop()
random_act['camera'] = [0, 0.3]
random_act['back'] = 0
random_act['forward'] = 1
random_act['jump'] = 1
random_act['attack'] = 1
obs, reward, done, info = self.env.step(random_act)
def run(self):
global EVALUATION_THREAD_COUNT
threads = [threading.Thread(target=self.evaluation_thread) for _ in range(EVALUATION_THREAD_COUNT)]
for t in threads:
t.start()
for t in threads:
t.join()
def evaluation_thread(self):
global EVALUATION_EPISODES_PROCESSED_LOCK, EVALUATION_EPISODES_PROCESSED, MINERL_MAX_EVALUATION_EPISODES
self.env = gym.make(MINERL_GYM_ENV)
while True:
run_next_episode = False
EVALUATION_EPISODES_PROCESSED_LOCK.acquire()
if EVALUATION_EPISODES_PROCESSED < MINERL_MAX_EVALUATION_EPISODES:
run_next_episode = True
EVALUATION_EPISODES_PROCESSED += 1
EVALUATION_EPISODES_PROCESSED_LOCK.release()
if run_next_episode:
self.inference()
else:
break
self.env.close()
_,obs,done,_ = single_episode_env.step(self.act(self.flatten_obs(obs)))
def main():
minerl_inference_obj = MineRLInference()
minerl_inference_obj.run()
class MineRLRandomAgent(MineRLAgentBase):
"""A random agent"""
def load_agent(self):
pass # Nothing to do, this agent is a random agent.
def run_agent_on_episode(self, single_episode_env : Episode):
obs = single_episode_env.reset()
done = False
while not done:
random_act = single_episode_env.action_space.sample()
single_episode_env.step()
#####################################################################
# IMPORTANT: SET THIS VARIABLE WITH THE AGENT CLASS YOU ARE USING #
######################################################################
AGENT_TO_TEST = None # MineRLMatrixAgent, MineRLRandomAgent, YourAgentHere
####################
# EVALUATION CODE #
####################
def main():
agent = AGENT_TO_TEST()
assert isinstance(agent, MineRLAgentBase)
agent.load_agent()
# Create the parallel envs (sequentially to prevent issues!)
envs = [gym.make(MINERL_GYM_ENV) for _ in range(EVALUATION_THREAD_COUNT)]
episodes_per_thread = MINERL_MAX_EVALUATION_EPISODES // EVALUATION_THREAD_COUNT
# A simple funciton to evaluate on episodes!
def evaluate(i, env):
print("[{}] Starting evaluator.".format(i))
for i in range(episodes_per_thread):
try:
agent.run_agent_on_episode(Episode(env))
except EpisodeDone:
print("[{}] Episode complete".format(i))
pass
evaluator_threads = [threading.Thread(target=evaluate, args=(i, envs[i])) for i in range(EVALUATION_THREAD_COUNT)]
for thread in evaluator_threads:
thread.start()
# wait fo the evaluation to finish
for thread in evaluator_threads:
thread.join()
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment