Introduction to Fuzzy Logic

You are presented with Ethernet cables, even if your keyword is “internet” cables, thanks to the mall’s fuzzy search algorithm.


SuggestionsAfter shopping and just waiting for your Ethernet cable to be delivered, you decided to browse the clothes section because you just feel like it.

A few seconds later you stumbled on a section called the Recommend Items section.

A fuzzy recommendation app will suggest product recommendations based on items you’ve previously purchased, or even products you’ve just looked into.

Now, in order for you to better understand how Fuzzy Logic works, we’ll need to go a little deeper to learn the basics.

You’re ready?Classical Set TheoryTo start off with, we’ll first head to the simple branch of mathematics that deals with sets — the Classical Set Theory.

In classical set theory, a set is a collection of element and stuff.

Take a look at the image below:We can have a set of numbers of 1 to 5, a set of characters between A to E, and even a set of words such asHTML, JavaScript, CSS, and PHP.

Fuzzy SetsFuzzy sets can be considered as an extension of classical set.

In classical set, it’s either you or you don’t belong in a set whereas in Fuzzy set, you can belong in one, either by half, quarter, or even a tiny bit.

For example, when we say cold, it doesn’t mean it’s entirely freezing cold, or that when we say hot, it doesn’t mean it’s entirely burning hot.

We usually specify a degree of coldness or hotness, mostly in the form of temperature regardless of whether it is Celsius or it is Fahrenheit.

Of course, it’s up to you on how would you put those realistic values in a range of 0 and 1.

Think of 0 and 1 as percentages written in decimal.

1 is 100% and is given to the best value while 0 is 0% and is given for the worst one.

Linguistic Variables and ValuesLinguistic variables are variables where words or even sentences derived from the language we use on a daily basis.

Linguistic values are essentially the values of linguistic variables.

temperature (t) = {cold, warm, hot}With the above example, temperature is your linguistic variables, and cold, warm, and hot are the linguistic values.

Membership Function and Fuzzy RulesIn a fuzzy set, the membership function defines a value or point.

For this example, we can have three membership functions as an input, say:ColdWarmHotWhile we can also have three membership functions as output:CoolNo changeHeatFuzzy Rules takes in the simple IF-THEN rule with a condition and conclusion.

Mapping the membership functions above into Fuzzy Rules, we have the following statements:IF (temperature is COLD) AND (target is COLD) THEN command is NO-CHANGEIF (temperature is COLD) AND (target is WARM) THEN command is HEATIF (temperature is COLD) AND (target is HOT) THEN command is HEATIF (temperature is WARM) AND (target is COLD) THEN command is COOLIF (temperature is WARM) AND (target is WARM) THEN command is NO-CHANGEIF (temperature is WARM) AND (target is HOT) THEN command is HEATIF (temperature is HOT) AND (target is COLD) THEN command is COOLIF (temperature is HOT) AND (target is WARM) THEN command is COOLIF (temperature is HOT) AND (target is HOT) THEN command is NO-CHANGEThe table below shows the 3 x 3 representation for the fuzzy rules:Fuzzy Set OperationsThe evaluations of the fuzzy rules and the combination of the results of the individual rules are performed using fuzzy set operations.

Fuzzy set operations differ from standard logic operations.

UnionThis is where the output contains all the elements of both sets.


IntersectionThis is where the output contains only the common element of both sets.


NegationThis is where the output contains everything that is not found in the set.


AggregationThis is where the output contains the combination of fuzzy sets representing the outputs of each rule.

DefuzzificationBecause the output of Fuzzy Set Operations is a fuzzy value, we need some way to convert it into values that machines can understand.

This is known as defuzzification.

A fuzzy value can be defuzzified in different ways.

Some of them are:Center of SumsCentroid MethodCenter of AreaWeighted Average MethodMax-Membership PrincipalApplying Fuzzy Logic in PHPAs developers, we were asked by the HR team to create an app that needs to pick the best candidate out of the applicants applying for the Web Developer position.

To keep things simple for this blog, we were given only the following criteria to judge:● The applicants’ year of experience in developing web applications.

● Their score in their practical test.

● Their score in Team and HR interview.

Imagine that we have the following list of applicants with their features:Constructing the codeFirst, we’re going to implement the membership and aggregation functions that we need.

There are many aggregation functions, but for simplicity we will use one of the famous aggregation functions, which is the arithmetic mean.

/** * Get the arithmetic mean of the features.

* @param array $aParams * @return float|int */private function getArithmeticMean(array $aParams){ $mCount = (float)count($aParams); $mSum = (float)array_sum($aParams); return $mSum / $mCount;}Then we will use the triangular function as the membership function.

/** * Triangular membership function * @param $fApplicantValue * @param $aTrimfValues * @return float|int */private function getTrimf(float $fApplicantValue, array $aTrimfValues){ $aTrimfValues[0] = (float)$aTrimfValues[0]; $aTrimfValues[1] = (float)$aTrimfValues[1]; $aTrimfValues[2] = (float)$aTrimfValues[2]; if ($fApplicantValue < $aTrimfValues[0]) { return 0; } if ($fApplicantValue >= $aTrimfValues[0] && $fApplicantValue <= $aTrimfValues[1]) { return ($fApplicantValue – $aTrimfValues[0]) / ($aTrimfValues[1] – $aTrimfValues[0]); } if ($fApplicantValue > $aTrimfValues[1] && $fApplicantValue <= $aTrimfValues[2]) { return ($aTrimfValues[1] – $fApplicantValue) / ($aTrimfValues[2] – $aTrimfValues[1]) + 1; } return 0;}After the aggregate and membership function has been implemented, we need to add the rest of the code to process the input.

