Branch data Line data Source code
1 : : /*
2 : : * Copyright 2022 Garena Online Private Limited
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #ifndef ENVPOOL_BOX2D_BIPEDAL_WALKER_H_
18 : : #define ENVPOOL_BOX2D_BIPEDAL_WALKER_H_
19 : :
20 : : #include <vector>
21 : :
22 : : #include "envpool/box2d/bipedal_walker_env.h"
23 : : #include "envpool/core/async_envpool.h"
24 : : #include "envpool/core/env.h"
25 : :
26 : : namespace box2d {
27 : :
28 : : class BipedalWalkerEnvFns {
29 : : public:
30 : : static decltype(auto) DefaultConfig() {
31 : : return MakeDict("reward_threshold"_.Bind(300.0), "hardcore"_.Bind(false));
32 : : }
33 : : template <typename Config>
34 : 22 : static decltype(auto) StateSpec(const Config& conf) {
35 : : #ifdef ENVPOOL_TEST
36 [ + - ]: 44 : return MakeDict("obs"_.Bind(Spec<float>({24})),
37 [ + - ]: 44 : "info:scroll"_.Bind(Spec<float>({-1})),
38 [ + - ]: 44 : "info:path2"_.Bind(Spec<float>({199, 2, 2})),
39 : : "info:path4"_.Bind(
40 [ + - + - ]: 44 : Spec<Container<float>>({-1}, Spec<float>({-1, 4, 2}))),
41 [ + - ]: 154 : "info:path5"_.Bind(Spec<float>({1, 5, 2})));
42 : : #else
43 : : return MakeDict("obs"_.Bind(Spec<float>({24})));
44 : : #endif
45 : : }
46 : : template <typename Config>
47 : 22 : static decltype(auto) ActionSpec(const Config& conf) {
48 : 44 : return MakeDict("action"_.Bind(Spec<float>({4}, {-1.0, 1.0})));
49 : : }
50 : : };
51 : :
52 : : using BipedalWalkerEnvSpec = EnvSpec<BipedalWalkerEnvFns>;
53 : :
54 : : class BipedalWalkerEnv : public Env<BipedalWalkerEnvSpec>,
55 : : public BipedalWalkerBox2dEnv {
56 : : public:
57 : 237 : BipedalWalkerEnv(const Spec& spec, int env_id)
58 : 237 : : Env<BipedalWalkerEnvSpec>(spec, env_id),
59 : : BipedalWalkerBox2dEnv(spec.config["hardcore"_],
60 [ + - ]: 237 : spec.config["max_episode_steps"_]) {}
61 : :
62 : 534102 : bool IsDone() override { return done_; }
63 : :
64 : 432 : void Reset() override {
65 : 432 : BipedalWalkerReset(&gen_);
66 : 432 : WriteState();
67 : 432 : }
68 : :
69 : 266514 : void Step(const Action& action) override {
70 [ + - + - : 533195 : BipedalWalkerStep(&gen_, action["action"_][0], action["action"_][1],
+ - + - ]
71 : : action["action"_][2], action["action"_][3]);
72 : 266716 : WriteState();
73 : 266686 : }
74 : :
75 : : private:
76 : 267131 : void WriteState() {
77 : 267131 : auto state = Allocate();
78 : : state["reward"_] = reward_;
79 : : state["obs"_].Assign(obs_.data(), obs_.size());
80 : : #ifdef ENVPOOL_TEST
81 : : state["info:scroll"_] = scroll_;
82 : : state["info:path2"_].Assign(terrain_edge_path2_.data(),
83 : : terrain_edge_path2_.size());
84 : : state["info:path5"_].Assign(hull_path5_.data(), hull_path5_.size());
85 : :
86 : : Container<float>& path4 = state["info:path4"_];
87 : : std::vector<float> path4_data;
88 [ + - ]: 266369 : path4_data.reserve(terrain_poly_path4_.size() + leg_path4_.size());
89 [ + - ]: 266988 : path4_data.insert(path4_data.end(), terrain_poly_path4_.begin(),
90 : : terrain_poly_path4_.end());
91 : 266879 : path4_data.insert(path4_data.end(), leg_path4_.begin(), leg_path4_.end());
92 : 534021 : auto* array = new TArray<float>(::Spec<float>(
93 [ + - + - ]: 533751 : std::vector<int>{static_cast<int>(path4_data.size()) / 8, 4, 2}));
94 : : array->Assign(path4_data.data(), path4_data.size());
95 : : path4.reset(array);
96 : : #endif
97 : 533841 : }
98 : : };
99 : :
100 : : using BipedalWalkerEnvPool = AsyncEnvPool<BipedalWalkerEnv>;
101 : :
102 : : } // namespace box2d
103 : :
104 : : #endif // ENVPOOL_BOX2D_BIPEDAL_WALKER_H_
|