Soletta machine learning
Machine learning for IoT devices
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
Data Structures | Macros | Enumerations | Functions
Fuzzy engine

The SML naive fuzzy logic engine. More...

Data Structures

struct  sml_fuzzy_term
 

Macros

#define SML_TERM_NAME_MAX_LEN   (127)
 

Enumerations

enum  sml_fuzzy_defuzzifier {
  SML_FUZZY_DEFUZZIFIER_BISECTOR, SML_FUZZY_DEFUZZIFIER_CENTROID, SML_FUZZY_DEFUZZIFIER_LARGEST_OF_MAXIMUM, SML_FUZZY_DEFUZZIFIER_MEAN_OF_MAXIMUM,
  SML_FUZZY_DEFUZZIFIER_SMALLEST_OF_MAXIMUM, SML_FUZZY_DEFUZZIFIER_WEIGHTED_AVERAGE, SML_FUZZY_DEFUZZIFIER_WEIGHTED_SUM
}
 The types of defuzzifier. More...
 
enum  sml_fuzzy_snorm {
  SML_FUZZY_SNORM_ALGEBRAIC_SUM, SML_FUZZY_SNORM_BOUNDED_SUM, SML_FUZZY_SNORM_DRASTIC_SUM, SML_FUZZY_SNORM_EINSTEIN_SUM,
  SML_FUZZY_SNORM_HAMACHER_SUM, SML_FUZZY_SNORM_MAXIMUM, SML_FUZZY_SNORM_NILPOTENT_MAXIMUM, SML_FUZZY_SNORM_NORMALIZED_SUM
}
 SNorm rules are also known as accumulation. More...
 
enum  sml_fuzzy_tnorm {
  SML_FUZZY_TNORM_ALGEBRAIC_PRODUCT, SML_FUZZY_TNORM_BOUNDED_DIFFERENCE, SML_FUZZY_TNORM_DRASTIC_PRODUCT, SML_FUZZY_TNORM_EINSTEIN_PRODUCT,
  SML_FUZZY_TNORM_HAMACHER_PRODUCT, SML_FUZZY_TNORM_MINIMUM, SML_FUZZY_TNORM_NILPOTENT_MINIMUM
}
 TNorm rules are also known as conjunction. More...
 

Functions

bool sml_fuzzy_conjunction_set (struct sml_object *sml, enum sml_fuzzy_tnorm norm)
 Set the conjunction fuzzy rule. More...
 
struct sml_objectsml_fuzzy_new (void)
 Creates a SML fuzzy engine. More...
 
bool sml_fuzzy_output_set_accumulation (struct sml_object *sml, struct sml_variable *sml_variable, enum sml_fuzzy_snorm accumulation)
 Set the output accumulation. More...
 
bool sml_fuzzy_output_set_defuzzifier (struct sml_object *sml, struct sml_variable *sml_variable, enum sml_fuzzy_defuzzifier defuzzifier, int defuzzifier_resolution)
 Set the defuzzifier for an output variable. More...
 
bool sml_fuzzy_set_simplification_disabled (struct sml_object *sml, bool disabled)
 Disable/Enable simplification. More...
 
bool sml_fuzzy_set_variable_terms_auto_balance (struct sml_object *sml, bool variable_terms_auto_balance)
 Set terms autobalance. More...
 
bool sml_fuzzy_supported (void)
 Check if SML was built with fuzzy support. More...
 
struct sml_fuzzy_termsml_fuzzy_variable_add_term_cosine (struct sml_object *sml, struct sml_variable *variable, const char *name, float center, float width)
 Add a cosine term for a variable. More...
 
struct sml_fuzzy_termsml_fuzzy_variable_add_term_gaussian (struct sml_object *sml, struct sml_variable *variable, const char *name, float mean, float standard_deviation)
 Add a gaussian term for a variable. More...
 
struct sml_fuzzy_termsml_fuzzy_variable_add_term_ramp (struct sml_object *sml, struct sml_variable *variable, const char *name, float start, float end)
 Add a ramp term for a variable. More...
 
struct sml_fuzzy_termsml_fuzzy_variable_add_term_rectangle (struct sml_object *sml, struct sml_variable *variable, const char *name, float start, float end)
 Add a rectangle term for a variable. More...
 
struct sml_fuzzy_termsml_fuzzy_variable_add_term_triangle (struct sml_object *sml, struct sml_variable *variable, const char *name, float vertex_a, float vertex_b, float vertex_c)
 Add a triangle term for a variable. More...
 
float sml_fuzzy_variable_get_default_term_width (struct sml_object *sml, struct sml_variable *sml_variable)
 Get the default term width used by fuzzy to create terms. More...
 
