diff --git a/analysis/hist.py b/analysis/hist.py index 33a18da..d708ed2 100644 --- a/analysis/hist.py +++ b/analysis/hist.py @@ -23,6 +23,7 @@ import os from glob import glob + # third-party libs import dill import numpy as np @@ -33,13 +34,15 @@ from ilurl.envs.base import TrafficLightQLEnv ROOT_DIR = os.environ['ILURL_HOME'] -EMISSION_DIR = f"{ROOT_DIR}/data/emissions/" -CONFIG_DIR = ('4545', '5040', '5434', '6030') +# EMISSION_DIR = f"{ROOT_DIR}/data/emissions/" +EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/" +# CONFIG_DIR = ('4545', '5040', '5434', '6030') +CONFIG_DIR = ('6030',) if __name__ == '__main__': # this loop acumulates experiments - ext = '.9000.w.info.json' + ext = '.9000.l.info.json' states = defaultdict(list) for config_dir in CONFIG_DIR: @@ -55,11 +58,22 @@ env = TrafficLightQLEnv.load(f"{filename}.pickle") # observation spaces - observation_spaces_per_cycle = output['observation_spaces'] - for observation_space in observation_spaces_per_cycle: - for i, values in enumerate(env.ql_params.split_space(observation_space)): + if 'cycle' not in output: + # 0x00, 0x01, 0x02, 0x03 + # deprecate + observation_spaces = [output['observation_spaces']] + else: + observation_spaces = output['observation_spaces'] + + for observation_space in observation_spaces: + variables = \ + env.ql_params.split_space(observation_space) + for i, values in enumerate(variables): + + () label = env.ql_params.states_labels[i] - states[label] += values + states[label] += \ + [val for value in values for val in value] # plot building num_bins = 50 @@ -72,7 +86,6 @@ # mean and standard deviation of the distribution mu = np.mean(values) sigma = np.std(values) - # the histogram of the data values_normalized = [ round((v - mu) / sigma, 2) for v in values diff --git a/analysis/q.py b/analysis/q.py index a13861e..e2e2b36 100644 --- a/analysis/q.py +++ b/analysis/q.py @@ -10,16 +10,14 @@ ROOT_DIR = os.environ['ILURL_HOME'] # EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x02/" -EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x03/" +EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/" # CONFIG_DIRS = ('4545', '5040', '5434', '6030') CONFIG_DIRS = ('6030',) -filenames = ('intersection_20200222-0900161582362016.287294.Q.1-49', - 'intersection_20200222-0900371582362037.617882.Q.1-49', - 'intersection_20200222-0900411582362041.078803.Q.1-49', - 'intersection_20200222-0900481582362048.594145.Q.1-49', - 'intersection_20200222-0900511582362051.183386.Q.1-49') +filenames = ('intersection_20200227-2043431582836223.796797.Q.1-99', + 'intersection_20200227-2128041582838884.952684.Q.1-100', + 'intersection_20200227-2128511582838931.2348.Q.1-100') def qdist(Q, Q1): diff --git a/analysis/rewards.py b/analysis/rewards.py index 788e732..0cbb788 100644 --- a/analysis/rewards.py +++ b/analysis/rewards.py @@ -1,7 +1,7 @@ """Analyses aggregate experiment files e.g info and envs""" __author__ = 'Guilherme Varela' __date__ = '2020-02-13' -import pdb + import os import argparse import json @@ -144,13 +144,13 @@ def str2bool(v): color = 'tab:blue' ax1.set_ylabel('Avg. speed', color=color) - ax1.plot(vels, color=color) + ax1.plot(np.mean(vels, axis=1), color=color) ax1.tick_params(axis='y', labelcolor=color) color = 'tab:red' ax2 = ax1.twinx() ax2.set_ylabel('Avg. vehicles', color=color) - ax2.plot(vehs, color=color) + ax2.plot(np.mean(vehs, axis=1), color=color) ax2.tick_params(axis='y', labelcolor=color) plt.title(f'{phase_split}:speed and count\n({db_type}, n={num_dbs})') @@ -164,7 +164,7 @@ def str2bool(v): ax1.set_ylabel('Avg. Reward per Cycle', color=color) ax2.tick_params(axis='y', labelcolor=color) - ax1.plot(rets, color=color) + ax1.plot(np.mean(rets, axis=1), color=color) plt.title(f'{phase_split}:avg. cycle return\n({db_type}, n={num_dbs})') plt.savefig(f'{files_dir}{phase_split}_{db_type}_rewards.png') plt.show() @@ -177,8 +177,6 @@ def str2bool(v): ax1.set_xlabel(f'Cycles ({cycle_time} sec)') ax1.set_ylabel('ratio optimal action') - import pdb - pdb.set_trace() cumacts = np.cumsum(acts == optact, axis=0) weights = np.arange(1, len(acts) + 1) diff --git a/analysis/scatter.py b/analysis/scatter.py index 8116d52..5656775 100644 --- a/analysis/scatter.py +++ b/analysis/scatter.py @@ -16,6 +16,7 @@ import os from glob import glob + # third-party libs import dill import numpy as np @@ -27,13 +28,13 @@ ROOT_DIR = os.environ['ILURL_HOME'] # EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x02/" -EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x03/" +EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/" # CONFIG_DIR = ('4545', '5040', '5434', '6030') CONFIG_DIR = ('6030',) if __name__ == '__main__': # this loop acumulates experiments - ext = '.450000.l.info.json' + ext = '.9000.l.info.json' phases = defaultdict(list) labels = [] desired_velocity = None @@ -66,11 +67,17 @@ category_counts = env.ql_params.category_counts # observation spaces - observation_spaces_per_cycle = output['observation_spaces'] - for intersection_space in observation_spaces_per_cycle: - for phase_space in intersection_space: - for i, phase in enumerate(phase_space): - phases[i] += [phase] + if 'cycle' not in output: + # deprecate: 0x00, 0x01, 0x02, 0x03 + observation_spaces = [output['observation_spaces']] + else: + observation_spaces = output['observation_spaces'] + + for observation_space in observation_spaces: + for intersection_space in observation_space: + for phase_space in intersection_space: + for i, phase in enumerate(phase_space): + phases[i] += [phase] _, ax = plt.subplots() for i, label in enumerate(labels): diff --git a/ilurl/core/experiment.py b/ilurl/core/experiment.py index 3941d83..f85ba68 100755 --- a/ilurl/core/experiment.py +++ b/ilurl/core/experiment.py @@ -18,7 +18,7 @@ import tempfile import time from collections import defaultdict -import pdb + from tqdm import tqdm diff --git a/ilurl/core/ql/choice.py b/ilurl/core/ql/choice.py index 77cd022..84af385 100644 --- a/ilurl/core/ql/choice.py +++ b/ilurl/core/ql/choice.py @@ -1,7 +1,7 @@ """The module helps define the q learning dictionary""" __author__ = "Guilherme Varela" __date__ = "2019-07-25" -import pdb + from numpy import argmax, sqrt, log, isnan from numpy.random import choice, rand diff --git a/ilurl/core/ql/reward.py b/ilurl/core/ql/reward.py index 9f0221c..94abaca 100644 --- a/ilurl/core/ql/reward.py +++ b/ilurl/core/ql/reward.py @@ -2,7 +2,7 @@ __author__ = "Guilherme Varela" __date__ = "2019-07-26" -import pdb + import numpy as np REWARD_TYPES = ('fix', 'weighted_average', 'score', 'target_velocity')