Skip to content

Implementing machine learning functions in ns-3 using mlpack

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.

Installing ns3-gym AI Model under Chroot Jail

Installing ns3-ai Model under “chroot Jail”

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 libarmadillo-dev
$ sudo apt-get install libensmallen-dev
mlpack is available in the repositories of many Linux distributions and so it may be easier to use the package manager for your system. For example, on Ubuntu, we can install mlpack with the following command and directly skip to the step 6 of this article:

Hidden Section! Contact Charles  

The key/passphrase will be given once you have been approved for getting paid research support/assistance from Charles. To get paid support, you may start a 'free' research discussion.  

WhatsApp chatDiscuss Through WhatsApp
 

Step 2:  Downloading mlpack

we can download the latest version of mlpack from Github repository  [1].

$ cd /home/your_home/
$ 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:

$ wget https://www.mlpack.org/files/mlpack-3.3.2.tar.gz

 

$ tar -xvzpf mlpack-3.3.2.tar.gz

 

Step 3: Preparing a directory for build and running cmake

$ mkdir mlpack-3.3.2/build && cd mlpack-3.3.2/build
$ cmake ../
The cmake will start running as follows:
At the successful run of cmake, it will end as follows:

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:

$ make -j6 # The -j is the number of cores you want to use for a build.
A typical make on mlpack will start like this:
And at the successful make, it will end like this:
If the cmake .. command fails, you are probably missing a dependency, so check the output and install any necessary libraries

Step 5: Installing the compiled mlpack in filesystem

$ sudo make install
Sometimes you may need to uninstall previously existing mlpack. In that case, you may automate the uninstall as follows:

Hidden Section! Contact Charles  

The key/passphrase will be given once you have been approved for getting paid research support/assistance from Charles. To get paid support, you may start a 'free' research discussion.  

WhatsApp chatDiscuss Through WhatsApp
 

A typical install process will start like this :
and will end like this:
This will install mlpack in the Linux filesystem; so that it can be accessed by any user/user program. We can check the installation of mlpack headers and the library by using “whereis” command
Step 6: Setting LD_LIBRARY_PATH
On Linux systems, mlpack will install by default to /usr/local/lib and you may need to set the LD_LIBRARY_PATH environment variable:
$ export LD_LIBRARY_PATH=/usr/local/lib
Step 7: Using mlpack by running an example :
We can now run the following example that was available at the home page of [3]

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

#include 

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

Hidden Section! Contact Charles  

The key/passphrase will be given once you have been approved for getting paid research support/assistance from Charles. To get paid support, you may start a 'free' research discussion.  

WhatsApp chatDiscuss Through WhatsApp
 

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.

Hidden Section! Contact Charles  

The key/passphrase will be given once you have been approved for getting paid research support/assistance from Charles. To get paid support, you may start a 'free' research discussion.  

WhatsApp chatDiscuss Through WhatsApp
 

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

Download a working copy of the ns-3-dev repository as follows:
Here, we assume that the current dev version is equal to ns-3.35. So in future, you may need to install the appropriate version of ns-3 which is compatible with nr and nr-u modules.
$ cd /home/your_home/
$ git clone https://gitlab.com/nsnam/ns-3-dev.git
The following screenshot shows the successful clone operation:

Step 9: Configuring ns-3 with mlpack

If you already have an ns-3-dev installation or any earlier installation with  CMake support, then you can easily make the mlpack library accessible from any ns-3 project under the ns-3 folder just by reconfiguring the CMake compile system
The following screenshot shows a successful ‘configure’ of ns-3

Hidden Section! Contact Charles  

The key/passphrase will be given once you have been approved for getting paid research support/assistance from Charles. To get paid support, you may start a 'free' research discussion.  

WhatsApp chatDiscuss Through WhatsApp
 

Step 4 : Recompiling ns-3

So, now simply need to recompile ns-3 – thats all.

$ cd /home/your_home/ ns-3-dev
$  ./ns3
After a successful recompile, the above command will simply make the mlpack library accessible from all the projects of ns-3 .

 

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

Now you can use the machine learning functions of mlpack library from  within any ns-3 project just by adding the necessary header files and using the appropriate namespace. The following example is a simple illustration which is directly incorporating the above-mentioned neighbor search example inside a ns-3 network simulation script.
#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.

It is possible to use the required machine learning functions of mlpack in the design of different layers of network protocols.

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

  1. https://github.com/mlpack/mlpack
  2. 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
  3. https://www.mlpack.org/doc/mlpack-3.3.2/doxygen/build.html
WhatsApp Discuss Through WhatsApp