-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint
More file actions
executable file
·107 lines (84 loc) · 2.79 KB
/
entrypoint
File metadata and controls
executable file
·107 lines (84 loc) · 2.79 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
#!/usr/bin/python3
""" Execute agent run."""
# standard libraries
import argparse
import logging
import sys
# non-standard libraries
from primitives import Agent, Path, State, Strategy, Task
def train(args):
""" Begin training."""
# load template
logging.info("Loading template...")
strategy = Strategy()
strategy.load(args.train)
logging.info("Strategy loaded!")
# declare a starting point
starting_point = State(0, 0)
ending_point = State((strategy.rows - 1), (strategy.cols - 1))
# spawn the agent at the starting point
agent = Agent(current_state=starting_point)
# choose a strategy
agent.choose_policy(args.policy)
# initialize the path (action sequence)
path = Path()
# traverse the state space, following the template,
# in accordance with the assigned strategy
# logging actions along the way
# and saving the action sequence, labeled with the task name
while agent.current_state.coord != ending_point.coord:
logging.info("Current state is: %s", agent.current_state.coord)
logging.info("Considering possible actions")
# evaluate adjacent values and compare with strategy
optimal_entry = agent.check_adjacents(template)
# take action
logging.info("Taking an action")
action = agent.change_state(optimal_entry, 'train')
agent.take_cost_reward()
agent.check_health()
logging.info("Logging the action")
path.log_moves(action)
logging.info("Reached the endpoint!")
# save your training output
logging.info("Saving action sequence as task")
task = args.train.replace('strategies/', '').replace('.json', '')
path.save(task)
return
def test(args):
""" Begin testing."""
# load task file
task = Task()
task.load(args.test)
# set starting point
starting_point = State(0, 0)
logging.info("Starting at: %s", starting_point.coord)
# spawn agent
agent = Agent(current_state=starting_point)
# run through actions
for task in task.task_data['task']:
agent.change_state(task, 'test')
# display the steps traversed
logging.info("Traversal path: %s", agent.traversal)
return
def main(args):
""" Main function."""
# initialize logging
logging.basicConfig(level=logging.INFO)
# if training
if args.train:
train(args)
# if testing
elif args.test:
test(args)
else:
logging.critical("Please specify a mode: --train or --test.")
sys.exit(1)
if __name__ == "__main__":
""" Spawn argument parser and main."""
# collect arguments
parser = argparse.ArgumentParser()
parser.add_argument('--train')
parser.add_argument('--test')
parser.add_argument('--policy')
args = parser.parse_args()
main(args)