You are on page 1of 8

Fuzzy Logic Processor

Jim Kunce (jdk_acct@yahoo.com)

Project Description

Implement a basic Fuzzy Logic Processor that utilizes user provided Fuzzy Sets, Fuzzy
Rules and "crisp" input parameters to determine a "crisp" output. The solver will be
implemented in MatLab, follow the standard Fuzzy Logic methodology and will have the
following specifications:

• Fuzzy Sets, Fuzzy Rules & Crisp Inputs will be provided as text (CSV) files
• All antecedent membership functions will be defined using the trapezoidal
function
• Consequent membership grades will be determined using Mamdani's fuzzy
implication method
• The "defuzzification" will implement the Center of Area method to determine
the Crisp Output

Project Flowchart

Fuzzy Sets
(CSV)

LoadFuzzySet Crisp Input Fuzzy Rules


(function) (CSV) (CSV)

FuzzySet LoadCrispInput LoadFuzzyRules


(object) (function) (function)

CrispInput FuzzyRules
(object) (object)

Fuzzification
(function)

Antecedent Membership Grades


(object)

Fuzzy Rule Processing


(function)

Consequent Membership Grades


(object)

De-Fuzzification
(function)

Crisp Output
(object)

5/5/2014 Page 1 of 8
Object Definitions

Fuzzy Sets

Fuzzy Set Input file definition


• The input will be a comma delimited text file
• There shall be no header row and the sets will start in row 1
• There shall be a set named "Output" which defines the output membership function
and needs to be in the last row of the file.

Fuzzy Set CSV file format


Column Description
1 The name of the set
2 The name of the item in the set
3 The trapezoidal parameter value for a
4 The trapezoidal parameter value for b
5 The trapezoidal parameter value for c
6 The trapezoidal parameter value for d

Fuzzy Set Object Definitions


Item Description
.Count The number of sets
.Sets{i,1} The name of the ith set
.ItemCount(i,1) The count of items in the ith set
.Items{i,1} List of items in the ith set
.Parms{i,1}(j,:) Vector of trapezoidal parameters for the jth item of the ith set
.Range{i,1} Vector containing the min and max values for the ith set [min,max]

MatLab Function Definition


function [ output ] = FLP_LoadFuzzySets( file_dir )
% FLP_LoadFuzzySets Reads Fuzzy Sets from comma delimited text file

% This function reads in a CSV delimited text file containing the details
% of the fuzzy set and formats the detail in the form of a structure
% array
%
% Input
% file_dir - the path and filename of the CSV file
%
% Output
% output - a FuzzySet object

Crisp Input

Crisp Input file definition


• The input will be a comma delimited text file
• The header row will contain the name of the set for each column
• Multiple rows are allowed and each row will be treated as a separate crisp input

5/5/2014 Page 2 of 8
• The number of columns in the input file needs to equal to the number of sets
,excluding the Output set, and contain the crisp value for the set

Crisp Input CSV file format


Column Description
1 ... n The crisp values

Crisp Input Object Definition


This will be a m x n matrix

MatLab Function Definition


function [ output ] = FLP_LoadCrispInput( FuzzySet, file_dir )
% FLP_LoadCrispInput Reads Crisp Input from comma delimited text file

% This function reads in a CSV delimited text file containing the crisp
% input values and creates a matrix with the values in a column
% corresponding to the order of the sets in FuzzySet. The first row of
% the CSV file should contain a header row that matches the name of the
% sets in the FuzzySet object.
%
% Input
% FuzzySet - a FuzzySet object generated by FLP_LoadFuzzySets
% file_dir - the path and filename of the CSV file
%
% Output
% output - a CrispInput object

Fuzzy Rules

Fuzzy Rules Input file definition


• The input will be a comma delimited text file
• The header row will contain the name of the set for each column
• There rightmost column should be named "Output" which defines consequents
• The cell should be blank if an item from that set is not used in the rule
• The antecedent terms are combined with AND

Fuzzy Rules CSV file format


