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_object * | sml_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_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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. 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... | |
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
#define SML_TERM_NAME_MAX_LEN (127) |
Maximum size of terms name
The types of defuzzifier.
enum sml_fuzzy_snorm |
SNorm rules are also known as accumulation.
enum sml_fuzzy_tnorm |
TNorm rules are also known as conjunction.
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
sml | The sml_object object. |
norm | The desired sml_fuzzy_tnorm. |
true
on success. false
on failure.struct sml_object* sml_fuzzy_new | ( | void | ) |
Creates a SML fuzzy engine.
A fuzzy term object.
NULL
on failure.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
sml | The sml_object object. |
sml_variable | The sml_variable. |
accumulation | The sml_fuzzy_snorm rule. |
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.
sml | The sml_object object. |
sml_variable | The sml_variable to use the given defuzzifier. |
defuzzifier | The sml_fuzzy_defuzzifier object. |
defuzzifier_resolution | A constant that will be used by the defuzzifier to obtain the crisp value. |
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.
sml | The sml_object object. |
disabled | true to disable, false to enable. |
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.
sml | The sml_object object. |
variable_terms_auto_balance | true to enable, false to disable. |
true
on success. false
on failure. bool sml_fuzzy_supported | ( | void | ) |
Check if SML was built with fuzzy support.
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.
sml | The sml_object object. |
variable | The sml_variable. |
name | The term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN. |
center | The center of the cosine curve. |
width | The width of the cosine curve. |
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.
sml | The sml_object object. |
variable | The sml_variable. |
name | The term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN. |
mean | The mean of the gaussian curve. |
standard_deviation | The standard deviation of the curve. |
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:
sml | The sml_object object. |
variable | The sml_variable. |
name | The term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN. |
start | The start of the ramp on the X axis. |
end | The end of the ramp on the X axis. |
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
.
sml | The sml_object object. |
variable | The sml_variable. |
name | The term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN. |
start | The point on the X axis that the term will start. |
end | The point on the X axis that the term will end. |
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
.
sml | The sml_object object. |
variable | The sml_variable. |
name | The term's name. Its length must be smaller than SML_TERM_NAME_MAX_LEN. |
vertex_a | The point on the X axis that the term will start. |
vertex_b | The point on the X axis that will be the middle of the triangle. |
vertex_c | the point on the X axis that the term will end. |
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.
sml | The sml_object object. |
sml_variable | The sml_variable. |
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.
sml | The sml_object object. |
sml_variable | The sml_variable. |
true
if sml_variable is an id field false
if sml_variable isn't an id fieldbool 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.
sml | The sml_object object. |
variable | The sml_variable to remove the term. |
term | The The sml_fuzzy_term to be removed. |
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:
Fuzzy engine will create 5 terms:
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:
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:
We would have 5 terms:
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.
sml | The sml_object object. |
sml_variable | The sml_variable. |
width | Default term width |
NAN
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
sml | The sml_object object. |
sml_variable | The sml_variable. |
is_id | true if this variable should be considered an id. false otherwise |
false
bool sml_is_fuzzy | ( | struct sml_object * | sml | ) |
Check if the SML object is a fuzzy engine.
sml | The sml_object object. |
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.
sml | The sml_object object. |
weight_threshold | The desired threshold. |
true
on success. false
on failure.