simpful module

simpful.simpful module

class simpful.simpful.AutoTriangle(n_sets=3, terms=None, universe_of_discourse=[0, 1], verbose=False)

Bases: simpful.simpful.LinguisticVariable

Creates a new linguistic variable, whose universe of discourse is automatically divided in a given number of fuzzy sets. The sets are all symmetrical, normalized, and for each element of the universe their memberships sum up to 1.

Parameters:
  • n_sets – (integer) number of fuzzy sets in which the universe of discourse must be divided.

  • terms – list of strings containing linguistic terms for the fuzzy sets (must be appropriate to the number of fuzzy sets).

  • universe_of_discourse – a list of two elements, specifying min and max of the universe of discourse.

  • verbose – True/False, toggles verbose mode.

class simpful.simpful.FuzzySystem(operators=None, show_banner=True, sanitize_input=False, verbose=True)

Bases: object

Creates a new fuzzy system.

Parameters:
  • operators – a list of strings, specifying fuzzy operators to be used instead of defaults. Currently supported operators: ‘AND_PRODUCT’.

  • show_banner – True/False, toggles display of banner.

  • sanitize_input – sanitize variables’ names to eliminate non-accepted characters (under development).

  • verbose – True/False, toggles verbose mode.

Mamdani_inference(terms=None, subdivisions=1000, aggregation_function=<built-in function max>, ignore_errors=False, ignore_warnings=False, verbose=False)

Performs Mamdani fuzzy inference.

Parameters:
  • terms – list of the names of the variables on which inference must be performed.If empty, all variables appearing in the consequent of a fuzzy rule are inferred.

  • subdivisions – the number of integration steps to be performed for calculating fuzzy set area (default: 1000).

  • aggregation_function – pointer to function used to aggregate fuzzy sets during Mamdani inference, default is max. Use Python sum function, or simpful’s probor function for sum and probabilistic OR, respectively.

  • ignore_errors – True/False, toggles the raising of errors during the inference.

  • ignore_warnings – True/False, toggles the raising of warnings during the inference.

  • verbose – True/False, toggles verbose mode.

Returns:

a dictionary, containing as keys the variables’ names and as values their numerical inferred values.

Sugeno_inference(terms=None, ignore_errors=False, ignore_warnings=False, verbose=False)

Performs Sugeno fuzzy inference.

Parameters:
  • terms – list of the names of the variables on which inference must be performed. If empty, all variables appearing in the consequent of a fuzzy rule are inferred.

  • ignore_errors – True/False, toggles the raising of errors during the inference.

  • ignore_warnings – True/False, toggles the raising of warnings during the inference.

  • verbose – True/False, toggles verbose mode.

Returns:

a dictionary, containing as keys the variables’ names and as values their numerical inferred values.

add_linguistic_variable(name, LV, verbose=False)

Adds a new linguistic variable to the fuzzy system.

Parameters:
  • name – string containing the name of the linguistic variable.

  • LV – linguistic variable object to be added to the fuzzy system.

  • verbose – True/False, toggles verbose mode.

add_rules(rules, verbose=False)

Adds new fuzzy rules to the fuzzy system.

Parameters:
  • rules – list of fuzzy rules to be added. Rules must be specified as strings, respecting Simpful’s syntax.

  • verbose – True/False, toggles verbose mode.

add_rules_from_file(path, verbose=False)

Imports new fuzzy rules by reading the strings from a text file.

Parameters:
  • path – path to the file containing the rules.

  • verbose – True/False, toggles verbose mode.

aggregate(list_variables, function)

Performs a fuzzy aggregation of linguistic variables contained in a FuzzySystem object.

Parameters:
  • list_variables – list of linguistic variables names in the FuzzySystem object to aggregate.

  • function – pointer to an aggregation function. The function must accept as an argument a list of membership values.

Returns:

the aggregated membership values.

get_firing_strengths(input_values=None)

If “input_values” is not provided, it returns a list of the firing strengths of the the rules, given the current state of input variables. “input_values” is an optional argument, in the form of a dictionary containing a list of input states for each variable. In this second case, it returns a 2D list of firing strengths of the given states.

Parameters:

input_values – dictionary where the keys are names of linguistic variables and the values are a list of input values for that variable.

Returns:

a list containing rules’ firing strengths, or a 2D list containing rules’ firing strengths for each given input state.

get_fuzzy_set(variable_name, fs_name)

Returns a FuzzySet object associated to a linguistic variable.

Parameters:
  • variable_name – name of the linguistic variable.

  • fs_name – linguistic term associated to the fuzzy set.

Returns:

a FuzzySet object.

get_fuzzy_sets(variable_name)

Returns the list of FuzzySet objects associated to one linguistic variable.

Parameters:

variable_name – name of the linguistic variable.

Returns:

a list containing FuzzySet objects.

get_rules()

Returns the rule base of the fuzzy system.

Returns:

a list containing the fuzzy rules as strings, in the same order they were added.

inference(terms=None, subdivisions=1000, aggregation_function=<built-in function max>, ignore_errors=False, ignore_warnings=False, verbose=False)

Performs the fuzzy inference, trying to automatically choose the correct inference engine.

Parameters:
  • terms – list of the names of the variables on which inference must be performed. If empty, all variables appearing in the consequent of a fuzzy rule are inferred.

  • subdivisions – the number of integration steps to be performed for calculating fuzzy set area (default: 1000).

  • aggregation_function – pointer to function used to aggregate fuzzy sets during Mamdani inference, default is max. Use Python sum function, or simpful’s probor function for sum and probabilistic OR, respectively.

  • ignore_errors – True/False, toggles the raising of errors during the inference.

  • ignore_warnings – True/False, toggles the raising of warnings during the inference.

  • verbose – True/False, toggles verbose mode.

