Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ endif()


protobuf_generate(
TARGET ${libopengym-obj}
TARGET opengym
IMPORT_DIRS model/
LANGUAGE cpp
PROTOC_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/model
)

protobuf_generate(
TARGET ${libopengym-obj}
TARGET opengym
IMPORT_DIRS model/
LANGUAGE python
PROTOC_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/model/ns3gym/ns3gym
Expand Down
2 changes: 1 addition & 1 deletion NS3-VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release ns-3.36
release ns-3.45
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ apt-get install pkg-config
3. Download and install ns3

```
wget https://www.nsnam.org/releases/ns-allinone-3.40.tar.bz2
tar xf ns-allinone-3.40.tar.bz2
cd ns-allinone-3.40
wget https://www.nsnam.org/releases/ns-allinone-3.45.tar.bz2
tar xf ns-allinone-3.45.tar.bz2
cd ns-allinone-3.45
```

4. Clone ns3-gym repository into `contrib` directory and change the branch:
```
cd ./ns-3.40/contrib
cd ./ns-3.45/contrib
git clone https://github.com/tkn-tub/ns3-gym.git ./opengym
cd opengym/
git checkout app-ns-3.36+
git checkout app-ns-3.45+
```
Check [working with cmake](https://www.nsnam.org/docs/manual/html/working-with-cmake.html)

It is important to use the `opengym` as the name of the ns3-gym app directory.
It is important to use the `opengym` as the name of the ns3-gym app directory.

5. Configure and build ns-3 project:
```
Expand Down Expand Up @@ -69,7 +69,7 @@ pip3 install ./model/ns3gym

8. Run example:
```
cd ./contrib/opengym/examples/opengym/
cd ./contrib/opengym/examples/opengym/
./simple_test.py
```

Expand All @@ -79,7 +79,7 @@ cd ./contrib/opengym/examples/opengym/
./ns3 run "opengym"

# Terminal 2
cd ./contrib/opengym/examples/opengym/
cd ./contrib/opengym/examples/opengym/
./test.py --start=0
```

Expand Down
4 changes: 2 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ build_lib_example(
)

build_lib_example(
NAME opengym
SOURCE_FILES opengym/sim.cc
NAME opengym-1
SOURCE_FILES opengym-1/sim.cc
LIBRARIES_TO_LINK
${libcore}
${libopengym}
Expand Down
4 changes: 3 additions & 1 deletion examples/linear-mesh-2/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ main (int argc, char *argv[])
Ipv4Address dest_ip_addr = dest_ipv4_int_addr.GetLocal ();

InetSocketAddress destAddress (dest_ip_addr, port);
destAddress.SetTos (0x70); //AC_BE
Ptr<Socket> socket = Socket::CreateSocket (srcNode, UdpSocketFactory::GetTypeId());
socket->SetIpTos (0x70); //AC_BE
UdpClientHelper source (destAddress);
source.SetAttribute ("MaxPackets", UintegerValue (pktPerSec * simulationTime));
source.SetAttribute ("PacketSize", UintegerValue (payloadSize));
Time interPacketInterval = Seconds (1.0/pktPerSec);
source.SetAttribute ("Interval", TimeValue (interPacketInterval)); //packets/s
source.SetAttribute ("Socket", PointerValue (socket));

ApplicationContainer sourceApps = source.Install (srcNode);
sourceApps.Start (Seconds (0.0));
Expand Down
4 changes: 3 additions & 1 deletion examples/linear-mesh/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,14 @@ main (int argc, char *argv[])
Ipv4Address dest_ip_addr = dest_ipv4_int_addr.GetLocal ();

InetSocketAddress destAddress (dest_ip_addr, port);
destAddress.SetTos (0x70); //AC_BE
Ptr<Socket> socket = Socket::CreateSocket (srcNode, UdpSocketFactory::GetTypeId());
socket->SetIpTos (0x70); //AC_BE
UdpClientHelper source (destAddress);
source.SetAttribute ("MaxPackets", UintegerValue (pktPerSec * simulationTime));
source.SetAttribute ("PacketSize", UintegerValue (payloadSize));
Time interPacketInterval = Seconds (1.0/pktPerSec);
source.SetAttribute ("Interval", TimeValue (interPacketInterval)); //packets/s
source.SetAttribute ("Socket", PointerValue (socket));

ApplicationContainer sourceApps = source.Install (srcNode);
sourceApps.Start (Seconds (0.0));
Expand Down
182 changes: 182 additions & 0 deletions examples/opengym-1/sim.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* -*- 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 <gawlowicz.p@gmail.com>
*
*/

#include "ns3/core-module.h"
#include "ns3/opengym-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("OpenGym");

/*
Define observation space
*/
Ptr<OpenGymSpace> MyGetObservationSpace(void)
{
uint32_t nodeNum = 5;
float low = 0.0;
float high = 10.0;
std::vector<uint32_t> shape = {nodeNum,};
std::string dtype = TypeNameGet<uint32_t> ();
Ptr<OpenGymBoxSpace> space = CreateObject<OpenGymBoxSpace> (low, high, shape, dtype);
NS_LOG_UNCOND ("MyGetObservationSpace: " << space);
return space;
}

/*
Define action space
*/
Ptr<OpenGymSpace> MyGetActionSpace(void)
{
uint32_t nodeNum = 5;

Ptr<OpenGymDiscreteSpace> space = CreateObject<OpenGymDiscreteSpace> (nodeNum);
NS_LOG_UNCOND ("MyGetActionSpace: " << space);
return space;
}

/*
Define game over condition
*/
bool MyGetGameOver(void)
{

bool isGameOver = false;
bool test = false;
static float stepCounter = 0.0;
stepCounter += 1;
if (stepCounter == 10 && test) {
isGameOver = true;
}
NS_LOG_UNCOND ("MyGetGameOver: " << isGameOver);
return isGameOver;
}

/*
Collect observations
*/
Ptr<OpenGymDataContainer> MyGetObservation(void)
{
uint32_t nodeNum = 5;
uint32_t low = 0.0;
uint32_t high = 10.0;
Ptr<UniformRandomVariable> rngInt = CreateObject<UniformRandomVariable> ();

std::vector<uint32_t> shape = {nodeNum,};
Ptr<OpenGymBoxContainer<uint32_t> > box = CreateObject<OpenGymBoxContainer<uint32_t> >(shape);

// generate random data
for (uint32_t i = 0; i<nodeNum; i++){
uint32_t value = rngInt->GetInteger(low, high);
box->AddValue(value);
}

NS_LOG_UNCOND ("MyGetObservation: " << box);
return box;
}

/*
Define reward function
*/
float MyGetReward(void)
{
static float reward = 0.0;
reward += 1;
return reward;
}

/*
Define extra info. Optional
*/
std::string MyGetExtraInfo(void)
{
std::string myInfo = "testInfo";
myInfo += "|123";
NS_LOG_UNCOND("MyGetExtraInfo: " << myInfo);
return myInfo;
}


/*
Execute received actions
*/
bool MyExecuteActions(Ptr<OpenGymDataContainer> action)
{
Ptr<OpenGymDiscreteContainer> discrete = DynamicCast<OpenGymDiscreteContainer>(action);
NS_LOG_UNCOND ("MyExecuteActions: " << action);
return true;
}

void ScheduleNextStateRead(double envStepTime, Ptr<OpenGymInterface> openGym)
{
Simulator::Schedule (Seconds(envStepTime), &ScheduleNextStateRead, envStepTime, openGym);
openGym->NotifyCurrentState();
}

int
main (int argc, char *argv[])
{
// Parameters of the scenario
uint32_t simSeed = 1;
double simulationTime = 1; //seconds
double envStepTime = 0.1; //seconds, ns3gym env step time interval
uint32_t openGymPort = 5555;
uint32_t testArg = 0;

CommandLine cmd;
// required parameters for OpenGym interface
cmd.AddValue ("openGymPort", "Port number for OpenGym env. Default: 5555", openGymPort);
cmd.AddValue ("simSeed", "Seed for random generator. Default: 1", simSeed);
// optional parameters
cmd.AddValue ("simTime", "Simulation time in seconds. Default: 10s", simulationTime);
cmd.AddValue ("testArg", "Extra simulation argument. Default: 0", testArg);
cmd.Parse (argc, argv);

NS_LOG_UNCOND("Ns3Env parameters:");
NS_LOG_UNCOND("--simulationTime: " << simulationTime);
NS_LOG_UNCOND("--openGymPort: " << openGymPort);
NS_LOG_UNCOND("--envStepTime: " << envStepTime);
NS_LOG_UNCOND("--seed: " << simSeed);
NS_LOG_UNCOND("--testArg: " << testArg);

RngSeedManager::SetSeed (1);
RngSeedManager::SetRun (simSeed);

// OpenGym Env
Ptr<OpenGymInterface> openGym = CreateObject<OpenGymInterface> (openGymPort);
openGym->SetGetActionSpaceCb( MakeCallback (&MyGetActionSpace) );
openGym->SetGetObservationSpaceCb( MakeCallback (&MyGetObservationSpace) );
openGym->SetGetGameOverCb( MakeCallback (&MyGetGameOver) );
openGym->SetGetObservationCb( MakeCallback (&MyGetObservation) );
openGym->SetGetRewardCb( MakeCallback (&MyGetReward) );
openGym->SetGetExtraInfoCb( MakeCallback (&MyGetExtraInfo) );
openGym->SetExecuteActionsCb( MakeCallback (&MyExecuteActions) );
Simulator::Schedule (Seconds(0.0), &ScheduleNextStateRead, envStepTime, openGym);

NS_LOG_UNCOND ("Simulation start");
Simulator::Stop (Seconds (simulationTime));
Simulator::Run ();
NS_LOG_UNCOND ("Simulation stop");

openGym->NotifySimulationEnd();
Simulator::Destroy ();

}
48 changes: 48 additions & 0 deletions examples/opengym-1/simple_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gym
import argparse
import ns3gym
from ns3gym import ns3env

__author__ = "Piotr Gawlowicz"
__copyright__ = "Copyright (c) 2018, Technische Universität Berlin"
__version__ = "0.1.0"
__email__ = "gawlowicz@tkn.tu-berlin.de"


#env = gym.make('ns3-v0')
env = ns3env.Ns3Env()
env.reset()

ob_space = env.observation_space
ac_space = env.action_space
print("Observation space: ", ob_space, ob_space.dtype)
print("Action space: ", ac_space, ac_space.dtype)

stepIdx = 0

try:
obs = env.reset()
print("Step: ", stepIdx)
print("---obs: ", obs)

while True:
stepIdx += 1

action = env.action_space.sample()
print("---action: ", action)
obs, reward, done, info = env.step(action)

print("Step: ", stepIdx)
print("---obs, reward, done, info: ", obs, reward, done, info)

if done:
break

except KeyboardInterrupt:
print("Ctrl-C -> Exit")
finally:
env.close()
print("Done")
Loading