bool sml_fuzzy_variable_get_is_id (struct sml_object *sml, struct sml_variable *sml_variable)
 Check if this variable is used as an id field. More...
 
bool sml_fuzzy_variable_remove_term (struct sml_object *sml, struct sml_variable *variable, struct sml_fuzzy_term *term)
 Remove a fuzzy term from the engine. More...
 
bool sml_fuzzy_variable_set_default_term_width (struct sml_object *sml, struct sml_variable *sml_variable, float width)
 Set the default term width used by fuzzy to create terms. More...
 
bool sml_fuzzy_variable_set_is_id (struct sml_object *sml, struct sml_variable *sml_variable, bool is_id)
 Check if this variable is used as an id field. More...
 
bool sml_is_fuzzy (struct sml_object *sml)
 Check if the SML object is a fuzzy engine. More...
 
bool sml_set_rule_weight_threshold (struct sml_object *sml, float weight_threshold)
 Rules below a given value will be ignored. More...
 

Detailed Description

The SML naive fuzzy logic engine.

The SML may use a modified fuzzy engine to predict values. The main difference from a normal fuzzy engine, is that the SML engine will create the fuzzy rules by itself. A good start to know more about fuzzy logic is visiting https://github.com/solettaproject/soletta/wiki/Soletta-Machine-Learning#fuzzy-logic

Macro Definition Documentation

#define SML_TERM_NAME_MAX_LEN   (127)

Maximum size of terms name

Enumeration Type Documentation

The types of defuzzifier.

See Also
sml_fuzzy_output_set_defuzzifier
Enumerator
SML_FUZZY_DEFUZZIFIER_BISECTOR 

The point that divide the curve into two equal sub-regions

SML_FUZZY_DEFUZZIFIER_CENTROID 

The center of the area under the curve

SML_FUZZY_DEFUZZIFIER_LARGEST_OF_MAXIMUM 

The largest value of the maximum degrees of membership

SML_FUZZY_DEFUZZIFIER_MEAN_OF_MAXIMUM 

The mean of the maximum degrees of membership

SML_FUZZY_DEFUZZIFIER_SMALLEST_OF_MAXIMUM 

The smallest value of the maximun degrees of membership

SML_FUZZY_DEFUZZIFIER_WEIGHTED_AVERAGE 

The average of the activation degrees multiplied by a weight

SML_FUZZY_DEFUZZIFIER_WEIGHTED_SUM 

The sum of the activation degrees multiplied by a weight

SNorm rules are also known as accumulation.

See Also
sml_fuzzy_output_set_accumulation
Enumerator
SML_FUZZY_SNORM_ALGEBRAIC_SUM 

A or B -> A+B - (A*B)

SML_FUZZY_SNORM_BOUNDED_SUM 

A or B -> min(A+B,1)

SML_FUZZY_SNORM_DRASTIC_SUM 

A or B -> if A=0 then B else if B=0 then A else 0

SML_FUZZY_SNORM_EINSTEIN_SUM 

A or B -> A+B/1+A*B

SML_FUZZY_SNORM_HAMACHER_SUM 

A or B -> if A=B=0 then 0 else A*B/A+B-A*B

SML_FUZZY_SNORM_MAXIMUM 

A or B -> max(A,B)

SML_FUZZY_SNORM_NILPOTENT_MAXIMUM 

A or B -> if A+B < 1 then max(A,B) else 1

SML_FUZZY_SNORM_NORMALIZED_SUM 

A or B -> A+B/max(1, max(A,B))

TNorm rules are also known as conjunction.

See Also
sml_fuzzy_conjunction_set
Enumerator
SML_FUZZY_TNORM_ALGEBRAIC_PRODUCT 

A and B -> A*B

SML_FUZZY_TNORM_BOUNDED_DIFFERENCE 

A and B -> max(A+B-1, 0)

SML_FUZZY_TNORM_DRASTIC_PRODUCT 

A and B -> if A=1 then B else if B=1 then A else 0

SML_FUZZY_TNORM_EINSTEIN_PRODUCT 

A and B -> A*B/2-(A+B-A*B)

SML_FUZZY_TNORM_HAMACHER_PRODUCT 

A and B -> A*B/A+B-A*B

SML_FUZZY_TNORM_MINIMUM 

A and B -> min(A,B)

SML_FUZZY_TNORM_NILPOTENT_MINIMUM 

A and B -> if A+B > 1 then min(A,B) else 0

Function Documentation

bool sml_fuzzy_conjunction_set ( struct sml_object sml,
enum sml_fuzzy_tnorm  norm 
)

