Skip to content

Commit db016f8

Browse files
committed
update
1 parent 781b3b6 commit db016f8

File tree

10 files changed

+504
-35
lines changed

10 files changed

+504
-35
lines changed

examples/multi-agent/agent1.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import argparse
5+
from ns3gym import ns3env
6+
7+
__author__ = "Piotr Gawlowicz"
8+
__copyright__ = "Copyright (c) 2020, Technische Universität Berlin"
9+
__version__ = "0.1.0"
10+
__email__ = "[email protected]"
11+
12+
13+
port = 5555
14+
env = ns3env.Ns3Env(port=port, startSim=False)
15+
env.reset()
16+
17+
ob_space = env.observation_space
18+
ac_space = env.action_space
19+
print("Observation space: ", ob_space, ob_space.dtype)
20+
print("Action space: ", ac_space, ac_space.dtype)
21+
22+
23+
stepIdx = 0
24+
currIt = 0
25+
iterationNum = 3
26+
27+
try:
28+
while True:
29+
obs = env.reset()
30+
print("Step: ", stepIdx)
31+
print("---obs: ", obs)
32+
33+
while True:
34+
stepIdx += 1
35+
action = env.action_space.sample()
36+
print("---action: ", action)
37+
38+
print("Step: ", stepIdx)
39+
obs, reward, done, info = env.step(action)
40+
print("---obs, reward, done, info: ", obs, reward, done, info)
41+
42+
input("press enter....")
43+
44+
if done:
45+
break
46+
47+
currIt += 1
48+
if currIt == iterationNum:
49+
break
50+
51+
52+
except KeyboardInterrupt:
53+
print("Ctrl-C -> Exit")
54+
finally:
55+
env.close()
56+
print("Done")

examples/multi-agent/agent2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import argparse
5+
from ns3gym import ns3env
6+
7+
__author__ = "Piotr Gawlowicz"
8+
__copyright__ = "Copyright (c) 2020, Technische Universität Berlin"
9+
__version__ = "0.1.0"
10+
__email__ = "[email protected]"
11+
12+
13+
port = 5556
14+
env = ns3env.Ns3Env(port=port, startSim=False)
15+
env.reset()
16+
17+
ob_space = env.observation_space
18+
ac_space = env.action_space
19+
print("Observation space: ", ob_space, ob_space.dtype)
20+
print("Action space: ", ac_space, ac_space.dtype)
21+
22+
23+
stepIdx = 0
24+
currIt = 0
25+
iterationNum = 3
26+
27+
try:
28+
while True:
29+
obs = env.reset()
30+
print("Step: ", stepIdx)
31+
print("---obs: ", obs)
32+
33+
while True:
34+
stepIdx += 1
35+
action = env.action_space.sample()
36+
print("---action: ", action)
37+
38+
print("Step: ", stepIdx)
39+
obs, reward, done, info = env.step(action)
40+
print("---obs, reward, done, info: ", obs, reward, done, info)
41+
42+
input("press enter....")
43+
44+
if done:
45+
break
46+
47+
currIt += 1
48+
if currIt == iterationNum:
49+
break
50+
51+
52+
except KeyboardInterrupt:
53+
print("Ctrl-C -> Exit")
54+
finally:
55+
env.close()
56+
print("Done")

examples/multi-agent/mygym.cc

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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

examples/multi-agent/mygym.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
22+
#ifndef MY_GYM_ENTITY_H
23+
#define MY_GYM_ENTITY_H
24+
25+
#include "ns3/opengym-module.h"
26+
27+
namespace ns3 {
28+
29+
class MyGymEnv : public OpenGymEnv
30+
{
31+
public:
32+
MyGymEnv ();
33+
MyGymEnv (uint32_t id, Time stepTime);
34+
virtual ~MyGymEnv ();
35+
static TypeId GetTypeId (void);
36+
virtual void DoDispose ();
37+
38+
Ptr<OpenGymSpace> GetActionSpace();
39+
Ptr<OpenGymSpace> GetObservationSpace();
40+
bool GetGameOver();
41+
Ptr<OpenGymDataContainer> GetObservation();
42+
float GetReward();
43+
std::string GetExtraInfo();
44+
bool ExecuteActions(Ptr<OpenGymDataContainer> action);
45+
46+
private:
47+
void ScheduleNextStateRead();
48+
49+
Time m_interval;
50+
uint32_t m_agentId;
51+
};
52+
53+
}
54+
55+
56+
#endif // MY_GYM_ENTITY_H

0 commit comments

Comments
 (0)