-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquick_start_guide.py
More file actions
187 lines (169 loc) · 6.98 KB
/
quick_start_guide.py
File metadata and controls
187 lines (169 loc) · 6.98 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
# needed to prevent numpy from using a ton of memory in env processes and causing them to throttle each other
os.environ["OPENBLAS_NUM_THREADS"] = "1"
def build_rlgym_v2_env():
import numpy as np
from rlgym.api import RLGym
from rlgym.rocket_league import common_values
from rlgym.rocket_league.action_parsers import LookupTableAction, RepeatAction
from rlgym.rocket_league.done_conditions import (
AnyCondition,
GoalCondition,
NoTouchTimeoutCondition,
TimeoutCondition,
)
from rlgym.rocket_league.obs_builders import DefaultObs
from rlgym.rocket_league.reward_functions import (
CombinedReward,
GoalReward,
TouchReward,
)
from rlgym.rocket_league.sim import RocketSimEngine
from rlgym.rocket_league.state_mutators import (
FixedTeamSizeMutator,
KickoffMutator,
MutatorSequence,
)
spawn_opponents = True
team_size = 2
blue_team_size = team_size
orange_team_size = team_size if spawn_opponents else 0
action_repeat = 8
no_touch_timeout_seconds = 30
game_timeout_seconds = 300
action_parser = RepeatAction(LookupTableAction(), repeats=action_repeat)
termination_condition = GoalCondition()
truncation_condition = AnyCondition(
NoTouchTimeoutCondition(timeout_seconds=no_touch_timeout_seconds),
TimeoutCondition(timeout_seconds=game_timeout_seconds),
)
reward_fn = CombinedReward((GoalReward(), 10), (TouchReward(), 0.1))
obs_builder = DefaultObs(
zero_padding=team_size,
pos_coef=np.asarray(
[
1 / common_values.SIDE_WALL_X,
1 / common_values.BACK_NET_Y,
1 / common_values.CEILING_Z,
]
),
ang_coef=1 / np.pi,
lin_vel_coef=1 / common_values.CAR_MAX_SPEED,
ang_vel_coef=1 / common_values.CAR_MAX_ANG_VEL,
boost_coef=1 / 100.0,
)
state_mutator = MutatorSequence(
FixedTeamSizeMutator(blue_size=blue_team_size, orange_size=orange_team_size),
KickoffMutator(),
)
return RLGym(
state_mutator=state_mutator,
obs_builder=obs_builder,
action_parser=action_parser,
reward_fn=reward_fn,
termination_cond=termination_condition,
truncation_cond=truncation_condition,
transition_engine=RocketSimEngine(),
)
if __name__ == "__main__":
from typing import Tuple
import numpy as np
from rlgym_learn_algos.logging import (
WandbMetricsLogger,
WandbMetricsLoggerConfigModel,
)
from rlgym_learn_algos.ppo import (
BasicCritic,
DiscreteFF,
ExperienceBufferConfigModel,
GAETrajectoryProcessor,
GAETrajectoryProcessorConfigModel,
NumpyExperienceBuffer,
PPOAgentController,
PPOAgentControllerConfigModel,
PPOLearnerConfigModel,
PPOMetricsLogger,
)
from rlgym_learn import (
BaseConfigModel,
LearningCoordinator,
LearningCoordinatorConfigModel,
NumpySerdeConfig,
ProcessConfigModel,
PyAnySerdeType,
SerdeTypesModel,
generate_config,
)
from rlgym_learn.rocket_league import GameStatePythonSerde
# The obs_space_type and action_space_type are determined by your choice of ObsBuilder and ActionParser respectively.
# The logic used here assumes you are using the types defined by the DefaultObs and LookupTableAction above.
DefaultObsSpaceType = Tuple[str, int]
DefaultActionSpaceType = Tuple[str, int]
def actor_factory(
obs_space: DefaultObsSpaceType,
action_space: DefaultActionSpaceType,
device: str,
):
return DiscreteFF(obs_space[1], action_space[1], (256, 256, 256), device)
def critic_factory(obs_space: DefaultObsSpaceType, device: str):
return BasicCritic(obs_space[1], (256, 256, 256), device)
# Create the config that will be used for the run
config = LearningCoordinatorConfigModel(
base_config=BaseConfigModel(
serde_types=SerdeTypesModel(
agent_id_serde_type=PyAnySerdeType.STRING(),
action_serde_type=PyAnySerdeType.NUMPY(np.int64),
obs_serde_type=PyAnySerdeType.NUMPY(np.float64),
reward_serde_type=PyAnySerdeType.FLOAT(),
obs_space_serde_type=PyAnySerdeType.TUPLE(
(PyAnySerdeType.STRING(), PyAnySerdeType.INT())
),
action_space_serde_type=PyAnySerdeType.TUPLE(
(PyAnySerdeType.STRING(), PyAnySerdeType.INT())
),
),
timestep_limit=1_000_000_000, # Train for 1B steps
),
process_config=ProcessConfigModel(
n_proc=32, # Number of processes to spawn to run environments. Increasing will use more RAM but should increase steps per second, up to a point
),
agent_controllers_config={
"PPO1": PPOAgentControllerConfigModel(
learner_config=PPOLearnerConfigModel(
ent_coef=0.01, # Sets the entropy coefficient used in the PPO algorithm
actor_lr=5e-5, # Sets the learning rate of the actor model
critic_lr=5e-5, # Sets the learning rate of the critic model
),
experience_buffer_config=ExperienceBufferConfigModel(
max_size=150_000, # Sets the number of timesteps to store in the experience buffer. Old timesteps will be pruned to only store the most recently obtained timesteps.
trajectory_processor_config=GAETrajectoryProcessorConfigModel(),
),
metrics_logger_config=WandbMetricsLoggerConfigModel(
group="rlgym-learn-testing"
),
)
},
agent_controllers_save_folder="agent_controllers_checkpoints", # (default value) WARNING: THIS PROCESS MAY DELETE ANYTHING INSIDE THIS FOLDER. This determines the parent folder for the runs for each agent controller. The runs folder for the agent controller will be this folder and then the agent controller config key as a subfolder.
)
# Generate the config file for reference (this file location can be
# passed to the learning coordinator via config_location instead of defining
# the config object in code and passing that)
generate_config(
learning_coordinator_config=config,
config_location="config.json",
force_overwrite=True,
)
learning_coordinator = LearningCoordinator(
build_rlgym_v2_env,
agent_controllers={
"PPO1": PPOAgentController(
actor_factory=actor_factory,
critic_factory=critic_factory,
experience_buffer=NumpyExperienceBuffer(GAETrajectoryProcessor()),
metrics_logger=WandbMetricsLogger(PPOMetricsLogger()),
obs_standardizer=None,
)
},
config=config,
)
learning_coordinator.start()