nrc.fuzzy
Class FuzzyValueVector

java.lang.Object
  |
  +--nrc.fuzzy.FuzzyValueVector
All Implemented Interfaces:
java.io.Serializable

public class FuzzyValueVector
extends java.lang.Object
implements java.io.Serializable

The FuzzyValueVector class implements a growable array of FuzzyValues. Like an array, the values can be accessed using an integer index. However, the size of a FuzzyValueVector can grow as needed to accommodate the addition of Intervals after the FuzzyValueVector has been created. A FuzzyValueVector is the used within the fuzzy package to store the antecedents and conclusions of in a FuzzyRule.

See Also:
Serialized Form

Field Summary
protected  nrc.fuzzy.FuzzyValue[] fuzzyValues
          The array of FuzzyValue values.
protected  int increment
          The increment amount by which the FuzzyValueVector expands when it reaches capacity and has to insert another FuzzyValue.
protected  int index
          The current number of FuzzyValues in the array, and the index of the array at which the next FuzzyValue should be inserted.
protected static int INITIAL_CAPACITY
          The default initial capacity of the FuzzyValueVector.
 
Constructor Summary
FuzzyValueVector()
          Creates an empty FuzzyValueVector with the default initial capacity and increment values.
FuzzyValueVector(nrc.fuzzy.FuzzyValue[] FuzzyValueArray, int length)
          Creates a FuzzyValueVector with the length specified in the parameters, and initialized for the full length of the FuzzyValueVector from the FuzzyValue values contained in the passed array.
FuzzyValueVector(int initialCapacity)
          Creates an empty FuzzyValueVector with the specified initial capacity and the default increment value.
FuzzyValueVector(int initialCapacity, int increment)
          Creates an empty FuzzyValueVector with the specified initial capacity and increment value.
 
Method Summary
 void addFuzzyValue(nrc.fuzzy.FuzzyValue fuzzyValue)
          Adds a FuzzyValue to the end of the FuzzyValueVector, increasing its size by one.
 void concat(nrc.fuzzy.FuzzyValueVector other)
          Concatenates the FuzzyValueVector argument with this FuzzyValueVector.
 nrc.fuzzy.FuzzyValue fuzzyIntersection()
          Returns a FuzzyValue that is the intersection of all of the FuzzyValues that this FuzzyValueVector contains.
 nrc.fuzzy.FuzzyValue fuzzyUnion()
          Returns a FuzzyValue that is the union of all of the FuzzyValues that this FuzzyValueVector contains.
 nrc.fuzzy.FuzzyValue fuzzyValueAt(int i)
          Returns the FuzzyValue at the specified index.
 void insertFuzzyValueAt(nrc.fuzzy.FuzzyValue fuzzyValue, int i)
          Inserts the FuzzyValue argument into the FuzzyValueVector at the specified index.
 boolean isEmpty()
          Tests whether or not the FuzzyValueVector is empty, ie.
 double maximumDefuzzify()
          Finds the mean of maxima of the x-values in all of the FuzzySets in the FuzzyValues of the FuzzyValueVector.
 double momentDefuzzify()
          Moment defuzzification defuzzifies a FuzzyVector of FuzzySets returning a floating point (double value) that represents the moment of these FuzzySets.
 void removeFuzzyValueAt(int i)
          Removes the FuzzyValue from the FuzzyValueVector at the specified index.
 void setFuzzyValueAt(nrc.fuzzy.FuzzyValue fuzzyValue, int i)
          Sets the FuzzyValue in the FuzzyValueVector at the specified index.
 int size()
          Returns the size of the FuzzyValueVector, or the number of FuzzyValues in the FuzzyValueVector.
 nrc.fuzzy.FuzzyValue[] toFuzzyValueArray()
          Returns the array of FuzzyValues, trimmed to size, that this FuzzyValueVector contains.
 java.lang.String toString()
           
 void trimToSize()
          Trims the capacity of this FuzzyValueVector to be the FuzzyValueVector's current size.
 double weightedAverageDefuzzify()
          Finds the weighted average of the x-values in all of the FuzzySets in the FuzzyValues of the FuzzyValueVector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INITIAL_CAPACITY

protected static final int INITIAL_CAPACITY
The default initial capacity of the FuzzyValueVector. This default initial capacity is only used when the initial capacity of the FuzzyValueVector is not a parameter passed to the constructor used.

