|
| 1 | +''' |
| 2 | +
|
| 3 | +Welcome to Smart Cab Log Analyser !!! |
| 4 | +
|
| 5 | +This is a simple script to do ETL on Agent.py log/output data to analyse the performance of Agent Learning |
| 6 | +
|
| 7 | +* trail : success or failure |
| 8 | +* deadline : last deadline value |
| 9 | +* reward : last reward value |
| 10 | +
|
| 11 | +You can redirect the trails logs posted by smartcab agent to a log_file and provide it as a input to analyse. |
| 12 | +
|
| 13 | +Note: |
| 14 | + * for learning update outformat is expecter as below. You need to add EXPECTED_REWARD to the following |
| 15 | + |
| 16 | + LearningAgent.update(): deadline = 30, inputs = {'light': 'red', 'oncoming': None, 'right': None, 'left': None}, action = None, expected_reward = 0, reward = 0.0 |
| 17 | +''' |
| 18 | + |
| 19 | +import sys |
| 20 | + |
| 21 | +from re import findall as re_findall |
| 22 | + |
| 23 | +import pandas as pd |
| 24 | + |
| 25 | +FILE = 'smartcab/test_log100' |
| 26 | + |
| 27 | +def fetch_data(filename): |
| 28 | + data = open(filename).readlines() |
| 29 | + return data |
| 30 | + |
| 31 | +def fetch_value(inp_str): |
| 32 | + return float(inp_str.split('=')[-1]) |
| 33 | + |
| 34 | +def check_environment_reset(env_reset): |
| 35 | + data = [] |
| 36 | + start, end, deadline = '', '', '' |
| 37 | + if env_reset.startswith('Environment.reset()'): |
| 38 | + data = re_findall('[\w.]+', env_reset) |
| 39 | + for i, each in enumerate(data): |
| 40 | + if each == 'start': |
| 41 | + start = map(int, [data[i + 1], data[i + 2]]) |
| 42 | + elif each == 'destination': |
| 43 | + end = map(int, [data[i + 1], data[i + 2]]) |
| 44 | + elif each == 'deadline': |
| 45 | + deadline = int(data[i+1]) |
| 46 | + return start, end, deadline |
| 47 | + |
| 48 | +def check_learning_update(learning_update): |
| 49 | + deadline, expected_reward, reward = 0, 0, 0 |
| 50 | + data = [] |
| 51 | + if learning_update.startswith('LearningAgent.update()'): |
| 52 | + data = re_findall('[\w.]+', learning_update) |
| 53 | + for i, each in enumerate(data): |
| 54 | + if each == 'deadline': |
| 55 | + deadline = data[i + 1] |
| 56 | + elif each == 'expected_reward': |
| 57 | + expected_reward = data[i + 1] |
| 58 | + elif each == 'reward': |
| 59 | + reward = data[i + 1] |
| 60 | + return deadline, expected_reward, reward |
| 61 | + |
| 62 | + |
| 63 | +def check_learning_update_old(learning_update): |
| 64 | + deadline, expected_reward, reward = 0, 0, 0 |
| 65 | + if learning_update.startswith('LearningAgent.update()'): |
| 66 | + learning_update = learning_update[24:].split(', ') |
| 67 | + # values |
| 68 | + deadline = fetch_value(learning_update[0]) |
| 69 | + expected_reward = fetch_value(learning_update[-2]) |
| 70 | + reward = fetch_value(learning_update[-1]) |
| 71 | + return deadline, expected_reward, reward |
| 72 | + |
| 73 | + |
| 74 | +def success_check(data): |
| 75 | + ''' |
| 76 | + Sequentially Parses the log and fetches data as and when we get |
| 77 | + ''' |
| 78 | + all_outcomes = [] |
| 79 | + all_trails = [] |
| 80 | + all_deadlines = [] |
| 81 | + all_expected_rewards = [] |
| 82 | + # vars |
| 83 | + all_rewards = [] |
| 84 | + all_deadlines = [] |
| 85 | + all_start = [] |
| 86 | + all_destinations = [] |
| 87 | + all_main_deadlines = [] |
| 88 | + # vars |
| 89 | + trail_last_update = False |
| 90 | + tmp_success = -1 |
| 91 | + counter = 0 |
| 92 | + reached_msg, aborted_msg = 'Primary agent has reached destination', 'Trial aborted' |
| 93 | + |
| 94 | + for i, each in enumerate(data): |
| 95 | + if each.startswith('Environment.reset()'): |
| 96 | + counter += 1 |
| 97 | + start_point, destination_point, main_deadline = check_environment_reset(each) |
| 98 | + |
| 99 | + if each.startswith('LearningAgent.update()'): |
| 100 | + deadline, expected_reward, reward = check_learning_update(each) |
| 101 | + # |
| 102 | + all_trails.append(counter) |
| 103 | + all_outcomes.append(-1) |
| 104 | + all_rewards.append(reward) |
| 105 | + # |
| 106 | + all_expected_rewards.append(expected_reward) |
| 107 | + all_start.append(start_point) |
| 108 | + all_destinations.append(destination_point) |
| 109 | + all_deadlines.append(deadline) |
| 110 | + all_main_deadlines.append(main_deadline) |
| 111 | + |
| 112 | + if trail_last_update: |
| 113 | + trail_last_update = False |
| 114 | + all_outcomes[-1] = tmp_success |
| 115 | + tmp_success = -1 |
| 116 | + # trail end check - if it is |
| 117 | + reached_chk = reached_msg in each |
| 118 | + aborted_chk = aborted_msg in each |
| 119 | + # trail ended |
| 120 | + if reached_chk or aborted_chk: |
| 121 | + trail_last_update = True |
| 122 | + if reached_chk: |
| 123 | + tmp_success = 1 |
| 124 | + else: |
| 125 | + tmp_success = 0 |
| 126 | + ret_dict = { |
| 127 | + 'all_trails' : all_trails, |
| 128 | + 'all_outcomes' : all_outcomes, |
| 129 | + 'all_rewards' : all_rewards, |
| 130 | + 'all_deadlines' : all_deadlines, |
| 131 | + 'all_expected_rewards' : all_expected_rewards, |
| 132 | + 'all_start' : all_start, |
| 133 | + 'all_destinations' : all_destinations, |
| 134 | + 'all_main_deadline' : all_main_deadlines |
| 135 | + } |
| 136 | + return ret_dict |
| 137 | + |
| 138 | + |
| 139 | +def total_stats(filename=FILE): |
| 140 | + data = fetch_data(filename) |
| 141 | + game = success_check(data) |
| 142 | + game_stats = pd.DataFrame.from_dict(game) |
| 143 | + |
| 144 | + for col in [u'all_deadlines', u'all_expected_rewards', u'all_trails', |
| 145 | + u'all_main_deadline', u'all_outcomes', u'all_rewards', # u'all_start', u'all_destinations' |
| 146 | + ]: |
| 147 | + game_stats[col] = pd.to_numeric(game_stats[col]) |
| 148 | + |
| 149 | + game_stats['Q_pred'] = game_stats.all_expected_rewards - game_stats.all_outcomes |
| 150 | + game_stats['steps'] = game_stats.all_main_deadline - game_stats.all_deadlines |
| 151 | + game_stats['avg_steps'] = game_stats.all_rewards / game_stats.steps |
| 152 | + |
| 153 | + # |
| 154 | + # Checking how many reached desctination |
| 155 | + # |
| 156 | + rewarded_deadlines = (game_stats.all_rewards >= 12) & (game_stats.all_deadlines >= 0) |
| 157 | + print '\nNo.of Successfully Trips is: %s' % game_stats[rewarded_deadlines].all_rewards.count() |
| 158 | + print 'Total reward sum: %s' % game_stats.all_rewards.sum() |
| 159 | + print 'Total time saved: %s' % int(game_stats[['steps']][rewarded_deadlines].sum()) |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + if len(sys.argv) > 1: |
| 163 | + filename = str(sys.argv[1]) |
| 164 | + total_stats(filename) |
| 165 | + else: |
| 166 | + total_stats('q_log02') |
| 167 | + |
| 168 | + |
0 commit comments