1
+ /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
+ /*
3
+ * Copyright (c) 2018 Technische Universität Berlin
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License version 2 as
7
+ * published by the Free Software Foundation;
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ *
18
+ * Author: Piotr Gawlowicz <[email protected] >
19
+ */
20
+
21
+ #include " mygym.h"
22
+ #include " ns3/object.h"
23
+ #include " ns3/core-module.h"
24
+ #include " ns3/wifi-module.h"
25
+ #include " ns3/node-list.h"
26
+ #include " ns3/log.h"
27
+ #include < sstream>
28
+ #include < iostream>
29
+
30
+ namespace ns3 {
31
+
32
+ NS_LOG_COMPONENT_DEFINE (" MyGymEnv" );
33
+
34
+ NS_OBJECT_ENSURE_REGISTERED (MyGymEnv);
35
+
36
+ MyGymEnv::MyGymEnv ()
37
+ {
38
+ NS_LOG_FUNCTION (this );
39
+ m_interval = Seconds (0.1 );
40
+
41
+ Simulator::Schedule (Seconds (0.0 ), &MyGymEnv::ScheduleNextStateRead, this );
42
+ }
43
+
44
+ MyGymEnv::MyGymEnv (uint32_t id, Time stepTime)
45
+ {
46
+ NS_LOG_FUNCTION (this );
47
+ m_agentId = id;
48
+ m_interval = stepTime;
49
+
50
+ Simulator::Schedule (Seconds (0.0 ), &MyGymEnv::ScheduleNextStateRead, this );
51
+ }
52
+
53
+ void
54
+ MyGymEnv::ScheduleNextStateRead ()
55
+ {
56
+ NS_LOG_FUNCTION (this );
57
+ Simulator::Schedule (m_interval, &MyGymEnv::ScheduleNextStateRead, this );
58
+ Notify ();
59
+ }
60
+
61
+ MyGymEnv::~MyGymEnv ()
62
+ {
63
+ NS_LOG_FUNCTION (this );
64
+ }
65
+
66
+ TypeId
67
+ MyGymEnv::GetTypeId (void )
68
+ {
69
+ static TypeId tid = TypeId (" MyGymEnv" )
70
+ .SetParent <OpenGymEnv> ()
71
+ .SetGroupName (" OpenGym" )
72
+ .AddConstructor <MyGymEnv> ()
73
+ ;
74
+ return tid;
75
+ }
76
+
77
+ void
78
+ MyGymEnv::DoDispose ()
79
+ {
80
+ NS_LOG_FUNCTION (this );
81
+ }
82
+
83
+ /*
84
+ Define observation space
85
+ */
86
+ Ptr<OpenGymSpace>
87
+ MyGymEnv::GetObservationSpace ()
88
+ {
89
+ uint32_t nodeNum = 5 ;
90
+ float low = 0.0 ;
91
+ float high = 10.0 ;
92
+ std::vector<uint32_t > shape = {nodeNum,};
93
+ std::string dtype = TypeNameGet<uint32_t > ();
94
+
95
+ Ptr<OpenGymDiscreteSpace> discrete = CreateObject<OpenGymDiscreteSpace> (nodeNum);
96
+ Ptr<OpenGymBoxSpace> box = CreateObject<OpenGymBoxSpace> (low, high, shape, dtype);
97
+
98
+ Ptr<OpenGymDictSpace> space = CreateObject<OpenGymDictSpace> ();
99
+ space->Add (" box" , box);
100
+ space->Add (" discrete" , discrete);
101
+
102
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyGetObservationSpace: " << space);
103
+ return space;
104
+ }
105
+
106
+ /*
107
+ Define action space
108
+ */
109
+ Ptr<OpenGymSpace>
110
+ MyGymEnv::GetActionSpace ()
111
+ {
112
+ uint32_t nodeNum = 5 ;
113
+ float low = 0.0 ;
114
+ float high = 10.0 ;
115
+ std::vector<uint32_t > shape = {nodeNum,};
116
+ std::string dtype = TypeNameGet<uint32_t > ();
117
+
118
+ Ptr<OpenGymDiscreteSpace> discrete = CreateObject<OpenGymDiscreteSpace> (nodeNum);
119
+ Ptr<OpenGymBoxSpace> box = CreateObject<OpenGymBoxSpace> (low, high, shape, dtype);
120
+
121
+ Ptr<OpenGymDictSpace> space = CreateObject<OpenGymDictSpace> ();
122
+ space->Add (" box" , box);
123
+ space->Add (" discrete" , discrete);
124
+
125
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyGetActionSpace: " << space);
126
+ return space;
127
+ }
128
+
129
+ /*
130
+ Define game over condition
131
+ */
132
+ bool
133
+ MyGymEnv::GetGameOver ()
134
+ {
135
+ bool isGameOver = false ;
136
+ bool test = false ;
137
+ static float stepCounter = 0.0 ;
138
+ stepCounter += 1 ;
139
+ if (stepCounter == 10 && test) {
140
+ isGameOver = true ;
141
+ }
142
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyGetGameOver: " << isGameOver);
143
+ return isGameOver;
144
+ }
145
+
146
+ /*
147
+ Collect observations
148
+ */
149
+ Ptr<OpenGymDataContainer>
150
+ MyGymEnv::GetObservation ()
151
+ {
152
+ uint32_t nodeNum = 5 ;
153
+ uint32_t low = 0.0 ;
154
+ uint32_t high = 10.0 ;
155
+ Ptr<UniformRandomVariable> rngInt = CreateObject<UniformRandomVariable> ();
156
+
157
+ std::vector<uint32_t > shape = {nodeNum,};
158
+ Ptr<OpenGymBoxContainer<uint32_t > > box = CreateObject<OpenGymBoxContainer<uint32_t > >(shape);
159
+
160
+ // generate random data
161
+ for (uint32_t i = 0 ; i<nodeNum; i++){
162
+ uint32_t value = rngInt->GetInteger (low, high);
163
+ box->AddValue (value);
164
+ }
165
+
166
+ Ptr<OpenGymDiscreteContainer> discrete = CreateObject<OpenGymDiscreteContainer>(nodeNum);
167
+ uint32_t value = rngInt->GetInteger (low, high);
168
+ discrete->SetValue (value);
169
+
170
+ Ptr<OpenGymTupleContainer> data = CreateObject<OpenGymTupleContainer> ();
171
+ data->Add (box);
172
+ data->Add (discrete);
173
+
174
+ // Print data from tuple
175
+ Ptr<OpenGymBoxContainer<uint32_t > > mbox = DynamicCast<OpenGymBoxContainer<uint32_t > >(data->Get (0 ));
176
+ Ptr<OpenGymDiscreteContainer> mdiscrete = DynamicCast<OpenGymDiscreteContainer>(data->Get (1 ));
177
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyGetObservation: " << data);
178
+ NS_LOG_UNCOND (" ---" << mbox);
179
+ NS_LOG_UNCOND (" ---" << mdiscrete);
180
+
181
+ return data;
182
+ }
183
+
184
+ /*
185
+ Define reward function
186
+ */
187
+ float
188
+ MyGymEnv::GetReward ()
189
+ {
190
+ static float reward = 0.0 ;
191
+ reward += 1 ;
192
+ return reward;
193
+ }
194
+
195
+ /*
196
+ Define extra info. Optional
197
+ */
198
+ std::string
199
+ MyGymEnv::GetExtraInfo ()
200
+ {
201
+ std::string myInfo = " testInfo" ;
202
+ myInfo += " |123" ;
203
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyGetExtraInfo: " << myInfo);
204
+ return myInfo;
205
+ }
206
+
207
+ /*
208
+ Execute received actions
209
+ */
210
+ bool
211
+ MyGymEnv::ExecuteActions (Ptr<OpenGymDataContainer> action)
212
+ {
213
+ Ptr<OpenGymDictContainer> dict = DynamicCast<OpenGymDictContainer>(action);
214
+ Ptr<OpenGymBoxContainer<uint32_t > > box = DynamicCast<OpenGymBoxContainer<uint32_t > >(dict->Get (" box" ));
215
+ Ptr<OpenGymDiscreteContainer> discrete = DynamicCast<OpenGymDiscreteContainer>(dict->Get (" discrete" ));
216
+
217
+ NS_LOG_UNCOND (" AgendId: " << m_agentId << " MyExecuteActions: " << action);
218
+ NS_LOG_UNCOND (" ---" << box);
219
+ NS_LOG_UNCOND (" ---" << discrete);
220
+ return true ;
221
+ }
222
+
223
+ } // ns3 namespace
0 commit comments