See Also:
Constant Field Values

increment

protected int increment
The increment amount by which the FuzzyValueVector expands when it reaches capacity and has to insert another FuzzyValue. This increment value can be set by choosing a constructor that allows you to specify it.


fuzzyValues

protected nrc.fuzzy.FuzzyValue[] fuzzyValues
The array of FuzzyValue values. This array is manipulated in such a way that it will expand as required to accomodate the addition of FuzzyValues after the FuzzyValueVector is created.


index

protected int index
The current number of FuzzyValues in the array, and the index of the array at which the next FuzzyValue should be inserted.

Constructor Detail

FuzzyValueVector

public FuzzyValueVector()
Creates an empty FuzzyValueVector with the default initial capacity and increment values.


FuzzyValueVector

public FuzzyValueVector(int initialCapacity)
Creates an empty FuzzyValueVector with the specified initial capacity and the default increment value.

Parameters:
initialCapacity - the int which specifies the desired initial capacity of the FuzzyValueVector. This is best used when it is known that the FuzzyValueVector will be quite large so as to avoid multiple expansions of the FuzzyValueVector, which is inefficient.

FuzzyValueVector

public FuzzyValueVector(int initialCapacity,
                        int increment)
Creates an empty FuzzyValueVector with the specified initial capacity and increment value.

Parameters:
initialCapacity - the int to specify the desired initial capacity of the FuzzyValueVector. This is best used when it is known that the FuzzyValueVector will be quite large so as to avoid multiple expansions of the FuzzyValueVector, which is inefficient.
increment - the int to specify the desired increment of the FuzzyValueVector

FuzzyValueVector

public FuzzyValueVector(nrc.fuzzy.FuzzyValue[] FuzzyValueArray,
                        int length)
Creates a FuzzyValueVector with the length specified in the parameters, and initialized for the full length of the FuzzyValueVector from the FuzzyValue values contained in the passed array.

Parameters:
FuzzyValueArray - the array of FuzzyValues which contains the FuzzyValues with which to initialize the FuzzyValueVector.
length - this parameter is basically equivalent to the initial capacity parameter in the other constructors. However, it has one additional implication. It is expected that length is less than or equal to the length of FuzzyValueArray.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the length parameter is greater than the length of FuzzyValueArray.
Method Detail

addFuzzyValue

public void addFuzzyValue(nrc.fuzzy.FuzzyValue fuzzyValue)
Adds a FuzzyValue to the end of the FuzzyValueVector, increasing its size by one.


concat

public void concat(nrc.fuzzy.FuzzyValueVector other)
Concatenates the FuzzyValueVector argument with this FuzzyValueVector. The values contained within the FuzzyValueVector argument are effectively added to the end of this FuzzyValueVector.

Parameters:
other - the FuzzyValueVector to concatenate onto the end of this FuzzyValueVector

fuzzyValueAt

public nrc.fuzzy.FuzzyValue fuzzyValueAt(int i)
Returns the FuzzyValue at the specified index.

Parameters:
i - the index in the FuzzyValueVector of the desired FuzzyValue
Returns:
the FuzzyValue at the specified index

setFuzzyValueAt

public void setFuzzyValueAt(nrc.fuzzy.FuzzyValue fuzzyValue,
                            int i)
Sets the FuzzyValue in the FuzzyValueVector at the specified index. If the index is < 0 or > the highest valid index in the vector nothing is done.

Parameters:
i - the index in the FuzzyValueVector of the FuzzyValue position that is desired to be set

removeFuzzyValueAt

public void removeFuzzyValueAt(int i)
Removes the FuzzyValue from the FuzzyValueVector at the specified index. The subsequent values in the FuzzyValueVector are then shifted down one index position. If the specified index is < 0 or > the largest valid index for the vector, nothing is done.

Parameters:
i - the position of the FuzzyValue to remove

insertFuzzyValueAt

public void insertFuzzyValueAt(nrc.fuzzy.FuzzyValue fuzzyValue,
                               int i)
Inserts the FuzzyValue argument into the FuzzyValueVector at the specified index. The FuzzyValue at the specified index and all subsequent values in the FuzzyValueVector are then shifted up one index position. If the specified index is < 0 then it is treated as 0. If the index is > the highest valid index the value is added to the end of the vector.

