# fuzzylite/fuzzylite

fuzzylite: a fuzzy logic control library in C++
C++ CMake Other
Latest commit 7af3913 Feb 18, 2017 jcrada committed on GitHub Merge pull request #71 from janisozaur/override
Mark hedge classes as final
 Failed to load latest commit information. examples Apr 1, 2015 fuzzylite Feb 17, 2017 .dockerignore Aug 13, 2016 .gitignore Aug 13, 2016 .travis.yml Aug 13, 2016 AUTHOR Jul 12, 2014 COPYING Jul 12, 2014 COPYING.LESSER Jul 12, 2014 ChangeLog Jul 1, 2015 Dockerfile Aug 13, 2016 INSTALL Jul 29, 2014 LICENSE Jul 12, 2014 NEWS Oct 27, 2014 README.md Dec 9, 2015 fuzzylite.png Jun 16, 2014

# fuzzylite ®

## A Fuzzy Logic Control Library in C++

Current version: 5.1

Next version: 6.0 (in progress)

NEWS

(9/Dec/2015) Check out the source code documentation for version 6.0 at
http://fuzzylite.github.io/fuzzylite/index.html

(01/Dec/2015) The branches in the repository have changed:

• the new release branch is the default branch and points to version 5.1
• the develop branch was merged into master
• the master branch contains work in progress
• the develop branch will be scheduled for deletion

The reason behind the new structure is due to some unexpected merges from develop into master, dating a few months ago.

Please, excuse me for any inconveniences the new structure may cause.

Cheers, Juan.

fuzzylite will always be free and open source. However, fuzzylite is no longer released under the Apache License. Since version 5.0, fuzzylite is dual-licensed under the GNU Lesser General Public License (LGPL) v3.0 and a paid commercial license.

In brief, an important restriction the LGPL imposes on your closed-source application is that you are no longer allowed to statically link against fuzzylite. If your application requires static linking, you will need to purchase a commercial license from FuzzyLite Limited. Please, contact sales@fuzzylite.com for commercial licenses, and refer to the GNU LGPL for further information on your rights.

The change of license is an attempt to raise funds in order to be able to work part-time in the development of the fuzzylite family of products, namely fuzzylite (C++), jfuzzylite (Java), pyfuzzylite (Python), and QtFuzzyLite (Windows/Linux/Mac).

There are still many things to do!

Besides donations, you can significantly contribute by purchasing a license of the entirely new QtFuzzyLite commercial application. In addition, if you require (paid) private support, please contact jcrada@fuzzylite.com.

### Introduction

fuzzylite is a free and open-source fuzzy logic control library programmed in C++ for multiple platforms (Windows, Linux, Mac, iOS). Its goal is to allow you to easily create fuzzy logic controllers in a few steps utilizing object-oriented programming without requiring any third-party libraries.

#### Reference

If you are using fuzzylite, please cite the following reference in your article:

Juan Rada-Vilela. fuzzylite: a fuzzy logic control library, 2014. URL http://www.fuzzylite.com.

 @misc{fl::fuzzylite,
title={fuzzylite: a fuzzy logic control library},
url={http://www.fuzzylite.com},
year={2014}}


### Features

Controllers Types (5) Mamdani, Takagi-Sugeno, Larsen, Tsukamoto, Inverse Tsukamoto

Linguistic terms Basic (4) triangle, trapezoid, rectangle, discrete. Extended (9) bell, cosine, gaussian, gaussian product, pi-shape, sigmoid difference, sigmoid product, spike. Edges (4) concave, ramp, sigmoid, s-shape, z-shape. Functions (3) constant, linear, function.

Conjunction and Activation T-Norm (7) minimum, algebraic product, bounded difference, drastic product, einstein product, hamacher product, nilpotent minimum.

Disjunction and Accumulation S-Norm (8) maximum, algebraic sum, bounded sum, normalized sum, drastic sum, einstein sum, hamacher sum, nilpotent maximum.

Defuzzifiers Integral (5) centroid, bisector, smallest of maximum, largest of maximum, mean of maximum, Weighted (2) weighted average, weighted sum.

Hedges Types (6) any, not, extremely, seldom, somewhat, very.

Import Types (3) FuzzyLite Language fll, Fuzzy Inference System fis, Fuzzy Control Language fcl.

Export Types (6) C++, Java, FuzzyLite Language fll, FuzzyLite Dataset fld, Fuzzy Inference System fis, Fuzzy Control Language fcl.

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

### Example

#include "fl/Headers.h"

int main(int argc, char* argv[]){
using namespace fl;
Engine* engine = new Engine("simple-dimmer");

InputVariable* ambient = new InputVariable;
ambient->setName("Ambient");
ambient->setRange(0.000, 1.000);

OutputVariable* power = new OutputVariable;
power->setName("Power");
power->setRange(0.000, 2.000);
power->setDefaultValue(fl::nan);

RuleBlock* ruleblock = new RuleBlock;
ruleblock->addRule(Rule::parse("if Ambient is DARK then Power is HIGH", engine));
ruleblock->addRule(Rule::parse("if Ambient is MEDIUM then Power is MEDIUM", engine));
ruleblock->addRule(Rule::parse("if Ambient is BRIGHT then Power is LOW", engine));

engine->configure("", "", "Minimum", "Maximum", "Centroid");

std::string status;
"The following errors were encountered:\n" + status, FL_AT);

for (int i = 0; i < 50; ++i){
scalar light = ambient->getMinimum() + i * (ambient->range() / 50);
ambient->setInputValue(light);
engine->process();
FL_LOG("Ambient.input = " << Op::str(light) << " -> " <<
"Power.output = " << Op::str(power->getOutputValue()));
}
}

### What's Next?

• Source code documentation
• Type-2 Fuzzy Logic Controllers
• Adaptive Neuro-Fuzzy Inference System (ANFIS)
• Fuzzy C-means data clustering

### Building from Source

Building from source requires you to have CMake installed.

The files fuzzylite/build.bat and fuzzylite/build.sh are automatic build scripts for Windows and Unix platforms, respectively. The usage of these scripts is presented as follows.

#### Windows

> build.bat help
Usage:  build.bat [options]
where   [options] can be any of the following:
all          builds fuzzylite in debug and release mode (default)
debug        builds fuzzylite in debug mode
release      builds fuzzylite in release mode
clean        erases previous builds
help         shows this information

#### Integral Defuzzifiers

• (important) Proper handling of indefinite integral defuzzification, that is, returning fl::nan when [minimum|maximum]=[NaN|Inf].
• Default resolution of integration is defined as static int IntegralDefuzzifier::defaultResolution=200, and can be changed via static IntegralDefuzzifier::setDefaultResolution().
• (important) In fuzzylite, the accumulation operator has been for several versions associated with the output variables and not with the rule blocks, despite that the FCL format and other fuzzy logic control libraries associate the accumulation operator with the rule blocks. The argument for such a decision is that fuzzylite provides coherent support for multiple rule blocks operating on the same engine and on the same output variables. For example, if multiple rule blocks operate on the same output variables, it only makes sense to have a single accumulation operator associated with each output variable such that the defuzzifier can naturally operate over the accumulated fuzzy output. Differently, if the accumulation operator were associated with the rule block, the possibility of having different accumulation operators in different rule blocks questions (1) the possibility of having multiple rule blocks operating over the same output variables; and (2) the usage of different accumulation operators over the accumulation and defuzzification processes. Certainly, if (1) is not possible, i.e, different rule blocks only operate on different output variables, then (2) is not a problem because the accumulation process and defuzzification of each variable will only have a single accumulation operator. It is therefore that the association of the accumulation operator with the output variable in fuzzylite provides a better design and an additional feature that allows having multiple rule blocks operating over the same output variables.
• Added copy constructors, assignment operators, move constructors and move operators.
• Added method Defuzzifier::clone().

#### Importers and Exporters

