Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include/cubeai/base/cubeai_version.h
include/cubeai/base/cubeai_config.h
include/cuberl/base/cubeai_version.h
include/cuberl/base/cubeai_config.h
67 changes: 32 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ ENDIF(COMMAND cmake_policy)

OPTION(ENABLE_WEBOTS_FLAG OFF)

# configure the project accordingly
# Set the build type options supported
# Debug and Release are the types supported
SET(CMAKE_BUILD_TYPE "Debug")
#SET(CMAKE_CXX_COMPILER g++-11)
SET(CMAKE_CXX_STANDARD 20)
#SET(CMAKE_CXX_STANDARD_REQUIRED True)
#SET(CMAKE_C_COMPILER gcc-11)
#SET(CMAKE_C_STANDARD 20)
#SET(CMAKE_C_STANDARD_REQUIRED True)
SET(CMAKE_LINKER_FLAGS "-pthread")

SET(USE_OPENMP ON)
SET(USE_LOG ON)
SET(USE_PYTORCH ON)
Expand All @@ -62,7 +50,21 @@ SET(USE_RL ON)
SET(DATA_SET_FOLDER ${PROJECT_SOURCE_DIR}/data)
SET(ENABLE_EXAMPLES_FLAG ON)
SET(ENABLE_TESTS_FLAG OFF) # Testing
SET(ENABLE_DOC_FLAG OFF) # Documentation


# configure the project accordingly
# Set the build type options supported
# Debug and Release are the types supported
SET(CMAKE_BUILD_TYPE "Release")
#SET(CMAKE_CXX_COMPILER g++-11)
SET(CMAKE_CXX_STANDARD 20)
#SET(CMAKE_CXX_STANDARD_REQUIRED True)
#SET(CMAKE_C_COMPILER gcc-11)
#SET(CMAKE_C_STANDARD 20)
#SET(CMAKE_C_STANDARD_REQUIRED True)
SET(CMAKE_LINKER_FLAGS "-pthread")



# if we enable Webots then specify the paths
# Note that if rlenvscpp is compiled with
Expand Down Expand Up @@ -174,8 +176,8 @@ MESSAGE(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
MESSAGE(STATUS "Project SRC directory: ${PROJECT_SOURCE_DIR}")

# write configuration and version files
configure_file(config.h.in ${PROJECT_SOURCE_DIR}/include/cubeai/base/cubeai_config.h @ONLY)
configure_file(version.h.in ${PROJECT_SOURCE_DIR}/include/cubeai/base/cubeai_version.h @ONLY)
configure_file(config.h.in ${PROJECT_SOURCE_DIR}/include/cuberl/base/cubeai_config.h @ONLY)
configure_file(version.h.in ${PROJECT_SOURCE_DIR}/include/cuberl/base/cubeai_version.h @ONLY)

# include directories
INCLUDE_DIRECTORIES(${BOOST_INCLUDEDIR})
Expand All @@ -189,23 +191,23 @@ ENDIF()
INCLUDE_DIRECTORIES(include/)

# collect the source files for the library
FILE(GLOB SRCS src/cubeai/base/*.cpp
src/cubeai/rl/*.cpp
src/cubeai/rl/*/*.cpp
src/cubeai/rl/algorithms/*.cpp
src/cubeai/rl/algorithms/*/*.cpp
src/cubeai/utils/*.cpp
src/cubeai/maths/*.cpp
src/cubeai/maths/statistics/*.cpp
src/cubeai/maths/statistics/distributions/*.cpp
src/cubeai/maths/optimization/*.cpp
src/cubeai/control/*.cpp
FILE(GLOB SRCS src/cuberl/base/*.cpp
src/cuberl/rl/*.cpp
src/cuberl/rl/*/*.cpp
src/cuberl/rl/algorithms/*.cpp
src/cuberl/rl/algorithms/*/*.cpp
src/cuberl/utils/*.cpp
src/cuberl/maths/*.cpp
src/cuberl/maths/statistics/*.cpp
src/cuberl/maths/statistics/distributions/*.cpp
src/cuberl/maths/optimization/*.cpp
src/cuberl/control/*.cpp
#src/cubeai/planning/*.cpp
)

ADD_LIBRARY(cubeailib SHARED ${SRCS})
SET_TARGET_PROPERTIES(cubeailib PROPERTIES LINKER_LANGUAGE CXX)
INSTALL(TARGETS cubeailib DESTINATION ${CMAKE_INSTALL_PREFIX})
ADD_LIBRARY(cuberllib SHARED ${SRCS})
SET_TARGET_PROPERTIES(cuberllib PROPERTIES LINKER_LANGUAGE CXX)
INSTALL(TARGETS cuberllib DESTINATION ${CMAKE_INSTALL_PREFIX})

IF(ENABLE_EXAMPLES_FLAG)
# Add the examples
Expand All @@ -221,12 +223,7 @@ ELSE()
MESSAGE(WARNING "Tests have not been enabled")
ENDIF()

IF(ENABLE_DOC_FLAG)
# Add the documentation
ADD_SUBDIRECTORY(doc)
ELSE()
MESSAGE(WARNING "Documentation will not be generated")
ENDIF()


MESSAGE(STATUS "Installation destination at: ${CMAKE_INSTALL_PREFIX}")
MESSAGE(STATUS "Type make install to build cubeAI")
2 changes: 1 addition & 1 deletion examples/filtering/filtering_example_1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SET(SOURCE ${EXECUTABLE}.cpp)

ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})

TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cubeailib bitrllib pthread openblas tbb boost_log)
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cuberllib bitrllib pthread openblas tbb boost_log)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Greg Welch and Gary Bishop
* */

#include "cubeai/base/cubeai_types.h"
#include "cubeai/estimation/kalman_filter.h"
#include "cubeai/estimation/kf_model_base.h"
#include "cuberl/base/cubeai_types.h"
#include "cuberl/estimation/kalman_filter.h"
#include "cuberl/estimation/kf_model_base.h"
#include "bitrl/utils/iteration_counter.h"
#include "bitrl/utils/io/csv_file_writer.h"

Expand Down
2 changes: 1 addition & 1 deletion examples/filtering/filtering_example_2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SET(SOURCE ${EXECUTABLE}.cpp)

ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})

TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cubeailib bitrllib pthread openblas tbb boost_log)
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cuberllib bitrllib pthread openblas tbb boost_log)



Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "cubeai/base/cubeai_types.h"
#include "cubeai/estimation/extended_kalman_filter.h"
#include "cuberl/base/cubeai_types.h"
#include "cuberl/estimation/extended_kalman_filter.h"
#include "bitrl/utils/iteration_counter.h"
#include "bitrl/utils/io/csv_file_writer.h"
#include "bitrl/dynamics/diff_drive_dynamics.h"
Expand Down
2 changes: 1 addition & 1 deletion examples/intro/intro_example_1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SET(SOURCE ${EXECUTABLE}.cpp)

ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})

TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cubeailib bitrllib pthread openblas tbb boost_log)
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cuberllib bitrllib pthread openblas tbb boost_log)



Expand Down
4 changes: 2 additions & 2 deletions examples/intro/intro_example_1/intro_example_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* */

#include "cubeai/base/cubeai_config.h"
#include "cubeai/base/cubeai_types.h"
#include "cuberl/base/cubeai_config.h"
#include "cuberl/base/cubeai_types.h"
#include "bitrl/utils/io/json_file_reader.h"
#include "bitrl/utils/iteration_counter.h"
#include "bitrl/utils/geometry/shapes/circle.h"
Expand Down
2 changes: 1 addition & 1 deletion examples/intro/intro_example_2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SET(SOURCE ${EXECUTABLE}.cpp)

ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})

TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cubeailib bitrllib pthread openblas tbb boost_log)
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cuberllib bitrllib pthread openblas tbb boost_log)



Expand Down
4 changes: 2 additions & 2 deletions examples/intro/intro_example_2/intro_example_2.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "cubeai/base/cubeai_config.h"
#include "cuberl/base/cubeai_config.h"

#ifdef USE_PYTORCH

#include "cubeai/base/cubeai_types.h"
#include "cuberl/base/cubeai_types.h"

#include <torch/torch.h>
#include <boost/log/trivial.hpp>
Expand Down
2 changes: 1 addition & 1 deletion examples/intro/intro_example_3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SET(SOURCE ${EXECUTABLE}.cpp)

ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})

TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cubeailib bitrllib pthread openblas tbb boost_log)
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE cuberllib bitrllib pthread openblas tbb boost_log)



