-
Notifications
You must be signed in to change notification settings - Fork 197
/
container.h
339 lines (272 loc) · 7.64 KB
/
container.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2018 Piotr Gawlowicz
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Piotr Gawlowicz <[email protected]>
*
*/
#ifndef OPENGYM_CONTAINER_H
#define OPENGYM_CONTAINER_H
#include "ns3/object.h"
#include "ns3/type-name.h"
#include "messages.pb.h"
namespace ns3 {
class OpenGymDataContainer : public Object
{
public:
OpenGymDataContainer ();
virtual ~OpenGymDataContainer ();
static TypeId GetTypeId ();
virtual ns3opengym::DataContainer GetDataContainerPbMsg() = 0;
static Ptr<OpenGymDataContainer> CreateFromDataContainerPbMsg(ns3opengym::DataContainer &dataContainer);
virtual void Print(std::ostream& where) const = 0;
friend std::ostream& operator<< (std::ostream& os, const Ptr<OpenGymDataContainer> container)
{
container->Print(os);
return os;
}
protected:
// Inherited
virtual void DoInitialize (void);
virtual void DoDispose (void);
};
class OpenGymDiscreteContainer : public OpenGymDataContainer
{
public:
OpenGymDiscreteContainer ();
OpenGymDiscreteContainer (uint32_t n);
virtual ~OpenGymDiscreteContainer ();
static TypeId GetTypeId ();
virtual ns3opengym::DataContainer GetDataContainerPbMsg();
virtual void Print(std::ostream& where) const;
friend std::ostream& operator<< (std::ostream& os, const Ptr<OpenGymDiscreteContainer> container)
{
container->Print(os);
return os;
}
bool SetValue(uint32_t value);
uint32_t GetValue();
protected:
// Inherited
virtual void DoInitialize (void);
virtual void DoDispose (void);
uint32_t m_n;
uint32_t m_value;
};
template <typename T = float>
class OpenGymBoxContainer : public OpenGymDataContainer
{
public:
OpenGymBoxContainer ();
OpenGymBoxContainer (std::vector<uint32_t> shape);
virtual ~OpenGymBoxContainer ();
static TypeId GetTypeId ();
virtual ns3opengym::DataContainer GetDataContainerPbMsg();
virtual void Print(std::ostream& where) const;
friend std::ostream& operator<< (std::ostream& os, const Ptr<OpenGymBoxContainer> container)
{
container->Print(os);
return os;
}
bool AddValue(T value);
T GetValue(uint32_t idx);
bool SetData(std::vector<T> data);
std::vector<T> GetData();
std::vector<uint32_t> GetShape();
protected:
// Inherited
virtual void DoInitialize (void);
virtual void DoDispose (void);
private:
void SetDtype();
std::vector<uint32_t> m_shape;
ns3opengym::Dtype m_dtype;
std::vector<T> m_data;
};
template <typename T>
TypeId
OpenGymBoxContainer<T>::GetTypeId (void)
{
std::string name = TypeNameGet<T> ();
static TypeId tid = TypeId (("ns3::OpenGymBoxContainer<" + name + ">").c_str ())
.SetParent<Object> ()
.SetGroupName ("OpenGym")
.template AddConstructor<OpenGymBoxContainer<T> > ()
;
return tid;
}
template <typename T>
OpenGymBoxContainer<T>::OpenGymBoxContainer()
{
SetDtype();
}
template <typename T>
OpenGymBoxContainer<T>::OpenGymBoxContainer(std::vector<uint32_t> shape):
m_shape(shape)
{
SetDtype();
}
template <typename T>
OpenGymBoxContainer<T>::~OpenGymBoxContainer ()
{
}
template <typename T>
void
OpenGymBoxContainer<T>::SetDtype ()
{
std::string name = TypeNameGet<T> ();
if (name == "int8_t" || name == "int16_t" || name == "int32_t" || name == "int64_t")
m_dtype = ns3opengym::INT;
else if (name == "uint8_t" || name == "uint16_t" || name == "uint32_t" || name == "uint64_t")
m_dtype = ns3opengym::UINT;
else if (name == "float")
m_dtype = ns3opengym::FLOAT;
else if (name == "double")
m_dtype = ns3opengym::DOUBLE;
else
m_dtype = ns3opengym::FLOAT;
}
template <typename T>
void
OpenGymBoxContainer<T>::DoDispose (void)
{
}
template <typename T>
void
OpenGymBoxContainer<T>::DoInitialize (void)
{
}
template <typename T>
ns3opengym::DataContainer
OpenGymBoxContainer<T>::GetDataContainerPbMsg()
{
ns3opengym::DataContainer dataContainerPbMsg;
ns3opengym::BoxDataContainer boxContainerPbMsg;
std::vector<uint32_t> shape = GetShape();
*boxContainerPbMsg.mutable_shape() = {shape.begin(), shape.end()};
boxContainerPbMsg.set_dtype(m_dtype);
std::vector<T> data = GetData();
if (m_dtype == ns3opengym::INT) {
*boxContainerPbMsg.mutable_intdata() = {data.begin(), data.end()};
} else if (m_dtype == ns3opengym::UINT) {
*boxContainerPbMsg.mutable_uintdata() = {data.begin(), data.end()};
} else if (m_dtype == ns3opengym::FLOAT) {
*boxContainerPbMsg.mutable_floatdata() = {data.begin(), data.end()};
} else if (m_dtype == ns3opengym::DOUBLE) {
*boxContainerPbMsg.mutable_doubledata() = {data.begin(), data.end()};
} else {
*boxContainerPbMsg.mutable_floatdata() = {data.begin(), data.end()};
}
dataContainerPbMsg.set_type(ns3opengym::Box);
dataContainerPbMsg.mutable_data()->PackFrom(boxContainerPbMsg);
return dataContainerPbMsg;
}
template <typename T>
bool
OpenGymBoxContainer<T>::AddValue(T value)
{
m_data.push_back(value);
return true;
}
template <typename T>
T
OpenGymBoxContainer<T>::GetValue(uint32_t idx)
{
T data = 0;
if (idx < m_data.size())
{
data = m_data.at(idx);
}
return data;
}
template <typename T>
bool
OpenGymBoxContainer<T>::SetData(std::vector<T> data)
{
m_data = data;
return true;
}
template <typename T>
std::vector<uint32_t>
OpenGymBoxContainer<T>::GetShape()
{
return m_shape;
}
template <typename T>
std::vector<T>
OpenGymBoxContainer<T>::GetData()
{
return m_data;
}
template <typename T>
void
OpenGymBoxContainer<T>::Print(std::ostream& where) const
{
where << "[";
for (auto i = m_data.begin(); i != m_data.end(); ++i)
{
where << std::to_string(*i);
auto i2 = i;
i2++;
if (i2 != m_data.end())
where << ", ";
}
where << "]";
}
class OpenGymTupleContainer : public OpenGymDataContainer
{
public:
OpenGymTupleContainer ();
virtual ~OpenGymTupleContainer ();
static TypeId GetTypeId ();
virtual ns3opengym::DataContainer GetDataContainerPbMsg();
virtual void Print(std::ostream& where) const;
friend std::ostream& operator<< (std::ostream& os, const Ptr<OpenGymTupleContainer> container)
{
container->Print(os);
return os;
}
bool Add(Ptr<OpenGymDataContainer> space);
Ptr<OpenGymDataContainer> Get(uint32_t idx);
protected:
// Inherited
virtual void DoInitialize (void);
virtual void DoDispose (void);
std::vector< Ptr<OpenGymDataContainer> > m_tuple;
};
class OpenGymDictContainer : public OpenGymDataContainer
{
public:
OpenGymDictContainer ();
virtual ~OpenGymDictContainer ();
static TypeId GetTypeId ();
virtual ns3opengym::DataContainer GetDataContainerPbMsg();
virtual void Print(std::ostream& where) const;
friend std::ostream& operator<< ( std::ostream& os, const Ptr<OpenGymDictContainer> container)
{
container->Print(os);
return os;
}
bool Add(std::string key, Ptr<OpenGymDataContainer> value);
Ptr<OpenGymDataContainer> Get(std::string key);
protected:
// Inherited
virtual void DoInitialize (void);
virtual void DoDispose (void);
std::map< std::string, Ptr<OpenGymDataContainer> > m_dict;
};
} // end of namespace ns3
#endif /* OPENGYM_CONTAINER_H */