bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
connect2_env.h
Go to the documentation of this file.
1/*
2 * Connect2 environment from
3 * <a href="https://github.com/JoshVarty/AlphaZeroSimple">AlphaZeroSimple</a>
4 *
5 *
6 *
7 *
8 */
9#ifndef CONNECT2_ENV_H
10#define CONNECT2_ENV_H
11
13#include "bitrl/envs/env_base.h"
16
17#include <atomic>
18#include <boost/noncopyable.hpp>
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24namespace bitrl
25{
26namespace envs
27{
28namespace connect2
29{
30
37class Connect2 final : public EnvBase<TimeStep<std::vector<uint_t>>,
38 DiscreteVectorStateDiscreteActionEnv<53, 0, 4, uint_t>>
39{
40
41 public:
45 static const std::string name;
46
53
58
63
68
73
78
82 using base_type::reset;
83
88
93 virtual void
94 make(const std::string &version, const std::unordered_map<std::string, std::any> &options,
95 const std::unordered_map<std::string, std::any> &reset_options) override final;
96
103 virtual time_step_type step(const action_type &action) override final;
104
108 virtual void close() override final;
109
113 virtual time_step_type reset() override final;
114
118 uint_t n_states() const noexcept { return state_space_type::size; }
119
123 uint_t n_actions() const noexcept { return action_space_type::size; }
124
128 time_step_type move(const uint_t pid, const action_type &action);
129
133 bool is_win(uint_t player) const noexcept;
134
138 bool has_legal_moves() const noexcept;
139
143 std::vector<uint_t> get_valid_moves() const;
144
149 static uint_t n_copies() { return n_copies_.load(); }
150
151 private:
155 real_t discount_;
156
160 const uint_t player_id_1_{1};
161
165 const uint_t player_id_2_{2};
166
170 const uint_t win_val_{2};
171
176 static std::atomic<uint_t> n_copies_;
177
181 std::vector<uint_t> board_;
182
186 bool is_finished_{false};
187};
188
189inline void Connect2::close()
190{
191 board_ = std::vector<uint_t>();
193}
194
195} // namespace connect2
196} // namespace envs
197} // namespace bitrl
198
199#endif
DiscreteVectorStateDiscreteActionEnv< 53, 0, 4, uint_t >::state_space state_space_type
Definition env_base.h:45
DiscreteVectorStateDiscreteActionEnv< 53, 0, 4, uint_t >::state_type state_type
Definition env_base.h:48
const std::unordered_map< std::string, std::any > & reset_options() const noexcept
Definition env_base.h:105
EnvBase(const std::string &idx=bitsim::consts::INVALID_STR, const std::string &name=bitsim::consts::INVALID_STR)
DiscreteVectorStateDiscreteActionEnv< 53, 0, 4, uint_t >::action_space action_space_type
Definition env_base.h:51
DiscreteVectorStateDiscreteActionEnv< 53, 0, 4, uint_t >::action_type action_type
Definition env_base.h:54
EnvBase< TimeStep< std::vector< uint_t > >, DiscreteVectorStateDiscreteActionEnv< 53, 0, 4, uint_t > > base_type
The base type.
Definition connect2_env.h:52
uint_t n_actions() const noexcept
n_actions. Returns the number of actions
Definition connect2_env.h:123
bool has_legal_moves() const noexcept
Returns true if there are still legal moves to do.
base_type::state_space_type state_space_type
The type describing the state space for the environment.
Definition connect2_env.h:62
static const std::string name
name
Definition connect2_env.h:45
base_type::action_type action_type
The type of the action to be undertaken in the environment.
Definition connect2_env.h:72
base_type::time_step_type time_step_type
The type of the time step.
Definition connect2_env.h:57
time_step_type move(const uint_t pid, const action_type &action)
Make a move for the player with the given id.
virtual time_step_type step(const action_type &action) override final
step. Move in the environment with the given action This function always moves player_1
std::vector< uint_t > get_valid_moves() const
Get the valid moves.
base_type::state_type state_type
The state type.
Definition connect2_env.h:77
base_type::action_space_type action_space_type
The type of the action space for the environment.
Definition connect2_env.h:67
static uint_t n_copies()
Definition connect2_env.h:149
uint_t n_states() const noexcept
n_states. Returns the number of states
Definition connect2_env.h:118
virtual void make(const std::string &version, const std::unordered_map< std::string, std::any > &options, const std::unordered_map< std::string, std::any > &reset_options) override final
make. Builds the environment. Optionally we can choose if the environment will be slippery
bool is_win(uint_t player) const noexcept
Returns true if the player has two stones in cells next to each other.
virtual time_step_type reset() override final
Reset the environment.
virtual void close() override final
close
Definition connect2_env.h:189
Definition connect2_env.h:29
Definition cart_pole.h:18
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
static constexpr uint_t size
size. The number of members in the space
Definition space_type.h:165
static constexpr uint_t size
Definition space_type.h:38