From d635c766a897e35b54b420a3b9f0012eb8e1ed40 Mon Sep 17 00:00:00 2001 From: ubuntu Date: Wed, 17 Sep 2025 03:02:08 +0200 Subject: [PATCH] Updated to build with ns-3.45 --- CMakeLists.txt | 4 +- NS3-VERSION | 2 +- README.md | 16 +-- examples/CMakeLists.txt | 4 +- examples/linear-mesh-2/sim.cc | 4 +- examples/linear-mesh/sim.cc | 4 +- examples/opengym-1/sim.cc | 182 ++++++++++++++++++++++++++++++ examples/opengym-1/simple_test.py | 48 ++++++++ examples/opengym-1/test.py | 77 +++++++++++++ examples/rl-tcp/tcp-rl.cc | 6 - examples/rl-tcp/tcp-rl.h | 3 +- test/opengym-test-suite.cc | 4 +- 12 files changed, 329 insertions(+), 25 deletions(-) create mode 100644 examples/opengym-1/sim.cc create mode 100755 examples/opengym-1/simple_test.py create mode 100755 examples/opengym-1/test.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 85de5ef4b9..eccbdef517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/NS3-VERSION b/NS3-VERSION index 17519c00b2..ed446e0f92 100644 --- a/NS3-VERSION +++ b/NS3-VERSION @@ -1 +1 @@ -release ns-3.36 +release ns-3.45 diff --git a/README.md b/README.md index 605163785e..543fc26e1f 100644 --- a/README.md +++ b/README.md @@ -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: ``` @@ -69,7 +69,7 @@ pip3 install ./model/ns3gym 8. Run example: ``` -cd ./contrib/opengym/examples/opengym/ +cd ./contrib/opengym/examples/opengym/ ./simple_test.py ``` @@ -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 ``` diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1fbd8fa817..1901d2c53f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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} diff --git a/examples/linear-mesh-2/sim.cc b/examples/linear-mesh-2/sim.cc index c82b6b35f9..e03efe5094 100644 --- a/examples/linear-mesh-2/sim.cc +++ b/examples/linear-mesh-2/sim.cc @@ -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::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)); diff --git a/examples/linear-mesh/sim.cc b/examples/linear-mesh/sim.cc index 55e99a1a92..4e251078f3 100644 --- a/examples/linear-mesh/sim.cc +++ b/examples/linear-mesh/sim.cc @@ -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::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)); diff --git a/examples/opengym-1/sim.cc b/examples/opengym-1/sim.cc new file mode 100644 index 0000000000..cbf82e6655 --- /dev/null +++ b/examples/opengym-1/sim.cc @@ -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 + * + */ + +#include "ns3/core-module.h" +#include "ns3/opengym-module.h" + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE ("OpenGym"); + +/* +Define observation space +*/ +Ptr MyGetObservationSpace(void) +{ + uint32_t nodeNum = 5; + float low = 0.0; + float high = 10.0; + std::vector shape = {nodeNum,}; + std::string dtype = TypeNameGet (); + Ptr space = CreateObject (low, high, shape, dtype); + NS_LOG_UNCOND ("MyGetObservationSpace: " << space); + return space; +} + +/* +Define action space +*/ +Ptr MyGetActionSpace(void) +{ + uint32_t nodeNum = 5; + + Ptr space = CreateObject (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 MyGetObservation(void) +{ + uint32_t nodeNum = 5; + uint32_t low = 0.0; + uint32_t high = 10.0; + Ptr rngInt = CreateObject (); + + std::vector shape = {nodeNum,}; + Ptr > box = CreateObject >(shape); + + // generate random data + for (uint32_t i = 0; iGetInteger(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 action) +{ + Ptr discrete = DynamicCast(action); + NS_LOG_UNCOND ("MyExecuteActions: " << action); + return true; +} + +void ScheduleNextStateRead(double envStepTime, Ptr 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 openGym = CreateObject (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 (); + +} diff --git a/examples/opengym-1/simple_test.py b/examples/opengym-1/simple_test.py new file mode 100755 index 0000000000..966ecfde31 --- /dev/null +++ b/examples/opengym-1/simple_test.py @@ -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") \ No newline at end of file diff --git a/examples/opengym-1/test.py b/examples/opengym-1/test.py new file mode 100755 index 0000000000..a1e53d0fd1 --- /dev/null +++ b/examples/opengym-1/test.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import argparse +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" + + +parser = argparse.ArgumentParser(description='Start simulation script on/off') +parser.add_argument('--start', + type=int, + default=1, + help='Start ns-3 simulation script 0/1, Default: 1') +parser.add_argument('--iterations', + type=int, + default=1, + help='Number of iterations, Default: 1') +args = parser.parse_args() +startSim = bool(args.start) +iterationNum = int(args.iterations) + +port = 5555 +simTime = 20 # seconds +stepTime = 0.5 # seconds +seed = 0 +simArgs = {"--simTime": simTime, + "--testArg": 123} +debug = False + +env = ns3env.Ns3Env(port=port, stepTime=stepTime, startSim=startSim, simSeed=seed, simArgs=simArgs, debug=debug) +# simpler: +#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 +currIt = 0 + +try: + while True: + print("Start iteration: ", currIt) + obs = env.reset() + print("Step: ", stepIdx) + print("---obs:", obs) + + while True: + stepIdx += 1 + action = env.action_space.sample() + print("---action: ", action) + + print("Step: ", stepIdx) + obs, reward, done, info = env.step(action) + print("---obs, reward, done, info: ", obs, reward, done, info) + + if done: + stepIdx = 0 + if currIt + 1 < iterationNum: + env.reset() + break + + currIt += 1 + if currIt == iterationNum: + break + +except KeyboardInterrupt: + print("Ctrl-C -> Exit") +finally: + env.close() + print("Done") \ No newline at end of file diff --git a/examples/rl-tcp/tcp-rl.cc b/examples/rl-tcp/tcp-rl.cc index 6b3de693ca..25049328cc 100644 --- a/examples/rl-tcp/tcp-rl.cc +++ b/examples/rl-tcp/tcp-rl.cc @@ -46,12 +46,6 @@ TcpSocketDerived::GetTypeId (void) return tid; } -TypeId -TcpSocketDerived::GetInstanceTypeId () const -{ - return TcpSocketDerived::GetTypeId (); -} - TcpSocketDerived::TcpSocketDerived (void) { } diff --git a/examples/rl-tcp/tcp-rl.h b/examples/rl-tcp/tcp-rl.h index 2bcc041b3e..a6d0ea3c7c 100644 --- a/examples/rl-tcp/tcp-rl.h +++ b/examples/rl-tcp/tcp-rl.h @@ -37,7 +37,6 @@ class TcpSocketDerived : public TcpSocketBase { public: static TypeId GetTypeId (void); - virtual TypeId GetInstanceTypeId () const; TcpSocketDerived (void); virtual ~TcpSocketDerived (void); @@ -121,4 +120,4 @@ class TcpRlTimeBased : public TcpRlBase } // namespace ns3 -#endif /* TCP_RL_H */ \ No newline at end of file +#endif /* TCP_RL_H */ diff --git a/test/opengym-test-suite.cc b/test/opengym-test-suite.cc index 45fb5be624..01315361b2 100644 --- a/test/opengym-test-suite.cc +++ b/test/opengym-test-suite.cc @@ -57,10 +57,10 @@ class OpengymTestSuite : public TestSuite }; OpengymTestSuite::OpengymTestSuite () - : TestSuite ("opengym", UNIT) + : TestSuite ("opengym", Type::UNIT) { // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER - AddTestCase (new OpengymTestCase1, TestCase::QUICK); + AddTestCase (new OpengymTestCase1, TestCase::Duration::QUICK); } // Do not forget to allocate an instance of this TestSuite