Expand Down
84 changes: 64 additions & 20 deletions examples/intro/intro_example_3/intro_example_3.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "cubeai/base/cubeai_config.h"
#include "cuberl/base/cubeai_config.h"

#ifdef USE_PYTORCH

#include "cubeai/base/cubeai_types.h"
#include "cuberl/base/cubeai_types.h"
#include "bitrl/utils/iteration_counter.h"
#include "bitrl/utils/io/json_file_reader.h"

Expand All @@ -13,7 +13,7 @@
#include <filesystem>
#include <string>

namespace intro_example_2
namespace example
{

using cuberl::real_t;
Expand All @@ -24,13 +24,10 @@ using bitrl::utils::IterationCounter;
namespace fs = std::filesystem;
const std::string CONFIG = "config.json";

}

int main() {

using namespace intro_example_2;

try{
std::string train_model()
{
try{

// load the json configuration
JSONFileReader json_reader(CONFIG);
Expand Down Expand Up @@ -59,7 +56,6 @@ int main() {
BOOST_LOG_TRIVIAL(info)<<"Max epochs: "<<num_epochs;
BOOST_LOG_TRIVIAL(info)<<"Learning rate: "<<learning_rate;


// figure out the device we are using
auto cuda_available = torch::cuda::is_available();
torch::Device device(cuda_available ? torch::kCUDA : torch::kCPU);
Expand All @@ -70,8 +66,6 @@ int main() {
BOOST_LOG_TRIVIAL(info)<<"CUDA is not available. Training on CPU "<<std::endl;
}

torch::manual_seed(42);

// Sample dataset
auto x_train = torch::randint(0, 10, {15, 1},
torch::TensorOptions(torch::kFloat).device(device));
Expand All @@ -87,14 +81,12 @@ int main() {
torch::optim::SGD optimizer(model->parameters(),
torch::optim::SGDOptions(learning_rate));

// Set floating point output precision
BOOST_LOG_TRIVIAL(info)<<"Start training...";


IterationCounter iteration_ctrl(num_epochs);
while(iteration_ctrl.continue_iterations()){

// Forward pass
// Forward pass
auto output = model->forward(x_train);
auto loss = torch::nn::functional::mse_loss(output, y_train);

Expand All @@ -109,20 +101,72 @@ int main() {
}
}

BOOST_LOG_TRIVIAL(info)<<"Training is finished... ";
BOOST_LOG_TRIVIAL(info)<<"Training finished... ";

// let's also save the model
auto model_filename = std::string(EXPERIMENT_DIR_PATH) + std::string("/linear_regression_model.pth");
torch::save(model, model_filename);
BOOST_LOG_TRIVIAL(info)<<"Saving model at: "<<model_filename<<std::endl;

torch::save(model, model_filename);
return model_filename;
}
catch(std::exception& e){
std::cout<<e.what()<<std::endl;
}
catch(...){

std::cout<<"Unknown exception occured"<<std::endl;
std::cout<<"Unknown exception occurred"<<std::endl;
}
}

void test_model(std::string& model_filename)
{
BOOST_LOG_TRIVIAL(info)<<"Start testing...";
// load the json configuration
JSONFileReader json_reader(CONFIG);
json_reader.open();

const auto input_size = json_reader.template get_value<uint_t>("input_size");
const auto output_size = json_reader.template get_value<uint_t>("output_size");

// figure out the device we are using
auto cuda_available = torch::cuda::is_available();
torch::Device device(cuda_available ? torch::kCUDA : torch::kCPU);
if(cuda_available){
BOOST_LOG_TRIVIAL(info)<<"CUDA available. Testing on GPU ";
}
else{
BOOST_LOG_TRIVIAL(info)<<"CUDA is not available. Testing on CPU ";
}

torch::nn::Linear model(input_size, output_size);
torch::load(model, model_filename);
model->to(device);

// we will evaluate the model
model -> eval();

// Sample dataset
auto x_test = torch::randint(0, 10, {15, 1},
torch::TensorOptions(torch::kFloat).device(device));
// get the test predictions
auto output = model->forward(x_test);

// do some comparisons
// ...

BOOST_LOG_TRIVIAL(info)<<"Testing finished... ";
}

}

int main() {

using namespace example;

// set seed for torch
torch::manual_seed(42);
auto model_filename = train_model();

test_model(model_filename);

return 0;
}
Expand Down
Loading