Column Description
1 ... n Items from each set used in the rule

Fuzzy Rules Object Definitions


• j - the number of Fuzzy Rules
• i - the number of input and output sets in the Fuzzy Sets
• Object will be a [j,i] matrix of enumerated rules, 0 will be used to indicated absence
of any item from a set in the rule

5/5/2014 Page 3 of 8
MatLab Function Definition
function [ output ] = FLP_LoadFuzzyRules( FuzzySet, file_dir )
% FLP_LoadFuzzyRules Reads the fuzzy rules from comma delimited text file

% This function reads in a CSV delimited text file containing the fuzzy
% rules and creates a matrix with the item values in a column
% corresponding to the order of the sets in FuzzySet
%
% Input
% FuzzySet - a FuzzySet object generated by FLP_LoadFuzzySets
% file_dir - the path and filename of the CSV file
%
% Output
% output - a FuzzyRules object

Antecedent Membership Grades

Object Definition
• i - the number of input sets in the Fuzzy Sets
• ji - the number of items in the ith Fuzzy Set
• m - the number of rows CrispInput
• Object will be a {i,1} cell array
• Within each cell, there will be a [m,ji] matrix containing the antecedent membership
grades.

Consequent Membership Grades

Object Definition
• j - the number of items in the "Output" Fuzzy Set
• m - the number of rows CrispInput
• Object will be a [m,j] matrix containing the consequent membership grades

Crisp Output

Object Definition
• m - the number of rows CrispInput
• Object will be a [m,1] matrix containing the crisp output values

5/5/2014 Page 4 of 8
Function Definitions

Fuzzification

Algorithm:
Input: FuzzySet Object, CrispInput Object
Output: Antecedent Membership Grades

For each set in the FuzzySet


For each item in the set
Calculate Membership Grades for all Crisp Inputs using Trapezoidal Rule
Store Membership Grades
Next item
Next set

MatLab Function Definition


function [ output ] = FLP_Fuzzification( FuzzySet, CrispInput )
% FLP_Fuzzification Calculates the fuzzy antecedent membership grades
%
% This function converts each of the crisp input values to a fuzzy
% membership grade for each item in the set. For example, if there are 10
% crisp input values and five items in a set, the resulting output is a 10
% by 5 matrix with the membership grades for each item being in a column
% and the rows being each crisp value. If there are multiple sets in the
% FuzzySet, the output will be a cell array with each cell containing the
% matrix of membership grades for that set.
%
% Input
% FuzzySet - a FuzzySet object generated by FLP_LoadFuzzySets
% CrispInput - a CrispInput object generated by FLP_LoadCrispInput
%
% Output
% output - the antecedent membership grades

Fuzzy Rule Processing

Algorithm:
Input: Antecedent Membership Grades (AMG), FuzzyRules Object
Output: Consequent Membership Grades

For each crispInputItem in AMG


For each membershipGradeSet in the AMG
map the membershipGrades to all FuzzyRules
Next membershipGradeSet
ruleGrade = minimum of the membership grades of items in the rule
map the ruleGrades to the Output set
Consequent Membership Grade = max membership grade for the item
Next crispInputItem

5/5/2014 Page 5 of 8
Matlab Function Definition
function [ consequentGrades ] = FLP_FuzzyRuleEval( AntMemberGrades, FuzzyRules )
% FLP_FuzzyRuleEval Evaluates Fuzzy Rules & Consequent Membership Grades
%
% Based on the antecedent membership grades, each of the fuzzy rules is
% evaluated using the Mamdani procedure. The Mamdani procedure consists of
% taking the minimum antecedant grade as the membership grade for the rule.
% The consequent membership grade is then determined by taking the maximum
% of the rule grades for each consequent.
%
% Input
% AntMemberGrades - a antecedent membership grade object generated by FLP_Fuzzification
% FuzzyRules - a fuzzy rules object generated by FLP_LoadFuzzyRules
%
% Output
% the consequent membership grades

De-Fuzzification

