Skip to content

Commit c5eb4ff

Browse files
authored
Merge pull request #162 from pockerman/bug/159-fix-envs-for-new-api
Refactor rest rl env client
2 parents 8c870f7 + 3732822 commit c5eb4ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1119
-1837
lines changed

.github/workflows/build.yml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build rlenvscpp
1+
name: Build bitrl
22

33
on:
44
push:
@@ -21,7 +21,39 @@ jobs:
2121
- uses: actions/checkout@v4
2222
- name: Install dependencies
2323
run: |
24-
sudo apt-get install -y g++ cmake libboost-all-dev libgtest-dev libeigen3-dev libblas-dev
24+
sudo apt-get install -y \
25+
build-essential \
26+
cmake \
27+
git \
28+
libboost-all-dev \
29+
libgtest-dev \
30+
libeigen3-dev \
31+
libblas-dev \
32+
libopencv-dev
33+
# g++ cmake libboost-all-dev libgtest-dev libeigen3-dev libblas-dev
34+
- name: Build and install Paho MQTT C
35+
run: |
36+
git clone https://github.com/eclipse/paho.mqtt.c.git
37+
cd paho.mqtt.c
38+
cmake -Bbuild -H. \
39+
-DPAHO_WITH_SSL=ON \
40+
-DPAHO_BUILD_SHARED=ON \
41+
-DPAHO_BUILD_STATIC=OFF
42+
cmake --build build
43+
sudo cmake --install build
44+
sudo ldconfig
45+
- name: Build and install Paho MQTT C++
46+
run: |
47+
git clone https://github.com/eclipse/paho.mqtt.cpp.git
48+
cd paho.mqtt.cpp
49+
cmake -Bbuild -H. \
50+
-DPAHO_WITH_SSL=ON \
51+
-DPAHO_BUILD_SHARED=ON \
52+
-DPAHO_BUILD_STATIC=OFF
53+
cmake --build build
54+
sudo cmake --install build
55+
sudo ldconfig
56+
2557
- name: Configure CMake
2658
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
2759
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MESSAGE(STATUS "Using CMake ${CMAKE_VERSION}")
33

44

55
SET(BITRL_VERSION_MAJOR 1)
6-
SET(BITRL_VERSION_MINOR 2)
6+
SET(BITRL_VERSION_MINOR 8)
77
SET(BITRL_VERSION_PATCH 0)
88

99
SET(BITRL_VERSION "${BITRL_VERSION_MAJOR}.${BITRL_VERSION_MINOR}.${BITRL_VERSION_PATCH}")

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22
# bitrl
33

44
```bitrl``` is an effort to provide implementations and wrappers of environments suitable for training reinforcement learning agents
5-
using C++.
5+
using C++. The documentation for the library can be found <a href="https://pockerman.github.io/bitrl/index.html">here</a>.
66

7-
Furthermore, there is some minimal support for working with Arduino UNO boards over USB or WiFi.
8-
See also <a href="https://rlenvscpp.readthedocs.io/en/latest/working_with_webots.html">Working with Webots</a>
9-
for how to integrate ```bitrl``` with <a href="https://cyberbotics.com/doc/guide/installing-webots">Webots</a>.
10-
11-
Various RL algorithms using the environments can be found at <a href="https://github.com/pockerman/cuberl/tree/master">cuberl</a>.
12-
13-
The documentation for the library can be found <a href="https://pockerman.github.io/bitrl/index.html">here</a>.
147
The following is an example how to use the
158
``FrozenLake`` environment from <a href="https://github.com/Farama-Foundation/Gymnasium/tree/main">Gymnasium</a>.
169