Set the conjunction fuzzy rule.

The conjunction rule is equivalent to the boolean and operation. Example: If the fuzzy engine encounters the following expression in a fuzzy rule, "... A and B ...". Given that A is 0.6 and B is 0.8 and the conjunction operator is SML_FUZZY_TNORM_MINIMUM, the result of the operation will be 0.6

Remarks
The default value is SML_FUZZY_TNORM_MINIMUM
Parameters
smlThe sml_object object.
normThe desired sml_fuzzy_tnorm.
Returns
true on success.
false on failure.
See Also
sml_fuzzy_tnorm
struct sml_object* sml_fuzzy_new ( void  )

Creates a SML fuzzy engine.

A fuzzy term object.

Remarks
It must be freed with sml_free after usage is done.
Returns
A sml_object object on success.
NULL on failure.
See Also
sml_free
sml_object
bool sml_fuzzy_output_set_accumulation ( struct sml_object sml,
struct sml_variable sml_variable,
enum sml_fuzzy_snorm  accumulation 
)

Set the output accumulation.

The output accumulation rule is the strategy that will be used by fuzzy lib to select the final result output value using the output values calculated for each fuzzy rule.

Example: If the output value for variable A is 0.6 for Rule 1, the output value for variable A is 0.4 for Rule 2 and the selected accumulator is SML_FUZZY_SNORM_MAXIMUM, then the final output value will be 0.6

Parameters
smlThe sml_object object.
sml_variableThe sml_variable.
accumulationThe sml_fuzzy_snorm rule.
Returns
true on success.
false on failure.
bool sml_fuzzy_output_set_defuzzifier ( struct sml_object sml,
struct sml_variable sml_variable,
enum sml_fuzzy_defuzzifier  defuzzifier,
int  defuzzifier_resolution 
)

Set the defuzzifier for an output variable.

Fuzzy defuzzifiers are used to convert degrees of membership to a crisp value (a real number) For example if from a given rule the fuzzy engine predict that the degrees of membership for a given variable for the term Hot is 10% and for the term Warm is 90%. Using the defuzzifier it will be possible the compute a real number to represent these degrees of membership.

Remarks
Defuzzifier only works for outputs variables, also the defuzzifier_resulution will be ignore if sml_fuzzy_defuzzifier is SML_FUZZY_DEFUZZIFIER_WEIGHTED_SUM or SML_FUZZY_DEFUZZIFIER_WEIGHTED_AVERAGE. The default defuzzifier is SML_FUZZY_DEFUZZIFIER_CENTROID.
Parameters
smlThe sml_object object.
sml_variableThe sml_variable to use the given defuzzifier.
defuzzifierThe sml_fuzzy_defuzzifier object.
defuzzifier_resolutionA constant that will be used by the defuzzifier to obtain the crisp value.
Returns
true on success.
false on failure.
bool sml_fuzzy_set_simplification_disabled ( struct sml_object sml,
bool  disabled 
)

Disable/Enable simplification.

The rule simplification uses a heuristic to try to simplify the fuzzy rules. For example, if the fuzzy engine is controlling a a home Light using two variables (Weekday and TimeOfTheDay) we can have the following rules:

If Weekday is Sunday and TimeOfTheDay is Night then Light is On.
If Weekday is Monday and TimeOfTheDay is Night then Light is On.
....
If Weekday is Saturday and TimeOfTheDay is Night then Light is On.

It is clear that the Weekday is not important, if TimeOfTheDay is Night then the Light should be On. Knowing that, the fuzzy engine will simplify those rules into one rule:

If TimeOfTheDay is Night then Light is On.

However if after the simplification a new rule is created that denies the simplificated rule, the simplification is undone.

Remarks
Rule simplification is enabled by default.
Parameters
smlThe sml_object object.
disabledtrue to disable, false to enable.
Returns
true on success.
false on failure.
bool sml_fuzzy_set_variable_terms_auto_balance ( struct sml_object sml,
bool  variable_terms_auto_balance 
)

Set terms autobalance.

Defining terms overlaps is a hard task, sometimes the user defined terms are not well suited for an application. If the term definitions are poor, the fuzzy engine the fuzzy predictions will be poor as well. If auto balance is enabled and if the fuzzy engine detects that the terms are not well set, it will recreate the terms.

Remarks
Terms auto balance is disabled by default
Parameters
smlThe sml_object object.
variable_terms_auto_balancetrue to enable, false to disable.
Returns
true on success.
false on failure.
bool sml_fuzzy_supported ( void  )

Check if SML was built with fuzzy support.

