-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.cpp
More file actions
executable file
·81 lines (74 loc) · 2.03 KB
/
memory.cpp
File metadata and controls
executable file
·81 lines (74 loc) · 2.03 KB
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
#include <iostream>
#include <random>
#include "config.hpp"
#include "utils.hpp"
#include "memory.hpp"
using namespace std;
Memory::Memory()
{
counter = 0;
size = Config::memoryCapacity;
nSamples = Config::nSamples;
nNeurons = Config::nNeurons;
nActions = Config::nActions;
nStates = Config::nStates;
full = false;
states = Utils::create2DArray(size, nStates);
nextStates = Utils::create2DArray(size, nStates);
rewards = Utils::create2DArray(size, 1);
actions = Utils::create2DArrayOfIntegers(size, 1);
}
void Memory::push(float** state, float** nextState, float** reward, int** action)
{
if (counter == size)
{
full = true;
counter = 0;
}
for (int i = 0 ; i < nStates ; i++)
{
states[counter][i] = state[0][i];
nextStates[counter][i] = nextState[0][i];
}
rewards[counter][0] = reward[0][0];
actions[counter][0] = action[0][0];
counter++;
}
void Memory::generateRandomIndices(int** result)
{
mt19937 engine(device());
if(full == false)
{
uniform_int_distribution<int> r(0,counter-1);
for (int i = 0 ; i < nSamples ; i++)
result[i][0] = r(engine);
}
else
{
uniform_int_distribution<int> r(0,size-1);
for (int i = 0 ; i < nSamples ; i++)
result[i][0] = r(engine);
}
}
void Memory::sampleStates(float** result, int** indices)
{
for (int i = 0 ; i < nSamples ; i++)
for (int j = 0 ; j < nStates ; j++)
result[i][j] = states[indices[i][0]][j];
}
void Memory::sampleNextStates(float** result, int** indices)
{
for (int i = 0 ; i < nSamples ; i++)
for (int j = 0 ; j < nStates ; j++)
result[i][j] = nextStates[indices[i][0]][j];
}
void Memory::sampleRewards(float** result, int** indices)
{
for (int i = 0 ; i < nSamples ; i++)
result[i][0] = rewards[indices[i][0]][0];
}
void Memory::sampleActions(int** result, int** indices)
{
for (int i = 0 ; i < nSamples ; i++)
result[i][0] = actions[indices[i][0]][0];
}