ECS 110 - Spring 1997

Homework 2 - Fuzzy Logic

Due 11:59 PM, Friday, May 2, 1997




Fuzzy logic is an extension of conventional (Boolean) logic which is meant to capture the notion that, in the real world, truth values may fall somewhere between being completely true and completely false. The idea, and a great deal of the theory, was developed by Dr. Lotfi Zadeh of UC Berkeley, beginning in the 1960's. 

Use of fuzzy logic is becoming increasingly popular and (especially in Japan!) one will find lots of consumer products which proudly advertise its use. 

Fuzzy truth assignments

In Boolean logic, a truth assignment, f, maps each variable from the universe of possible variables, S, into the two-point set {0,1} (representing false and true): 

       f:S -> {0,1}

In Fuzzy logic, a truth assignment, f, maps each variable to a real number in the interval [0,1]: 

       f:S -> [0,1]

The intent is that if f(X)=1 then X is completely true; if f(X)=0 then X is completely false; and if f(X) is somewhere in between, well, then X is neither wholly true nor false, but is true to the extent indicated by f

In Fuzzy logic, f can be used in interesting ways. For example, let's talk about the property ``the person is tall.'' Suppose I write a statement such as "Frank is Tall". Instead of simply saying this statement is true or false, a more accurate characterization of the truthfulness of this statement might be to develop a function like: 

 
                  / 0                            if person.height < 5.5 ft.,
   Tall(person) = | 2 * (person.height - 5.5)    if 5.5 <= person.height <= 6.0, and
                  \ 1                            if person.height > 6.0 ft.

Now we can interpret "Frank is Tall" as "Tall(Frank)" and the function Tall (playing the role of f) indicates the degree to which "Frank is tall" should be regarded as a true statement. 

Fuzzy logic

OK, now that we understand what something like Tall(Frank) means, the question is how to we interpret something like 

    (Tall(Frank) and Short(Sue)) or (Young(Paul))

The standard definitions in fuzzy logic for these operators are: 

    truth (not X)   = 1.0 - truth(X)
    truth (X and Y) = minimum (truth(X), truth(Y))
    truth (X or Y)  = maximum (truth(X), truth(Y))

(Some researchers have explored the use of other interpretations for AND and OR.) Note that if you plug just the values 0 and 1 into these definitions, you get the same truth tables as you would expect from conventional Boolean logic. Of course truth(f(X)) is defined as f(X). 

Formulas

We now describe what formulas look like. A letter is any of the symbols a,..., z, A,...,Z. A word is a sequence of 1 or more letters. A reserved word is any of the following words: not, and, or, and implies. A name is a word which is not a reserved word. For example, "John" and "Tall" are names. A non-empty string which is any sequence of spaces, tabs, and newlines is called whitespace. A statement is any name followed by a left parenthesis followed by a comma-separated list of names followed by a right parenthesis. A statement may also contain whitespace before or after a comma or parenthesis. For example, "Tall(John)" and "MARRIED(Alice, Bob)" are statements. For simplicity in your assignment, no statement may have more than two names (arguments) inside the parentheses. The following are formulas

  1. A statement is a formula.
  2. If s is a formula then not s is a formula.
  3. If s and t are formulas then s and t is a formula.
  4. If s and t are formulas then s or t is a formula.
  5. If s and t are formulas then s implies t is a formula.
  6. If s is a formula, then (s) is a formula.

In addition, formulas may contain whitespace on either side of statements, reserved words, or parentheses. We require a formula to contain whitespace to separate a reserved word from an adjoining statement. 

The order of precedence is: not (highest), then and, then or, then implies. Parentheses can be used in the usual way. The semantics of P implies Q is defined by not P or Q

In your input file, each formula will be terminated by a period.  If a formula does not parse correctly just print an error message and go on to the next formula. 

Sample input

Here is an example of a valid input file: 

               age      height     weight.
Bill            19        5.75       155
Yan             20        5.9        140
.
Tall(Bill) and Tall(Yan).
Tall(Bill) and not Tall(Bill).
(SimilarHeight(Bill,Yan) and Tall(Yan)) implies Tall(Bill).

Above we have specified three attributes (called "age", "height" and "weight") for each of two people (named "Bill" and "Yan"). Notice the period at the end of the attribute list denotes the end of the list.  Also, a period is used to denote the end of the person list. If the above were the input, C++ functions for the statements Tall() and SimilarHeight() would be provided to you. This will be done in files ~cs110/hw2/code/Functions.h and ~cs110/hw2/code/Functions.cpp. There is also a file ~cs110/hw2/code/Main.cpp to illustrate the use of the above. There is also a sample data file ~cs110/hw2/code/hw2-data which you can test with. In particular, you'll have to know the calling convention to get the statements evaluated at the desired points.

If you find it convenient, you may assume some maximal number of people (say 10, but use a const) and some maximal number of attributes (say 20, again, use a const). 

Sample output

You are to evaluate the fuzzy logic formulas. Each function will depend on the attribute(s) of each person in the call. Here is the output associated to the first formula (assuming our earlier definition of Tall

Formula:     Tall(Bill) and Tall(Yan)
In postfix:  Tall(Bill) Tall(Yan) and
Truth value: .50

That is, echo each formula as you read it. Print it in postfix notation. Then, evaluate the formula, calling our functions as needed to get truth values for the statements.

To turn in

Call your executable fuzzy. As before, submit a softcopy using the handin program (of course you need to include a working makefile). Use the input file and file of statements which will be provided to you. Turn in a hardcopy of your program listing along with your writeup. The writeup should detail your approach.