Returns
true If it is supported.
false If not supported.
struct sml_fuzzy_term* sml_fuzzy_variable_add_term_cosine ( struct sml_object sml,
struct sml_variable variable,
const char *  name,
float  center,
float  width 
)

Add a cosine term for a variable.

Cosine term value is obtained by the function value from a cosine function centered in X coordinate center and with width defined by width parameter. The maximum value (1.0) of the cosine function is in X coordinate center.

Remarks
The term name can not contain spaces!
Parameters
smlThe sml_object object.
variableThe sml_variable.
nameThe term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN.
centerThe center of the cosine curve.
widthThe width of the cosine curve.
Returns
sml_fuzzy_term on success.
NULL on failure.
struct sml_fuzzy_term* sml_fuzzy_variable_add_term_gaussian ( struct sml_object sml,
struct sml_variable variable,
const char *  name,
float  mean,
float  standard_deviation 
)

Add a gaussian term for a variable.

Gaussian term value is obtained by the function value from a gaussian function defined by the parameters mean, standard_deviation. The maximum value in Y axis of the gaussian function is 1.0.

Remarks
The term name can not contain spaces!
Parameters
smlThe sml_object object.
variableThe sml_variable.
nameThe term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN.
meanThe mean of the gaussian curve.
standard_deviationThe standard deviation of the curve.
Returns
sml_fuzzy_term on success.
NULL on failure.
struct sml_fuzzy_term* sml_fuzzy_variable_add_term_ramp ( struct sml_object sml,
struct sml_variable variable,
const char *  name,
float  start,
float  end 
)

Add a ramp term for a variable.

A ramp term uses a mathematical function defined by its start and end points. Start parameters is the coordinate in X axis where the ramp is in its minimum value (zero). End parameter is the coordinate in Y axis where the ramp is in its maximum value (1.0). If start < end we will have a positive ramp. If end < start we will have a negative ramp.
For example:

//this will create a ramp going up, connecting points (0.3; 0.0) and (0.7; 1.0). In X position 0.3, the ramp value will be 0.0. In X position 0.7, the ramp value will be 1.0
sml_fuzzy_variable_add_term_ramp (sml, var, "ramp_up", 0.3, 0.7);
//this will create a ramp going down, connecting points (0.3; 1.0) and (0.7; 0.0). In X position 0.3, the ramp value will be 1.0. In X position 0.7, the ramp value will be 0.0
sml_fuzzy_variable_add_term_ramp (sml, var, "ramp_up", 0.7, 0.3);
Remarks
The term name can not contain spaces!
Parameters
smlThe sml_object object.
variableThe sml_variable.
nameThe term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN.
startThe start of the ramp on the X axis.
endThe end of the ramp on the X axis.
Returns
sml_fuzzy_term on success.
NULL on failure.
struct sml_fuzzy_term* sml_fuzzy_variable_add_term_rectangle ( struct sml_object sml,
struct sml_variable variable,
const char *  name,
float  start,
float  end 
)

Add a rectangle term for a variable.

A rectangle term uses a mathematical function defined by its start and end points. For any X value from start to end, the function value will be 1.0. For all other X values, the function value will be zero.

Remarks
The term name can not contain spaces!
Parameters
smlThe sml_object object.
variableThe sml_variable.
nameThe term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN.
startThe point on the X axis that the term will start.
endThe point on the X axis that the term will end.
Returns
sml_fuzzy_term on success.
NULL on failure.
struct sml_fuzzy_term* sml_fuzzy_variable_add_term_triangle ( struct sml_object sml,
struct sml_variable variable,
const char *  name,
float  vertex_a,
float  vertex_b,
float  vertex_c 
)

Add a triangle term for a variable.

A triangle term uses is a mathematical function defined by 3 vertex (a, b c). For X coordinates between vertex a and b, function value is obtained by the linear function connecting points (vertex_a; 0) to (vertex_b; 1). In vertex_b, function value will 1.0 and from vertex_b to vertex_c, function value is obtained by the linear function connecting points (vertex_b; 1.0) to (vertex_c; 0). For all other X values, the function value will be zero.

Remarks
The term name can not contain spaces!
Parameters
smlThe sml_object object.
variableThe sml_variable.
nameThe term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN.
vertex_aThe point on the X axis that the term will start.
vertex_bThe point on the X axis that will be the middle of the triangle.
vertex_cthe point on the X axis that the term will end.
Returns
sml_fuzzy_term on success.
NULL on failure.
float sml_fuzzy_variable_get_default_term_width ( struct sml_object sml,
struct sml_variable sml_variable 
)

Get the default term width used by fuzzy to create terms.