Parameters:
i - the index position of the FuzzyValueVector into which the FuzzyValue argument is to be inserted

trimToSize

public void trimToSize()
Trims the capacity of this FuzzyValueVector to be the FuzzyValueVector's current size. This can be used to minimize the storage of a FuzzyValueVector.


size

public int size()
Returns the size of the FuzzyValueVector, or the number of FuzzyValues in the FuzzyValueVector.

Returns:
the size of the FuzzyValueVector, or the number of FuzzyValues contained within the FuzzyValueVector

isEmpty

public boolean isEmpty()
Tests whether or not the FuzzyValueVector is empty, ie. contains zero FuzzyValues.

Returns:
true if the FuzzyValueVector is empty, if it contains zero FuzzyValues

toFuzzyValueArray

public nrc.fuzzy.FuzzyValue[] toFuzzyValueArray()
Returns the array of FuzzyValues, trimmed to size, that this FuzzyValueVector contains.

Returns:
the FuzzyValue[] of the values contained by the FuzzyValueVector. The length of the returned FuzzyValue vector is the current size of the FuzzyValueVector; in other words, the exact number of FuzzyValues.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

fuzzyUnion

public nrc.fuzzy.FuzzyValue fuzzyUnion()
                                throws XValueOutsideUODException,
                                       IncompatibleFuzzyValuesException
Returns a FuzzyValue that is the union of all of the FuzzyValues that this FuzzyValueVector contains. All of the FuzzyValues in the FuzzyValueVector must have the same FuzzyVariable.

Returns:
the FuzzyValue that is the union of all of the FuzzyValues that this FuzzyValueVector contains.
Throws:
XValueOutsideUODException - if the fuzzy set x values are not within the range of the universe of discourse
IncompatibleFuzzyValuesException - if the fuzzy values do not have identical fuzzy variables the operation cannot be done

fuzzyIntersection

public nrc.fuzzy.FuzzyValue fuzzyIntersection()
                                       throws XValueOutsideUODException,
                                              IncompatibleFuzzyValuesException
Returns a FuzzyValue that is the intersection of all of the FuzzyValues that this FuzzyValueVector contains. All of the FuzzyValues in the FuzzyValueVector must have the same FuzzyVariable.

Returns:
the FuzzyValue that is the intersection of all of the FuzzyValues that this FuzzyValueVector contains.
Throws:
XValueOutsideUODException - if the fuzzy set x values are not within the range of the universe of discourse
IncompatibleFuzzyValuesException - if the fuzzy values do not have identical fuzzy variables the operation cannot be done

weightedAverageDefuzzify

public double weightedAverageDefuzzify()
                                throws XValuesOutOfOrderException,
                                       InvalidDefuzzifyException,
                                       IncompatibleFuzzyValuesException
Finds the weighted average of the x-values in all of the FuzzySets in the FuzzyValues of the FuzzyValueVector. The weights are the membership values. This is the defuzzification value for the FuzzyValueVector. This is slightly different than the maximumDefuzzify since the maximumDefuzzify uses only points that have the same membership value (the one that is the maximum in the set of points). The weightedAverageDefuzzify uses all of the points with non-zero membership values and calculates the average of the x values using the membership values as weights in this average.
NOTE: All FuzzyValues in the FuzzyValueVector MUST have the same FuzzyVariable.
NOTE: This doesn't always work well because there can be x ranges where the y value is constant at the max value and other places where the max is only reached for a single x value. When this happens the single value gets too much of a say in the defuzzified value. So use moment defuzzify in cases like this.
             /------\                   /\
            /        \                 /  \
         --/          \---------------/    \-------------
                  ^       ^
                  |       |
                  |       | gives this as the weighted average
                  | this is more reasonable???

 
However, it is especially effective if the output has a number of singleton points in it. This might be, for example, when one has only singleton values describing the output fuzzy values (Takagi-Sugeno-Kang zero order type rules). In this case one gets a result that would be expected and one that would result in an exception with the momentDefuzzify (area is 0) and a poor result with the maximumDefuzzify (since only the values with the max membership are used - 20 in the example below).
    
      1.0                 |
                          |
      0.5        |        |
                 |        |        |
         --------|--------|--------|--------
         0      10        20       30      40
                        ^
                        |
                        | gives 18.57 as the weighted average   
 
      (10*0.5 + 20*1 + 30*0.25) / (0.5 + 1 + 0.25) = 18.57

 