Returns:

a dictionary, containing as keys the variables’ names and as values their numerical inferred values.

plot_surface(variables, output, detail=40, color_map='plasma')

Plots the surface induced by the rules.

Parameters:
  • variables – a pair of linguistic variables for the x and y axis.

  • output – the output variable to be computed.

  • detail – number of subdivisions along each axis.

  • color_map – the color map to be used for the plot.

Returns:

a matplotlib figure object.

plot_variable(var_name, outputfile='', TGT=None, element=None, highlight=None, ax=None, xscale='linear')

Plots all fuzzy sets contained in a liguistic variable. Options for saving the figure and draw on a matplotlib ax are provided.

Parameters:
  • var_name – string containing the name of the linguistic variable to plot.

  • outputfile – string containing path and filename where the plot must be saved.

  • TGT – (deprecated) show the memberships of a specific element of discourse TGT in the figure.

  • element – show the memberships of a specific element of discourse in the figure.

  • highlight – string, indicating the linguistic term/fuzzy set to highlight in the plot.

  • ax – a matplotlib ax where the variable will be plotted.

  • xscale – default “linear”, supported scales “log”. Changes the scale of the xaxis.

produce_figure(outputfile='', max_figures_per_row=4, element_dict=None)

Plots the membership functions of each linguistic variable contained in the fuzzy system.

Parameters:
  • outputfile – string containing path and filename where the plot must be saved.

  • max_figures_per_row – maximum number of figures per row in the plot.

  • element_dict – dictionary of elements of the universe of discourse whose membership must be plotted over the fuzzy sets.

replace_rule(i, new_rule, verbose=False)

Replaces the i-th rule in the FuzzySystem object. Rules are stored in the same order they were added.

Parameters:
  • i – index of the rule to be replaced in the fuzzy system.

  • new_rule – fuzzy rule to be used for the replacement.

  • verbose – True/False, toggles verbose mode.

set_constant(name, value, verbose=False)

Sets the numerical value of a linguistic variable to a constant value (i.e. ignore fuzzy inference).

Parameters:
  • name – name of the linguistic variables to be set to a constant value.

  • value – numerical value to be set.

  • verbose – True/False, toggles verbose mode.

set_crisp_output_value(name, value, verbose=False)

Adds a new crisp output value to the fuzzy system.

Parameters:
  • name – string containing the identifying name of the crisp output value.

  • value – numerical value of the crisp output value to be added to the fuzzy system.

  • verbose – True/False, toggles verbose mode.

set_output_function(name, function, verbose=False)

Adds a new output function to the fuzzy system.

Parameters:
  • name – string containing the identifying name of the output function.

  • function – string containing the output function to be added to the fuzzy system. The function specified in the string must use the names of linguistic variables contained in the fuzzy system object.

  • verbose – True/False, toggles verbose mode.

set_variable(name, value, verbose=False)

Sets the numerical value of a linguistic variable.

Parameters:
  • name – name of the linguistic variables to be set.

  • value – numerical value to be set.

  • verbose – True/False, toggles verbose mode.

class simpful.simpful.LinguisticVariable(FS_list=[], concept=None, universe_of_discourse=None)

Bases: object

Creates a new linguistic variable.

Parameters:
  • FS_list – a list of FuzzySet instances.

  • concept – a string providing a brief description of the concept represented by the linguistic variable (optional).

  • universe_of_discourse – a list of two elements, specifying min and max of the universe of discourse. Optional, but it must be specified to exploit plotting facilities.

draw(ax, TGT=None, element=None, highlight=None, xscale='linear')

This method returns a matplotlib ax, representing all fuzzy sets contained in the liguistic variable.

Parameters:
  • ax – the matplotlib axis to plot to.

  • TGT – (deprecated) show the memberships of a specific element of discourse TGT in the figure.

  • element – show the memberships of a specific element of discourse in the figure.

  • highlight – string, indicating the linguistic term/fuzzy set to highlight in the plot.

  • xscale – default “linear”, supported scales “log”. Changes the scale of the xaxis.

Returns:

A matplotlib axis, representing all fuzzy sets contained in the liguistic variable.

get_universe_of_discourse()

This method provides the leftmost and rightmost values of the universe of discourse of the linguistic variable.

Returns:

the two extreme values of the universe of discourse.

plot(outputfile='', TGT=None, element=None, highlight=None, xscale='linear')

Shows a plot representing all fuzzy sets contained in the liguistic variable.

Parameters:
  • outputfile – path and filename where the plot must be saved.

  • TGT – (deprecated) show the memberships of a specific element of discourse TGT in the figure.

  • element – show the memberships of a specific element of discourse in the figure.

  • highlight – string, indicating the linguistic term/fuzzy set to highlight in the plot.

  • xscale – default “linear”, supported scales “log”. Changes the scale of the xaxis.

exception simpful.simpful.UndefinedUniverseOfDiscourseError(message)

Bases: Exception

simpful.simpful.probor(m_list)

Performs aggregation of membership values using the probabilistic OR operation.

Parameters:

m_list – list of membership values to aggregate.

Returns:

the aggregated membership value.

simpful.simpful.prod(m_list)

Performs aggregation of membership values using the product operation.

Parameters:

m_list – list of membership values to aggregate.

Returns:

the aggregated membership value.