Program Listing for File env_base.h

Return to documentation for file (src/rlenvs/envs/env_base.h)

#ifndef ENV_BASE_H
#define ENV_BASE_H

#include "rlenvs/rlenvs_types_v2.h"
#include "rlenvs/rlenvs_consts.h"
#include "rlenvs/envs/synchronized_env_mixin.h"

#include <unordered_map>
#include <any>
#include <string>
#include <type_traits>


namespace rlenvscpp{
namespace envs{


template<typename TimeStepType, typename SpaceType>
class EnvBase: public SpaceType, public synchronized_env_mixin
{
public:

    static_assert(std::is_default_constructible<TimeStepType>::value && "TimeStepType should be default constructible");
    static_assert(std::is_default_constructible<SpaceType>::value && "SpaceType should be default constructible");

    static const uint_t DEFAULT_ENV_SEED = 42;

    typedef TimeStepType time_step_type;

    typedef typename SpaceType::state_space state_space_type;

    typedef typename SpaceType::state_type state_type;

    typedef typename SpaceType::action_space action_space_type;

    typedef typename SpaceType::action_type action_type;

    virtual ~EnvBase()=default;

    virtual void make(const std::string& version,
                      const std::unordered_map<std::string, std::any>& options) = 0;

    virtual void close()=0;

    virtual time_step_type reset(uint_t seed,
                                 const std::unordered_map<std::string, std::any>& options)=0;

    time_step_type reset(){
        return reset(DEFAULT_ENV_SEED, std::unordered_map<std::string, std::any>());}

    time_step_type reset(uint_t seed){
        return reset(seed, std::unordered_map<std::string, std::any>());}

    virtual time_step_type step(const action_type& action)=0;

    bool is_created()const noexcept{return is_created_;}

    std::string version()const noexcept{return version_;}

    std::string env_name()const noexcept{return name_;}

    const std::unordered_map<std::string, std::any>& make_options()const noexcept{return make_options_;}

    template<typename T>
    T read_option(const std::string& op_name)const;

    uint_t cidx()const noexcept{return cidx_;}

protected:

    explicit EnvBase(const uint_t cidx=0,
                     const std::string& name=rlenvscpp::consts::INVALID_STR);

    EnvBase(const EnvBase&);

    void set_version_(const std::string& version )noexcept{version_ = version;}

    void set_make_options_(const std::unordered_map<std::string, std::any>& options) noexcept{make_options_ = options;}

    void invalidate_is_created_flag_()noexcept{is_created_ = false;}

    void make_created_()noexcept{is_created_= true;}

    time_step_type& get_current_time_step_()noexcept{return current_state_;}
    const time_step_type& get_current_time_step_()const noexcept{return current_state_;}

private:

    bool is_created_;

    uint_t cidx_;

    std::string version_;

    const std::string name_;

    std::unordered_map<std::string, std::any> make_options_;

    time_step_type current_state_;
};


template<typename TimeStepType, typename SpaceType>
EnvBase<TimeStepType, SpaceType>::EnvBase(const uint_t cidx, const std::string& name)
:
SpaceType(),
synchronized_env_mixin(),
is_created_(false),
cidx_(cidx),
version_(),
name_(name),
current_state_()
{}

template<typename TimeStepType, typename SpaceType>
EnvBase<TimeStepType, SpaceType>::EnvBase(const EnvBase<TimeStepType, SpaceType>& other)
:
SpaceType(),
synchronized_env_mixin(),
is_created_(other.is_created_),
cidx_(other.cidx_),
version_(other.version_),
name_(other.name_),
current_state_()
{}

template<typename TimeStepType, typename SpaceType>
void
EnvBase<TimeStepType, SpaceType>::close(){
    this -> is_created_ = false;
}

template<typename TimeStepType, typename SpaceType>
template<typename T>
T
EnvBase<TimeStepType, SpaceType>::read_option(const std::string& op_name)const{

    auto op_itr = make_options_.find(op_name);
    if(op_itr != make_options_.end()){
        return std::any_cast<T>(op_itr -> second);
    }

    throw std::logic_error("Option: " + op_name + " not found");
}

}
}

#endif // ENV_BASE_H