-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddi.cc
116 lines (94 loc) · 3.13 KB
/
ddi.cc
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
/* Implementation dependencies */
#include <iostream>
#include <algorithm>
#include <cmath>
#include "ddi.hh"
const double DDI::dt = 0.05; // 50 ms update
const double DDI::noise = 0.01; // noise
/*!
*
*/
DDI::DDI(double gamma) : stateDimension(2), actionDimension(1)
{
/* Initialize our initial state */
initialState.resize(stateDimension);
for(int i=0; i<actionDimension; i++) {
initialState[i] = 0.0;
initialState[i+actionDimension] = 1.0;
}
/* Create max and min action range */
maxStateRange.resize(stateDimension);
minStateRange.resize(stateDimension);
std::fill(maxStateRange.begin(), maxStateRange.end(), 2.0);
std::fill(minStateRange.begin(), minStateRange.end(), -2.0);
maxActionRange.resize(actionDimension);
minActionRange.resize(actionDimension);
std::fill(maxActionRange.begin(), maxActionRange.end(), 2.0);
std::fill(minActionRange.begin(), minActionRange.end(), -2.0);
/* Initialize gamma */
this->gamma = gamma;
}
void DDI::setParam(double d, int i)
{
actionDimension = i;
stateDimension = 2*i;
/* Initialize our initial state */
initialState.resize(stateDimension);
for(int i=0; i<actionDimension; i++) {
initialState[i] = 0.0;
initialState[i+actionDimension] = 1.0;
}
/* Create max and min action range */
maxStateRange.resize(stateDimension);
minStateRange.resize(stateDimension);
std::fill(maxStateRange.begin(), maxStateRange.end(), 2.0);
std::fill(minStateRange.begin(), minStateRange.end(), -2.0);
maxActionRange.resize(actionDimension);
minActionRange.resize(actionDimension);
std::fill(maxActionRange.begin(), maxActionRange.end(), 2.0);
std::fill(minActionRange.begin(), minActionRange.end(), -2.0);
}
/*!
*
*/
SARS *DDI::step(State s, Action a)
{
SARS *sars = new SARS(stateDimension, actionDimension);
sars->s = s;
sars->a = a;
/*
* Compute update step. This is simple linear dynamics with noise
* added. A = [1 0;dt 1] and B = [dt 0]
*/
unsigned int numActions = a.size();
for(unsigned int i=0; i<numActions; i++) {
sars->s_prime[i] = s[i] + a[i] * dt;// + gsl_ran_flat(rng, -noise, noise);
sars->s_prime[i+numActions] = s[i+numActions] + s[i] * dt;// + gsl_ran_flat(rng, -noise, noise);
}
/*
* Reward function is a simple quadratic reward with Q = [0 0; 0 1]
* and R = [1]. For some reason, we convert this to reward instead
* of cost as the LQR forumulation states.
*/
sars->reward = 0.0;
for(unsigned int i=0; i<numActions; i++) {
sars->reward += pow(sars->s[i+numActions],2) + pow(a[i],2);
}
sars->reward = -sars->reward;
/*
* Terminate if we exceed operating parameters. This may or may not
* change optimal policies.
*/
sars->terminal = false;
for(unsigned int i = 0; i<sars->s_prime.size(); i++) {
if(sars->s_prime[i] > maxStateRange[i] || sars->s_prime[i] < minStateRange[i]) {
sars->terminal = true;
}
}
/*
* Rescale reward the reward function to zero to one. This allows us
* to standardize our algorithms to match most theoretical work
*/
sars->reward = (1.0 - 0.0) / (0.0 - (-8*actionDimension)) * (sars->reward - (-8*actionDimension));
return sars;
}