Implementing machine learning functions in ns-3 using mlpack
  QXbgObrvFwNo 2023年11月19日 66 0

https://www.projectguideline.com/implementing-machine-learning-functions-in-ns-3-using-mlpack/

 

Article Written by: CharlesPandian

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss Through WhatsApp

January 13, 2023

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.

Implementing machine learning functions in ns-3 using mlpack_ML_02

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 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!

  

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.  

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss 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

Implementing machine learning functions in ns-3 using mlpack_ML_04

 

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

 

 

Implementing machine learning functions in ns-3 using mlpack_ci_05

 

 

 

$ tar -xvzpf mlpack-3.3.2.tar.gz

Implementing machine learning functions in ns-3 using mlpack_ci_06

 

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:

 

Implementing machine learning functions in ns-3 using mlpack_ci_07

 

At the successful run of cmake, it will end as follows:

Implementing machine learning functions in ns-3 using mlpack_ML_08

 

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:

Implementing machine learning functions in ns-3 using mlpack_ci_09

 

And at the successful make, it will end like this:

Implementing machine learning functions in ns-3 using mlpack_ci_10

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!

  

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.  

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss Through WhatsApp

 

 

 

 

 

A typical install process will start like this :

Implementing machine learning functions in ns-3 using mlpack_ci_12

 

and will end like this:

Implementing machine learning functions in ns-3 using mlpack_App_13

 

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

 

Implementing machine learning functions in ns-3 using mlpack_App_14

 

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]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40


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

 

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<nearestneighborsort, manhattandistance=""> nn(data);

 

// Create the object we will store the nearest neighbors in.

arma::Mat<size_t> 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;

}

 

</size_t></nearestneighborsort,></mlpack.hpp>


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 :

Implementing machine learning functions in ns-3 using mlpack_ci_15

We should edit the code to avoid this error.

 

Hidden Section!

  

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.  

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss Through WhatsApp

 

 

 

 

Even after editing the code,  it may end with a linker error like the one below:

Implementing machine learning functions in ns-3 using mlpack_App_17

 

We should compile the code correctly to avoid this failure.

 

Hidden Section!

  

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.  

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss Through WhatsApp

 

 

 

After a successful compilation, it may end with the following screen outputs with some warnings :

Implementing machine learning functions in ns-3 using mlpack_ML_19

Now, as usual, the output binary will be in the name “a.out”. So now we can run it as follows:

Implementing machine learning functions in ns-3 using mlpack_ML_20

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:

Implementing machine learning functions in ns-3 using mlpack_ML_21

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

Implementing machine learning functions in ns-3 using mlpack_ML_22

 

Hidden Section!

  

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.  

Implementing machine learning functions in ns-3 using mlpack_ML

Discuss 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 <mlpack core.hpp="">
#include <mlpack methods="" neighbor_search="" neighbor_search.hpp="">
 
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<nearestneighborsort, manhattandistance=""> nn(data);
 
// Create the object we will store the nearest neighbors in.
arma::Mat<size_t> 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 ();
}
 
</size_t></nearestneighborsort,></mlpack></mlpack>


 

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.

Implementing machine learning functions in ns-3 using mlpack_ML_24

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



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
QXbgObrvFwNo