From 38e5413e1679e199acb2fea9ba47150b8388b371 Mon Sep 17 00:00:00 2001 From: muyuanshen Date: Thu, 22 Jun 2023 23:02:38 +0800 Subject: [PATCH] updated README and removed interference-pattern example --- README.md | 101 ++++++-- .../cognitive-agent-v1.py | 140 ----------- examples/interference-pattern/mygym.cc | 233 ------------------ examples/interference-pattern/mygym.h | 109 -------- .../interference-pattern/requirements.txt | 10 - examples/interference-pattern/sim.cc | 211 ---------------- examples/interference-pattern/simple_test.py | 46 ---- 7 files changed, 75 insertions(+), 775 deletions(-) delete mode 100755 examples/interference-pattern/cognitive-agent-v1.py delete mode 100644 examples/interference-pattern/mygym.cc delete mode 100644 examples/interference-pattern/mygym.h delete mode 100644 examples/interference-pattern/requirements.txt delete mode 100644 examples/interference-pattern/sim.cc delete mode 100755 examples/interference-pattern/simple_test.py diff --git a/README.md b/README.md index 4952015..0158ccf 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,71 @@ # ns3-ai + ## Online Tutorial: -Join us in this [online recording](https://vimeo.com/566296651) to get better knowledge about ns3-ai! The slides introduce the ns3-ai model could also be found [here](https://www.nsnam.org/wp-content/uploads/2021/tutorials/ns3-ai-tutorial-June-2021.pdf)! + +Join us in this [online recording](https://vimeo.com/566296651) to get better knowledge about ns3-ai! The slides +introduce the ns3-ai model could also be +found [here](https://www.nsnam.org/wp-content/uploads/2021/tutorials/ns3-ai-tutorial-June-2021.pdf)! + ## Description - The [ns–3](https://www.nsnam.org/) simulator is an open-source networking simulation tool implemented by C++ and wildly used for network research and education. Currently, more and more researchers are willing to apply AI algorithms to network research. Most AI algorithms are likely to rely on open source frameworks such as [TensorFlow](https://www.tensorflow.org/) and [PyTorch](https://pytorch.org/). These two parts are developed independently and extremely hard to merge, so it is more reasonable and convenient to connect these two tasks with data interaction. Our model provides a high-efficiency solution to enable the data interaction between ns-3 and other python based AI frameworks. - This module does not provide any AI algorithms or rely on any frameworks but instead is providing a Python module that enables AI interconnect, so the AI framework needs to be separately installed. You only need to clone or download this work, then import the Python modules, you could use this work to exchange data between ns-3 and your AI algorithms. +The [ns–3](https://www.nsnam.org/) simulator is an open-source networking simulation tool implemented by C++ and wildly +used for network research and education. Currently, more and more researchers are willing to apply AI algorithms to +network research. Most AI algorithms are likely to rely on open source frameworks such +as [TensorFlow](https://www.tensorflow.org/) and [PyTorch](https://pytorch.org/). These two parts are developed +independently and extremely hard to merge, so it is more reasonable and convenient to connect these two tasks with data +interaction. Our model provides a high-efficiency solution to enable the data interaction between ns-3 and other python +based AI frameworks. +This module does not provide any AI algorithms or rely on any frameworks but instead is providing a Python module that +enables AI interconnect, so the AI framework needs to be separately installed. You only need to clone or download this +work, then import the Python modules, you could use this work to exchange data between ns-3 and your AI algorithms. - Inspired by [ns3-gym](https://github.com/tkn-tub/ns3-gym), but using a different approach which is faster and more flexible. +Inspired by [ns3-gym](https://github.com/tkn-tub/ns3-gym), but using a different approach which is faster and more +flexible. ### Features -- High-performance data interaction module (using shared memory). + +- High-performance data interaction module (using shared memory). - Provide a high-level interface for different AI algorithms. - Easy to integrate with other AI frameworks. - ## Installation + ### 1. Install this module in ns-3 -#### Get ns-3: + +#### Get ns-3: + This module needs to be built within ns-3, so you need to get a ns-3-dev or other ns-3 codes first. Check [ns-3 installation wiki](https://www.nsnam.org/wiki/Installation) for detailed instructions. #### Add this module + ```Shell cd $YOUR_NS3_CODE/contrib git clone https://github.com/hust-diangroup/ns3-ai.git ``` -#### Rebuild ns-3 +#### Reconfigure ns-3 + ```Shell -./waf configure -./waf +./ns3 clean +./ns3 configure ``` ### 2. Add Python interface #### Install -Python3 is used and tested. + +Python 3 is used and tested. It's recommended to use ns3-ai under a Conda environment. ```Shell cd $YOUR_NS3_CODE/contrib/ns3-ai/py_interface - pip3 install . --user ``` -#### Baisc usage +#### Basic usage + ``` Python import py_interface mempool_key = 1234 # memory pool key, arbitrary integer large than 1000 @@ -59,9 +79,21 @@ with v as o: print(*o) py_interface.FreeMemory() ``` + ## Shared Memory Pool -The ns3-ai module interconnects the ns-3 and AI frameworks by transferring data through the shared memory pool. The memory can be accessed by both sides and controlled mainly in ns-3. The shared memory pool is defined in `ns3-ai/model/memory-pool.h`. -The `CtrlInfoBlock` is the control block of the all shared memory pool, the `SharedMemoryCtrl` is the control block of each shared memory, and the `SharedMemoryLockable` is the actual shared memory used for data exchange. In each memory block, we use version and nextVersion as the lock indicator. The synchronization for reading/writing locks and the events update are accomplished by the lock indicator. For every process that wants to access or modify the data, it will compare the `version` variable and the `nextVersion` variable. If they are the same, it means that the memory is reachable. Then it will add one to the next version atomically to lock the memory and also add one to the version after its operation to the memory to unlock the memory. Besides the version of the memory acts as the signal to tell different processes the current state of the memory block, which provides different methods to synchronize. + +The ns3-ai module interconnects the ns-3 and AI frameworks by transferring data through the shared memory pool. The +memory can be accessed by both sides and controlled mainly in ns-3. The shared memory pool is defined +in `ns3-ai/model/memory-pool.h`. +The `CtrlInfoBlock` is the control block of the all shared memory pool, the `SharedMemoryCtrl` is the control block of +each shared memory, and the `SharedMemoryLockable` is the actual shared memory used for data exchange. In each memory +block, we use version and nextVersion as the lock indicator. The synchronization for reading/writing locks and the +events update are accomplished by the lock indicator. For every process that wants to access or modify the data, it will +compare the `version` variable and the `nextVersion` variable. If they are the same, it means that the memory is +reachable. Then it will add one to the next version atomically to lock the memory and also add one to the version after +its operation to the memory to unlock the memory. Besides the version of the memory acts as the signal to tell different +processes the current state of the memory block, which provides different methods to synchronize. + ``` |SharedMemoryBlock1| |SharedMemoryBlock2| @@ -75,44 +107,59 @@ The `CtrlInfoBlock` is the control block of the all shared memory pool, the `Sha |MemoryPoolContrlBlk| ``` +## Examples +### Quick Start on how to us ns3-ai - [a_plus_b](https://github.com/hust-diangroup/ns3-ai/tree/master/examples/a_plus_b) -## Examples -### Quick Statrt on how to us ns3-ai - [a_plus_b](https://github.com/hust-diangroup/ns3-ai/tree/master/examples/a_plus_b) -This example show how you can use ns3-ai by a very simple case that you transfer the data from ns-3 to python side and calculate a + b in the python to put back the results. Please check the README in it for more details. +This example show how you can use ns3-ai by a very simple case that you transfer the data from ns-3 to python side and +calculate a + b in the python to put back the results. Please check the README in it for more details. ### [RL-TCP](https://github.com/hust-diangroup/ns3-ai/blob/master/examples/rl-tcp/) -This example is inspired by [ns3-gym example](https://github.com/tkn-tub/ns3-gym#rl-tcp). We bulid this example for the benchmarking and to compare with their module. + +This example is inspired by [ns3-gym example](https://github.com/tkn-tub/ns3-gym#rl-tcp). We build this example for the +benchmarking and to compare with their module. #### Build and Run + Run ns-3 example: + ``` -cp -r contrib/ns3-ai/example/rl-tcp scratch/ +cp -r contrib/ns3-ai/examples/rl-tcp scratch/ +cd scratch/rl-tcp/ python3 run_tcp_rl.py --use_rl --result ``` ### [LTE_CQI](https://github.com/hust-diangroup/ns3-ai/blob/master/examples/lte_cqi/) -This original work is done based on [5G NR](https://5g-lena.cttc.es/) branch in ns-3. We made some changes to make it also run in LTE codebase in ns-3 mainline. We didn't reproduce all the experiments on LTE, and the results used in this document are based on NR work. + +This original work is done based on [5G NR](https://5g-lena.cttc.es/) branch in ns-3. We made some changes to make it +also run in LTE codebase in ns-3 mainline. We didn't reproduce all the experiments on LTE, and the results used in this +document are based on NR work. #### Build and Run Run ns-3 example: -If you want to test the LSTM, you can run another python script but you may need to install [TensorFlow](https://www.tensorflow.org/) environment first. +If you want to test the LSTM, you can run another python script but you may need to +install [TensorFlow](https://www.tensorflow.org/) environment first. + ```Shell +cp -r contrib/ns3-ai/examples/lte_cqi scratch/ cd scratch/lte_cqi/ - python3 run_online_lstm.py 1 ``` -**NOTE: If the program does not exit normally, you need to run freeshm.sh to release the shared memory manually.** + +**NOTE: If the program does not exit normally, you need to run `freeshm.sh` to release the shared memory manually.** ### [Rate-Control](https://github.com/hust-diangroup/ns3-ai/tree/master/examples/rate-control) -This is an example that shows how to develop a new rate control algorithm for the Wi-Fi model in ns-3 using the ns3-ai model. + +This is an example that shows how to develop a new rate control algorithm for the Wi-Fi model in ns-3 using the ns3-ai +model. + #### Usage Copy this example to scratch: ```shell -cp -r contrib/ns3-ai/example/rate-control scratch/ +cp -r contrib/ns3-ai/examples/rate-control scratch/ cd scratch/rate-control ``` @@ -129,7 +176,9 @@ python3 ai_thompson_sampling.py ``` ## Cite our work + Please use the following bibtex: + ``` @inproceedings{10.1145/3389400.3389404, author = {Yin, Hao and Liu, Pengyu and Liu, Keshu and Cao, Liu and Zhang, Lytianyang and Gao, Yayu and Hei, Xiaojun}, diff --git a/examples/interference-pattern/cognitive-agent-v1.py b/examples/interference-pattern/cognitive-agent-v1.py deleted file mode 100755 index 30e3f55..0000000 --- a/examples/interference-pattern/cognitive-agent-v1.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import tensorflow as tf -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -from tensorflow import keras -from py_interface import * -from ctypes import * -import sys - -tf.random.set_seed(0) -np.random.seed(0) - -MAX_CHANNEL_NUM = 64 - - -class sEnv(Structure): - # _pack_ = 1 - _fields_ = [ - ('reward', c_float), - ('done', c_bool), - ('channNum', c_uint32), - ('channelOccupation', c_uint32*MAX_CHANNEL_NUM), - ] - - -class sAct(Structure): - # _pack_ = 1 - _fields_ = [ - ('nextChannel', c_uint32), - ] - - -class sInfo(Structure): - # _pack_ = 1 - _fields_ = [ - ('channelNum', c_uint32), - ] - - -exp = Experiment(1234, 4096, 'interference-pattern', '../../') -var = Ns3AIRL(2333, sEnv, sAct, sInfo) - -model = keras.Sequential() -model.add(keras.layers.Dense(4, input_shape=(4,), activation='tanh')) -model.compile(optimizer='adam', - loss='mean_squared_error', - metrics=['accuracy']) - -total_episodes = 20 -max_env_steps = 100 - -epsilon = 1.0 -epsilon_min = 0.01 -epsilon_decay = 0.99 - -time_history = [] -rew_history = [] -dec_arr = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [ - 0, 0, 0, 1]], dtype=np.float64) -train = 3 -try: - for e in range(total_episodes): - exp.reset() - exp.run(show_output=0) - state = np.array([1, 0, 0, 0]) - state = np.reshape(state, [1, 4]) - rewardsum = 0 - lst_act = 0 - for time in range(max_env_steps): - if np.random.rand(1) < epsilon: - action = np.random.randint(0, 4) - else: - res = model.predict(state)[0] - action = np.argmax(res) - - with var as data: - if data == None: - break - state = data.env.channelOccupation[:data.env.channNum] - reward = int(data.env.reward) - done = data.env.done - data.act.nextChannel = c_uint32(action) - state = np.array(state) - state = np.reshape(state, [1, 4]) - - if done: - break - - target = model.predict(state)[0]-reward * \ - dec_arr[lst_act & 3:(lst_act & 3)+1] - target -= np.mean(target) - target /= np.std(target) - if reward < 0: - epoch = 10 - else: - epoch = 5 - if train > 0: - model.fit(state, target.reshape((1, 4)), epochs=epoch, verbose=0) - - lst_act = action - rewardsum += reward - if epsilon > epsilon_min: - epsilon *= epsilon_decay - - if rewardsum >= 90: - if train>0: - train -= 1 - else: - train = 3 - print("episode: {}/{}, time: {}, rew: {}" - .format(e, total_episodes, time, rewardsum)) - - time_history.append(time) - rew_history.append(rewardsum) - exp.kill() - print("Plot Learning Performance") - mpl.rcdefaults() - mpl.rcParams.update({'font.size': 16}) - - fig, ax = plt.subplots(figsize=(10, 4)) - plt.grid(True, linestyle='--') - plt.title('Learning Performance') - plt.plot(range(len(time_history)), time_history, label='Steps', - marker="^", linestyle=":") # , color='red') - plt.plot(range(len(rew_history)), rew_history, label='Reward', - marker="", linestyle="-") # , color='k') - plt.xlabel('Episode') - plt.ylabel('Time') - plt.legend(prop={'size': 12}) - - plt.savefig('learning.pdf', bbox_inches='tight') - plt.show() -except Exception as e: - print('Something wrong') - print(e) -finally: - del exp \ No newline at end of file diff --git a/examples/interference-pattern/mygym.cc b/examples/interference-pattern/mygym.cc deleted file mode 100644 index d281e1d..0000000 --- a/examples/interference-pattern/mygym.cc +++ /dev/null @@ -1,233 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2018 Technische Universität Berlin - * - * 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 "mygym.h" -#include "ns3/object.h" -#include "ns3/core-module.h" -#include "ns3/wifi-module.h" -#include "ns3/node-list.h" -#include "ns3/log.h" -#include -#include - -namespace ns3 -{ - -NS_LOG_COMPONENT_DEFINE("MyAIEnv"); - -// NS_OBJECT_ENSURE_REGISTERED (MyAIEnv); - -MyAIEnv::MyAIEnv(uint16_t id) : Ns3AIRL(id) -{ - NS_LOG_FUNCTION(this); - m_currentNode = 0; - m_currentChannel = 0; - m_collisionTh = 3; - m_channelNum = 1; - m_channelOccupation.clear(); - - SetCond(2, 0); -} - -MyAIEnv::MyAIEnv(uint16_t id, uint32_t channelNum) : Ns3AIRL(id) -{ - NS_LOG_FUNCTION(this); - m_currentNode = 0; - m_currentChannel = 0; - m_collisionTh = 3; - m_channelNum = channelNum; - m_channelOccupation.clear(); - - SetCond(2, 0); - auto info = InfoSetterCond(); - info->channelNum = channelNum; - // SetCompleted(); -} - -MyAIEnv::~MyAIEnv() -{ - NS_LOG_FUNCTION(this); -} - -// TypeId -// MyAIEnv::GetTypeId (void) -// { -// static TypeId tid = TypeId ("MyAIEnv") -// .SetParent> () -// .SetGroupName ("MyAIEnv") -// .AddConstructor () -// ; -// return tid; -// } - -// void -// MyAIEnv::DoDispose () -// { -// NS_LOG_FUNCTION (this); -// } - -// Ptr -// MyAIEnv::GetActionSpace() -// { -// NS_LOG_FUNCTION (this); -// Ptr space = CreateObject (m_channelNum); -// NS_LOG_UNCOND ("GetActionSpace: " << space); -// return space; -// } - -// Ptr -// MyAIEnv::GetObservationSpace() -// { -// NS_LOG_FUNCTION (this); -// float low = 0.0; -// float high = 1.0; -// std::vector shape = {m_channelNum,}; -// std::string dtype = TypeNameGet (); -// Ptr space = CreateObject (low, high, shape, dtype); -// NS_LOG_UNCOND ("GetObservationSpace: " << space); -// return space; -// } - -bool MyAIEnv::GetGameOver() -{ - NS_LOG_FUNCTION(this); - bool isGameOver = false; - - uint32_t collisionNum = 0; - for (auto &v : m_collisions) - collisionNum += v; - - if (collisionNum >= m_collisionTh) - { - isGameOver = true; - } - // NS_LOG_UNCOND("MyGetGameOver: " << isGameOver); - return isGameOver; -} - -// Ptr -// MyAIEnv::GetObservation() -// { -// NS_LOG_FUNCTION (this); -// std::vector shape = {m_channelNum,}; -// Ptr > box = CreateObject >(shape); - -// for (uint32_t i = 0; i < m_channelOccupation.size(); ++i) { -// uint32_t value = m_channelOccupation.at(i); -// box->AddValue(value); -// } - -// NS_LOG_UNCOND ("MyGetObservation: " << box); -// return box; -// } - -float MyAIEnv::GetReward() -{ - NS_LOG_FUNCTION(this); - float reward = 1.0; - if (m_channelOccupation.size() == 0) - { - return 0.0; - } - uint32_t occupied = m_channelOccupation.at(m_currentChannel); - if (occupied == 1) - { - reward = -1.0; - m_collisions.erase(m_collisions.begin()); - m_collisions.push_back(1); - } - else - { - m_collisions.erase(m_collisions.begin()); - m_collisions.push_back(0); - } - // NS_LOG_UNCOND("MyGetReward: " << reward); - return reward; -} - -// std::string -// MyAIEnv::GetExtraInfo() -// { -// NS_LOG_FUNCTION (this); -// std::string myInfo = "info"; -// NS_LOG_UNCOND("MyGetExtraInfo: " << myInfo); -// return myInfo; -// } - -// bool -// MyAIEnv::ExecuteActions(Ptr action) -// { -// NS_LOG_FUNCTION (this); -// Ptr discrete = DynamicCast(action); -// uint32_t nextChannel = discrete->GetValue(); -// m_currentChannel = nextChannel; - -// NS_LOG_UNCOND ("Current Channel: " << m_currentChannel); -// return true; -// } - -void MyAIEnv::CollectChannelOccupation(uint32_t chanId, uint32_t occupied) -{ - NS_LOG_FUNCTION(this); - m_channelOccupation.push_back(occupied); -} - -bool MyAIEnv::CheckIfReady() -{ - NS_LOG_FUNCTION(this); - return m_channelOccupation.size() == m_channelNum; -} - -void MyAIEnv::ClearObs() -{ - NS_LOG_FUNCTION(this); - m_channelOccupation.clear(); -} - -void MyAIEnv::PerformCca(Ptr entity, uint32_t channelId, Ptr avgPowerSpectralDensity) -{ - double power = Integral(*(avgPowerSpectralDensity)); - double powerDbW = 10 * std::log10(power); - double threshold = -60; - uint32_t busy = powerDbW > threshold; - // NS_LOG_UNCOND("Channel: " << channelId << " CCA: " << busy << " RxPower: " << powerDbW); - - entity->CollectChannelOccupation(channelId, busy); - - if (entity->CheckIfReady()) - { - // entity->Notify(); - auto env = entity->EnvSetterCond(); - env->reward = entity->GetReward(); - env->done = entity->GetGameOver(); - env->channNum = entity->m_channelOccupation.size(); - for (uint32_t i = 0; i < entity->m_channelOccupation.size(); ++i) - { - env->channelOccupation[i] = entity->m_channelOccupation.at(i); - } - entity->SetCompleted(); - entity->ClearObs(); - auto act = entity->ActionGetterCond(); - entity->m_currentChannel = act->nextChannel; - entity->GetCompleted(); - } -} - -} // namespace ns3 \ No newline at end of file diff --git a/examples/interference-pattern/mygym.h b/examples/interference-pattern/mygym.h deleted file mode 100644 index 7910834..0000000 --- a/examples/interference-pattern/mygym.h +++ /dev/null @@ -1,109 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2018 Technische Universität Berlin - * - * 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 - */ - -#ifndef MY_GYM_ENTITY_H -#define MY_GYM_ENTITY_H - -#include "ns3/stats-module.h" -// #include "ns3/opengym-module.h" -#include "ns3/ns3-ai-module.h" -#include "ns3/spectrum-module.h" - -namespace ns3 -{ - -class Node; -class WifiMacQueue; -class Packet; - -#define MAX_CHANNEL_NUM 64 - -struct sEnv -{ - float reward; - bool done; - uint32_t channNum; - uint32_t channelOccupation[MAX_CHANNEL_NUM]; -}; -struct sAct -{ - uint32_t nextChannel; -}; -struct sInfo -{ - uint32_t channelNum; -}; - -class MyAIEnv : public Ns3AIRL -{ - MyAIEnv(); - -public: - MyAIEnv(uint16_t id); - MyAIEnv(uint16_t id, uint32_t channelNum); - virtual ~MyAIEnv(); - // static TypeId GetTypeId (void); - // virtual void DoDispose (); - - // Ptr GetActionSpace(); - // Ptr GetObservationSpace(); - bool GetGameOver(); - // Ptr GetObservation(); - float GetReward(); - // std::string GetExtraInfo(); - // bool ExecuteActions(Ptr action); - - // the function has to be static to work with MakeBoundCallback - // that is why we pass pointer to MyAIEnv instance to be able to store the context (node, etc) - static void PerformCca(Ptr entity, uint32_t channelId, Ptr avgPowerSpectralDensity); - void CollectChannelOccupation(uint32_t chanId, uint32_t occupied); - bool CheckIfReady(); - void ClearObs(); - -private: - void ScheduleNextStateRead(); - Ptr GetQueue(Ptr node); - bool SetCw(Ptr node, uint32_t cwMinValue = 0, uint32_t cwMaxValue = 0); - - Time m_interval = Seconds(0.1); - Ptr m_currentNode; - uint64_t m_rxPktNum; - uint32_t m_channelNum; - std::vector m_channelOccupation; - uint32_t m_currentChannel; - - uint32_t m_collisionTh; - std::vector m_collisions = { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - }; -}; - -} // namespace ns3 - -#endif // MY_GYM_ENTITY_H diff --git a/examples/interference-pattern/requirements.txt b/examples/interference-pattern/requirements.txt deleted file mode 100644 index 1c55158..0000000 --- a/examples/interference-pattern/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -tensorflow==2.3.1 -tensorflow-estimator==2.3.0 -tensorboard==2.3.0 -numpy==1.18.1 -Keras==2.4.3 -Keras-Applications==1.0.8 -Keras-Preprocessing==1.1.2 -matplotlib==3.3.2 -psutil==5.7.2 - diff --git a/examples/interference-pattern/sim.cc b/examples/interference-pattern/sim.cc deleted file mode 100644 index 86286f8..0000000 --- a/examples/interference-pattern/sim.cc +++ /dev/null @@ -1,211 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2018 Technische Universität Berlin - * - * 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/applications-module.h" -#include "ns3/packet-sink.h" -#include "ns3/mobility-module.h" -#include "ns3/wifi-module.h" -#include "ns3/internet-module.h" -#include "ns3/spectrum-module.h" -#include "ns3/stats-module.h" -#include "ns3/flow-monitor-module.h" - -#include "mygym.h" - -using namespace ns3; - -NS_LOG_COMPONENT_DEFINE ("Interference-Pattern"); - - -int main (int argc, char *argv[]) -{ - // Parameters of the environment - uint32_t simSeed = 1; - double simulationTime = 1000; //seconds - // double envStepTime = 0.1; //seconds, ns3gym env step time interval - // uint32_t openGymPort = 5555; - uint32_t testArg = 0; - - //Parameters of the scenario - uint32_t nodeNum = 2; - double distance = 10.0; - bool enableFading = false; - double interfererPower = 10; - - // Interference Pattern - double interferenceSlotTime = 0.1; // seconds; - - // time, channel usage - std::map > interferencePattern; - interferencePattern.insert(std::pair > (0, {1,0,0,0})); - interferencePattern.insert(std::pair > (1, {0,1,0,0})); - interferencePattern.insert(std::pair > (2, {0,0,1,0})); - interferencePattern.insert(std::pair > (3, {0,0,0,1})); - - // set channel number correctly, do not modify - std::vector tmp = interferencePattern.at(0); - uint32_t channNum = tmp.size(); - - 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.AddValue ("enableFading", "If fading should be enabled. Default: false", enableFading); - 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 = CreateObject (openGymPort); - // Ptr myGymEnv = CreateObject (channNum); - // myGymEnv->SetOpenGymInterface(openGymInterface); - Ptr myAIEnv = Create (2333, channNum); - - NodeContainer nodes; - nodes.Create (nodeNum); - - // Channel - Ptr spectrumChannel = CreateObject (); - Ptr lossModel = CreateObject (); - Ptr fadingModel = CreateObject (); - if (enableFading) { - lossModel->SetNext (fadingModel); - } - spectrumChannel->AddPropagationLossModel (lossModel); - Ptr delayModel = CreateObject (); - spectrumChannel->SetPropagationDelayModel (delayModel); - - // Mobility model - MobilityHelper mobility; - mobility.SetPositionAllocator ("ns3::GridPositionAllocator", - "MinX", DoubleValue (0.0), - "MinY", DoubleValue (0.0), - "DeltaX", DoubleValue (distance), - "DeltaY", DoubleValue (distance), - "GridWidth", UintegerValue (nodeNum), // will create linear topology - "LayoutType", StringValue ("RowFirst")); - mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); - mobility.Install (nodes); - - // Define channel models - std::map > spectrumModels; - for (uint32_t chanId=0; chanId sm = Create (bands); - spectrumModels.insert(std::pair>(chanId, sm)); - } - - // Spectrum Analyzer --- Channel Sensing - Ptr sensingNode = nodes.Get(0); - SpectrumAnalyzerHelper spectrumAnalyzerHelper; - spectrumAnalyzerHelper.SetChannel (spectrumChannel); - spectrumAnalyzerHelper.SetPhyAttribute ("Resolution", TimeValue (MilliSeconds (100))); - spectrumAnalyzerHelper.SetPhyAttribute ("NoisePowerSpectralDensity", DoubleValue (1e-15)); // -120 dBm/Hz - NetDeviceContainer spectrumAnalyzers; - - for (uint32_t chanId=0; chanId netDev = spectrumAnalyzers.Get(i); - Ptr nonCommNetDev = DynamicCast(netDev); - Ptr spectrumPhy = nonCommNetDev->GetPhy(); - Ptr spectrumAnalyzer = DynamicCast(spectrumPhy); - spectrumAnalyzer->Start (); - - std::ostringstream oss; - oss.str (""); - uint32_t devId = netDev->GetIfIndex(); - oss << "/NodeList/" << sensingNode->GetId () << "/DeviceList/" << devId << "/$ns3::NonCommunicatingNetDevice/Phy/AveragePowerSpectralDensityReport"; - uint32_t channelId = i; - // Config::ConnectWithoutContext (oss.str (),MakeBoundCallback (&MyGymEnv::PerformCca, myGymEnv, channelId)); - Config::ConnectWithoutContext (oss.str (),MakeBoundCallback (&MyAIEnv::PerformCca, myAIEnv, channelId)); - } - - // Signal Generator --- Generate interference pattern - Ptr interferingNode = nodes.Get(1); - WaveformGeneratorHelper waveformGeneratorHelper; - waveformGeneratorHelper.SetChannel (spectrumChannel); - waveformGeneratorHelper.SetPhyAttribute ("Period", TimeValue (Seconds (interferenceSlotTime))); - waveformGeneratorHelper.SetPhyAttribute ("DutyCycle", DoubleValue (1.0)); - NetDeviceContainer waveformGeneratorDevices; - - for (uint32_t chanId=0; chanId wgPsd = Create (spectrumModels.at(chanId)); - *wgPsd = interfererPower / (20e6); - NS_LOG_DEBUG ("wgPsd : " << *wgPsd << " integrated power: " << Integral (*(GetPointer (wgPsd)))); - waveformGeneratorHelper.SetTxPowerSpectralDensity (wgPsd); - waveformGeneratorDevices.Add(waveformGeneratorHelper.Install (interferingNode)); - } - - // Schedule interference pattern - double scheduledTime = 0; - while (scheduledTime < simulationTime) - { - std::map >::iterator it; - for (it=interferencePattern.begin(); it!=interferencePattern.end(); it++) { - std::vector channels = (*it).second; - - for (uint32_t chanId = 0; chanId < channels.size(); chanId++){ - uint32_t occupied = channels.at(chanId); - NS_LOG_DEBUG("scheduledTime: " << scheduledTime << " ChanId " << chanId << " Occupied: " << occupied); - if (occupied == 1) { - Simulator::Schedule (Seconds (scheduledTime), &WaveformGenerator::Start, - waveformGeneratorDevices.Get (chanId)->GetObject ()->GetPhy ()->GetObject ()); - } else { - Simulator::Schedule (Seconds (scheduledTime), &WaveformGenerator::Stop, - waveformGeneratorDevices.Get (chanId)->GetObject ()->GetPhy ()->GetObject ()); - } - } - scheduledTime += interferenceSlotTime; - } - } - - NS_LOG_UNCOND ("Simulation start"); - Simulator::Stop (Seconds (simulationTime)); - Simulator::Run (); - NS_LOG_UNCOND ("Simulation stop"); - // myGymEnv->NotifySimulationEnd(); - myAIEnv->SetFinish(); - - Simulator::Destroy (); - NS_LOG_UNCOND ("Simulation exit"); -} diff --git a/examples/interference-pattern/simple_test.py b/examples/interference-pattern/simple_test.py deleted file mode 100755 index 9c56873..0000000 --- a/examples/interference-pattern/simple_test.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import gym -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" - - -env = gym.make('ns3-v0') -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