Centre of gravity (moment) defuzzify is likely more useful most of the time.
Where would one use this? Normally when rules fire and fuzzy outputs are generated the FuzzyValue outputs with the same FuzzyVariable are combined (global contribution) using a fuzzyUnion operation. Then a defuzzification operation is performed on this resultant FuzzyValue after all the rules have fired. But sometimes the union operation would 'hide' some of the results. For example in the example above it might be that some rules generated singletons at the same x value. The union operation would combine these 2 x values and look as if there was just one (the one with the largest membership value). Then the weighting would not take into account that there were 2 outputs at this x value. Now in most case this is the desired effect but in some cases it is not. In order to get the desired effect, the union must not be done and the collection of outputs can be saved in a FuzzyValueVector. Then the FuzzyValueVector can use this method to allow all values to be used in the calculation.
NOTE: All FuzzyValues in the FuzzyValueVector MUST have the same FuzzyVariable.

Returns:
the floating value that is the weighted average of the values of all of the FuzzySets in the FuzzyValues in the FuzzyValueVector.
Throws:
InvalidDefuzzifyException - there are no points in the FuzzySet(s)
XValuesOutOfOrderException - occurs when the MinUOD is greater than or equal to the MaxUOD parameter (this should never happen).
IncompatibleFuzzyValuesException - if the fuzzy values do not have identical fuzzy variables or there are no FuzzyValues in the FuzzyValueVector the operation cannot be done

maximumDefuzzify

public double maximumDefuzzify()
                        throws XValuesOutOfOrderException,
                               InvalidDefuzzifyException,
                               IncompatibleFuzzyValuesException
Finds the mean of maxima of the x-values in all of the FuzzySets in the FuzzyValues of the FuzzyValueVector. NOTE: This doesn't always work well because there can be x ranges where the y value is constant at the max value and other places where the max is only reached for a single x value. When this happens the single value gets too much of a say in the defuzzified value. So use moment defuzzify in cases like this.
NOTE: All FuzzyValues in the FuzzyValueVector MUST have the same FuzzyVariable.

             /------\                   /\
            /        \                 /  \
         --/          \---------------/    \-------------
                 ^       ^
                 |       |
                 |       | gives this as the mean of maximum
                 | this is more reasonable???

 

Returns:
the floating value that is the first average of the maximum values of the FuzzySets in the FuzzyValues of the FuzzyValueVector.
Throws:
InvalidDefuzzifyException - there are no points in the FuzzySet
XValuesOutOfOrderException - occurs when the xMin parameter is greater than or equal to the xMax parameter.
IncompatibleFuzzyValuesException - if the fuzzy values do not have identical fuzzy variables or there are no FuzzyValues in the FuzzyValueVector the operation cannot be done

momentDefuzzify

public double momentDefuzzify()
                       throws XValuesOutOfOrderException,
                              InvalidDefuzzifyException,
                              IncompatibleFuzzyValuesException
Moment defuzzification defuzzifies a FuzzyVector of FuzzySets returning a floating point (double value) that represents the moment of these FuzzySets. It calculates the first moment of area of these FuzzySets about the y axis. Each set is subdivided into different shapes by partitioning vertically at each point in the set, resulting in rectangles, triangles, and trapezoids. The centre of gravity (moment) and area of each subdivision is calculated using the appropriate formulas for each shape. The first moment of area of all of the FuzzySets is then:
      sum of ( moment(i) * area(i) )          <-- top
      ------------------------------
          sum of (area(i))                    <-- bottom
  
If the total area is 0 then throw an exception since the moment is not defined.
NOTE: All FuzzyValues in the FuzzyValueVector MUST have the same FuzzyVariable.

Returns:
the floating value that is the first moment of the fuzzy set
Throws:
InvalidDefuzzifyException - there is no valid moment for the fuzzy set (generally the area is 0 under the fuzzy set graph)
XValuesOutOfOrderException - occurs when the xMin parameter is greater than or equal to the xMax parameter.
IncompatibleFuzzyValuesException