/* Sketch that handles the fuzzy calculation of the FAMM(Fuzzy associative memory matrix) It also has functions to draw the inputs and the FAMM It handels 2 input and 1 output Versions: 1.0 Handels only 3 mamberships 1.1 displayFAMM and displayInputs are added 1.2 FAMM gets scalely and there are now limits of nr of memberships 1.3 Bugg fixed with the displayInputs class FAMM FAMM(int _matrixSize) fuzzification(float inputValue1, float inputValue2) deffuzify displayFAMM(int _x, int _y, int _width, int _height) displayInputs(int _x, int _y, int _spacing, int _width, int _height) boolean lineSegmentIntersection( float Ax, float Ay, float Bx, float By, float Cx, float Cy, float Dx, float Dy) class Variable Variable(String _name) Variable(FuzzySet _fuzzySet, String _name) void setValue(float _value) addFuzzySet(FuzzySet setToAdd) class FuzzySet FuzzySet() FuzzySet(float _minValue, float _maxValue) addMembership(String name, float a, float b, float c, float d) checkValue(float _value) class Membership Membership (String _name, float _a, float _b, float _c, float _d) float GetOutput(float input) */ class FAMM{ //ArrayList inputs; //float w1, w2, w3, w4, w5, w6, w7, w8, w9; //float m1, m2, m3, m4, m5, m6, m7, m8, m9; float[] m; float[] w; float X,Y; int matrixSize; Variable[] input = new Variable[2]; FAMM(int _matrixSize){ //inputs = new ArrayList(); matrixSize = _matrixSize; m = new float[matrixSize*matrixSize]; w = new float[matrixSize*matrixSize]; } // Makes the input values fuzzy with the fuzzy set and it's memberships void fuzzification(float inputValue1, float inputValue2) { input[0].value = inputValue1; input[1].value = inputValue2; Membership mship1_1 = (Membership) input[0].fuzzySet.memberships.get(0); Membership mship1_2 = (Membership) input[0].fuzzySet.memberships.get(1); Membership mship1_3 = (Membership) input[0].fuzzySet.memberships.get(2); Membership mship2_1 = (Membership) input[1].fuzzySet.memberships.get(0); Membership mship2_2 = (Membership) input[1].fuzzySet.memberships.get(1); Membership mship2_3 = (Membership) input[1].fuzzySet.memberships.get(2); w[0] = (mship1_1.GetOutput(inputValue1) * mship2_1.GetOutput(inputValue2)); w[1] = (mship1_1.GetOutput(inputValue1) * mship2_2.GetOutput(inputValue2)); w[2] = (mship1_1.GetOutput(inputValue1) * mship2_3.GetOutput(inputValue2)); w[3] = (mship1_2.GetOutput(inputValue1) * mship2_1.GetOutput(inputValue2)); w[4] = (mship1_2.GetOutput(inputValue1) * mship2_2.GetOutput(inputValue2)); w[5] = (mship1_2.GetOutput(inputValue1) * mship2_3.GetOutput(inputValue2)); w[6] = (mship1_3.GetOutput(inputValue1) * mship2_1.GetOutput(inputValue2)); w[7] = (mship1_3.GetOutput(inputValue1) * mship2_2.GetOutput(inputValue2)); w[8] = (mship1_3.GetOutput(inputValue1) * mship2_3.GetOutput(inputValue2)); } // Deffuzify the output to a crisp one public float deffuzify() { float temp=0; float temp2=0; for(int i=0; i= a && value <= d){ fill(255,200); if(value >= b && value <= c){ quad(a, diagramHeight, b, 0, c, 0, d, diagramHeight); }else{ if(value < b){ lineSegmentIntersection(a, diagramHeight, b, 0, value, diagramHeight, value, 0); float tempX = X; float tempY = Y; if(c != d){ lineSegmentIntersection(X, Y, X+(d-X), Y, d, diagramHeight, c, 0); quad(a, diagramHeight, tempX, tempY, X, Y, d, diagramHeight); }else{ quad(a, diagramHeight, tempX, tempY, tempX+(d-tempX), tempY, d, diagramHeight); } }else if(value > c){ lineSegmentIntersection(d, diagramHeight, c, 0, value, diagramHeight, value, 0); float tempX = X; float tempY = Y; if(a != b){ lineSegmentIntersection(X, Y, X-(X+a), Y, a, diagramHeight, b, 0); quad(a, diagramHeight, X, Y, tempX, tempY, d, diagramHeight); }else{ quad(a, diagramHeight, (tempX-(tempX-a)), tempY, tempX, tempY, d, diagramHeight); } } } } } // Draws a line for the current value stroke(100,100); float value = map(input[i].fuzzySet.checkValue(input[i].value),input[i].fuzzySet.minValue,input[i].fuzzySet.maxValue,0,diagramWidth); line(value, diagramHeight, value, 0); // Writes the input name fill(255,150); textFont(myFont); textAlign(LEFT); text(input[i].name,0,0-5); } popMatrix(); } // Function to find where to lines intersect each other boolean lineSegmentIntersection( float Ax, float Ay, float Bx, float By, float Cx, float Cy, float Dx, float Dy){ float distAB, theCos, theSin, newX, ABpos ; // Fail if either line segment is zero-length. if (Ax==Bx && Ay==By || Cx==Dx && Cy==Dy) return false; // Fail if the segments share an end-point. if (Ax==Cx && Ay==Cy || Bx==Cx && By==Cy || Ax==Dx && Ay==Dy || Bx==Dx && By==Dy) { return false; } // (1) Translate the system so that point A is on the origin. Bx-=Ax; By-=Ay; Cx-=Ax; Cy-=Ay; Dx-=Ax; Dy-=Ay; // Discover the length of segment A-B. distAB=sqrt((float)Bx*(float)Bx+(float)By*(float)By); // (2) Rotate the system so that point B is on the positive X axis. theCos=Bx/distAB; theSin=By/distAB; newX=Cx*theCos+Cy*theSin; Cy =Cy*theCos-Cx*theSin; Cx=newX; newX=Dx*theCos+Dy*theSin; Dy =Dy*theCos-Dx*theSin; Dx=newX; // Fail if segment C-D doesn't cross line A-B. if (Cy<0. && Dy<0. || Cy>=0. && Dy>=0.) return false; // (3) Discover the position of the intersection point along line A-B. ABpos=Dx+(Cx-Dx)*Dy/(Dy-Cy); // Fail if segment C-D crosses line A-B outside of segment A-B. if (ABpos<0. || ABpos>distAB) return false; // (4) Apply the discovered position to line A-B in the original coordinate system. X=Ax+ABpos*theCos; Y=Ay+ABpos*theSin; // Success. return true; } } // Class for a input variable class Variable{ FuzzySet fuzzySet; String name; float value; Variable(String _name){ name = _name; value = 0; } Variable(FuzzySet _fuzzySet, String _name){ fuzzySet = _fuzzySet; name = _name; value = 0; } void setValue(float _value){ value = _value; } void addFuzzySet(FuzzySet setToAdd){ fuzzySet = setToAdd; } } // Class for a fuzzy set class FuzzySet{ ArrayList memberships; String name; float minValue; float maxValue; FuzzySet(){ memberships = new ArrayList(); minValue = 0; maxValue = 0; } FuzzySet(float _minValue, float _maxValue){ minValue = _minValue; maxValue = _maxValue; memberships = new ArrayList(); } void addMembership(String name, float a, float b, float c, float d){ memberships.add(new Membership(name, checkValue(a), checkValue(b), checkValue(c), checkValue(d))); } float checkValue(float _value){ float value = _value; if(_value < minValue) value = minValue; if(_value > maxValue) value = maxValue; return value; } } //class for a membership class Membership{ float a; float b; float c; float d; String name; Membership (String _name, float _a, float _b, float _c, float _d){ a = _a; b = _b; c = _c; d = _d; name = _name; } public float GetOutput(float input){ float temp, temp2; temp = (input - a) / (b - a); temp2 = (d - input) / (d - c); temp = min(temp, min(1, temp2)); return (max(temp, 0)); } }