Skip to content

Secrets of Using Fuzzy Logic in ns-2 using Fyzzylite Library

Why Software like Matlab should not be used for Designing and Testing a Network Protocol?

The choice of using Matlab or similar tools for a Network Protocol-related research is not good, but some of the previous ‘fuzzy logic” based publications were based on Matlab only because of their inability to code it under good open-source simulators such as ns-2, ns-3 and Omnet++.

Simulating “complex, networking aspects” under Matlab is not possible and will not be accurate while comparing it with ns-2 or ns-3.

Of course, one can use Matlab for doing research related to “signal processing” components of a typical network – but not to do a research on the entire network at all. For example, if one wishes to test the properties of a new propagation model, they may do some prototype on Matlab – but if they wish to test it in a realistic network environment, they should at least code it under a good simulator such as ns-3 and test it in with a realistic network scenario.

Of course, Matlab has a rich Fuzzy Logic toolbox that can be used for any FLS-based research – but not definitely for a work which will address “complex, Networking aspects” of a typical research (such as routing, transport, scheduling etc.,).

So, if one wishes to implement a fuzzy logic based routing algorithm or transport algorithm, then they should code it at least under a good network simulator such as ns-3. Implementing a “FL based routing logic” as a “stand-alone” piece of software using Matlab will never become equal to that of (genuinely) implementing the same under a realistic network simulator such as ns-3.

There is an excellent, state-of-the-art C++ library called Fuzzylite that can be integrated/used with ns-2 or ns-3 or even on any network simulator. You may check it under the following link.
https://www.fuzzylite.com/

Of course, it will require some hard work to use fuzzylite under ns-2 or ns-3 – but it will be worth the outcome of genuine, quality results that one will get in the end.

So, For example, if your UWSN research is about implementing a “network-related protocol”, then learn ns-2/ns-3, learn aquasim extension of ns-2/ns-3 and learn to use fuzzylite under ns-2/ns-3.  Because, there is no other “simple”, “genuine” way of doing an UWSN research.

Note: This article assumes that we are going to use fuzzylite in a UWSN simulation. 

Fuzzylite

Fuzzylite[1] is an excellent, rich library that contains almost all the concepts of fuzzy logic that you may see in a text book.

As presented in [1], the FuzzyLite library have the following features:

Controllers: Mamdani, Takagi-Sugeno, Larsen, Tsukamoto, Inverse Tsukamoto, Hybrids

Linguistic terms:

Basic: triangle, trapezoid, rectangle, discrete.
Extended:  bell, cosine, gaussian, gaussian product, pi-shape, sigmoid difference, sigmoid product, spike.
Edges:  binary, concave, ramp, sigmoid, s-shape, z-shape.
Functions: constant, linear, function.

Activation methods: general, proportional, threshold, first, last, lowest, highest.

Conjunction and Implication (T-Norms): minimum, algebraic product, bounded difference, drastic product, einstein product, hamacher product, nilpotent minimum, function.

Disjunction and Aggregation (S-Norms): maximum, algebraic sum, bounded sum, drastic sum, einstein sum, hamacher sum, nilpotent maximum, normalized sum, unbounded sum, function.

Defuzzifiers:

Integral: centroid, bisector, smallest of maximum, largest of maximum, mean of maximum.
Weighted: weighted average, weighted sum.

Hedges: any, not, extremely, seldom, somewhat, very, function.

Importers: FuzzyLite Language fll, Fuzzy Inference System fis, Fuzzy Control Language fcl.

Exporters: C++, Java, FuzzyLite Language fll, FuzzyLite Dataset fld, R script, Fuzzy Inference System fis, Fuzzy Control Language fcl.

Examples:  for Mamdani, Takagi-Sugeno, Tsukamoto, and Hybrid controllers from fuzzylite, Octave, and Matlab, each included in the following formats: C++, Java, fll, fld, R, fis, and fcl.

In addition, we can easily:

  • Create your own classes inheriting from fuzzylite, register them in the factories, and incorporate them to operate in fuzzylite.
  • Utilize multiple rule blocks within a single engine, each containing any number of (possibly weighted) rule, and different conjunction, disjunction and activation operators.
  • Write inference rules just naturally, e.g., “if obstacle is left then steer is right”.
  • Return a default output value, lock the output values to be within specific ranges, lock the previous valid output value when no rules are activated.
  • Explore the function space of your controller.
  • Utilize the entire library across multiple threads as it is thread-safe.
  • Download the sources, documentation, and binaries for the major platforms in the Downloads tab.

