mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-12-12 10:30:37 +01:00
recoder adaption
This commit is contained in:
@@ -3,10 +3,10 @@ from pathlib import Path
|
||||
|
||||
import yaml
|
||||
from natsort import natsorted
|
||||
from stable_baselines3.common.evaluation import evaluate_policy
|
||||
from environments import helpers as h
|
||||
|
||||
from environments.factory.factory_dirt import DirtProperties, DirtFactory
|
||||
from environments.factory.factory_item import ItemProperties, ItemFactory
|
||||
from environments.factory.factory_dirt_item import DirtItemFactory
|
||||
from environments.logging.recorder import RecorderCallback
|
||||
|
||||
warnings.filterwarnings('ignore', category=FutureWarning)
|
||||
warnings.filterwarnings('ignore', category=UserWarning)
|
||||
@@ -14,27 +14,35 @@ warnings.filterwarnings('ignore', category=UserWarning)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
model_name = 'DQN_1631092016'
|
||||
model_name = 'PPO_1631187073'
|
||||
run_id = 0
|
||||
seed = 69
|
||||
out_path = Path(__file__).parent / 'debug_out'
|
||||
out_path = Path(__file__).parent / 'study_out' / 'e_1_1631709932'/ 'no_obs' / 'itemdirt'/'A2C_1631709932' / '0_A2C_1631709932'
|
||||
model_path = out_path / model_name
|
||||
|
||||
with (model_path / f'env_{model_name}.yaml').open('r') as f:
|
||||
with (out_path / f'env_params.json').open('r') as f:
|
||||
env_kwargs = yaml.load(f, Loader=yaml.FullLoader)
|
||||
env_kwargs.update(verbose=True, env_seed=seed)
|
||||
if False:
|
||||
env_kwargs.update(dirt_properties=DirtProperties(clean_amount=1, gain_amount=0.1, max_global_amount=20,
|
||||
max_local_amount=1, spawn_frequency=5, max_spawn_ratio=0.05,
|
||||
dirt_smear_amount=0.5),
|
||||
combin_agent_slices_in_obs=True, omit_agent_slice_in_obs=True)
|
||||
with ItemFactory(**env_kwargs) as env:
|
||||
env_kwargs.update(verbose=False, env_seed=seed, record_episodes=True)
|
||||
|
||||
# Edit THIS:
|
||||
env.seed(seed)
|
||||
model_files = list(natsorted((model_path / f'{run_id}_{model_name}').rglob('model_*.zip')))
|
||||
this_model = model_files[0]
|
||||
model_cls = next(val for key, val in model_map.items() if key in model_name)
|
||||
model = model_cls.load(this_model)
|
||||
evaluation_result = evaluate_policy(model, env, n_eval_episodes=100, deterministic=False, render=True)
|
||||
print(evaluation_result)
|
||||
this_model = out_path / 'model.zip'
|
||||
|
||||
model_cls = next(val for key, val in h.MODEL_MAP.items() if key in model_name)
|
||||
model = model_cls.load(this_model)
|
||||
|
||||
with RecorderCallback(filepath=Path() / 'recorder_out.json') as recorder:
|
||||
# Init Env
|
||||
with DirtItemFactory(**env_kwargs) as env:
|
||||
# Evaluation Loop for i in range(n Episodes)
|
||||
for episode in range(5):
|
||||
obs = env.reset()
|
||||
rew, done_bool = 0, False
|
||||
while not done_bool:
|
||||
action = model.predict(obs, deterministic=False)[0]
|
||||
env_state, step_r, done_bool, info_obj = env.step(action[0])
|
||||
recorder.read_info(0, info_obj)
|
||||
rew += step_r
|
||||
if done_bool:
|
||||
recorder.read_done(0, done_bool)
|
||||
break
|
||||
print(f'Factory run {episode} done, reward is:\n {rew}')
|
||||
print('all done')
|
||||
|
||||
Reference in New Issue
Block a user