Introduction


The emergence of online markets, has changed the way of interacting with suppliers of different products. More and more, the online shopping method is imposed as everything to supply the particular needs. However, although online shopping is part of a computer system, there are always alternatives to purchase different products, we want to analyze the decisions that are made about where, when and how these products are purchased. We will try to determine the parameters that experts use to perform this type of process, as well as the ways to qualify these activities and in general acquire and translate into an expert system all this information, to achieve this goal we will use and analyze methods such as Delphy among others.

In this research we will use the package “sets” for creating the fuzzy system. For more information about “sets”, check out the official documentation at https://cran.r-project.org/web/packages/sets/sets.pdf

Set up a simple fuzzy system


The first step in creating a fuzzy system using ‘sets’ is to set the range of the system. The range is a boundary in which all values should belong.

Block Diagram

Block Diagram

The survey:

Block Diagram

The variables


The next step is to define the variables of the fuzzy system.

For this fuzzy system, which is produc-related, the variables we chose are ‘Quality’, ‘Seller’ and ‘Price’. So, as mentioned before, the state of the product (bad, ok or perfect), will be determined according to this variables.

The survey database with Google SpreadSheets:

Block Diagram

time seller price quality
27/2/2018 17:17:10 Exelente Costoso Exelente
27/2/2018 17:17:42 Exelente Costoso Exelente
27/2/2018 17:21:02 Regular Justo Aceptable
27/2/2018 17:21:12 Mala Adsequible Mala
27/2/2018 18:00:31 Exelente Costoso Exelente
27/2/2018 18:00:41 Exelente Justo Exelente
27/2/2018 18:31:01 Exelente Costoso Exelente
27/2/2018 19:36:38 Regular Costoso Exelente
1/3/2018 17:40:19 Exelente Costoso Exelente

Price:

price freq
Adsequible 1
Costoso 6
Justo 2

Quality:

quality freq
Aceptable 1
Exelente 7
Mala 1

Seller:

## [1] 11.11111
seller freq
Exelente 6
Mala 1
Regular 2

Charge vars:

variables <- set(
  quality = fuzzy_partition(varnames = c(cold = 30, good = 70, hot = 90),
                                sd = 5.0),
  seller = fuzzy_partition(varnames = c(dry = 30, good = 60, wet = 80), 
                             sd = 3.0),
  price = fuzzy_partition(varnames = c(no.rain = 30, little.rain = 60,
                                               rain = 90), sd = 7.5),
  product = fuzzy_partition(varnames = c(bad = 40, ok = 65, perfect = 80),
                            FUN = fuzzy_cone, radius = 10)
)

You might be wondering what are those values and variables that are inside the parentheses. The answer is that at the time of defining the system, you need to specify the attributes of the variables and give a value to them. For example, the first variable ‘quality’, has three different attributes or levels: ‘cold’, ‘good’ and ‘hot’, with the values of 30, 70 and 90. This means that if the quality (using Fahrenheit this time) is 30, then it is ‘cold’, if it 70, it is ‘good’ and if it is 90, it is ‘hot’. For the variables ‘seller’ and ‘price’ you could read it as “if the seller percentage is 30, then it is dry” or “there’s a 60% chance that it will rain today”.

The last variable ‘product’ is the responsible of defining the state of the system. We will see more of this at the end.

The fuzzy rules


Once the variables are defined, the next step is to define the fuzzy rules of the system. In the previous section, I showed a variable called ‘product’ that is the final state or the response of the system. The fuzzy rules are the links between the “non-final” variables (quality, seller and price) and ‘product’.

# Fuzzy rules
rules <- set(
  fuzzy_rule(quality %is% good && seller %is% dry &&
               price %is% no.rain, product %is% perfect),
  fuzzy_rule(quality %is% hot && seller %is% wet &&
               price %is% rain, product %is% bad),
  fuzzy_rule(quality %is% cold, product %is% bad),
  fuzzy_rule(quality %is% good || seller %is% good ||
               price %is% little.rain, product %is% ok),
  fuzzy_rule(quality %is% hot && price %is% little.rain,
             product %is% ok),
  fuzzy_rule(quality %is% hot && seller %is% dry &&
               price %is% little.rain, product %is% ok)
)

For this system, six rules were defined.

Note the && and ||.

Now, let’s build the system.

The system


model <- fuzzy_system(variables, rules)

The variables and rules of the system.

print(model)
## A fuzzy system consisting of 4 variables and 6 rules.
## 
## Variables:
## 
## product(bad, ok, perfect)
## seller(dry, good, wet)
## quality(cold, good, hot)
## price(no.rain, little.rain, rain)
## 
## Rules:
## 
## quality %is% hot && price %is% little.rain => product %is% ok
## quality %is% hot && seller %is% dry && price %is% little.rain => product %is% ok
## quality %is% hot && seller %is% wet && price %is% rain => product %is% bad
## quality %is% good && seller %is% dry && price %is% no.rain => product %is% perfect
## quality %is% good || seller %is% good || price %is% little.rain => product %is% ok
## quality %is% cold => product %is% bad

This is the plot of the system.

plot(model)

As you can see, some of the plots present overlapping between the properties of the variables. For example, in the quality plot, there is overlapping between ‘good’ and ‘hot’, so if the quality is 80, then it is around 0.15 ‘good’, 0.15 ‘hot’ and 0.0 ‘cold’.

Examples


These are some examples done to test the system.

Temperature = 75, seller = 0 and price = 70

example.1 <- fuzzy_inference(model, list(quality = 75, seller = 0,
                                       price = 70))

Now, we defuzzify the example to transform the parameters into an actual number.

gset_defuzzify(example.1, "centroid")
## [1] 65
plot(example.1)

So, according to the system, the product is 0.6 ok (see the product plot)

Temperature = 30, seller = 0 and price = 70

The next example, is very similar to the previous one. The only difference is the quality parameter, which now is 30. What do you think is going to happen?

example.2 <- fuzzy_inference(model, list(quality = 30, seller = 0,
                                       price = 70))
gset_defuzzify(example.2, "largestofmax")
## [1] 40
plot(example.2)

sets_options("universe", NULL)  # Reset the universe

By lowering the quality, the model lowers the amount of ‘ok’ product to around 0.4 and creates a new ?smoothpeak with global maxima at 40, meaning that the product is 1.0 ‘bad’.

I did a little cheating to get this result. If you look at the variables and the rules, you can see that a quality of 30 is ‘cold’ and that there is a rule stating that if the quality is ‘cold’, then the product is ‘bad’.

Closure


In this tutorial, I introduced the basic of fuzzy logic and presented an example using R. If you are interested in learning more about this topic and how to apply it using R, I recommend the official documentation of the ‘sets’ package linked at the beginning. Also, the Wikipedia page about fuzzy logic is pretty good (https://en.wikipedia.org/wiki/Fuzzy_logic).

Notes