Cheryl0605 / ns3-ai

Enable the interaction between ns-3 and popular frameworks using Python, which mean you can train and test your AI algorithms in ns-3 without changing any frameworks you are using now!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ns3-ai

Description

The ns–3 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 and PyTorch. 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, but using a different approach which is faster and more flexible.

Features

  • 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:

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 for detailed instructions.

Add this module

cd $YOUR_NS3_CODE/contrib
git clone https://github.com/hust-diangroup/ns3-ai.git

Rebuild ns-3

./waf configure
./waf

2. Add Python interface

Install

Python3 is used and tested.

cd $YOUR_NS3_CODE/contrib/ns3-ai/py_interface

python setup.py install --user

Baisc usage

import py_interface
py_interface.Init(1234, 4096) # key poolSize
v = NS3BigVar(233, c_int*10)
with v as o:
    for i in range(10):
        o[i] = c_int(i)
    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.

|SharedMemoryBlock1|
|SharedMemoryBlock2|
|SharedMemoryBlock3|
...
...
...
|ControlMemoryBlock3|
|ControlMemoryBlock2|
|ControlMemoryBlock1|
|MemoryPoolContrlBlk|

Examples

This example is inspired by ns3-gym example. We bulid 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/

./waf --run "rl-tcp"

Run Python code:

cd contrib/ns3-ai/example/rl-tcp/

python testtcp.py

NOTE: Currently the RL test in python script is not fully enabled, coming soon.

This original work is done based on 5G NR 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

Apply the lte patch (in your ns3 dir)

cp contrib/ns3-ai/example/lte_cqi/lte_cqi_predict.patch .

git apply --stat lte_cqi_predict.patch

git apply --check lte_cqi_predict.patch

git am -s < lte_cqi_predict.patch

Run ns-3 example:

cp -r contrib/ns3-ai/example/lte_cqi scratch/

./waf --run "lte_cqi"

Run Python code:

cd scratch/lte_cqi/

python run_online.py 

If you want to test the LSTM, you can run another python script but you may need to install TensorFlow environment first.

cd scratch/lte_cqi/

python 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.

About

Enable the interaction between ns-3 and popular frameworks using Python, which mean you can train and test your AI algorithms in ns-3 without changing any frameworks you are using now!


Languages

Language:C++ 82.4%Language:Python 17.3%Language:Shell 0.3%