-
Notifications
You must be signed in to change notification settings - Fork 3
/
dqn.h
150 lines (139 loc) · 6.11 KB
/
dqn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include "problems/snake/problem.h"
#include "dqn_policy.h"
#include "rlop/rl/dqn/dqn.h"
#include "rlop/common/circular_stack.h"
namespace snake {
class DQN : public rlop::DQN {
public:
DQN(
Int num_envs,
bool render,
Int replay_buffer_capacity,
Int learning_starts,
Int batch_size,
double lr,
double tau,
double gamma,
double max_grad_norm,
double exploration_fraction,
double initial_eps,
double final_eps,
Int train_freq,
Int gradient_steps,
Int target_update_interval,
std::string output_path,
const torch::Device& device
) :
rlop::DQN(
learning_starts,
batch_size,
lr,
tau,
gamma,
initial_eps,
max_grad_norm,
train_freq,
gradient_steps,
target_update_interval,
output_path,
device
),
problem_(num_envs, render),
replay_buffer_capacity_(replay_buffer_capacity),
score_stack_(problem_.max_num_steps())
{
linear_fn_ = rlop::MakeLinearFn(initial_eps, final_eps, exploration_fraction);
}
void Reset() override {
rlop::DQN::Reset();
for (Int env_i=0; env_i<problem_.num_problems(); ++env_i) {
problem_.Reset(env_i, env_i);
}
eps_ = linear_fn_(time_steps_ / (double)max_time_steps_);
}
void RegisterLogItems() override {
rlop::DQN::RegisterLogItems();
log_items_["score"] = torch::Tensor();
score_stack_.Reset();
}
std::shared_ptr<rlop::ReplayBuffer> MakeReplayBuffer() const override {
return std::make_shared<rlop::ReplayBuffer>(
replay_buffer_capacity_,
problem_.num_problems(),
problem_.observation_sizes(),
problem_.action_sizes(),
torch::kFloat32,
torch::kInt64
);
}
std::shared_ptr<rlop::RLPolicy> MakePolicy() const override {
return std::make_shared<DQNPolicy>(replay_buffer_->observation_sizes(), problem_.NumActions());
}
torch::Tensor SampleActions() override {
return torch::randint(0, problem_.NumActions(), { problem_.num_problems() }, torch::TensorOptions().device(device_).dtype(torch::kInt64));
}
Int NumEnvs() const override {
return problem_.num_problems();
}
torch::Tensor ResetEnv() override {
std::vector<torch::Tensor> observation_list(problem_.num_problems());
#pragma omp parallel for
for (Int i=0; i<problem_.num_problems(); ++i) {
problem_.Reset(i);
observation_list[i] = problem_.GetObservation(i);
}
problem_.Render();
return torch::stack(observation_list);
}
std::array<torch::Tensor, 5> Step(const torch::Tensor& actions) override {
std::vector<torch::Tensor> observation_list(problem_.num_problems());
std::vector<torch::Tensor> reward_list(problem_.num_problems());
std::vector<torch::Tensor> termination_list(problem_.num_problems());
std::vector<torch::Tensor> score_list(problem_.num_problems());
std::vector<torch::Tensor> terminal_obseravtion_list(problem_.num_problems());
#pragma omp parallel for
for (Int i=0; i<problem_.num_problems(); ++i) {
Int num_foods = problem_.engines()[i].snakes()[0].num_foods;
if (!problem_.Step(i, { problem_.GetAction(actions[i].item<Int>()) })) {
double reward;
if (problem_.engines()[i].snakes()[0].alive)
reward = problem_.engines()[i].snakes()[0].num_foods - num_foods + problem_.engines()[i].min_num_foods() + 0.001 * problem_.engines()[i].num_steps();
else
reward = 0;
reward_list[i] = torch::tensor(reward, torch::kFloat32);
termination_list[i] = torch::tensor(1, torch::kFloat32);
terminal_obseravtion_list[i] = problem_.GetObservation(i);
problem_.Reset(i);
}
else {
double reward = problem_.engines()[i].snakes()[0].num_foods - num_foods + 0.001;
reward_list[i] = torch::tensor(reward, torch::kFloat32);
termination_list[i] = torch::tensor(0, torch::kFloat32);
terminal_obseravtion_list[i] = torch::zeros_like(problem_.GetObservation(i));
}
observation_list[i] = problem_.GetObservation(i);
score_list[i] = torch::tensor(problem_.engines()[i].snakes()[0].num_foods, torch::kFloat32);
}
torch::Tensor next_observations = torch::stack(observation_list);
torch::Tensor rewards = torch::stack(reward_list);
torch::Tensor terminations = torch::stack(termination_list);
torch::Tensor truncations = torch::zeros_like(terminations);
torch::Tensor terminal_observations = torch::stack(terminal_obseravtion_list);
score_stack_.Push(torch::stack(score_list));
if (score_stack_.full())
log_items_["score"] = torch::stack(score_stack_.vec()).mean();
problem_.Render();
return { next_observations, rewards, terminations, truncations, terminal_observations };
}
void OnCollectRolloutStep() override {
rlop::DQN::OnCollectRolloutStep();
eps_ = linear_fn_(time_steps_ / (double)max_time_steps_);
}
protected:
VectorProblem problem_;
Int replay_buffer_capacity_;
rlop::CircularStack<torch::Tensor> score_stack_;
std::function<double(double)> linear_fn_;
};
}