File size: 8,492 Bytes
a241478 4b36c77 a241478 4b36c77 a241478 4b36c77 a241478 ff58990 4b36c77 a241478 ff58990 a241478 ff58990 a241478 ff58990 a241478 ff58990 a241478 ff58990 4b36c77 a241478 ff58990 4b36c77 a241478 ff58990 a241478 ff58990 4b36c77 ff58990 a241478 4b36c77 ff58990 a241478 ff58990 a241478 ff58990 a241478 ff58990 a241478 ff58990 4b36c77 ff58990 4b36c77 ff58990 4b36c77 ff58990 4b36c77 ff58990 4b36c77 a241478 ff58990 4b36c77 ff58990 4b36c77 ff58990 4b36c77 ff58990 4b36c77 a241478 ff58990 a241478 ff58990 a241478 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #
# -*- coding: utf-8 -*-
import os
import gc
import sys
import json
import random
BASE_DIR = os.path.dirname(os.path.abspath('__file__'))
AGENTS_DIR = os.path.join(BASE_DIR,'agents')
sys.path.append(AGENTS_DIR)
from agents.PDPPOAgent_one_critic import PDPPOAgent_one_critic
from agents.PPOAgent_two_critics import PPOAgent_two_critics
from agents.PDPPOAgent import PDPPOAgent
from agents.PPOAgent import PPOAgent
from agents.stableBaselineAgents import StableBaselineAgent
from test_functions import test_agents
import numpy as np
import torch
from envs import SimplePlant
from scenarioManager.stochasticDemandModel import StochasticDemandModel
#'15items_5machines_i100','25items_10machines'
if __name__ == '__main__':
# experiments = ['15items_5machines_t100_i100']
# experiments = ['20items_10machines_t100_i100']
# experiments = ['25items_10machines_t100_i100']
experiments = ['25items_15machines_t100_i100']
#experiments = ['15items_5machines_t100_i100', '20items_10machines_t100_i100']
for experiment_name in experiments:
for i in range(20,21):
# Setting the seeds
np.random.seed(i)
random.seed(i)
torch.manual_seed(i)
# If you are using CUDA (PyTorch with GPU support)
if torch.cuda.is_available():
torch.cuda.manual_seed(i)
torch.cuda.manual_seed_all(i) # if you are using multi-GPU.
# Environment setup load:
file_path = os.path.abspath(f"./cfg_env/setting_{experiment_name}.json")
fp = open(file_path, 'r')
settings = json.load(fp)
fp.close()
# Models setups:
stoch_model = StochasticDemandModel(settings)
settings['time_horizon'] = 100
env = SimplePlant(settings, stoch_model)
settings['dict_obs'] = False
setting_sol_method = {
'discount_rate': 0.99,
'experiment_name': experiment_name,
'parallelization': False,
'model_name': 'PPO',
'branching_factors': [4, 2, 2],
'dict_obs': False # To be employed if dictionary observations are necessary
}
# Parameters for the ADPHS:
setting_sol_method['regressor_name'] = 'plain_matrix_I2xM1'
setting_sol_method['discount_rate'] = 0.99
setting_sol_method['run'] = i
agents = []
# Parameters for the RL:
training_epochs_RL = 10000 # 30000
env = SimplePlant(settings, stoch_model)
# Number of test execution (number of complet environment iterations)
nreps = 100
###########################################################################
# Post-decision PPO - Dual critic
###########################################################################
base_model_name = 'PDPPO'
pdppo_agent = PDPPOAgent(
env,
setting_sol_method
)
pdppo_agent.learn(n_episodes=training_epochs_RL*settings['time_horizon'] ) # Each ep with 200 steps
#load best agent before appending in the test list
BEST_MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath('__file__')),'logs',f'best_{base_model_name}_{experiment_name}','best_model')
pdppo_agent.load_agent(BEST_MODEL_DIR) # For training purposes
agents.append(("PDPPO", pdppo_agent))
###########################################################################
# PPO
###########################################################################
base_model_name = 'PPO'
ppo_agent = PPOAgent(
env,
setting_sol_method
)
ppo_agent.learn(n_episodes=training_epochs_RL*settings['time_horizon'] ) # Each ep with 200 steps
#load best agent before appending in the test list
BEST_MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath('__file__')),'logs',f'best_{base_model_name}_{experiment_name}','best_model')
ppo_agent.load_agent(BEST_MODEL_DIR) # For training purposes
agents.append(("PPO", ppo_agent))
###########################################################################
# Post-decision PPO - Single Critic
###########################################################################
base_model_name = 'PDPPO_one_critic'
pdppo_agent_one_critic = PDPPOAgent_one_critic(
env,
setting_sol_method
)
pdppo_agent_one_critic.learn(n_episodes=training_epochs_RL*settings['time_horizon'] ) # Each ep with 200 steps
#load best agent before appending in the test list
BEST_MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath('__file__')),'logs',f'best_{base_model_name}_{experiment_name}','best_model')
pdppo_agent_one_critic.load_agent(BEST_MODEL_DIR) # For training purposes
agents.append(("PDPPO_one_critic", pdppo_agent_one_critic))
###########################################################################
# PPO - two critic
###########################################################################
# base_model_name = 'PPO_two_critics'
# ppo_agent_two_critics = PPOAgent_two_critics(
# env,
# setting_sol_method
# )
# ppo_agent_two_critics.learn(n_episodes=training_epochs_RL*settings['time_horizon'] ) # Each ep with 200 steps
# #load best agent before appending in the test list
# BEST_MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath('__file__')),'logs',f'best_{base_model_name}_{experiment_name}','best_model')
# ppo_agent_two_critics.load_agent(BEST_MODEL_DIR) # For training purposes
# agents.append(("PPO_two_critics", ppo_agent_two_critics))
###########################################################################
# RL A2C
###########################################################################
# setting_sol_method['multiagent'] = False
# setting_sol_method['parallelization'] = False
# base_model_name = 'A2C'
# setting_sol_method['parallelization'] = False
# env = SimplePlant(settings, stoch_model)
# setting_sol_method['model_name'] = base_model_name
# rl_agent = StableBaselineAgent(
# env,
# setting_sol_method
# )
# rl_agent.learn(epochs=training_epochs_RL) # Each ep with 200 steps
# #load best agent before appending in the test list
# BEST_MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath('__file__')),'logs',f'best_{base_model_name}_{experiment_name}','best_model')
# rl_agent.load_agent(BEST_MODEL_DIR)
# agents.append(("A2C", rl_agent))
###########################################################################
#TESTING
# settings['dict_obs'] = False
# setting_sol_method['multiagent'] = False
# setting_sol_method['dict_obs'] = False
# env = SimplePlant(settings, stoch_model)
# setting_sol_method['experiment_name'] = experiment_name
# dict_res = test_agents(
# env,
# agents=agents,
# n_reps=nreps,
# setting_sol_method = setting_sol_method,
# use_benchmark_PI=False
# )
# for key,_ in agents:
# cost = dict_res[key,'costs']
# print(f'\n Cost in {nreps} iterations for the model {key}: {cost}')
# try:
# cost = dict_res['PI','costs']
# print(f'\n Cost in {nreps} repetitions for the model PI: {cost}')
# except:
# pass
#del multiagent
del env
gc.collect()
|