-
Notifications
You must be signed in to change notification settings - Fork 1
/
vizdoom_agent.py
156 lines (136 loc) · 5.7 KB
/
vizdoom_agent.py
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
147
148
149
150
151
152
153
154
155
156
import time
import random
import numpy as np
from collections import deque
import torch
from vizdoom import *
from agent import Agent
import constants
class VizDoomAgent(Agent):
def __init__(self, placeRecognition, navigation, wad, game_args=[], teachCommandsFile=None):
super(VizDoomAgent, self).__init__(placeRecognition, navigation)
self.game = self.initialize_game(wad, game_args)
self.goal = None
self.init = None
self.teachCommandsFile = teachCommandsFile
self.place_recognition.model.eval()
self.navigation.model.eval()
self.new_seed()
def initialize_game(self, wad, game_args):
game = DoomGame()
game.load_config(constants.VIZDOOM_DEFAULT_CONFIG)
for args in game_args:
game.add_game_args(args)
game.set_doom_scenario_path(wad)
game.set_seed(self.new_seed())
game.init()
return game
def new_seed(self):
self.seed = random.randint(1, 1234567890)
return self.seed
def reset_map(self):
self.new_seed()
self.game.set_doom_map(constants.VIZDOOM_MAP_NAME_TEMPLATE % random.randint(constants.VIZDOOM_MIN_RANDOM_TEXTURE_MAP_INDEX,
constants.VIZDOOM_MAX_RANDOM_TEXTURE_MAP_INDEX))
self.game.set_seed(self.seed)
self.game.new_episode()
state = self.game.get_state().screen_buffer.transpose([1, 2, 0])
return state
def reset_episode(self):
self.game.set_seed(self.seed)
self.game.new_episode()
state = self.game.get_state().screen_buffer.transpose([1, 2, 0])
return state
def step(self, action, repeat=4):
self.game.make_action(constants.VIZDOOM_ACTIONS_LIST[action], repeat)
state = self.game.get_state().screen_buffer.transpose([1, 2, 0])
return state
def random_walk(self):
state = self.reset_map()
self.init = state
print ("state: ", self.game.get_state().game_variables)
for i in range(constants.AIRSIM_AGENT_TEACH_LEN):
action = random.randint(0, constants.LOCO_NUM_CLASSES-1)
next_state = self.step(action)
print ("state: ", self.game.get_state().game_variables)
print ("random walk: index %d action %d" % (i, action))
rep, _ = self.sptm.append_keyframe(state, action, done)
self.goal = state
state = next_state
if done:
break
def commanded_walk(self):
action_file = open(self.teachCommandsFile)
if action_file == None:
return None
state = self.reset_map()
print ("state: ", self.game.get_state().game_variables)
self.init = state
i = 0
actions = [int(val) for val in action_file.read().split('\n') if val.isdigit()]
for action in actions:
next_state = self.step(action)
print ("state: ", self.game.get_state().game_variables)
print ("commanded walk: index %d action %d" % (i, action))
rep, _ = self.sptm.append_keyframe(state, action, False)
self.goal = state
state = next_state
i = i+1
time.sleep(0.1)
def teach(self):
if (self.teachCommandsFile == None):
self.random_walk()
else:
self.commanded_walk()
def repeat(self):
self.sptm.build_graph()
goal, goal_index, similarity = self.sptm.find_closest(self.goal)
if (goal_index < 0):
print ("cannot find goal")
return
current_state = self.reset_episode()
print ("state: ", self.game.get_state().game_variables)
previous_state = current_state
previous_action = -1
self.sptm.clear_sequence()
while (True):
matched_index, similarity_score, best_velocity = self.sptm.relocalize(current_state)
path = self.sptm.find_shortest_path(matched_index, goal_index)
print (matched_index, similarity_score, path)
if (len(path) < 2): # achieved the goal
break
action, future_state = self.navigate(current_state, path, previous_action)
from PIL import Image
current_image = Image.fromarray(current_state)
future_image = Image.fromarray(future_state)
current_image.save("current.png", "PNG")
future_image.save("future.png", "PNG")
next_state = self.step(action)
print ("state: ", self.game.get_state().game_variables)
previous_state = current_state
current_state = next_state
previous_action = action
time.sleep(0.2)
def run(self):
# init_position, init_orientation = [10, 0, -6], [0, 0, 0]
# self.env.set_initial_pose(init_position, init_orientation)
# self.env.set_mode(constants.AIRSIM_MODE_TEACH)
time.sleep(1)
print ("Running teaching phase")
self.teach()
# print ("Running repeating backward phase")
# self.env.set_mode(constants.AIRSIM_MODE_REPEAT)
# time.sleep(1)
# self.repeat_backward()
# init_position, init_orientation = [10, 2, -6], [0, 0, 0]
# self.env.set_initial_pose(init_position, init_orientation)
# self.env.set_mode(constants.AIRSIM_MODE_REPEAT)
time.sleep(1)
print ("Running repeating phase")
self.repeat()
# init_position, init_orientation = [10, 4, -6], [0, 0, 0]
# self.env.set_initial_pose(init_position, init_orientation)
# self.env.set_mode(constants.AIRSIM_MODE_REPEAT)
time.sleep(1)
print ("Running repeating phase")
self.repeat()