diff --git a/.clang-tidy b/.clang-tidy index 4eca3ea5..d62bd5b9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -20,23 +20,15 @@ Checks: ' performance-*, portability-*, readability-*, - -clang-diagnostic-delete-non-abstract-non-virtual-dtor, - -google-runtime-references, - -modernize-use-trailing-return-type, - -readability-magic-numbers, - -readability-static-accessed-through-instance, - -readability-uppercase-literal-suffix, -bugprone-easily-swappable-parameters, -bugprone-implicit-widening-of-multiplication-result, -bugprone-narrowing-conversions, - -google-readability-casting, - -modernize-return-braced-init-list, - -modernize-use-default-member-init, - -performance-no-int-to-ptr, + -modernize-use-trailing-return-type, -readability-function-cognitive-complexity, -readability-identifier-length, - -readability-redundant-smartptr-get, - -readability-simplify-boolean-expr, + -readability-magic-numbers, + -readability-static-accessed-through-instance, + -readability-uppercase-literal-suffix, ' CheckOptions: - { key: readability-identifier-naming.ClassCase, value: CamelCase } diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c3d33955..f7b3b4fa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: jobs: release: runs-on: ubuntu-latest - container: trinkle23897/envpool-release:2022-12-28-e857ca2 + container: trinkle23897/envpool-release:2022-12-29-03c3d95 strategy: matrix: python-version: ['3.7', '3.8', '3.9', '3.10', '3.11'] diff --git a/Makefile b/Makefile index ca88efc9..d05833f7 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ cpplint-install: $(call check_install, cpplint) clang-format-install: - command -v clang-format-11 || sudo apt-get install -y clang-format-11 + command -v clang-format || sudo apt-get install -y clang-format clang-tidy-install: command -v clang-tidy || sudo apt-get install -y clang-tidy @@ -77,7 +77,7 @@ cpplint: cpplint-install cpplint $(CPP_FILES) clang-format: clang-format-install - clang-format-11 --style=file -i $(CPP_FILES) -n --Werror + clang-format --style=file -i $(CPP_FILES) -n --Werror # bazel file linter @@ -87,10 +87,10 @@ buildifier: buildifier-install # bazel build/test bazel-pip-requirement-dev: - cp third_party/pip_requirements/requirements-dev.txt third_party/pip_requirements/requirements.txt + cd third_party/pip_requirements && (cmp requirements.txt requirements-dev.txt || ln -sf requirements-dev.txt requirements.txt) bazel-pip-requirement-release: - cp third_party/pip_requirements/requirements-release.txt third_party/pip_requirements/requirements.txt + cd third_party/pip_requirements && (cmp requirements.txt requirements-release.txt || ln -sf requirements-release.txt requirements.txt) clang-tidy: clang-tidy-install bazel-pip-requirement-dev bazel build $(BAZELOPT) //... --config=clang-tidy --config=test @@ -142,7 +142,7 @@ lint: buildifier flake8 py-format clang-format cpplint clang-tidy mypy docstyle format: py-format-install clang-format-install buildifier-install addlicense-install isort $(PYTHON_FILES) yapf -ir $(PYTHON_FILES) - clang-format-11 -style=file -i $(CPP_FILES) + clang-format -style=file -i $(CPP_FILES) buildifier -r -lint=fix $(BAZEL_FILES) addlicense -c $(COPYRIGHT) -l apache -y 2023 $(PROJECT_FOLDER) @@ -157,16 +157,16 @@ docker-ci-push: docker-ci docker push $(DOCKER_USER)/$(PROJECT_NAME):$(DOCKER_TAG) docker-ci-launch: docker-ci - docker run --network=host -v /home/ubuntu:/home/github-action -it $(PROJECT_NAME):$(DOCKER_TAG) bash + docker run --network=host -v /home/ubuntu:/home/github-action --shm-size=4gb -it $(PROJECT_NAME):$(DOCKER_TAG) bash docker-dev: docker-ci - docker run --network=host -v /:/host -it $(PROJECT_NAME):$(DOCKER_TAG) bash + docker run --network=host -v /:/host --shm-size=4gb -it $(PROJECT_NAME):$(DOCKER_TAG) bash # for mainland China docker-dev-cn: docker build --network=host -t $(PROJECT_NAME):$(DOCKER_TAG) -f docker/dev-cn.dockerfile . echo successfully build docker image with tag $(PROJECT_NAME):$(DOCKER_TAG) - docker run --network=host -v /:/host -it $(PROJECT_NAME):$(DOCKER_TAG) bash + docker run --network=host -v /:/host --shm-size=4gb -it $(PROJECT_NAME):$(DOCKER_TAG) bash docker-release: docker build --network=host -t $(PROJECT_NAME)-release:$(DOCKER_TAG) -f docker/release.dockerfile . @@ -177,7 +177,7 @@ docker-release-push: docker-release docker push $(DOCKER_USER)/$(PROJECT_NAME)-release:$(DOCKER_TAG) docker-release-launch: docker-release - docker run --network=host -v /:/host -it $(PROJECT_NAME)-release:$(DOCKER_TAG) bash + docker run --network=host -v /:/host --shm-size=4gb -it $(PROJECT_NAME)-release:$(DOCKER_TAG) bash pypi-wheel: auditwheel-install bazel-release ls dist/*.whl -Art | tail -n 1 | xargs auditwheel repair --plat manylinux_2_17_x86_64 diff --git a/docker/dev-cn.dockerfile b/docker/dev-cn.dockerfile index 21a6567e..aed6a766 100644 --- a/docker/dev-cn.dockerfile +++ b/docker/dev-cn.dockerfile @@ -7,7 +7,7 @@ ARG HOME=/root ARG PATH=$PATH:$HOME/go/bin RUN apt-get update \ - && apt-get install -y python3-pip python3-dev golang-1.18 clang-format-11 git wget swig tmux clang-tidy \ + && apt-get install -y python3-pip python3-dev golang-1.18 clang-format-11 git wget swig tmux clang-tidy vim \ && rm -rf /var/lib/apt/lists/* RUN ln -s /usr/bin/python3 /usr/bin/python RUN ln -sf /usr/lib/go-1.18/bin/go /usr/bin/go diff --git a/docker/dev.dockerfile b/docker/dev.dockerfile index 858bfe95..f368a633 100644 --- a/docker/dev.dockerfile +++ b/docker/dev.dockerfile @@ -7,7 +7,7 @@ ARG HOME=/root ARG PATH=$PATH:$HOME/go/bin RUN apt-get update \ - && apt-get install -y python3-pip python3-dev golang-1.18 clang-format-11 git wget swig tmux clang-tidy \ + && apt-get install -y python3-pip python3-dev golang-1.18 clang-format-11 git wget swig tmux clang-tidy vim \ && rm -rf /var/lib/apt/lists/* RUN ln -s /usr/bin/python3 /usr/bin/python RUN ln -sf /usr/lib/go-1.18/bin/go /usr/bin/go diff --git a/envpool/__init__.py b/envpool/__init__.py index 84dc7404..7466c5a6 100644 --- a/envpool/__init__.py +++ b/envpool/__init__.py @@ -24,7 +24,7 @@ register, ) -__version__ = "0.6.7" +__version__ = "0.7.0" __all__ = [ "register", "make", diff --git a/envpool/atari/atari_env.h b/envpool/atari/atari_env.h index bca85d13..bc601fa5 100644 --- a/envpool/atari/atari_env.h +++ b/envpool/atari/atari_env.h @@ -32,14 +32,14 @@ namespace atari { -bool TurnOffVerbosity() { +auto TurnOffVerbosity() { ale::Logger::setMode(ale::Logger::Error); return true; } static bool verbosity_off = TurnOffVerbosity(); -std::string GetRomPath(const std::string& base_path, const std::string& task) { +auto GetRomPath(const std::string& base_path, const std::string& task) { std::stringstream ss; // hardcode path here :( ss << base_path << "/atari/roms/" << task << ".bin"; @@ -88,9 +88,9 @@ class AtariEnv : public Env { std::unique_ptr env_; ale::ActionVect action_set_; int max_episode_steps_, elapsed_step_, stack_num_, frame_skip_; - bool fire_reset_, reward_clip_, zero_discount_on_life_loss_; + bool fire_reset_{false}, reward_clip_, zero_discount_on_life_loss_; bool gray_scale_, episodic_life_, use_inter_area_resize_; - bool done_; + bool done_{true}; int lives_; FrameSpec raw_spec_, resize_spec_, transpose_spec_; std::deque stack_buf_; @@ -107,13 +107,11 @@ class AtariEnv : public Env { elapsed_step_(max_episode_steps_ + 1), stack_num_(spec.config["stack_num"_]), frame_skip_(spec.config["frame_skip"_]), - fire_reset_(false), reward_clip_(spec.config["reward_clip"_]), zero_discount_on_life_loss_(spec.config["zero_discount_on_life_loss"_]), gray_scale_(spec.config["gray_scale"_]), episodic_life_(spec.config["episodic_life"_]), use_inter_area_resize_(spec.config["use_inter_area_resize"_]), - done_(true), raw_spec_({kRawHeight, kRawWidth, gray_scale_ ? 1 : 3}), resize_spec_({spec.config["img_height"_], spec.config["img_width"_], gray_scale_ ? 1 : 3}), diff --git a/envpool/box2d/bipedal_walker_env.cc b/envpool/box2d/bipedal_walker_env.cc index 9e68cc05..d97a9cee 100644 --- a/envpool/box2d/bipedal_walker_env.cc +++ b/envpool/box2d/bipedal_walker_env.cc @@ -65,9 +65,7 @@ BipedalWalkerBox2dEnv::BipedalWalkerBox2dEnv(bool hardcore, : max_episode_steps_(max_episode_steps), elapsed_step_(max_episode_steps + 1), hardcore_(hardcore), - done_(true), - world_(new b2World(b2Vec2(0.0, -10.0))), - hull_(nullptr) { + world_(new b2World(b2Vec2(0.0, -10.0))) { for (const auto* p : kHullPoly) { hull_poly_.emplace_back(Vec2(p[0] / kScaleDouble, p[1] / kScaleDouble)); } diff --git a/envpool/box2d/bipedal_walker_env.h b/envpool/box2d/bipedal_walker_env.h index 640476e7..9d0179dd 100644 --- a/envpool/box2d/bipedal_walker_env.h +++ b/envpool/box2d/bipedal_walker_env.h @@ -75,7 +75,7 @@ class BipedalWalkerBox2dEnv { protected: int max_episode_steps_, elapsed_step_; float reward_, prev_shaping_; - bool hardcore_, done_; + bool hardcore_, done_{true}; std::array obs_; // info float scroll_; @@ -83,7 +83,7 @@ class BipedalWalkerBox2dEnv { // box2d related std::unique_ptr world_; - b2Body* hull_; + b2Body* hull_{nullptr}; std::vector hull_poly_; std::vector terrain_; std::array legs_; diff --git a/envpool/box2d/car_dynamics.cc b/envpool/box2d/car_dynamics.cc index 573c0b49..c2081cc0 100644 --- a/envpool/box2d/car_dynamics.cc +++ b/envpool/box2d/car_dynamics.cc @@ -36,7 +36,7 @@ b2PolygonShape GeneratePolygon(const float (*poly)[2], int size) { // NOLINT Car::Car(std::shared_ptr world, float init_angle, float init_x, float init_y) - : world_(std::move(world)), hull_(nullptr), fuel_spent_(0) { + : world_(std::move(world)) { // Create hull b2BodyDef bd; bd.position.Set(init_x, init_y); @@ -186,7 +186,7 @@ void Car::Step(float dt) { } else if (w->skid_start == nullptr) { w->skid_start = std::make_unique(w->body->GetPosition()); } else { - w->skid_particle = CreateParticle(*(w->skid_start.get()), + w->skid_particle = CreateParticle(*(w->skid_start.get()), // NOLINT w->body->GetPosition(), grass); w->skid_start = nullptr; } @@ -264,7 +264,7 @@ void Car::Draw(const cv::Mat& surf, float zoom, cv::fillPoly(surf, poly, color); auto* user_data = - reinterpret_cast(body->GetUserData().pointer); + reinterpret_cast(body->GetUserData().pointer); // NOLINT if (user_data == nullptr || user_data->type != WHEEL_TYPE) { continue; } diff --git a/envpool/box2d/car_dynamics.h b/envpool/box2d/car_dynamics.h index 41ff9828..21a2cfd9 100644 --- a/envpool/box2d/car_dynamics.h +++ b/envpool/box2d/car_dynamics.h @@ -138,9 +138,9 @@ class Car { std::deque> particles_; std::vector drawlist_; std::shared_ptr world_; - b2Body* hull_; + b2Body* hull_{nullptr}; std::vector wheels_; - float fuel_spent_; + float fuel_spent_{0}; std::shared_ptr CreateParticle(b2Vec2 point1, b2Vec2 point2, bool grass); diff --git a/envpool/box2d/car_racing_env.cc b/envpool/box2d/car_racing_env.cc index c0e10fdf..f2c6f154 100644 --- a/envpool/box2d/car_racing_env.cc +++ b/envpool/box2d/car_racing_env.cc @@ -34,9 +34,9 @@ void CarRacingFrictionDetector::Contact(b2Contact* contact, bool begin) { Tile* tile = nullptr; Wheel* obj = nullptr; - auto* u1 = reinterpret_cast( + auto* u1 = reinterpret_cast( // NOLINT contact->GetFixtureA()->GetBody()->GetUserData().pointer); - auto* u2 = reinterpret_cast( + auto* u2 = reinterpret_cast( // NOLINT contact->GetFixtureB()->GetBody()->GetUserData().pointer); if (u1 == nullptr || u2 == nullptr) { @@ -84,7 +84,6 @@ CarRacingBox2dEnv::CarRacingBox2dEnv(int max_episode_steps, : lap_complete_percent_(lap_complete_percent), max_episode_steps_(max_episode_steps), elapsed_step_(max_episode_steps + 1), - done_(true), world_(new b2World(b2Vec2(0.0, 0.0))) { b2PolygonShape shape; std::array vertices = {b2Vec2(0, 0), b2Vec2(1, 0), b2Vec2(1, -1), diff --git a/envpool/box2d/car_racing_env.h b/envpool/box2d/car_racing_env.h index 97fa2b94..aa1c23ad 100644 --- a/envpool/box2d/car_racing_env.h +++ b/envpool/box2d/car_racing_env.h @@ -80,7 +80,7 @@ class CarRacingBox2dEnv { float reward_{0}; float prev_reward_{0}; float step_reward_{0}; - bool done_; + bool done_{true}; cv::Mat surf_; cv::Mat img_array_; diff --git a/envpool/box2d/lunar_lander_env.cc b/envpool/box2d/lunar_lander_env.cc index d8dd22aa..d72d0a2b 100644 --- a/envpool/box2d/lunar_lander_env.cc +++ b/envpool/box2d/lunar_lander_env.cc @@ -52,10 +52,7 @@ LunarLanderBox2dEnv::LunarLanderBox2dEnv(bool continuous, int max_episode_steps) : max_episode_steps_(max_episode_steps), elapsed_step_(max_episode_steps + 1), continuous_(continuous), - done_(true), - world_(new b2World(b2Vec2(0.0, -10.0))), - moon_(nullptr), - lander_(nullptr) { + world_(new b2World(b2Vec2(0.0, -10.0))) { for (const auto* p : kLanderPoly) { lander_poly_.emplace_back(Vec2(p[0] / kScale, p[1] / kScale)); } diff --git a/envpool/box2d/lunar_lander_env.h b/envpool/box2d/lunar_lander_env.h index 81ba8644..f5299786 100644 --- a/envpool/box2d/lunar_lander_env.h +++ b/envpool/box2d/lunar_lander_env.h @@ -53,12 +53,12 @@ class LunarLanderBox2dEnv { protected: int max_episode_steps_, elapsed_step_; float reward_, prev_shaping_; - bool continuous_, done_; + bool continuous_, done_{true}; std::array obs_; // box2d related std::unique_ptr world_; - b2Body *moon_, *lander_; + b2Body *moon_{nullptr}, *lander_{nullptr}; std::vector particles_; std::vector lander_poly_; std::array legs_; diff --git a/envpool/box2d/utils.cc b/envpool/box2d/utils.cc index 8298365d..a9a6145b 100644 --- a/envpool/box2d/utils.cc +++ b/envpool/box2d/utils.cc @@ -17,7 +17,7 @@ namespace box2d { b2Vec2 Vec2(double x, double y) { - return b2Vec2(static_cast(x), static_cast(y)); + return {static_cast(x), static_cast(y)}; } float Sign(double val, double eps) { @@ -43,7 +43,7 @@ b2Vec2 RotateRad(const b2Vec2& v, float angle) { b2Vec2 Multiply(const b2Transform& trans, const b2Vec2& v) { float x = (trans.q.c * v.x - trans.q.s * v.y) + trans.p.x; float y = (trans.q.s * v.x + trans.q.c * v.y) + trans.p.y; - return b2Vec2(x, y); + return {x, y}; } } // namespace box2d diff --git a/envpool/classic_control/acrobot.h b/envpool/classic_control/acrobot.h index bc466d51..afbeae59 100644 --- a/envpool/classic_control/acrobot.h +++ b/envpool/classic_control/acrobot.h @@ -53,10 +53,10 @@ class AcrobotEnv : public Env { V5(double s0, double s1, double s2, double s3, double s4) : s0(s0), s1(s1), s2(s2), s3(s3), s4(s4) {} V5 operator+(const V5& v) const { - return V5(s0 + v.s0, s1 + v.s1, s2 + v.s2, s3 + v.s3, s4 + v.s4); + return {s0 + v.s0, s1 + v.s1, s2 + v.s2, s3 + v.s3, s4 + v.s4}; } V5 operator*(double v) const { - return V5(s0 * v, s1 * v, s2 * v, s3 * v, s4 * v); + return {s0 * v, s1 * v, s2 * v, s3 * v, s4 * v}; } }; @@ -74,15 +74,14 @@ class AcrobotEnv : public Env { int max_episode_steps_, elapsed_step_; V5 s_; std::uniform_real_distribution<> dist_; - bool done_; + bool done_{true}; public: AcrobotEnv(const Spec& spec, int env_id) : Env(spec, env_id), max_episode_steps_(spec.config["max_episode_steps"_]), elapsed_step_(max_episode_steps_ + 1), - dist_(-kInitRange, kInitRange), - done_(true) {} + dist_(-kInitRange, kInitRange) {} bool IsDone() override { return done_; } @@ -164,7 +163,7 @@ class AcrobotEnv : public Env { kM * kL * kLC * dtheta1 * dtheta1 * std::sin(theta2) - phi2) / (kM * kLC * kLC + kI - d2 * d2 / d1); double ddtheta1 = -(d2 * ddtheta2 + phi1) / d1; - return V5(dtheta1, dtheta2, ddtheta1, ddtheta2, 0); + return {dtheta1, dtheta2, ddtheta1, ddtheta2, 0}; } void WriteState(float reward) { diff --git a/envpool/classic_control/cartpole.h b/envpool/classic_control/cartpole.h index 98fb8099..57dca894 100644 --- a/envpool/classic_control/cartpole.h +++ b/envpool/classic_control/cartpole.h @@ -63,15 +63,14 @@ class CartPoleEnv : public Env { int max_episode_steps_, elapsed_step_; double x_, x_dot_, theta_, theta_dot_; std::uniform_real_distribution<> dist_; - bool done_; + bool done_{true}; public: CartPoleEnv(const Spec& spec, int env_id) : Env(spec, env_id), max_episode_steps_(spec.config["max_episode_steps"_]), elapsed_step_(max_episode_steps_ + 1), - dist_(-kInitRange, kInitRange), - done_(true) {} + dist_(-kInitRange, kInitRange) {} bool IsDone() override { return done_; } diff --git a/envpool/classic_control/mountain_car.h b/envpool/classic_control/mountain_car.h index 69793e04..f1535091 100644 --- a/envpool/classic_control/mountain_car.h +++ b/envpool/classic_control/mountain_car.h @@ -56,15 +56,14 @@ class MountainCarEnv : public Env { int max_episode_steps_, elapsed_step_; double pos_, vel_; std::uniform_real_distribution<> dist_; - bool done_; + bool done_{true}; public: MountainCarEnv(const Spec& spec, int env_id) : Env(spec, env_id), max_episode_steps_(spec.config["max_episode_steps"_]), elapsed_step_(max_episode_steps_ + 1), - dist_(-0.6, -0.4), - done_(true) {} + dist_(-0.6, -0.4) {} bool IsDone() override { return done_; } diff --git a/envpool/classic_control/mountain_car_continuous.h b/envpool/classic_control/mountain_car_continuous.h index 21a1f231..a86739cb 100644 --- a/envpool/classic_control/mountain_car_continuous.h +++ b/envpool/classic_control/mountain_car_continuous.h @@ -56,15 +56,14 @@ class MountainCarContinuousEnv : public Env { int max_episode_steps_, elapsed_step_; double pos_, vel_; std::uniform_real_distribution<> dist_; - bool done_; + bool done_{true}; public: MountainCarContinuousEnv(const Spec& spec, int env_id) : Env(spec, env_id), max_episode_steps_(spec.config["max_episode_steps"_]), elapsed_step_(max_episode_steps_ + 1), - dist_(-0.6, -0.4), - done_(true) {} + dist_(-0.6, -0.4) {} bool IsDone() override { return done_; } diff --git a/envpool/classic_control/pendulum.h b/envpool/classic_control/pendulum.h index d3cc891a..f2a594ad 100644 --- a/envpool/classic_control/pendulum.h +++ b/envpool/classic_control/pendulum.h @@ -53,7 +53,7 @@ class PendulumEnv : public Env { int version_; double theta_, theta_dot_; std::uniform_real_distribution<> dist_, dist_dot_; - bool done_; + bool done_{true}; public: PendulumEnv(const Spec& spec, int env_id) @@ -62,8 +62,7 @@ class PendulumEnv : public Env { elapsed_step_(max_episode_steps_ + 1), version_(spec.config["version"_]), dist_(-M_PI, M_PI), - dist_dot_(-1, 1), - done_(true) {} + dist_dot_(-1, 1) {} bool IsDone() override { return done_; } diff --git a/envpool/core/array.h b/envpool/core/array.h index e99cd51b..84aabf35 100644 --- a/envpool/core/array.h +++ b/envpool/core/array.h @@ -116,8 +116,8 @@ class Array { if (shape_[0] > 0) { offset = start * size / shape_[0]; } - return Array(ptr_.get() + offset * element_size, std::move(new_shape), - element_size, [](char* p) {}); + return {ptr_.get() + offset * element_size, std::move(new_shape), + element_size, [](char* p) {}}; } /** diff --git a/envpool/core/async_envpool.h b/envpool/core/async_envpool.h index 089f998f..4ce04f40 100644 --- a/envpool/core/async_envpool.h +++ b/envpool/core/async_envpool.h @@ -117,7 +117,7 @@ class AsyncEnvPool : public EnvPool { } } - ~AsyncEnvPool() { + ~AsyncEnvPool() override { stop_ = 1; // LOG(INFO) << "envpool send: " << dur_send_.count(); // LOG(INFO) << "envpool recv: " << dur_recv_.count(); diff --git a/envpool/core/dict.h b/envpool/core/dict.h index 064b1eb1..263ce461 100644 --- a/envpool/core/dict.h +++ b/envpool/core/dict.h @@ -205,8 +205,11 @@ class Dict : public std::decay_t { std::enable_if_t::value, bool> = true> [[nodiscard]] std::vector AllValues() const { std::vector rets; - std::apply([&](auto&&... value) { (rets.push_back(Type(value)), ...); }, - *static_cast(this)); + std::apply( + [&](auto&&... value) { + (rets.push_back(static_cast(value)), ...); + }, + *static_cast(this)); return rets; } @@ -297,7 +300,7 @@ std::vector MakeArray(const std::tuple& specs) { * Takes a vector of `ShapeSpec`. */ std::vector MakeArray(const std::vector& specs) { - return std::vector(specs.begin(), specs.end()); + return {specs.begin(), specs.end()}; } #endif // ENVPOOL_CORE_DICT_H_ diff --git a/envpool/core/env.h b/envpool/core/env.h index d8f3381f..4d866072 100644 --- a/envpool/core/env.h +++ b/envpool/core/env.h @@ -58,7 +58,7 @@ class Env { private: StateBufferQueue* sbq_; - int order_, current_step_; + int order_, current_step_{-1}; bool is_single_player_; StateBuffer::WritableSlice slice_; // for parsing single env action from input action batch @@ -79,7 +79,6 @@ class Env { env_id_(env_id), seed_(spec.config["seed"_] + env_id), gen_(seed_), - current_step_(-1), is_single_player_(max_num_players_ == 1), action_specs_(spec.action_spec.template AllValues()), is_player_action_(Transform(action_specs_, [](const ShapeSpec& s) { @@ -88,6 +87,8 @@ class Env { slice_.done_write = [] { LOG(INFO) << "Use `Allocate` to write state."; }; } + virtual ~Env() = default; + void SetAction(std::shared_ptr> action_batch, int env_index) { action_batch_ = std::move(action_batch); diff --git a/envpool/core/env_spec.h b/envpool/core/env_spec.h index 32c4e866..123dae92 100644 --- a/envpool/core/env_spec.h +++ b/envpool/core/env_spec.h @@ -51,8 +51,8 @@ class EnvSpec { using Config = decltype(ConcatDict(common_config, EnvFns::DefaultConfig())); using ConfigKeys = typename Config::Keys; using ConfigValues = typename Config::Values; - using StateSpec = decltype( - ConcatDict(common_state_spec, EnvFns::StateSpec(std::declval()))); + using StateSpec = decltype(ConcatDict( + common_state_spec, EnvFns::StateSpec(std::declval()))); using ActionSpec = decltype(ConcatDict( common_action_spec, EnvFns::ActionSpec(std::declval()))); using StateKeys = typename StateSpec::Keys; diff --git a/envpool/core/envpool.h b/envpool/core/envpool.h index 75e945f5..0798581a 100644 --- a/envpool/core/envpool.h +++ b/envpool/core/envpool.h @@ -33,6 +33,7 @@ class EnvPool { using State = NamedVector>; using Action = NamedVector>; explicit EnvPool(EnvSpec spec) : spec(std::move(spec)) {} + virtual ~EnvPool() = default; protected: virtual void Send(const std::vector& action) { diff --git a/envpool/core/py_envpool.h b/envpool/core/py_envpool.h index f41f48fb..35a33e45 100644 --- a/envpool/core/py_envpool.h +++ b/envpool/core/py_envpool.h @@ -72,8 +72,8 @@ struct ArrayToNumpyHelper> { reinterpret_cast((*inner_ptr)->Data()), capsule); } } - return py::array(py::dtype("object"), a.Shape(), - reinterpret_cast(ptr->get()), capsule); + return {py::dtype("object"), a.Shape(), + reinterpret_cast(ptr->get()), capsule}; } }; @@ -83,7 +83,7 @@ Array NumpyToArray(const py::array& arr) { ArrayT arr_t(arr); ShapeSpec spec(arr_t.itemsize(), std::vector(arr_t.shape(), arr_t.shape() + arr_t.ndim())); - return Array(spec, reinterpret_cast(arr_t.mutable_data())); + return {spec, reinterpret_cast(arr_t.mutable_data())}; } template diff --git a/envpool/core/spec.h b/envpool/core/spec.h index fceb5310..909977f9 100644 --- a/envpool/core/spec.h +++ b/envpool/core/spec.h @@ -44,7 +44,7 @@ class ShapeSpec { [[nodiscard]] ShapeSpec Batch(int batch_size) const { std::vector new_shape = {batch_size}; new_shape.insert(new_shape.end(), shape.begin(), shape.end()); - return ShapeSpec(element_size, std::move(new_shape)); + return {element_size, std::move(new_shape)}; } [[nodiscard]] std::vector Shape() const { auto s = std::vector(shape.size()); diff --git a/envpool/core/state_buffer_queue_test.cc b/envpool/core/state_buffer_queue_test.cc index caac0394..2c8890fb 100644 --- a/envpool/core/state_buffer_queue_test.cc +++ b/envpool/core/state_buffer_queue_test.cc @@ -31,14 +31,18 @@ TEST(StateBufferQueueTest, Basic) { std::srand(std::time(nullptr)); std::size_t size = 0; for (std::size_t i = 0; i < batch; ++i) { + LOG(INFO) << i << " start"; std::size_t num_players = 1; auto slice = queue.Allocate(num_players); + LOG(INFO) << i << " allocate"; slice.done_write(); + LOG(INFO) << i << " done_write"; EXPECT_EQ(slice.arr[0].Shape(0), 10); EXPECT_EQ(slice.arr[1].Shape(0), 1); size += num_players; } std::vector out = queue.Wait(); + LOG(INFO) << "finish wait"; EXPECT_EQ(out[0].Shape(0), size); EXPECT_EQ(out[1].Shape(0), size); EXPECT_EQ(batch, size); diff --git a/envpool/dummy/dummy_envpool.h b/envpool/dummy/dummy_envpool.h index 9e2b82a2..a0ff212d 100644 --- a/envpool/dummy/dummy_envpool.h +++ b/envpool/dummy/dummy_envpool.h @@ -115,15 +115,14 @@ using DummyEnvSpec = EnvSpec; */ class DummyEnv : public Env { protected: - int state_; + int state_{0}; public: /** * Initilize the env, in this function we perform tasks like loading the game * rom etc. */ - DummyEnv(const Spec& spec, int env_id) - : Env(spec, env_id), state_(0) { + DummyEnv(const Spec& spec, int env_id) : Env(spec, env_id) { if (seed_ < 1) { seed_ = 1; } diff --git a/envpool/mujoco/dmc/manipulator.h b/envpool/mujoco/dmc/manipulator.h index 39db29c5..d508cbfc 100644 --- a/envpool/mujoco/dmc/manipulator.h +++ b/envpool/mujoco/dmc/manipulator.h @@ -173,7 +173,7 @@ class ManipulatorEnv : public Env, public MujocoEnv { while (penetrating) { for (std::size_t i = 0; i < kArmJoints.size(); ++i) { int id_joint = id_arm_joints_[i]; - bool is_limited = model_->jnt_limited[id_joint] == 1 ? true : false; + bool is_limited = model_->jnt_limited[id_joint] == 1; mjtNum lower = is_limited ? model_->jnt_range[id_joint * 2 + 0] : -M_PI; mjtNum upper = is_limited ? model_->jnt_range[id_joint * 2 + 1] : M_PI; data_->qpos[id_arm_qpos_[i]] = RandUniform(lower, upper)(gen_); diff --git a/envpool/mujoco/dmc/mujoco_env.cc b/envpool/mujoco/dmc/mujoco_env.cc index b4d4faa0..952175b9 100644 --- a/envpool/mujoco/dmc/mujoco_env.cc +++ b/envpool/mujoco/dmc/mujoco_env.cc @@ -26,8 +26,7 @@ MujocoEnv::MujocoEnv(const std::string& base_path, const std::string& raw_xml, int n_sub_steps, int max_episode_steps) : n_sub_steps_(n_sub_steps), max_episode_steps_(max_episode_steps), - elapsed_step_(max_episode_steps + 1), - done_(true) { + elapsed_step_(max_episode_steps + 1) { // initialize vfs from common assets and raw xml // https://github.com/deepmind/dm_control/blob/1.0.2/dm_control/mujoco/wrapper/core.py#L158 // https://github.com/deepmind/mujoco/blob/main/python/mujoco/structs.cc diff --git a/envpool/mujoco/dmc/mujoco_env.h b/envpool/mujoco/dmc/mujoco_env.h index 4e4434a1..23a75264 100644 --- a/envpool/mujoco/dmc/mujoco_env.h +++ b/envpool/mujoco/dmc/mujoco_env.h @@ -46,7 +46,7 @@ class MujocoEnv { mjData* data_; int n_sub_steps_, max_episode_steps_, elapsed_step_; float reward_, discount_; - bool done_; + bool done_{true}; #ifdef ENVPOOL_TEST std::unique_ptr qpos0_; #endif diff --git a/envpool/mujoco/gym/mujoco_env.h b/envpool/mujoco/gym/mujoco_env.h index cf70a8c8..03db2b6f 100644 --- a/envpool/mujoco/gym/mujoco_env.h +++ b/envpool/mujoco/gym/mujoco_env.h @@ -38,7 +38,7 @@ class MujocoEnv { int frame_skip_; bool post_constraint_; int max_episode_steps_, elapsed_step_; - bool done_; + bool done_{true}; public: MujocoEnv(const std::string& xml, int frame_skip, bool post_constraint, @@ -54,8 +54,7 @@ class MujocoEnv { frame_skip_(frame_skip), post_constraint_(post_constraint), max_episode_steps_(max_episode_steps), - elapsed_step_(max_episode_steps + 1), - done_(true) { + elapsed_step_(max_episode_steps + 1) { std::memcpy(init_qpos_, data_->qpos, sizeof(mjtNum) * model_->nq); std::memcpy(init_qvel_, data_->qvel, sizeof(mjtNum) * model_->nv); } diff --git a/envpool/toy_text/blackjack.h b/envpool/toy_text/blackjack.h index fd7b1f65..db7461df 100644 --- a/envpool/toy_text/blackjack.h +++ b/envpool/toy_text/blackjack.h @@ -51,15 +51,14 @@ class BlackjackEnv : public Env { bool natural_, sab_; std::vector player_, dealer_; std::uniform_int_distribution<> dist_; - bool done_; + bool done_{true}; public: BlackjackEnv(const Spec& spec, int env_id) : Env(spec, env_id), natural_(spec.config["natural"_]), sab_(spec.config["sab"_]), - dist_(1, 13), - done_(true) {} + dist_(1, 13) {} bool IsDone() override { return done_; } diff --git a/envpool/toy_text/catch.h b/envpool/toy_text/catch.h index 83e8b860..97a84305 100644 --- a/envpool/toy_text/catch.h +++ b/envpool/toy_text/catch.h @@ -48,15 +48,14 @@ class CatchEnv : public Env { protected: int x_, y_, height_, width_, paddle_; std::uniform_int_distribution<> dist_; - bool done_; + bool done_{true}; public: CatchEnv(const Spec& spec, int env_id) : Env(spec, env_id), height_(spec.config["height"_]), width_(spec.config["width"_]), - dist_(0, width_ - 1), - done_(true) {} + dist_(0, width_ - 1) {} bool IsDone() override { return done_; } diff --git a/envpool/toy_text/cliffwalking.h b/envpool/toy_text/cliffwalking.h index 5cd18dec..be1dfc6e 100644 --- a/envpool/toy_text/cliffwalking.h +++ b/envpool/toy_text/cliffwalking.h @@ -47,11 +47,11 @@ using CliffWalkingEnvSpec = EnvSpec; class CliffWalkingEnv : public Env { protected: int x_, y_; - bool done_; + bool done_{true}; public: CliffWalkingEnv(const Spec& spec, int env_id) - : Env(spec, env_id), done_(true) {} + : Env(spec, env_id) {} bool IsDone() override { return done_; } diff --git a/envpool/toy_text/frozen_lake.h b/envpool/toy_text/frozen_lake.h index 1197e642..efa47dc8 100644 --- a/envpool/toy_text/frozen_lake.h +++ b/envpool/toy_text/frozen_lake.h @@ -51,7 +51,7 @@ class FrozenLakeEnv : public Env { protected: int x_, y_, size_, max_episode_steps_, elapsed_step_; std::uniform_int_distribution<> dist_; - bool done_; + bool done_{true}; std::vector map_; public: @@ -59,8 +59,7 @@ class FrozenLakeEnv : public Env { : Env(spec, env_id), size_(spec.config["size"_]), max_episode_steps_(spec.config["max_episode_steps"_]), - dist_(-1, 1), - done_(true) { + dist_(-1, 1) { if (size_ != 8) { map_ = std::vector({"SFFF", "FHFH", "FFFH", "HFFG"}); } else { diff --git a/envpool/toy_text/nchain.h b/envpool/toy_text/nchain.h index 0ac4732e..5ba2a991 100644 --- a/envpool/toy_text/nchain.h +++ b/envpool/toy_text/nchain.h @@ -48,14 +48,13 @@ class NChainEnv : public Env { protected: int s_, max_episode_steps_, elapsed_step_; std::uniform_real_distribution<> dist_; - bool done_; + bool done_{true}; public: NChainEnv(const Spec& spec, int env_id) : Env(spec, env_id), max_episode_steps_(spec.config["max_episode_steps"_]), - dist_(0, 1), - done_(true) {} + dist_(0, 1) {} bool IsDone() override { return done_; } diff --git a/envpool/toy_text/taxi.h b/envpool/toy_text/taxi.h index d09fcf7e..3beca4d9 100644 --- a/envpool/toy_text/taxi.h +++ b/envpool/toy_text/taxi.h @@ -50,7 +50,7 @@ class TaxiEnv : public Env { protected: int x_, y_, s_, t_, max_episode_steps_, elapsed_step_; std::uniform_int_distribution<> dist_car_, dist_loc_; - bool done_; + bool done_{true}; std::vector> loc_; std::vector map_, loc_map_; @@ -60,7 +60,6 @@ class TaxiEnv : public Env { max_episode_steps_(spec.config["max_episode_steps"_]), dist_car_(0, 3), dist_loc_(0, 4), - done_(true), loc_({{0, 0}, {0, 4}, {4, 0}, {4, 3}}), map_({"|:|::|", "|:|::|", "|::::|", "||:|:|", "||:|:|"}), loc_map_({"0 1", " ", " ", " ", "2 3 "}) {} diff --git a/envpool/vizdoom/vizdoom_env.h b/envpool/vizdoom/vizdoom_env.h index a579e782..8f4cb191 100644 --- a/envpool/vizdoom/vizdoom_env.h +++ b/envpool/vizdoom/vizdoom_env.h @@ -143,11 +143,11 @@ class VizdoomEnv : public Env { std::deque stack_buf_; std::string lmp_dir_; bool save_lmp_, episodic_life_, use_combined_action_, use_inter_area_resize_; - bool done_; + bool done_{true}; int max_episode_steps_, elapsed_step_, stack_num_, frame_skip_, - episode_count_, channel_; + episode_count_{0}, channel_; int deathcount_idx_, hitcount_idx_, damagecount_idx_; // bugged var - double last_deathcount_, last_hitcount_, last_damagecount_; + double last_deathcount_{0}, last_hitcount_{0}, last_damagecount_{0}; int selected_weapon_, selected_weapon_count_, weapon_duration_; std::vector action_set_; std::vector