Problems in using Fuzzylite with ns-2

If you search the internet with the question “how to use Fuzzylite with ns-2?” – google will provide some answer text which only contain the same question in a different form.  Or will only point you to this article, Or it may take you to some ns-3 related articles. Probably you will not find an answer anywhere on the internet.

The reason is:  ns-2 is an old piece of software that will not get compiled with the latest gcc/g++ compilers.  Basically one should face a lot of troubles while trying to compile ns-2 on the latest machines with latest OS versions(of Linux).

On the other hand,  it will be easy to compile the Fuzzylite library on c++11 or higher standard compilers which is commonly found on the latest Linux distributions.

Even it is easy to incorporate Fuzzylite with ns-3 through waf based compile process[2]. We already presented a complex procedure to incorporate Fuzzylite with dev version of ns-3 which only will use cmake.   Our previous article [3] explained that procedure.

Secrets of Using Fuzzy Logic in ns-3 using Fyzzylite Library

Trying to compile and use Fuzzylite in ns-2 will lead to the following challenges:

  1. How we are going to compile the latest version of Fuzzylite on a decade-old operating system like Ubuntu 16.04 (in which only an old version of ns-2 get compiled easily) ?
  2. How we are going to compile ns-2 (which expects an old compiler) with Fuzzylite (which expects the latest compiler such as C++11)?

And most importantly, I  am going to use Fuzzylite on an old ns-2 version ns-2.30. The reason is : that some of the very good extensions like “aquasim” can be compiled easily on ns-2.30. Further, if we need to use a 3D visualization tool Aqua3D, then it can be compiled only on an old Linux system.

So, before proceeding to Fuzzylite compile process, we have to first install the suitable Linux distribution and install an old ns-2 version in it (for example aquasim is nothing but a modified version of ns-2.30 – so we can use that for out experiments).

The following article explains the way of installing a 64bit Lubuntu 16.04 LTS on VirtualBox:

Installing an old Linux under VirtualBox and using Shared Folders.

The following article explains the way of installing a aquasim (ns-2.30) and aqua3D (that can be only compiled on a very old version of gcc compiler)

 

Installing Aquasim & Aqua3D on an old Linux under VirtualBox

 

So before proceeding with the installation of Fuzzylite, we have to complete the above two (in fact thee: ubuntu, aquasim and aqua3D)  installation procedures. After that only we come to the even harder part of the installation – integrating Fuzzylite with ns-2.

Yes.  It is really a strange and complex task.

 

If you already completed the above two installation procedures, then you may proceed with the installation of Fuzzylite.

 

Installing Fuzzylite

Step 1:  Downloading Fuzzylite

One may download source and binary versions of Fuzzylite from [4] or download the source version from [5].

In this procedure, I used the Fuzzylite version from [5].

$ cd /home/your_home
$ git clone https://github.com/fuzzylite/fuzzylite

Step 2:  Compiling Fuzzylite

After downloading the latest version of Fuzzylite from [5], you can try to compile it as explained in the standard procedure that you may see at [5].

we may try to compile it as follows:

 

$ cd /home/your_home/fuzzylite/fuzzylite
$ bash ./build.sh

Generally, the above command will compile debug and release the version of the Fuzzylite library if you are using a higher version of a compiler such as c++11.  But on our decade old Linux system, simply it will end up with the following Error:

 

 

I found a trick to compile Fuzzylite on our old OS. I found this solution only on a trial and error basis – so it may not be a “state of the art” technique or standard way of doing it.

Still, there may be some elegant way of compiling Fuzzylite on older systems – but I didn’t find any such solution on the Internet.

The following section explains my simple and dirty trick:

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
 

Now “hopefully” it will get compiled correctly and you will see the following screen at the successful end of the compile process.

 

Even though it is getting compiled successfully (or not), you have to manually install the compiled binaries. So, now I am trying to automate it 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
 

Step 3:  Testing the good working of Fuzzylite

After installing Fuzzylite, before trying to use it with ns-2, we have to test the good working status of the library installation by using it in a simple CPP program.

The following “ObstacleAvoidance.cpp” is one such sample program provided at [1]. If we run this program, the for loop will generate 50 locations and will make fuzzy decisions using fuzzy logic.

