-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqLearning_script.py
More file actions
141 lines (112 loc) · 3.96 KB
/
Copy pathqLearning_script.py
File metadata and controls
141 lines (112 loc) · 3.96 KB
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
import argparse
from simulation_constants import EPISODE_COUNT, E, ALPHA
from algorithms.qlearning import qLearning
#import racetrack
#import demo_constants.demo_racetrack_data as data
#from windy_gridworld import WindyGridworld
#import demo_constants.demo_windy_gridworld_data as data
from maze_gridworld import MazeGridWorld
import demo_constants.demo_maze_data as data
# Script arguments
run_demo_test = False
train = False
display_policy = False
display_track = True
output_filename = "output.py"
episode_count = EPISODE_COUNT
debug_level = 1
"""
env = WindyGridworld(data.GRIDWORLD_SCHEMA, data.ACTIONS,
data.REWARD, data.COL_TO_WIND)
env = racetrack.Racetrack(data.DEMO_RACETRACK_SCHEMA,
data.MAX_VELOCITY, data.ACTIONS, data.REWARD, data.FAIL_REWARD)
"""
env = MazeGridWorld(data.DEMO_MAZE_SCHEMA, data.ACTIONS, data.FINISH_REWARD)
def parse_args():
"""
Description
----------
Handles the argument parsing of the script
"""
parser = argparse.ArgumentParser(
description='A script used to run an off-policy monte carlo control method for a specific gridworld problem. It can test and create policies.')
parser.add_argument(
'-d', '--display_policy', help='Determines whether or not to display the policy after training', required=False, action='store_true')
parser.add_argument(
'-t', '--train', help='Determines whether or not to run a training session', required=False, action='store_true')
parser.add_argument(
'-o', '--output', help='The output file use for storing the generated policy', required=False)
parser.add_argument(
'-r', '--run_demo', help='Toggles on running the policy stored in demo_policy.py', required=False, action='store_true')
parser.add_argument(
'-n', '--no_track_display', help='Toggles off a display of the track', required=False, action='store_false')
parser.add_argument(
'-e', '--episode_count', help='The amount of episodes to train on', required=False)
parser.add_argument(
'-i', '--info_level', help='Determines the level of information to be shown when running (0-3)', required=False)
args = parser.parse_args()
if args.display_policy:
global display_policy
display_policy = True
if args.train:
global train
train = True
if args.no_track_display:
global display_track
display_track = args.no_track_display
if args.run_demo:
global run_demo_test
run_demo_test = True
if args.output:
global output_filename
output_filename = args.output
if args.episode_count:
global episode_count
episode_count = args.episode_count
if args.info_level:
global debug_level
debug_level = args.info_level
def run_demo():
"""
Description
----------
Runs a demo of the policy stored in demo_policy.py
"""
#from demo_policies.racetrack.qlearning_policy import POLICY
#from demo_policies.windy_gridworld.qlearning_policy import POLICY
from demo_policies.maze_gridworld.qlearning import POLICY
episode = env.generateEpisodeFromQValues(POLICY, 0)
for pair in episode:
print(pair)
print("EPISODE LENGTH:", len(episode))
return
def train_policy():
"""
Description
----------
Handles the actual learning for the policy
"""
# Training takes place here
result = qLearning(env, data.DISCOUNT, ALPHA, E,
episode_count, debug_level)
if display_policy:
print(result)
if output_filename:
with open(output_filename, 'w') as f:
f.write("POLICY = " + str(result) + "\n")
f.close()
def main():
"""
Description
----------
Main control flow of the script
"""
parse_args()
if display_track:
env.displayEnvironment()
if train:
train_policy()
if run_demo_test:
run_demo()
if __name__ == "__main__":
main()