-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPrioritizedExperienceReplay.cpp
62 lines (49 loc) · 1.67 KB
/
PrioritizedExperienceReplay.cpp
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
//
// Created by Navneet Madhu Kumar on 2019-07-18.
//
//
// Created by Navneet Madhu Kumar on 2019-07-08.
//
#include "PrioritizedExperienceReplay.h"
#include <memory>
#include <vector>
#include <iostream>
#include <torch/torch.h>
#include <c10/util/ArrayRef.h>
#include <algorithm>
#include <iterator>
#include <random>
PrioritizedExperienceReplay::PrioritizedExperienceReplay(int64_t size) {
capacity = size;
}
void PrioritizedExperienceReplay::push(torch::Tensor state,torch::Tensor new_state,torch::
Tensor action,torch::Tensor done,torch::Tensor reward, float_t td_error, int64_t ind){
float_t error(td_error);
int64_t index(ind);
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor> sample (state, new_state, action, reward, done);
element sample_struct(error, index, sample);
if (buffer.size() < capacity){
buffer.push(sample_struct);
}
else {
while (buffer.size() >= capacity) {
buffer.pop();
}
buffer.push(sample_struct);
}
}
std::vector<std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>>
PrioritizedExperienceReplay::sample_queue(
int64_t batch_size){
std::vector<std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>> b(batch_size);
while (batch_size > 0 and buffer.size() > 0){
element s = buffer.top();
buffer.pop();
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor> sample = s.transition;
b.push_back(sample);
}
return b;
}
int64_t PrioritizedExperienceReplay::size_buffer(){
return buffer.size();
}