• (EXTREMELY important) Since terms have a new height property, [Fll|Fis|Fcl]Exporter exports terms with an additional scalar at the end, which indicates the height of the term. However, if height=1.0, the additional scalar is not exported.
• (EXTREMELY important) In [Fll|Fis|Fcl]Importer, when importing terms, if there is an additional scalar it will be assumed as the height of the term. For example, term: high Gaussian 1.0 0.5 0.75 will create a Gaussian term with mean 1.0, standard deviation 0.5 and height 0.75. This is extremely important because there are some examples from Matlab in fis format that append a useless 0.0 to some terms.
• (EXTREMELY important) In FisExporter, if the Takagi-Sugeno controller has no activation or accumulation operators (as it should generally be the case), Octave and Matlab will not be able to import the fis file. To overcome this issue, you will have to set ImpMethod="min" and AggMethod="max", where ImpMethod and AggMethod are just dummy operators that can be set to any TNorm and SNorm, respectively.
• (important) Improved compatibility of the exported code obtained with [Fis|Fcl]Exporter by exporting the additional features of fuzzylite only when these are different from the default operation. For example, the following features will not be exported given their values: [Input|Output]Variable::enabled = true;, OutputVariable::lock-previous = false;, OutputVariable::lock-range = false;, amongst others.
• (important) Renamed FLL property 'lock-valid' to 'lock-previous'.
• (important) Renamed FIS property 'LockValid' to 'LockPrevious'.
• (important) Renamed FCL property 'LOCK: VALID' to 'LOCK: PREVIOUS'.
• (important) Export your controllers to files using [Fll|Fld|Fis|Fcl]Exporter::toFile().
• (important) Import your controllers from files using [Fll|Fis|Fcl]Importer::fromFile().
• (important) FldExporter exports the FuzzyLite Dataset of an engine utilizing the input values of another FuzzyLite Dataset.
• FldExporter no longer restarts the engine when exporting.
• (important) Renamed method FldExporter::toWriter() to FldExporter::write().
• Removed variable and methods for property int FldExporter::_maximum.
• Added option in CppExporter to prepend the namespace prefix fl:: to the classes, and by default it does not prepend prefix.
• Improvement accuracy of FisImporter when importing fis files whose scalar values have more than three decimal numbers.
• Renamed methods in [Fis|Fcl]Importer::extract* to [Fis|Fcl]Importer::parse*.

#### Factories

• Created a generic CloningFactory<T> to create clones of objects.
• (important) Created FunctionFactory based on CloningFactory<Function::Element> where function operators and methods are stored to be cloned as necessary by Function. Additional functions and operators can be easily registered.
• (VERY important) Moved built-in functions and operators from Function to FunctionFactory.
• Renamed methods Factory<T>::[register|deregister]Class() to Factory<T>::[register|deregister]Constructor().
• (important) Renamed Factory<T> to ConstructionFactory<T>.
• Renamed typedef Factory::Creator to typedef Factory::Constructor.
• Changed pointers of FactoryManager to smart pointers (FL_unique_ptr).

#### Examples

• (important) Added two examples for basic rule chaining: mamdani/SimpleDimmerInverse.fll and mamdani/Laundry.fll.
• Included the original example files in fis format.
• Added conversion of examples/original/*.fis to examples/original/*.fll.
• Modified original/takagi-sugeno examples to reflect activation: none; accumulation: none;.
• Updated FLD examples produced from the original examples.
• (important) Added file fuzzylite/src/m/compare.m to compare the output values of your fuzzylite engines with the evaluation of the same engine in Octave/Matlab.
• (important) Added file examples/examples.mat containing the comparison of the output values between fuzzylite and Matlab's Fuzzy Logic Toolbox.
• Added code to perform benchmarks in Linux.

#### Console

• (important) Console includes option to import custom input dataset from file an export its respective output values.
• (important) Created the FuzzyLite Interactive Console, which can be started by specifying an input file and the output format, e.g., fuzzylite -i SimpleDimmer.fll -of fld.

#### Fixes Bugs and Leaks

• (important) Fixed operation when using multiple hedges to operate from right-most to left-most, e.g. if Ambient is not very extremely bright evaluates as follows not(very(extremely(bright))).
• (important) Fixed membership functions of specific cases of Triangle when a=b or b=c, and Trapezoid when a=b or c=d.
• Fixed minor memory leak at ~RuleBlock::[conjunction|disjunction|activation].
• Fixed minor memory leak at ~Accumulated::accumulation.
• Fixed minor memory leak at ~OutputVariable::defuzzifier.
• Fixed minor memory leak at ~Function::Node.
• Fixed minor memory leak at ~FactoryManager::[factories].
• Fixed some rethrowing of exceptions to provide proper information about errors. Specifically, changed some rethrown exceptions from throw ex; to just throw;
• Fixed building using -DFL_USE_FLOAT=ON