The SML common functions. More...
Data Structures | |
struct | sml_object |
struct | sml_variable |
struct | sml_variables_list |
Macros | |
#define | SML_INTERNAL_ERROR 3 |
#define | SML_VARIABLE_NAME_MAX_LEN (127) |
Typedefs | |
typedef void(* | sml_change_cb )(struct sml_object *sml, struct sml_variables_list *changed, void *data) |
Called every time the SML made a prediction. More... | |
typedef bool(* | sml_read_state_cb )(struct sml_object *sml, void *data) |
A user defined callback to read the variables values. More... | |
Functions | |
bool | sml_erase_knowledge (struct sml_object *sml) |
Erase all previous knowledge. More... | |
void | sml_free (struct sml_object *sml) |
Frees the SML engine. More... | |
bool | sml_load (struct sml_object *sml, const char *path) |
Load the SML state from the disk. More... | |
bool | sml_load_debug_log_file (struct sml_object *sml, const char *str) |
Load to current engine the debug data logged to a file. More... | |
bool | sml_load_fll_file (struct sml_object *sml, const char *filename) |
Reads a FLL file. More... | |
bool | sml_predict (struct sml_object *sml) |
Make a prediction based on the most recent observations. More... | |
void | sml_print_debug (struct sml_object *sml, bool full) |
Prints SML debug information. More... | |
int | sml_process (struct sml_object *sml) |
Process variables and make predictions. More... | |
bool | sml_save (struct sml_object *sml, const char *path) |
Save the SML state on the disk. More... | |
bool | sml_set_debug_log_file (struct sml_object *sml, const char *str) |
Set the file to be used to debug data changes in this engine. More... | |
bool | sml_set_learn_disabled (struct sml_object *sml, bool disable) |
Disable the SML learning. More... | |
bool | sml_set_max_memory_for_observations (struct sml_object *sml, unsigned int max_size) |
Set maximum memory that can be used to store observation history data. More... | |
bool | sml_set_output_state_changed_callback (struct sml_object *sml, sml_change_cb output_state_changed_cb, void *data) |
Register a change callback. More... | |
bool | sml_set_read_state_callback (struct sml_object *sml, sml_read_state_cb read_state_cb, void *data) |
Register a read callblack. More... | |
bool | sml_set_stabilization_hits (struct sml_object *sml, uint16_t hits) |
Set the stabilization hits. More... | |
The SML common functions.
The functions here are common to all engines. They are used to free the engines, create variables, save/load SML state and more.
The SML main flow consist in the following steps:
As a simple example imagine that one wants to control an indoor light that turns on automatically if a person enters in the room and goes off if the person leaves. For that, this person has a presence sensor.
Choosing engines and creating variables is straight-forward after the solution is modeled. Let's say fuzzy was the chosen one. Registering callbacks is simple as well:
Terms are a way to split the values in the range in meaningful parts. Fuzzy supports some functions to describe them, as ramps, triangles and others.
Although we have the possibility to define each term to describe your problem, for this example we will let the fuzzy engine do this for you. To improve the quality of the automatically created terms, we need to give fuzzy engine some hints about the variable being used. As both variables are boolean values, lets set the default term width to 0.5 and the range from 0 to 1. So we will have 2 terms, one for on and another for off.
After everything is set up, it's time to trigger the processing. It will be done in a simple loop, making 150 sml_process() calls, but more elaborated ways to call process() can be implemented, using mainloops.
Read and change callbacks implementation varies a lot depending on machine learning usage, since they're the interface with the rest of the world.
Read callback is a function that will be called at each processing cycle to update variables values.
So in this function the values of presence sensor and light must to be fetched. It could be done via GPIO, OIC, or any other way, depending in your product. It may be synchronous or asynchronous.
To keep it simple, yet illustrative, we're going to simulate the values in a function read_state_cb() Variable sensor_state represents presence sensor reading, switch_state represents light state.
This variable will be global since its going to be used by the callback of state changes (you could use a struct with this variable and pass them to callbacks).
To simulate a daily based used, a day would be a cycle of 15 readings, user will be present on last 5 readings of each "day". When she leaves she remembers to turn lights off... most of the time. This is simulated by off_count . When she forgets lights on and leaves, machine learning should suggest to turn lights off.
After states are "fetch", they're set on sml variables (input and output).
To fetch predicted output values, it's required to set a callback function using sml_set_output_state_changed_callback()
This callback will be called when SML makes a prediction for at least one output variable.
Change state callback only will be called on output value changes. But most of the time, this prediction should matches current state of output variable. On this case no action must to be taken. Also, sometimes SML doesn't have enough information to make a prediction, setting output variable value to NaN.
So in change state callback we're going to check first if it was able to predict a value, then check if we need to act, in case prediction and current light state diverges.
In this example we'll just print a message informing the light state should be changed.
A few tips to obtain good predictions.
#define SML_INTERNAL_ERROR 3 |
SML error code. Could not complete an operation
#define SML_VARIABLE_NAME_MAX_LEN (127) |
Maximum size of variables name
typedef void(* sml_change_cb)(struct sml_object *sml, struct sml_variables_list *changed, void *data) |
Called every time the SML made a prediction.
sml | The sml_object Object. |
changed | A sml_variables_list with the predicted variables. |
data | User defined data. |
typedef bool(* sml_read_state_cb)(struct sml_object *sml, void *data) |
A user defined callback to read the variables values.
sml | The sml_object object |
data | The user defined data |
true
on success. false
if no reads were done. bool sml_erase_knowledge | ( | struct sml_object * | sml | ) |
Erase all previous knowledge.
Erases everything that the SML has learned, this will required that the SML is trained again.
sml | The sml_object object. |
true
on success. false
on failure. void sml_free | ( | struct sml_object * | sml | ) |
Frees the SML engine.
sml | The engine to be freed. |
bool sml_load | ( | struct sml_object * | sml, |
const char * | path | ||
) |
Load the SML state from the disk.
sml | The sml_object object. |
path | A direcoty to load the engine files. |
true
on success. false
on failure. bool sml_load_debug_log_file | ( | struct sml_object * | sml, |
const char * | str | ||
) |
Load to current engine the debug data logged to a file.
Load all data logged in file set by sml_set_debug_log_file to current engine. Used for debug purposes.
To use this feature, sml must be compiled with build type set to Debug.
sml | The sml_object object. |
str | A string with full path of file to be used to load debug data. |
false
if operation failed or if sml was not compiled using Debug build type.bool sml_load_fll_file | ( | struct sml_object * | sml, |
const char * | filename | ||
) |
Reads a FLL file.
FLL stands for Fuzzylite language. It can be used to create/change the fuzzy terms/defuzzifiers without using the SML apis.
sml | The sml_object object. |
filename | A fll file path. |
true
on success. false
on failure. bool sml_predict | ( | struct sml_object * | sml | ) |
Make a prediction based on the most recent observations.
This is useful for making predictions without the normal SML flow, without a mainloop and a registered sml_change_cb. Take a look in the following example:
sml | The sml_object object. |
true
on success. false
on failure. void sml_print_debug | ( | struct sml_object * | sml, |
bool | full | ||
) |
Prints SML debug information.
sml | The sml_object object. |
full | true for full log, false otherwise. |
int sml_process | ( | struct sml_object * | sml | ) |
Process variables and make predictions.
This function is used to read the variables via sml_read_state_cb, process them and call sml_change_cb if necessary.
sml | The sml_object object. |
bool sml_save | ( | struct sml_object * | sml, |
const char * | path | ||
) |
Save the SML state on the disk.
sml | The sml_object object. |
path | A direcoty to save the engine files. |
true
on success. false
on failure. bool sml_set_debug_log_file | ( | struct sml_object * | sml, |
const char * | str | ||
) |
Set the file to be used to debug data changes in this engine.
This file will be used to log all calls to methods that changes the current state of the sml engine data. This information may be used to reproduce the execution of sml for debug purposes. Methods that configure the sml are not logged.
To use this feature, sml must be compiled with build type set to Debug.
sml | The sml_object object. |
str | A string with full path of file to be used to write debug data. Use NULL or an empty string to disable data debug. |
false
if operation failed or if sml was not compiled using Debug build type.bool sml_set_learn_disabled | ( | struct sml_object * | sml, |
bool | disable | ||
) |
Disable the SML learning.
All the reads will be ignore and they will not be used to learn new patterns. However predictions can still be made.
sml | The sml_object object. |
disable | true to disable learn, false otherwise |
true
on success. false
on failure. bool sml_set_max_memory_for_observations | ( | struct sml_object * | sml, |
unsigned int | max_size | ||
) |
Set maximum memory that can be used to store observation history data.
sml | The sml_object object. |
max_size | Max memory size in bytes. |
true
on success. false
on failure. bool sml_set_output_state_changed_callback | ( | struct sml_object * | sml, |
sml_change_cb | output_state_changed_cb, | ||
void * | data | ||
) |
Register a change callback.
It should be used to set a callback function to fetch the predicted output variables. This callback will be called when the SML makes a prediction for at least on output variable.
sml | The sml_object object. |
output_state_changed_cb | A sml_change_cb. |
data | User data to sml_read_state_cb. |
true
on success. false
on failure. bool sml_set_read_state_callback | ( | struct sml_object * | sml, |
sml_read_state_cb | read_state_cb, | ||
void * | data | ||
) |
Register a read callblack.
It should be used to set a callback function to read variables values. This callback must return true if it was able to read all variables or false on error. If an error happens sml_process will be aborted, returning an error value. Otherwise, sml_process will proceed with updated variables values.
sml | The sml_object object. |
read_state_cb | A sml_read_state_cb. |
data | User data to sml_read_state_cb. |
true
on success. false
on failure. bool sml_set_stabilization_hits | ( | struct sml_object * | sml, |
uint16_t | hits | ||
) |
Set the stabilization hits.
Amount of reads without input changes to consider input stable. Only stable inputs are used to run predictions or to train the SML engine. This is necessary to remove noises in read data that could lead to incorrect previsions.
sml | The sml_object object. |
hits | The number of hits to be considered stable. |
true
on success. false
on failure.