forked from stepjam/RLBench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
few_shot_rl.py
56 lines (41 loc) · 1.53 KB
/
few_shot_rl.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
import numpy as np
from rlbench.action_modes.action_mode import MoveArmThenGripper
from rlbench.action_modes.arm_action_modes import JointVelocity
from rlbench.action_modes.gripper_action_modes import Discrete
from rlbench.environment import Environment
from rlbench.observation_config import ObservationConfig
from rlbench.tasks import FS10_V1
class Agent(object):
def __init__(self, action_shape):
self.action_shape = action_shape
def act(self, obs):
arm = np.random.normal(0.0, 0.1, size=(self.action_shape[0] - 1,))
gripper = [1.0] # Always open
return np.concatenate([arm, gripper], axis=-1)
obs_config = ObservationConfig()
obs_config.set_all(True)
env = Environment(
action_mode=MoveArmThenGripper(
arm_action_mode=JointVelocity(), gripper_action_mode=Discrete()),
obs_config=ObservationConfig(),
headless=False)
env.launch()
agent = Agent(env.action_shape)
train_tasks = FS10_V1['train']
test_tasks = FS10_V1['test']
training_cycles_per_task = 3
training_steps_per_task = 80
episode_length = 40
for _ in range(training_cycles_per_task):
task_to_train = np.random.choice(train_tasks, 1)[0]
task = env.get_task(task_to_train)
task.sample_variation() # random variation
for i in range(training_steps_per_task):
if i % episode_length == 0:
print('Reset Episode')
descriptions, obs = task.reset()
print(descriptions)
action = agent.act(obs)
obs, reward, terminate = task.step(action)
print('Done')
env.shutdown()