-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmomdp_planner.cpp
More file actions
191 lines (168 loc) · 3.59 KB
/
momdp_planner.cpp
File metadata and controls
191 lines (168 loc) · 3.59 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "momdp_planner.h"
#include <numeric>
using std::vector;
using std::endl;
AlphaVector::AlphaVector(int a, vector<float> vec)
{
this->a = a;
this->vec = vec;
}
MOMDPPlanner::MOMDPPlanner(string paramfile, string logpath)
:MyPlanner(paramfile, logpath) {}
// Returns true if ok, false if bad
int MOMDPPlanner::initialize()
{
/* Create the logging file */
if (start_log())
return -1;
string path = read_config(_param_file);
if (path == "error")
return -1;
/* check that the filter is of the correct type */
if (this->filter->type != 0)
{
planner_log << "MOMDP expects a discrete filter." << endl;
return -1;
}
DF * f = static_cast<DF *>(this->filter);
_n = f->n;
int n2 = _n*_n;
policy.resize(n2);
_x = _n/2;
_y = _n/2;
string a_path = path + "policy/alpha_actions.bin";
string o_path = path + "policy/alpha_obs.bin";
string v_path = path + "policy/alpha_vectors.bin";
ifstream a_file, o_file, v_file;
a_file.open(a_path);
o_file.open(o_path);
v_file.open(v_path);
if (!a_file.is_open())
{
planner_log << "MOMDP claims action binary file DNE." << endl;
return -1;
}
if (!o_file.is_open())
{
planner_log << "MOMDP claims obs binary file DNE." << endl;
return -1;
}
if (!v_file.is_open())
{
planner_log << "MOMDP claims alpha vector file DNE." << endl;
return -1;
}
int i, a, o;
float v;
int s;
vector<float>* temp;
while (!a_file.eof())
{
a_file.read(reinterpret_cast<char*>(&a), sizeof(int));
o_file.read(reinterpret_cast<char*>(&o), sizeof(int));
//create a temporary vector
temp = new vector<float>(n2);
for (s = 0; s < n2; s++)
{
v_file.read(reinterpret_cast<char*>(&v), sizeof(float));
(*temp)[s] = v;
}
policy[o].push_back(AlphaVector(a, *temp)); //TODO new issues
}
/* close the files */
o_file.close();
a_file.close();
v_file.close();
return 0;
}
vector<float> MOMDPPlanner::get_action()
{
/* Convert 2-D belief into 1-D vector */
DF * f = static_cast<DF *>(this->filter);
vector<float> b1d (_n*_n);
int x,y,i;
i = 0;
for (x = 0; x < _n; x++)
{
for (y = 0; y < _n; y++)
{
b1d[i] = f->b[x][y];
i++;
}
}
/* Determine observed index from vehicle state */
int obs_ind = _x + _n*_y;
/* inner_product */
int max_a, num_vecs;
double V, max_V;
//loop
bool not_rotated = true;
bool finished = false;
int dx, dy;
int old_x = _x;
int old_y = _y;
while (not_rotated)
{
num_vecs = policy[obs_ind].size(); //alpha vecs for this obs_ind
max_V = -99999; // TODO: maybe use int_min or something
max_a = 0;
for (i = 0; i < num_vecs; i++)
{
V = inner_product(b1d.begin(), b1d.end(), policy[obs_ind][i].vec.begin(), 0);
if (V > max_V)
{
max_V = V;
max_a = policy[obs_ind][i].a;
}
}
//std::cout << "max_a = " << max_a << std::endl;
// If we rotate, that is cool
if (max_a > 7);
not_rotated = false;
if (max_a == 9)
finished = true;
// convert action to change in _x, _y
// TODO: need to check that we stay in bounds
if (max_a == 0)
_y += 1;
else if (max_a == 1)
{
_x++;
_y++;
}
else if (max_a == 2)
_x++;
else if (max_a == 3)
{ //se
_x++;
_y--;
}
else if (max_a == 4)
_y--;
else if (max_a == 5)
{
_x--;
_y--;
}
else if (max_a == 6)
_x--;
else if (max_a == 7)
{
_x--;
_y++;
}
}
/* Create the command and move the UAV */
double step_size = _search_size / _n;
vector<float> command(3,0.0);
if (finished)
{
planner_log << "Search Complete." << endl;
command[0] = -1000.0;
command[1] = -1000.0;
return command;
}
command[0] = step_size*(_y - old_y);
command[1] = step_size*(_x - old_x);
return command;
}