File size: 5,005 Bytes
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 | # -*- coding: utf-8 -*-
import os
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from envs.simplePlant import SimplePlant
from agents import PerfectInfoAgent
def _test_agent(env: SimplePlant, agent, label, verbose=True):
actions = []
setup_costs = []
lost_sales = []
holding_costs = []
demands = []
observations = []
# TEST
if verbose: print("******")
if verbose: print(f" {label} ")
if verbose: print("******")
obs = env.reset_time()
if verbose: env.render()
done = False
while not done:
action = agent.get_action(obs)
if verbose: print(">>> action:", action)
obs, _, done, info = env.step(action,verbose=verbose)
demands.append(env.demand)
actions.append(action)
observations.append(obs)
setup_costs.append(info['setup_costs'])
lost_sales.append(info['lost_sales'])
holding_costs.append(info['holding_costs'])
if verbose: env.render()
return actions, setup_costs, lost_sales, holding_costs , demands, observations
def plot_comparison(env, dict_results, col_dict={}):
N_PLOT = 3
if env.n_machines == 1:
N_PLOT = 4
plt.subplot(N_PLOT,1,1)
for key in dict_results:
plt.plot(dict_results[key]['setup_costs'], col_dict[key] if key in col_dict else "-b", label=key)
plt.legend()
plt.ylabel('setup')
plt.subplot(N_PLOT,1,2)
for key in dict_results:
plt.plot(
dict_results[key]['lost_sales'],
col_dict[key] if key in col_dict else "-b",
label=key
)
plt.legend()
plt.ylabel('lost sales')
plt.subplot(N_PLOT,1,3)
for key in dict_results:
plt.plot(
dict_results[key]['holding_costs'],
col_dict[key] if key in col_dict else "-b",
label=key
)
plt.legend()
plt.ylabel('holding costs')
if env.n_machines == 1:
plt.subplot(N_PLOT,1,4)
for key in dict_results:
plt.plot(
dict_results[key]['actions'],
col_dict[key] if key in col_dict else "-b",
label=key
)
plt.legend()
plt.ylabel('action')
plt.show()
def test_agents(env: SimplePlant, agents: list, n_reps: int =10, use_benchmark_PI=True, verbose=False, setting_sol_method = ''):
dict_res = {}
key_files = ['actions','setup_costs','lost_sales','holding_costs','demands','observations']
for key, _ in agents:
for key_file in key_files:
dict_res[(key,key_file)] = []
dict_res[(key,'costs')] = 0
if use_benchmark_PI:
for key_file in key_files:
dict_res[('PI',key_file)] = []
dict_res[('PI','costs')] = 0
for _ in tqdm(range(n_reps)):
# create a new scenarios
env.reset()
if use_benchmark_PI:
pi_agent = PerfectInfoAgent(env, setting_sol_method)
actions, setup_costs, lost_sales, holding_costs, demands, observations = _test_agent(
env, pi_agent, label='PI', verbose=verbose
)
test_output = [actions, setup_costs, lost_sales, holding_costs, demands, observations]
# appends the data from the tests
for i in range(len(test_output)):
dict_res[('PI',key_files[i])].append(test_output[i])
if verbose: print("***")
key = 'PI'
dict_res[(key,'costs')] += (sum(setup_costs) / n_reps)
dict_res[(key,'costs')] += (sum(lost_sales) / n_reps)
dict_res[(key,'costs')] += (sum(holding_costs) / n_reps)
# run each agent on the same scenario
for key, agent in agents:
actions, setup_costs, lost_sales, holding_costs,demands , observations = _test_agent(
env, agent, label=key, verbose=verbose
)
test_output = [actions, setup_costs, lost_sales, holding_costs,demands , observations]
dict_res[(key,'costs')] += (sum(setup_costs) / n_reps)
dict_res[(key,'costs')] += (sum(lost_sales) / n_reps)
dict_res[(key,'costs')] += (sum(holding_costs) / n_reps)
if verbose: print("***")
for i in range(len(test_output)):
dict_res[(key,key_files[i])].append(test_output[i])
# saves the files from the tests
save_files(
dict_res,
setting_sol_method
)
return dict_res
def save_files(dict_res,setting_sol_method):
try:
experiment_name = setting_sol_method['experiment_name']
for key in dict_res:
try:info = np.stack(dict_res[key])
except:info = dict_res[key]
np.save(
os.path.join(
'results',
f'{key[0]}_{experiment_name}_{key[1]}_test'
),
info
)
except:
print('Fail to save the files.')
|