Skip to content

Commit

Permalink
fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alicia1529 committed Nov 11, 2022
1 parent f28c1eb commit ae4d6fb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 40 deletions.
12 changes: 8 additions & 4 deletions envpool/box2d/box2d_correctness_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def run_space_check(self, env0: gym.Env, env1: Any) -> None:
elif isinstance(act0, gym.spaces.Discrete):
np.testing.assert_allclose(act0.n, act1.n)

def bipedal_walker_space(self) -> None:
def test_bipedal_walker_space(self) -> None:
env0 = gym.make("BipedalWalker-v3")
env1 = make_gym("BipedalWalker-v3")
self.run_space_check(env0, env1)

def lunar_lander_space(self) -> None:
def test_lunar_lander_space(self) -> None:
env0 = gym.make("LunarLander-v2")
env1 = make_gym("LunarLander-v2")
self.run_space_check(env0, env1)
Expand All @@ -54,6 +54,11 @@ def lunar_lander_space(self) -> None:
env1 = make_gym("LunarLanderContinuous-v2")
self.run_space_check(env0, env1)

def test_car_racing_space(self) -> None:
env0 = gym.make("CarRacing-v2")
env1 = make_gym("CarRacing-v2")
self.run_space_check(env0, env1)

@staticmethod
def heuristic_lunar_lander_policy(
s: np.ndarray, continuous: bool
Expand Down Expand Up @@ -124,7 +129,7 @@ def solve_car_racing(self, num_envs: int, action, target_reward) -> None:
obs = env.reset(env_id)
rewards = np.zeros(num_envs)
action = np.tile(action, (num_envs, 1))
for i in range(max_episode_steps):
for _ in range(max_episode_steps):
obs, rew, terminated, truncated, info = env.step(action, env_id)
env_id = info["env_id"]
rewards[env_id] += rew
Expand All @@ -135,7 +140,6 @@ def solve_car_racing(self, num_envs: int, action, target_reward) -> None:
env_id = env_id[~done]
mean_reward = np.mean(rewards)
logging.info(f"{np.mean(rewards):.6f} ± {np.std(rewards):.6f}")
print(f"{np.mean(rewards):.6f} ± {np.std(rewards):.6f}")

self.assertTrue(abs(target_reward - mean_reward) < 10, (mean_reward))

Expand Down
4 changes: 1 addition & 3 deletions envpool/box2d/box2d_deterministic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ def run_deterministic_check(
env1 = make_gym(task_id, num_envs=num_envs, seed=0, **kwargs)
env2 = make_gym(task_id, num_envs=num_envs, seed=1, **kwargs)
act_space = env0.action_space
for i in range(5000):
for _ in range(5000):
action = np.array([act_space.sample() for _ in range(num_envs)])
obs0 = env0.step(action)[0]
obs1 = env1.step(action)[0]
obs2 = env2.step(action)[0]
np.testing.assert_allclose(obs0, obs1)
if i % 1000 == 0:
print(obs0)
self.assertFalse(np.allclose(obs0, obs2))

def test_car_racing(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions envpool/box2d/car_dynamics.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ b2PolygonShape GeneratePolygon(const float* array, int size) {
return polygon;
}

Car::Car(std::shared_ptr<b2World>& world, float init_angle, float init_x,
Car::Car(const std::shared_ptr<b2World>& world, float init_angle, float init_x,
float init_y)
: world_(world), hull_(nullptr) {
// Create hull
Expand Down Expand Up @@ -195,8 +195,8 @@ void Car::step(float dt) {
}
}

void Car::draw(cv::Mat& surf, float zoom, std::array<float, 2>& translation,
float angle) {
void Car::draw(const cv::Mat& surf, float zoom,
const std::array<float, 2>& translation, float angle) {
for (size_t i = 0; i < drawlist_.size(); i++) {
auto body = drawlist_[i];
cv::Scalar color;
Expand Down Expand Up @@ -242,4 +242,4 @@ std::vector<float> Car::GetBrake() {
wheels_[3]->brake};
}

} // namespace box2d
} // namespace box2d
8 changes: 4 additions & 4 deletions envpool/box2d/car_dynamics.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ class Wheel : public UserData {

class Car {
public:
Car(std::shared_ptr<b2World>& world, float init_angle, float init_x,
Car(const std::shared_ptr<b2World>& world, float init_angle, float init_x,
float init_y);
void gas(float g);
void brake(float b);
void steer(float s);
void step(float dt);
void draw(cv::Mat& surf, float zoom, std::array<float, 2>& translation,
float angle);
void draw(const cv::Mat& surf, float zoom,
const std::array<float, 2>& translation, float angle);
void destroy();
float GetFuelSpent();
std::vector<float> GetGas();
Expand All @@ -108,4 +108,4 @@ class Car {

} // namespace box2d

#endif // ENVPOOL_BOX2D_CAR_DYNAMICS_H_
#endif // ENVPOOL_BOX2D_CAR_DYNAMICS_H_
2 changes: 1 addition & 1 deletion envpool/box2d/car_racing.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ using CarRacingEnvPool = AsyncEnvPool<CarRacingEnv>;

} // namespace box2d

#endif // ENVPOOL_BOX2D_CAR_RACING_H_
#endif // ENVPOOL_BOX2D_CAR_RACING_H_
30 changes: 16 additions & 14 deletions envpool/box2d/car_racing_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

#include "car_racing_env.h"

#include <algorithm>
#include <cmath>
#include <memory>
#include <random>
#include <utility>
#include <vector>

namespace box2d {
Expand Down Expand Up @@ -357,8 +360,9 @@ void CarRacingBox2dEnv::CreateImageArray() {
}

void CarRacingBox2dEnv::DrawColoredPolygon(
std::array<std::array<float, 2>, 4>& field, cv::Scalar color, float zoom,
std::array<float, 2>& translation, float angle, bool clip) {
const std::array<std::array<float, 2>, 4>& field, cv::Scalar color,
float zoom, const std::array<float, 2>& translation, float angle,
bool clip) {
// This checks if the polygon is out of bounds of the screen, and we skip
// drawing if so. Instead of calculating exactly if the polygon and screen
// overlap, we simply check if the polygon is in a larger bounding box whose
Expand All @@ -367,26 +371,24 @@ void CarRacingBox2dEnv::DrawColoredPolygon(

bool exist = false;
std::vector<cv::Point> poly;
for (int i = 0; i < 4; i++) {
field[i] = RotateRad(field[i], angle);
field[i] = {field[i][0] * zoom + translation[0],
field[i][1] * zoom + translation[1]};
poly.push_back(cv::Point(field[i][0], field[i][1]));
if (-kMaxShapeDim <= field[i][0] && field[i][0] <= windowW + kMaxShapeDim &&
-kMaxShapeDim <= field[i][1] && field[i][1] <= windowH + kMaxShapeDim) {
for (const auto& f : field) {
auto f_roated = RotateRad(f, angle);
f_roated = {f_roated[0] * zoom + translation[0],
f_roated[1] * zoom + translation[1]};
poly.push_back(cv::Point(f_roated[0], f_roated[1]));
if (-kMaxShapeDim <= f_roated[0] && f_roated[0] <= windowW + kMaxShapeDim &&
-kMaxShapeDim <= f_roated[1] && f_roated[1] <= windowH + kMaxShapeDim) {
exist = true;
}
}

if (!clip || exist) {
cv::fillPoly(surf_, poly, color);
// gfxdraw.aapolygon(self.surf, poly, color)
// gfxdraw.filled_polygon(self.surf, poly, color)
}
}

void CarRacingBox2dEnv::RenderRoad(float zoom,
std::array<float, 2>& translation,
const std::array<float, 2>& translation,
float angle) {
std::array<std::array<float, 2>, 4> field;
field[0] = {kPlayfiled, kPlayfiled};
Expand Down Expand Up @@ -502,8 +504,8 @@ void CarRacingBox2dEnv::Render(RenderMode mode) {
float scroll_x = -car_->hull_->GetPosition().x * zoom;
float scroll_y = -car_->hull_->GetPosition().y * zoom;

// trans = pygame.math.Vector2((scroll_x, scroll_y)).rotate_rad(angle)
std::array<float, 2> trans = RotateRad({scroll_x, scroll_y}, angle);
std::array<float, 2> scroll = {scroll_x, scroll_y};
std::array<float, 2> trans = RotateRad(scroll, angle);
trans = {windowW / 2 + trans[0], windowH / 4 + trans[1]};

RenderRoad(zoom, trans, angle);
Expand Down
13 changes: 9 additions & 4 deletions envpool/box2d/car_racing_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

#include <box2d/box2d.h>

#include <algorithm>
#include <cmath>
#include <memory>
#include <random>
#include <unordered_set>
#include <utility>
#include <vector>

#include "car_dynamics.h"
Expand Down Expand Up @@ -97,11 +100,13 @@ class CarRacingBox2dEnv {
public:
CarRacingBox2dEnv(int max_episode_steps, float lap_complete_percent);
void Render(RenderMode mode);
void RenderRoad(float zoom, std::array<float, 2>& translation, float angle);

void RenderRoad(float zoom, const std::array<float, 2>& translation,
float angle);
void RenderIndicators();
void DrawColoredPolygon(std::array<std::array<float, 2>, 4>& field,
void DrawColoredPolygon(const std::array<std::array<float, 2>, 4>& field,
cv::Scalar color, float zoom,
std::array<float, 2>& translation, float angle,
const std::array<float, 2>& translation, float angle,
bool clip = true);
void CarRacingReset(std::mt19937* gen);
void CarRacingStep(std::mt19937* gen, float action0, float action1,
Expand All @@ -121,4 +126,4 @@ class CarRacingBox2dEnv {

} // namespace box2d

#endif // ENVPOOL_BOX2D_CAR_RACING_ENV_H_
#endif // ENVPOOL_BOX2D_CAR_RACING_ENV_H_
7 changes: 4 additions & 3 deletions envpool/box2d/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ float Sign(double val, double eps) {
}
return 0;
}
std::array<float, 2> RotateRad(std::array<float, 2> vec, float angle) {

std::array<float, 2> RotateRad(const std::array<float, 2>& vec, float angle) {
return {std::cos(angle) * vec[0] - std::sin(angle) * vec[1],
std::sin(angle) * vec[0] + std::cos(angle) * vec[1]};
}

b2Vec2 RotateRad(b2Vec2& v, float angle) {
b2Vec2 RotateRad(const b2Vec2& v, float angle) {
return {std::cos(angle) * v.x - std::sin(angle) * v.y,
std::sin(angle) * v.x + std::cos(angle) * v.y};
}

b2Vec2 Multiply(b2Transform& trans, b2Vec2& v) {
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);
Expand Down
7 changes: 4 additions & 3 deletions envpool/box2d/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ b2Vec2 Vec2(double x, double y);

float Sign(double val, double eps = 1e-8);

std::array<float, 2> RotateRad(std::array<float, 2> vec, float angle);
b2Vec2 RotateRad(b2Vec2& v, float angle);
std::array<float, 2> RotateRad(const std::array<float, 2>& vec, float angle);

b2Vec2 Multiply(b2Transform& trans, b2Vec2& v);
b2Vec2 RotateRad(const b2Vec2& v, float angle);

b2Vec2 Multiply(const b2Transform& trans, const b2Vec2& v);
} // namespace box2d

#endif // ENVPOOL_BOX2D_UTILS_H_

0 comments on commit ae4d6fb

Please sign in to comment.