-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
94 lines (90 loc) · 3 KB
/
misc.py
File metadata and controls
94 lines (90 loc) · 3 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
import numpy as np
import time
# Makes a function getch() which gets a char from user without waiting for enter
try:
# Windows
from msvcrt import getch
except ImportError:
# UNIX
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
# Runs the simulation based on keyboard input / human control
def run_human(sim, DQN=None):
key_map = {'w':0, 's':1, 'd':2, 'a':3, ' ':4, 'n':5}
done = False
total_reward = 0
state = sim.reset()
state = np.reshape(state, [1] + list(state.shape))
sim.render()
while not done:
print("WASD to move, Space to dig,")
print("'n' to wait, 'q' to quit.")
print("'v' to show Q-values,")
print("'i' to inspect a single cell,")
print("'l' to show a layer of the map,")
print("'p' to print general info,")
print("'m' to print all metadata info.\n")
char = getch()
if char == 'q':
return "Cancelled"
elif DQN is not None and len(DQN.sim.W.border_points) == 0:
done = True
elif char in key_map:
action = key_map[char]
# Do action, observe environment
sprime, reward, done, _ = sim.step(action)
sprime = np.reshape(sprime, [1] + list(sprime.shape))
# Store experience in memory
if DQN is not None:
DQN.remember(state, action, reward, sprime, done)
# Current state is now next state
state = sprime
total_reward += reward
elif char == 'v':
if DQN is not None:
DQN.show_best_action()
else:
print("Only works if you pass a DQN object to this function")
elif char == 'i':
print("Inspect a cell")
x = int(input("X coordinate: "))
y = int(input("Y coordinate: "))
sim.W.inspect((x, y))
elif char == 'l':
inp = input("Which layer? (string input) ")
if inp in sim.layer:
sim.W.show_layer(inp)
else:
sim.W.show_layer()
elif char == 'p':
print("General Info")
sim.W.print_info(total_reward)
elif char == 'm':
sim.W.print_metadata()
else:
print("Invalid action, not good if collecting memories for DQN.\n")
sim.render()
print(f"Total Reward: {total_reward}")
# Time the simulation speed
def time_simulation_run(num_runs=100):
import timeit
setup = """
from Simulation.forest_fire import ForestFire
sim = ForestFire()
"""
code = """
sim.reset()
sim.step("D")
while sim.W.RUNNING:
sim.step(" ")
"""
total = timeit.timeit(setup=setup, stmt=code, number=num_runs)
r1, r2 = round(total, 4), round(total / num_runs, 4)
print("Total:", r1, "Average per run", r2)