Algorithm:
Input: Consequent Membership Grades (CMG), FuzzySet Object
Output: CrispOutput

Divide the range of the Output into N intervals


For each group of CMG's resulting from each CrispInput
for each item
Calculate the membership values at the midpoint of each interval
item grade in interval = min(membership function, CMG)
next item
interval membership value = max of item grades
Calculate the CrispOutput as:
the sum(interval mid-points * interval grades) / sum (interval grades)
Next group of CMG's

MatLab Function Definition


function [ crispOutput, outputMF, X ] = FLP_DeFuzzification( ConsqMemberGrades, FuzzySet, N )
% FLP_DeFuzzification Calculates the crisp values using center of area
%
% Mamdani's fuzzy implication method is used to combine the membership
% functions of the output set which is the combined conclusion for all the
% rules. This pointwise function is then defuzzified to a single value
% using the Center of Area (COA) method.
%
% Input
% ConsqMemberGrades - a consequent membership grade object generated by FLP_FuzzyRuleEval
% FuzzySet - a FuzzySet object generated by FLP_LoadFuzzySets
% N - the number of increments in the output range used in the COA calc
%
% Output
% crispOutput - the crisp output values
% outputMF - columns of pointwise membership functions (each of length N)
% corresponding to each crisp input
% X - the vector of output values at which the pointwise membership
% functions are evaluated

5/5/2014 Page 6 of 8
Helper Functions

Trapezoidal Membership Function

MatLab Function Definition


function [ mf ] = FLP_trapzMF( x,tparms )
% FLP_trapzMF Calculates the value of the trapezoidal membership function at
% each point x.
%
% Input
% x - a column vector of values
% tparms - a vector with the a, b, c, & d trapezoidal parameters
%
% Output
% a columnar vector with membership function values

Crisp Output Surface Plot Function

MatLab Function Definition


function [ output ] = FLP_3DMFplot( FuzzySet, FuzzyRules, FS1, FS2, N )
% FLP_3DMFplot Plots the crisp output surface vs two of the fuzzy sets
% The function creates a surface plot of the crisp output with the fussy
% set feature 1 (FS1) along the X axis, the fuzzy set feature 2 (FS2)
% along the y axis and the crisp output on the z axis. The data and
% labels used to generate the chart are returned from the output of the
% function.
%
% Input
% FuzzySet - a FuzzySet object generated by FLP_LoadFuzzySets
% FuzzyRules - a fuzzy rules object generated by FLP_LoadFuzzyRules
% FS1 - the number of the fuzzy set to plot on the x axis
% FS2 - the number of the fuzzy set to plot on the y axis
% N - the NxN size of the meshgrid to create for the crisp output
%
% Output
% output - a struct object with the data and chart labels

5/5/2014 Page 7 of 8
References

Agarwal M. (2007). Fuzzy Logic Control of Washing Machines, Indian Institute of


Technology, Department of Mechanical Engineering

Hellmann M. (2001). Fuzzy logic introduction, Laboratoire Antennas Radar Telecom,


F.R.E CNRS 2272, Equipe Ra Polarimetrie

Dourra H., Siy P. (2002). Investment using technical analysis and fuzzy logic, Fuzzy Sets
and Systems, Volume 127, Issue 2, Pages 221-240

Iancu I. (2012). A Mamdani Type Fuzzy Logic Controller, Fuzzy Logic - Controls, Concepts,
Theories and Applications, Prof. Elmer Dadios (Ed.), ISBN: 978-953-51-0396-7, InTech

Jantzen J. (1999). Tutorial on Fuzzy Logic. Technical University of Denmark, Department


of Automation, Tech. Report No. 98–E 868 (logic)

Mamdani, E., Assilian S. (1975), An experiment in linguistic synthesis with a fuzzy logic
controller, International journal of man-machine studies 7, no. 1, Pages 1-13

Sivanandam S., Deepa S. (2007). Introduction to Fuzzy Logic using MATLAB

5/5/2014 Page 8 of 8

You might also like