forked from GOAL-Robots/REALCompetitionStartingKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
110 lines (83 loc) · 2.89 KB
/
demo.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
from OpenGL import GLU
import numpy as np
import realcomp
from realcomp.envs.realcomp_env import Goal
import gym
import os
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(os.path.dirname(currentdir))
os.sys.path.insert(0,parentdir)
from my_controller import MyController
Controller = MyController
scores = {}
def add_scores(challenge, score):
if challenge in scores.keys():
scores[challenge] += [score]
else:
scores[challenge] = [score]
def report_score():
print("*****************")
total_score = 0
challenges = ['2D','2.5D','3D']
for key in challenges:
if key in scores.keys():
results = scores[key]
formatted_results = ", ".join(["{:.4f}".format(r) for r in results])
challenge_score = np.mean(results)
else:
results = []
formatted_results = "None"
challenge_score = 0
print("Challenge {} - {:.4f}".format(key, challenge_score))
print("Goals: {}".format(formatted_results))
total_score += challenge_score
total_score /= len(challenges)
print("Overall Score: {:.4f}".format(total_score))
print("*****************")
def demo_run(extrinsic_trials=350):
env = gym.make('REALComp-v0')
controller = Controller(env.action_space)
# change length of simulation for testing purposes
env.intrinsic_timesteps = 1e7 #default = 1e7
env.extrinsic_timesteps = 2e3 #default = 2e3
# render simulation on screen
# env.render('human')
# reset simulation
observation = env.reset()
reward = 0
done = False
# intrinsic phase
print("Starting intrinsic phase...")
while not done:
# Call your controller to chose action
action = controller.step(observation, reward, done)
# do action
observation, reward, done, _ = env.step(action)
# get frames for video making
# rgb_array = env.render('rgb_array')
# extrinsic phase
print("Starting extrinsic phase...")
totalScore = 0
for k in range(extrinsic_trials):
# reset simulation
observation = env.reset()
reward = 0
done = False
# set the extrinsic goal to pursue
env.set_goal()
print("Starting extrinsic trial...")
while not done:
# Call your controller to chose action
action = controller.step(observation, reward, done)
# do action
observation, reward, done, _ = env.step(action)
# get frames for video making
# rgb_array = env.render('rgb_array')
add_scores(*env.evaluateGoal())
print("Current score:")
report_score()
print("Final score:")
report_score()
if __name__=="__main__":
demo_run()