-
-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathparallelization.py
More file actions
109 lines (86 loc) · 3.69 KB
/
Copy pathparallelization.py
File metadata and controls
109 lines (86 loc) · 3.69 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
108
109
# Copyright 2020 Tensorforce Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from threading import Thread
from tensorforce import Environment, Runner
def main():
local()
local_vectorized()
multiprocessing()
socket()
def local():
"""
Train agent on experience collected in parallel from 4 local CartPole environments.
Typical use case:
time for batched agent.act() ~ time for agent.act() > time for environment.execute()
"""
agent = 'benchmarks/configs/ppo.json'
environment = 'benchmarks/configs/cartpole.json'
runner = Runner(agent=agent, environment=environment, num_parallel=4)
# Batch act/observe calls to agent, unless environment.is_vectorizable()
# (otherwise essentially equivalent to single environment)
runner.run(num_episodes=100, batch_agent_calls=True)
runner.close()
def local_vectorized():
"""
Train agent on experience collected in parallel from one vectorized CartPole environment.
Typical use case:
time for vectorized environment < time for sequential execution
"""
agent = 'benchmarks/configs/ppo.json'
environment = 'custom_cartpole'
runner = Runner(agent=agent, environment=environment, max_episode_timesteps=500, num_parallel=4)
runner.run(num_episodes=100)
runner.close()
def multiprocessing():
"""
Train agent on experience collected in parallel from 4 CartPole environments running in
separate processes.
Typical use case:
(a) time for batched agent.act() ~ time for agent.act()
> time for environment.execute() + remote communication
--> batch_agent_calls = True
(b) time for environment.execute() > time for agent.act() + process communication
--> batch_agent_calls = False
"""
agent = 'benchmarks/configs/ppo.json'
environment = 'benchmarks/configs/cartpole.json'
runner = Runner(agent=agent, environment=environment, num_parallel=4, remote='multiprocessing')
runner.run(num_episodes=100, batch_agent_calls=True) # optional: batch_agent_calls=True
runner.close()
def socket():
"""
Train agent on experience collected in parallel from 2 CartPole environments running on
another machine.
Typical use case: same as mode 2, but generally remote communication socket > process
Simulate remote environment, usually run on another machine via:
python run.py --environment gym --level CartPole-v1 --remote socket-server --port 65432
"""
agent = 'benchmarks/configs/ppo.json'
environment = 'benchmarks/configs/cartpole.json'
def server(port):
Environment.create(environment=environment, remote='socket-server', port=port)
server1 = Thread(target=server, kwargs=dict(port=65432))
server2 = Thread(target=server, kwargs=dict(port=65433))
server1.start()
server2.start()
runner = Runner(
agent=agent, num_parallel=2, remote='socket-client', host='127.0.0.1', port=65432
)
runner.run(num_episodes=100) # optional: batch_agent_calls=True
runner.close()
server1.join()
server2.join()
if __name__ == '__main__':
main()