//File: ObstacleAvoidance.cpp
#include “fl/Headers.h”

int main(int argc, char* argv[]){

using namespace fl;

Engine* engine = new Engine;
engine->setName(“ObstacleAvoidance”);
engine->setDescription(“”);

InputVariable* obstacle = new InputVariable;
obstacle->setName(“obstacle”);
obstacle->setDescription(“”);
obstacle->setEnabled(true);
obstacle->setRange(0.000, 1.000);
obstacle->setLockValueInRange(false);
obstacle->addTerm(new Ramp(“left”, 1.000, 0.000));
obstacle->addTerm(new Ramp(“right”, 0.000, 1.000));
engine->addInputVariable(obstacle);

OutputVariable* mSteer = new OutputVariable;
mSteer->setName(“mSteer”);
mSteer->setDescription(“”);
mSteer->setEnabled(true);
mSteer->setRange(0.000, 1.000);
mSteer->setLockValueInRange(false);
mSteer->setAggregation(new Maximum);
mSteer->setDefuzzifier(new Centroid(100));
mSteer->setDefaultValue(fl::nan);
mSteer->setLockPreviousValue(false);
mSteer->addTerm(new Ramp(“left”, 1.000, 0.000));
mSteer->addTerm(new Ramp(“right”, 0.000, 1.000));
engine->addOutputVariable(mSteer);

RuleBlock* mamdani = new RuleBlock;
mamdani->setName(“mamdani”);
mamdani->setDescription(“”);
mamdani->setEnabled(true);
mamdani->setConjunction(fl::null);
mamdani->setDisjunction(fl::null);
mamdani->setImplication(new AlgebraicProduct);
mamdani->setActivation(new General);
mamdani->addRule(Rule::parse(“if obstacle is left then mSteer is right”, engine));
mamdani->addRule(Rule::parse(“if obstacle is right then mSteer is left”, engine));
engine->addRuleBlock(mamdani);

std::string status;

if (not engine->isReady(&status))
throw Exception(“[engine error] engine is not ready:n” + status, FL_AT);

for (int i = 0; i <= 50; ++i){

scalar location = obstacle->getMinimum() + i * (obstacle->range() / 50);
obstacle->setValue(location);
engine->process();
FL_LOG(“obstacle.input = ” << Op::str(location) <<
” => ” << “steer.output = ” << Op::str(mSteer->getValue()));

}

}

If you compile the code using gcc or g++,  then you may end up with an error like the following one:

or you may see the following error(if you are using any higher version of gcc):

To successfully run the code, you have to do the following two things:

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
 

The above command will successfully compile the code, and we will have the binary as “a.out”.

So now we can run this example as follows:

 

#if needed, change to the folder where the’ a.out’ is stored
$ cd /home/your_home/
$ ./a.out
The above command will simply run the demo and will end up with the following outputs:

Using Fuzzylite in a ns-2 Project

On internet, we may see some of the complex ways of using a shared library under ns-2,  using procedures like the one explained in [3]. We need not do that many of complex changes in ns-2 files to make it work.
The following section explains a simple and easy way of integrating Fuzzylite with ns-2 and designing any fuzzy logic based network protocol under ns-2.
Now you can use the functions of Fuzzylite library from  within any ns-2 project just by adding the necessary header files and using the appropriate namespace as follows :

#include “fl/Headers.h”

using namespace fl;

Engine* engine = new Engine;

//Now configure fuzzy sets (inputs and outpus), rules

. . . . .

. . . . .

// check whether the engine is ready after the above settings
std::string status;
if (not engine->isReady(&status))
throw Exception(“[engine error] engine is not ready:n” + status, FL_AT);

//Give a crisp input andget a crisp output
AAAA->setValue(aaa);
engine->process();

. . . . .

. . . . .

//Make use of the output values

Now we can implement fuzzy logic inside any ns-2 project by following the above steps.

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
 

The following is the output of one such example programme based on the inputs and outputs of  “ObstacleAvoidance.cpp” –  but it is implemented inside a network protocol of ns-2.

In the above output, each line is created at a packet receive function of a typical protocol layer and every line corresponds to one received packet. So now it is possible to use Fuzzylite under UWSN simulation or any kind of network simulation.

References

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
 

WhatsApp Discuss Through WhatsApp