How to Install Fuzzy CLIPS: A Quick and Easy Setup Tutorial

Ema Suriano - Sep 26 - - Dev Community

Fuzzy CLIPS is an extension of CLIPS (C Language Integrated Production System) designed for handling fuzzy logic. It allows systems to make decisions based on approximate or imprecise information.

Fuzzy CLIPS is an older tool with limited and outdated online documentation. This is the main reason I'm writing this short post.

Why Fuzzy Logic?

Fuzzy logic excels at handling real-world complexity and imprecision, it allows you to model human-like reasoning, deal with ambiguous data, and create more intuitive control systems. By using degrees of truth instead of binary (true/false) decisions, fuzzy logic can provide smoother, more accurate results in situations where traditional logic falls short

It's precious in fields like control engineering, decision support systems, and AI, which can lead to more robust and flexible solutions that better mimic human decision-making processes.

Installing Fuzzy CLIPS

The source code can be found in GitHub but as expected, this cannot be directly executed in our system. We need to compile the program to run it locally.

Let's start by cloning the repository:

$ git clone https://github.com/rorchard/FuzzyCLIPS && cd FuzzyCLIPS/source

Enter fullscreen mode Exit fullscreen mode

Now we are going to make use of the make command which is available by default in OSX and Linux. In case you are running Windows, please check to install using choco (reference).

$ make fzclips

Enter fullscreen mode Exit fullscreen mode

This should create an executable binary called fzclips which we can execute:

$ ./fz_clips

FuzzyCLIPS V6.10d (10/22/2004)
FuzzyCLIPS>

Enter fullscreen mode Exit fullscreen mode

Fuzzy Concepts

To understand how Fuzzy Clips works, is better to do a small recap of some of the core concepts of Fuzzy Logic.

Fuzzy sets : extend classical set theory by allowing partial membership. In a classical set, an element either belongs to the set (1) or doesn't (0). In a fuzzy set, an element can have any value between 0 and 1, indicating its degree of membership.

  • Example: In a fuzzy set for "tall people," someone who is 5'10" might have a membership degree of 0.7, while someone who is 6'2" might have a membership degree of 0.9.

Linguistic variables : These are variables whose values are words or sentences rather than numbers. They allow us to express complex concepts in natural language terms.

  • Example: Temperature could be a linguistic variable with values like "cold," "cool," "warm," and "hot."

Fuzzy rules : These are conditional statements that use linguistic variables, typically in an IF-THEN format. They form the basis of fuzzy reasoning.

  • Example: "IF temperature is hot AND humidity is high THEN comfort is low"

Fuzzification : This is the process of converting crisp (precise) input values into fuzzy values. It involves determining the degree to which these inputs belong to each of the appropriate fuzzy sets.

  • Example: Converting a precise temperature of 28°C into fuzzy values like "0.7 warm" and "0.3 hot"

Inference : This step applies the fuzzy rules to the fuzzified inputs to determine the fuzzy output. It involves evaluating all rules in parallel and combining their results.

  • Example: Applying multiple rules about temperature, humidity, and wind to determine the overall comfort level

Defuzzification : This is the final step where the fuzzy output is converted back into a crisp (precise) value. There are several methods for this, such as the centroid method or mean of maximum.

  • Example: Converting a fuzzy comfort level (e.g., "0.6 uncomfortable") into a specific value on a scale of 1-10

Demo time: Fan Control System

Let's create a fuzzy logic system to control a fan based on temperature and humidity. The system will adjust the fan speed (low, medium, high) depending on the fuzzy input values for temperature and humidity.

Create a new file with the extension .clp and open it with any code editor that you want.

1. Definition of Membership functions

(deftemplate Temperature
  0 40
  ((cold (0 1) (10 0))
   (warm (5 0) (20 1) (30 0))
   (hot (25 0) (40 1) (40 1))))

(deftemplate Humidity
  0 100
  ((low (0 1) (20 0))
   (medium (10 0) (50 1) (90 0))
   (high (70 0) (100 1) (100 1))))

Enter fullscreen mode Exit fullscreen mode

Here, Temperature and Humidity are the fuzzy variables with membership functions cold, warm, hot, low, medium, and high. These are going to be our entry points for the system.

(deftemplate FanSpeed
  0 100
  ((slow (0 1) (30 0))
   (medium (20 0) (50 1) (80 0))
   (fast (60 0) (100 1) (100 1))))

Enter fullscreen mode Exit fullscreen mode

Lastly let's add the FanSpeed with slow, medium, and fast functions, which are going to get activated based on the rules of the system.

2. Definition of rules

Create rules to determine the fan speed based on temperature and humidity:

(defrule cool-fan
  (Temperature cold)
  (Humidity low)
 =>
  (assert (FanSpeed slow)))

(defrule moderate-fan
  (Temperature warm)
  (Humidity medium)
 =>
  (assert (FanSpeed medium)))

(defrule hot-humid-fan
  (Temperature hot)
  (Humidity high)
 =>
  (assert (FanSpeed fast)))

Enter fullscreen mode Exit fullscreen mode

These rules set the fan speed to slow, medium, or fast based on the combination of temperature and humidity.

3. Definition of Facts

Provide sample data for temperature and humidity:

;; Sample facts for temperature and humidity
(deffacts sample-facts
  (Temperature (25 0) (25 1) (25 0))
  (Humidity (40 0) (40 1) (40 0)))

Enter fullscreen mode Exit fullscreen mode

These facts represent a temperature of 25°C and a humidity of 40%.

4. Running the system

Open a new terminal in your system and execute the program fz_clips - the one that we compiled in the beginning. Once running, we need to load our program into the library. For this, we need to use the load function and send the path of the file containing the rules we defined previously:

FuzzyCLIPS> (load ./demo.clp)
Defining deftemplate: Temperature
Defining deftemplate: Humidity
Defining deftemplate: FanSpeed
Defining defrule: cool-fan +j+j
Defining defrule: moderate-fan +j+j
Defining defrule: hot-humid-fan +j+j
Defining deffacts: sample-facts
TRUE

Enter fullscreen mode Exit fullscreen mode

Now we need to initialize the environment, to load all the facts and rules into the memory:

FuzzyCLIPS> (reset)

Enter fullscreen mode Exit fullscreen mode

We can double-check if everything was loaded correctly with:

FuzzyCLIPS> (facts)
f-0 (initial-fact) CF 1.00
f-1 (Temperature ???) CF 1.00
        ( (25.0 0.0) (25.0 1.0) (25.0 0.0) )

f-2 (Humidity ???) CF 1.00
        ( (40.0 0.0) (40.0 1.0) (40.0 0.0) )

For a total of 3 facts.


FuzzyCLIPS> (rules)
cool-fan
moderate-fan
hot-humid-fan
For a total of 3 defrules.

Enter fullscreen mode Exit fullscreen mode

Now it's time to execute the rules based on the asserted facts.

FuzzyCLIPS> (run)

Enter fullscreen mode Exit fullscreen mode

Alternatively, we can make partial execution by specifying the number of steps, for example, rule by rule:

FuzzyCLIPS> (run 1)

Enter fullscreen mode Exit fullscreen mode

At any point, we always check our facts to see the status of our system.

FuzzyCLIPS> (facts)
f-0 (initial-fact) CF 1.00
f-1 (Temperature ???) CF 1.00
        ( (25.0 0.0) (25.0 1.0) (25.0 0.0) )

f-2 (Humidity ???) CF 1.00
        ( (40.0 0.0) (40.0 1.0) (40.0 0.0) )

f-3 (FanSpeed ???) CF 1.00
        ( (20.0 0.0) (35.0 0.5) (65.0 0.5) (80.0 0.0) )

For a total of 4 facts.

Enter fullscreen mode Exit fullscreen mode

As we can see the fact of FanSpeed has been loaded and some values set. Fuzzy clips provides a way to visualize it in the terminal by using the function plot-fuzzy-value:

FuzzyCLIPS> (plot-fuzzy-value t + 0 100 3)

Fuzzy Value: FanSpeed
Linguistic Value: ??? (+)

 1.00
 0.95
 0.90
 0.85
 0.80
 0.75
 0.70
 0.65
 0.60
 0.55
 0.50 +++++++++++++++
 0.45 + +
 0.40 + +
 0.35 + +
 0.30
 0.25 + +
 0.20 + +
 0.15 + +
 0.10
 0.05 + +
 0.00+++++++++++ +++++++++++
     |----|----|----|----|----|----|----|----|----|----|
    0.00 20.00 40.00 60.00 80.00 100.00

Universe of Discourse: From 0.00 to 100.00

Enter fullscreen mode Exit fullscreen mode

Finally, we can perform a defuzzification process on any fact to see its crisp value:

FuzzyCLIPS> (moment-defuzzify 3)
50.0

Enter fullscreen mode Exit fullscreen mode

Fun fact: Clips is compatible with Clojure syntax

In case want to keep a strict formatting in your code, I have good news for you! You can change your syntax language to Clojure and it will format your code accordingly. The main reason is because Clojure tries to keep the same formatting practices as in LISP, therefore it's compatbile with CLIPS as well!

You can easily install any formatter in your code editor and on save you can pass from this:

(deftemplate Humidity
    0 100
    (
        (low (0 1) (20 0))
        (medium (10 0) (50 1) (90 0))
        (high (70 0) (100 1) (100 1))
    )
)

Enter fullscreen mode Exit fullscreen mode

To this

(deftemplate Humidity
  0 100
  ((low (0 1) (20 0))
   (medium (10 0) (50 1) (90 0))
   (high (70 0) (100 1) (100 1))))

Enter fullscreen mode Exit fullscreen mode

Which one is better? I actually don't care so much, what I care is all my code is now formatted in the same way and I don't have to manually start adding spacing.

Another tip, you can use the formatter as some sort of compiler mostly to check if you skip some parenthesis while you were writing. As you can see from the previous example, there quite a lot of them and it can quite easy to forget to close one of them. The formatter will fail in case you have some parenthesis that are not properly closed, which means that you can speed up your process because you don't have to load and execute your program in Fuzzy CLIPS to see the error message.

Closing words

As I mentioned at the beginning of this article, the amount of information about this framework is quite scarce and it took me quite some time to gather all this information. Hopefully, more people will find it handy as well!

Thanks for reading.

. . . . . . . . . . . . . . . . . .
Terabox Video Player