1710
```
1811
1912
#include "bitrl/bitrl_types.h"
2013
#include "bitrl/envs/gymnasium/toy_text/frozen_lake_env.h"
21-
#include "bitrl/envs/api_server/apiserver.h"
14+
#include "bitrl/network/rest_rl_env_client.h"
2215
2316
#include <iostream>
2417
#include <string>
@@ -32,29 +25,33 @@ const std::string SERVER_URL = "http://0.0.0.0:8001/api";
3225
using bitrl::envs::gymnasium::FrozenLake;
3326
using bitrl::envs::RESTApiServerWrapper;
3427
35-
3628
void test_frozen_lake(const RESTApiServerWrapper& server){
3729
3830
FrozenLake<4> env(server);
3931
4032
std::cout<<"Environame URL: "<<env.get_url()<<std::endl;
4133
4234
// make the environment
43-
std::unordered_map<std::string, std::any> options;
44-
options.insert({"is_slippery", false});
45-
env.make("v1", options);
35+
std::unordered_map<std::string, std::any> make_ops;
36+
make_ops.insert({"is_slippery", false});
37+
38+
std::unordered_map<std::string, std::any> reset_ops;
39+
reset_ops.insert({"seed", static_cast<uint_t>(42)});
40+
env.make("v1", make_ops, reset_ops);
4641
4742
std::cout<<"Is environment created? "<<env.is_created()<<std::endl;
4843
std::cout<<"Is environment alive? "<<env.is_alive()<<std::endl;
4944
std::cout<<"Number of valid actions? "<<env.n_actions()<<std::endl;
5045
std::cout<<"Number of states? "<<env.n_states()<<std::endl;
46+
std::cout<<"Env idx: "<<env.idx()<<std::endl;
5147
5248
// reset the environment
53-
auto time_step = env.reset(42, std::unordered_map<std::string, std::any>());
49+
auto time_step = env.reset();
5450
5551
std::cout<<"Reward on reset: "<<time_step.reward()<<std::endl;
5652
std::cout<<"Observation on reset: "<<time_step.observation()<<std::endl;
5753
std::cout<<"Is terminal state: "<<time_step.done()<<std::endl;
54+
std::cout<<"Env idx: "<<env.idx()<<std::endl;
5855
5956
//...print the time_step
6057
std::cout<<time_step<<std::endl;
@@ -73,7 +70,6 @@ void test_frozen_lake(const RESTApiServerWrapper& server){
7370
std::cout<<"Dynamics for state="<<state<<" and action="<<action<<std::endl;
7471
7572
for(auto item:dynamics){
76-
7773
std::cout<<std::get<0>(item)<<std::endl;
7874
std::cout<<std::get<1>(item)<<std::endl;
7975
std::cout<<std::get<2>(item)<<std::endl;
@@ -85,13 +81,10 @@ void test_frozen_lake(const RESTApiServerWrapper& server){
8581
8682
new_time_step = env.step(action);
8783
std::cout<<new_time_step<<std::endl;
88-
89-
std::cout<<"env cidx: "<<env.cidx()<<std::endl;
9084
9185
// close the environment
9286
env.close();
9387
}
94-
9588
}
9689
9790
@@ -109,3 +102,11 @@ int main(){
109102
}
110103
111104
```
105+
106+
Gymnasium environments exposed over a REST like API can be found at: <href="https://github.com/pockerman/bitrl-rest-api">bitrl-rest-api</a>
107+
Various RL algorithms using the environments can be found at <a href="https://github.com/pockerman/cuberl/tree/master">cuberl</a>.
108+
109+
Furthermore, there is some minimal support for working with Arduino UNO boards over USB or WiFi.
110+
See also <a href="https://rlenvscpp.readthedocs.io/en/latest/working_with_webots.html">Working with Webots</a>
111+
for how to integrate ```bitrl``` with <a href="https://cyberbotics.com/doc/guide/installing-webots">Webots</a>.
112+

examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ IF(BITRL_WEBOTS)
2525
ENDIF()
2626

2727
ADD_SUBDIRECTORY(example_1)
28-
ADD_SUBDIRECTORY(example_2)
29-
ADD_SUBDIRECTORY(example_3)
28+
#ADD_SUBDIRECTORY(example_2)
29+
#ADD_SUBDIRECTORY(example_3)
3030
#ADD_SUBDIRECTORY(example_4)
3131
ADD_SUBDIRECTORY(example_5)
3232
ADD_SUBDIRECTORY(example_6)

examples/box2d/box2d_example.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
//
44

55
#include "bitrl/envs/gymnasium/box2d/lunar_lander_env.h"
6-
#include "bitrl/envs/api_server/apiserver.h"
6+
#include "bitrl/network/rest_rl_env_client.h"
77

88
#include <unordered_map>
99
#include <vector>
1010
#include <iostream>
1111

12+
#include "../../src/bitrl/sensors/ekf_sensor_fusion.h"
13+
1214
namespace box2d_example
1315
{
16+
using namespace bitrl;
1417
const std::string SERVER_URL = "http://0.0.0.0:8001/api";
1518
using bitrl::real_t;
16-
using bitrl::envs::RESTApiServerWrapper;
1719
using bitrl::envs::gymnasium::LunarLanderDiscreteEnv;
1820
using bitrl::envs::gymnasium::LunarLanderContinuousEnv;
1921
}
@@ -22,7 +24,7 @@ int main()
2224
{
2325
using namespace box2d_example;
2426

25-
RESTApiServerWrapper server(SERVER_URL, true);
27+
bitrl::network::RESTRLEnvClient server(SERVER_URL, true);
2628

2729
std::unordered_map<std::string, std::any> options;
2830
options["wind_power"] = std::any(static_cast<bitrl::real_t>(10.0));
@@ -57,7 +59,9 @@ int main()
5759
std::cout<<"Working with LunarLanderContinuousEnv..."<<std::endl;
5860

5961
LunarLanderContinuousEnv env(server);
60-
env.make("v3", options);
62+
63+
std::unordered_map<std::string, std::any> reset_options;
64+
env.make("v3", options, reset_options);
6165

6266
std::cout<<"Is environment created? "<<env.is_created()<<std::endl;
6367
std::cout<<"Is environment alive? "<<env.is_alive()<<std::endl;
@@ -70,13 +74,8 @@ int main()
7074
std::vector<real_t> action = {0.8, 0.9};
7175
time_step = env.step(action);
7276
std::cout<<"Time step: "<<time_step<<std::endl;
73-
74-
auto copy = env.make_copy(1);
75-
time_step = copy.reset();
76-
std::cout<<"Time step: "<<time_step<<std::endl;
77-
7877
env.close();
79-
copy.close();
78+
8079
}
8180

8281
}

0 commit comments

Comments
 (0)