/** * Analyze the following features, and applicants * @param array $aSetFeatures * @param array $aApplicants * @return array */public function analyzeItems(array $aSetFeatures, array $aApplicants): array{ $aResults = []; for ($iCount = 0, $iCountMax = count($aApplicants); $iCount < $iCountMax; $iCount++) { $aResults[$iCount]['identifier'] = $aApplicants[$iCount]['identifier']; $aResults[$iCount]['score'] = $this->aggregateSet($aApplicants[$iCount], $aSetFeatures); } return $aResults;}/** * @param array $aApplicant * @param array $aSetFeatures * @return float|int */private function aggregateSet(array $aApplicant, array $aSetFeatures){ $aFeatureScores = []; for ($iCount = 0, $iCountMax = count($aSetFeatures); $iCount < $iCountMax; $iCount++) { $aFeatureScores[$iCount] = $this->getTrimf($aApplicant['features'][$aSetFeatures[$iCount]['identifier']], $aSetFeatures[$iCount]['values']); } return $this->getArithmeticMean($aFeatureScores);}After adding the process, we need to specify the features we will use when analyzing the list of applicants.

Since we’re using the triangular membership function, the second parameter in the values parameter is our ideal value.

For example, in experience_years, we have the following values: 0, 3, 5.

This means that the preferred year of experience is 3 among those who don’t have a year of experience yet and those who already had 5 years of experience and above.

$this->aSetFeatures = [ [ 'identifier' => 'experience_years', 'values' => [0, 3, 5] ], [ 'identifier' => 'practical_test_score', 'values' => [0, 100, 100] ], [ 'identifier' => 'team_interview_score', 'values' => [0, 5, 5] ], [ 'identifier' => 'hr_interview_score', 'values' => [0, 10, 10] ]];We will now need to specify the inputs for each applicant:$this->aApplicants = [ [ 'identifier' => 'Applicant A', 'features' => [ 'experience_years' => 3, 'practical_test_score' => 87.

30, 'team_interview_score' => 5, 'hr_interview_score' => 9 ] ], [ 'identifier' => 'Applicant B', 'features' => [ 'experience_years' => 4, 'practical_test_score' => 88.

25, 'team_interview_score' => 3, 'hr_interview_score' => 8 ] ], [ 'identifier' => 'Applicant C', 'features' => [ 'experience_years' => 4, 'practical_test_score' => 81.

67, 'team_interview_score' => 5, 'hr_interview_score' => 7 ] ], [ 'identifier' => 'Applicant D', 'features' => [ 'experience_years' => 1, 'practical_test_score' => 91.

90, 'team_interview_score' => 4, 'hr_interview_score' => 7 ] ], [ 'identifier' => 'Applicant E', 'features' => [ 'experience_years' => 1, 'practical_test_score' => 89.

58, 'team_interview_score' => 4, 'hr_interview_score' => 8 ] ], [ 'identifier' => 'Applicant F', 'features' => [ 'experience_years' => 2, 'practical_test_score' => 89.

49, 'team_interview_score' => 4, 'hr_interview_score' => 7 ] ], [ 'identifier' => 'Applicant G', 'features' => [ 'experience_years' => 4, 'practical_test_score' => 98.

94, 'team_interview_score' => 4, 'hr_interview_score' => 8 ] ], [ 'identifier' => 'Applicant H', 'features' => [ 'experience_years' => 4, 'practical_test_score' => 80.

80, 'team_interview_score' => 4, 'hr_interview_score' => 8 ] ], [ 'identifier' => 'Applicant I', 'features' => [ 'experience_years' => 2, 'practical_test_score' => 82.

97, 'team_interview_score' => 4, 'hr_interview_score' => 8 ] ], [ 'identifier' => 'Applicant J', 'features' => [ 'experience_years' => 2, 'practical_test_score' => 81.

91, 'team_interview_score' => 3, 'hr_interview_score' => 7 ] ]];Finally, we have to add the code to run the analyzer and display the results.

/** * Run analyzer and return the results.

* @return Factory|View */public function index(){ $aResults = $this->analyzeItems($this->aSetFeatures, $this->aApplicants); return view('index', compact($aResults));}Output:Using the triangle membership function and the arithmetic mean aggregation, we could say that among others, Applicant A is the most preferred applicant to be hired.

But take note, however, we didn’t specify any weights in this demo.

Also, the Arithmetic Mean and Triangular Membership function doesn’t really fit with this kind of scenario — we’ve only used this because it’s simple and should give you an idea of how Fuzzy Logic works in PHP.

You could also visit this GitHub repository to learn more.

This is where most of the demo code was based.

If you like, you can also read the references below.

Anyways, peace out!.✌References:Fuzzy Sets* by L.


ZadehA Short Fuzzy Logic TutorialWhat is Fuzzy Logic?Introduction to fuzzy logicTemperature Control using Fuzzy LogicArtificial Intelligence — Fuzzy Logic Systemsketili/fuzzydm.

. More details

Leave a Reply