Why mlpack?
In fact, there are two popular ways to incorporate AI/ML functionalities under ns-3. They are ns3-gym and ns3-ai. The following articles explained the way of installing and using them.
Even though both of the above two frameworks are good, they are somewhat bulk in nature. They will need a lot of dependencies. Most of the scholars who are using them will simply fail in one way or another during trying to install them and use them.
In fact, in the above two methods, the AI/ML part of the code will run separately and the ns-3 part of the code will be able to communicate with it. But, I always wish to use AI and ML functions directly in the ns-3 code itself; and it is now possible if we use mlpack with ns-3. If we compile a simulation which is using mlpack, the compiled binary itself will now contain the AI/ML part integrated into it.
This article explores the way of incorporating machine language-related functions of mlpack under ns-3 simulations for the design of AI and ML-based network protocol design.
mlpack.
mlpack is an intuitive, fast, and flexible header-only C++ machine-learning library with bindings to other languages. It aims to implement a wide array of machine learning methods and functions for machine learning researchers[1]. We can easily use the machine-learning functionalities of mlpack in any C++ project and even use it under Python, Julia, Go and R using the provided bindings.
mlpack contains a number of standard machine-learning algorithms[2], such as
- logistic regression,
- random forests,
- k-means clustering,
- compile-time optimized deep learning
- reinforcement learning framework
- dual-tree algorithms for nearest neighbour search and other tasks
- a generic optimization framework with numerous optimizers (Curtin et al. 2017),
- a generic hyper-parameter tuner,
- and other recently published machine learning algorithms.
Note: Even though we did this installation under a chroot jail based virtualization (where the command ‘sudo’ will not work), for the purpose of understanding, we just added ‘sudo’ while illustrating a command (so that, the users who will not use chroot based install can run the command properly). The same procedure will work on any ubuntu/Debian variant operating system.
For those who are interested to know the power of using chroot-jail, they may first read the following article :
Installing ns3.35 in Debian 10 chroot Jail Under Debian 11 Host OS or any Version of Linux Host
Installing and using mlpack
Step 1: Install Dependencies
mlpack required the following dependencies :
- Armadillo
- Ensmallen
- Cereal
We may install the dependencies as follows:
[3] discusses how to build mlpack from source. These build directions will work for any Linux-like shell environment.
$ sudo apt-get install libensmallen-dev
Step 2: Downloading mlpack
we can download the latest version of mlpack from Github repository [1].
$ mkdir mlpack
$ cd /home/your_home/mlpack
$ git clone https://github.com/mlpack/mlpack
or we can download a specific version of mlpack from https://www.mlpack.org as follows:
Step 3: Preparing a directory for build and running cmake
$ cmake ../
Step 4: Compiling mlpack using make
The cmake command that was run in previous step will gnerate a Makefile. Now we can compile mlpack by running make as follows:
cmake
.. command fails, you are probably missing a dependency, so check the output and install any necessary librariesStep 5: Installing the compiled mlpack in filesystem
/usr/local/lib
and you may need to set the LD_LIBRARY_PATH
environment variable:// This simple program uses the mlpack::neighbor::NeighborSearch object // to find the nearest neighbor of each point in a dataset using the L1 metric, // and then print the index of the neighbor and the distance of it to stdout. #includeusing namespace mlpack; int main() { // Load the data from data.csv (hard-coded). Use CLI for simple command-line // parameter handling. arma::mat data("0.339406815,0.843176636,0.472701471; \ 0.212587646,0.351174901,0.81056695; \ 0.160147626,0.255047893,0.04072469; \ 0.564535197,0.943435462,0.597070812"); data = data.t(); // Use templates to specify that we want a NeighborSearch object which uses // the Manhattan distance. NeighborSearch nn(data); // Create the object we will store the nearest neighbors in. arma::Mat neighbors; arma::mat distances; // We need to store the distance too. // Compute the neighbors. nn.Search(1, neighbors, distances); // Write each neighbor and distance using Log. for (size_t i = 0; i < neighbors.n_elem; ++i) { std::cout << "Nearest neighbor of point " << i << " is point " << neighbors[i] << " and the distance is " << distances[i] << "." << std::endl; } return 0; }
Compiling the Example :
If we try to compile the above example by usual way of compiling c++ code as follows,
# g++ NeighborSearch.cpp
then we may probably end up with the following error :
We should edit the code to avoid this error.
Even after editing the code, it may end with a linker error like the one below:
We should compile the code correctly to avoid this failure.
After a successful compilation, it may end with the following screen outputs with some warnings :
Now, as usual, the output binary will be in the name “a.out”. So now we can run it as follows:
Installing ns-3 and Using mlpack in Network Simulations
Step 8: Download ns-3-dev Version
$ git clone https://gitlab.com/nsnam/ns-3-dev.git
Step 9: Configuring ns-3 with mlpack
Step 4 : Recompiling ns-3
So, now simply need to recompile ns-3 – thats all.
$ ./ns3
Now we can implement any machine learning algorithm using mlpack inside any ns-3 project by following the above steps. If you want to use the mlpack library in a ns-3 project or a particular application/protocol code, you should add the necessary things in the header file of the application/protocol.
If you are successfully compiling ns-3 with mlpack, then, as a simple case, you can use it in your ns-3 simulations as explained below:
Using mlpack in a ns-3 Simulation
#Neighbor_Search_Simulation.cpp" #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" #include#include using namespace mlpack; using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort using namespace mlpack::metric; // ManhattanDistance using namespace ns3; int main (int argc, char *argv[]) { NodeContainer nodes; nodes.Create (2);PointToPointHelper channel; channel.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); channel.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer netDevices; netDevices = channel.Install (nodes); . . . . . . . . . . . . . . . . . . . arma::mat data("0.339406815,0.843176636,0.472701471; \ 0.212587646,0.351174901,0.81056695; \ 0.160147626,0.255047893,0.04072469; \ 0.564535197,0.943435462,0.597070812"); data = data.t(); // Use templates to specify that we want a NeighborSearch object which uses // the Manhattan distance. NeighborSearch nn(data); // Create the object we will store the nearest neighbors in. arma::Mat neighbors; arma::mat distances; // We need to store the distance too. // Compute the neighbors. nn.Search(1, neighbors, distances); // Write each neighbor and distance using Log. for (size_t i = 0; i < neighbors.n_elem; ++i) { std::cout << "Nearest neighbor of point " << i << " is point " << neighbors[i] << " and the distance is " << distances[i] << "." << std::endl; } . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . return 0; Simulator::Run (); }
The above code segments highlight a simple way of directly using mlpack in a ns-3 simulation script itself.
$ ./ns3 run Neighbor_Search_Simulation
The following is the output of one such example “Neighbor_Search_Simulation.cpp” is based on the previously mentioned neighbour search implementation using mlpack– it is implemented inside a simple network simulation code of ns-3.
Conclusion
This article presents a simple way of using machine learning algorithms in ns-3 simulations using mlpack library. In this example, we only used a simple simulation script to test the working of the Neighbor Search algorithm of mlpack library. It is possible to design fully functional machine learning-based protocols and machine learning-related things in network protocol design. Or future articles may explain the way of designing one such ML-based network protocol using mlpack.
References
- https://github.com/mlpack/mlpack
- Curtin, Ryan R. and Edel, Marcus and Lozhnikov, Mikhail and Mentekidis, Yannis and Ghaisas, Sumedh and Zhang,Shangtong, “mlpack 3: a fast, flexible machine learning library”, Journal of Open Source Software, 2018, doi :10.21105/joss.00726, url :https://doi.org/10.21105/joss.00726
- https://www.mlpack.org/doc/mlpack-3.3.2/doxygen/build.html