Parameters
smlThe sml_object object.
sml_variableThe sml_variable.
See Also
sml_fuzzy_variable_set_default_term_width
bool sml_fuzzy_variable_get_is_id ( struct sml_object sml,
struct sml_variable sml_variable 
)

Check if this variable is used as an id field.

Parameters
smlThe sml_object object.
sml_variableThe sml_variable.
Returns
true if sml_variable is an id field
false if sml_variable isn't an id field
See Also
sml_fuzzy_variable_set_is_id
bool sml_fuzzy_variable_remove_term ( struct sml_object sml,
struct sml_variable variable,
struct sml_fuzzy_term term 
)

Remove a fuzzy term from the engine.

Parameters
smlThe sml_object object.
variableThe sml_variable to remove the term.
termThe The sml_fuzzy_term to be removed.
Returns
true on success.
false on failure.
bool sml_fuzzy_variable_set_default_term_width ( struct sml_object sml,
struct sml_variable sml_variable,
float  width 
)

Set the default term width used by fuzzy to create terms.

If sml_process is called and a variable has no terms fuzzy engine will automatically create terms for this variable. Some properties are important help fuzzy engine to improve the quality of the created terms.

Width is the width of each created term. Other important properties are is_id, set by sml_fuzzy_variable_set_is_id and range (min and max), set by sml_variable_set_range.

For example, for the variable created by the following code:

struct sml_variable *var = sml_new_input(sml, "my_var");
sml_variable_set_range(sml, var, 0, 5);

Fuzzy engine will create 5 terms:

  • ramp from 0 to 1
  • triangle from 1 to 2, with peak at 0.5
  • triangle from 2 to 3, with peak at 1.5
  • triangle from 3 to 4, with peak at 3.5
  • ramp from 4 to 5.

After that, a small overlap will be added between each term. The default overlap value is 10% of width, in this case, 0.1. So at the end we would have:

  • ramp from 0 to 1.1
  • triangle from 0.9 to 2.1, with peak at 0.5
  • triangle from 1.9 to 3.1, with peak at 1.5
  • triangle from 2.9 to 4.1, with peak at 3.5
  • ramp from 3.9 to 5.

In case the variable is an id, the first and last element will have a size width/2. This is necessary so we can have a constant distance from the peak of each term. This distance is always equal width.

For example, for the variable created by the following code:

struct sml_variable *var = sml_new_input(sml, "my_var");
sml_variable_set_range(sml, var, 0, 5);

We would have 5 terms:

  • ramp from 0 to 0.6
  • triangle from 0.4 to 1.6, with peak at 1
  • triangle from 1.4 to 2.6 with peak at 2
  • triangle from 2.4 to 3.6, with peak at 3
  • triange from 3.4 to 4.6, with peak at 4
  • ramp from 4.4 to 5

Terms autocreated are also rebalanced every time sml_variable_set_range is called. So new terms are created to make sure all variable range is covered. And terms that are no longer necessary are removed.

Parameters
smlThe sml_object object.
sml_variableThe sml_variable.
widthDefault term width
Remarks
The default value is NAN
See Also
sml_fuzzy_variable_get_default_term_width
bool sml_fuzzy_variable_set_is_id ( struct sml_object sml,
struct sml_variable sml_variable,
bool  is_id 
)

Check if this variable is used as an id field.

The is_id property is used by fuzzy engine when it is creating terms automatically. Terms created by fuzzy engine will be optimized to be used by variables that keeps ids.

For more information take a look at sml_fuzzy_variable_set_default_term_width

Parameters
smlThe sml_object object.
sml_variableThe sml_variable.
is_idtrue if this variable should be considered an id. false otherwise
Remarks
The default value is false
See Also
sml_fuzzy_variable_get_is_id
bool sml_is_fuzzy ( struct sml_object sml)

Check if the SML object is a fuzzy engine.

Parameters
smlThe sml_object object.
Returns
true If it is fuzzy.
false If it is not fuzzy.
bool sml_set_rule_weight_threshold ( struct sml_object sml,
float  weight_threshold 
)

Rules below a given value will be ignored.

Fuzzy rules created by fuzzy engine have weights associated with it, bigger weights means bigger relevance. The range of weight value is from zero (no relevance) to 1 (very relevant).

Weight threshold property is used to select if rules with low relevance are going to be used in output predictions. If weight_threshold is a value close to zero, even low relevance rules are going to be used. If weight_threshold is close to 1, only very relevant rules are going to be used.

Values greater than 1 or lower than 0 are not accepted and rules with weight zero are always ignored.

Remarks
The default weight threshold is 0.05
Parameters
smlThe sml_object object.
weight_thresholdThe desired threshold.
Returns
true on success.
false on failure.