/r/fuzzylogic

Photograph via snooOG

A subreddit for fuzzy logic, fuzzy sets, and related material.

/r/fuzzylogic

299 Subscribers

1

Tutorial for a mini fuzzy system for traffic categorization

On a busy street, there are endless amount of cars, buses and trucks who are driving in both directions. The objective is to count and categorize them. For reason of simplification there are only three length-related categories available:

carlength=Neuron({"short":[0,5], "medium":[5,10], "long":[10,20]})

The measurement apparatus provides a signal with the precise length in meters, for example: 4.52 meters. Such kind of precision isn't needed in statistics so the signal gets discretized into a categorical variable with the help of a binning algorithm. The method "setcrisp()" iterates over all possible categories and determines the membership function for each category. In other words, it checks, if the measured signal fits to one of the bins. The result is stored in the neuron.

def setcrisp(self,crisp):
  for i in self.data:
    a=self.data[i][0]
    b=self.data[i][1]
    self.fuzzyvalue[i]=self.getmf(a,b,crisp)

To investigate the system under practical conditions, some random measurements are needed which are simulated in a loop:

for i in range(10):
  signal=round(random.random()*20,2)
  carlength.setcrisp(signal)
  print(signal,carlength.fuzzyvalue)

The overall program is available in the appendix. After starting the program, the following result gets generated:

5.84 {'short': 0, 'medium': 1, 'long': 0}
14.38 {'short': 0, 'medium': 0, 'long': 1}
11.65 {'short': 0, 'medium': 0, 'long': 1}
9.27 {'short': 0, 'medium': 1, 'long': 0}
6.36 {'short': 0, 'medium': 1, 'long': 0}
7.07 {'short': 0, 'medium': 1, 'long': 0}
18.64 {'short': 0, 'medium': 0, 'long': 1}
9.68 {'short': 0, 'medium': 1, 'long': 0}
9.85 {'short': 0, 'medium': 1, 'long': 0}
5.55 {'short': 0, 'medium': 1, 'long': 0}

What we see is a list with measured car length in the simulation and the mapped category to the signal. For example, the first line assigns the length=5.84 meter value to the category "medium".

There is no need for using dedicated Fuzzy membership functions which are working with overlapping categories. In the simplified example, all the categories are boolean. That means, a car belongs either to the medium or to long category, but never to both at the same time. This fits better to classical statistics and it allows to convert the data into a histogram.

import random

class Neuron():
  def __init__(self,data):
    self.data=data
    self.fuzzyvalue={}
  def setcrisp(self,crisp):
    for i in self.data:
      a=self.data[i][0]
      b=self.data[i][1]
      self.fuzzyvalue[i]=self.getmf(a,b,crisp)
  def getmf(self,a,b,crisp): # Binning membership function
    # input: [0,4] 1.5, return: value between 0 and 1
    if a <= crisp < b: return 1 # in between
    else: return 0

if __name__ == '__main__':
  carlength=Neuron({"short":[0,5], "medium":[5,10], "long":[10,20]})
  for i in range(10):
    signal=round(random.random()*20,2)
    carlength.setcrisp(signal)
    print(signal,carlength.fuzzyvalue)
0 Comments
2024/06/22
05:17 UTC

3

Tutorial for a minimalistic fuzzy system

The best way to learn something in computer science is a hands on approach. Instead of using existing software created by someone else, the idea is to implement an algorithm from scratch. Not because the resulting software is more efficient, but because it allows to learn something about a domain.

Programming an entire Fuzzy inference system (FIS) is a very complex task which goes beyond this mini tutorial. What can be explained instead, is how to implement a single neuron as a building block for a larger FIS. Instead of using multiple Fuzzy membership functions only a simplified membership function is introduced which is basically a binning algorithm.

The program starts with a Python class which provides a membership function:

class Neuron():
  def __init__(self,data):
    self.data=data
    self.fuzzyvalue={}
  def getmf(self,a,b,crisp): 
    # Binning membership function
    # input: [0,4] 1.5, return: value between 0 and 1
    if a <= crisp < b: return 1 # in between
    else: return 0

This class gets initialized from the command line by creating a new neuron for storing the x_position of a measured signal:

xpos=Neuron({"left":[-4,0], "right":[0,4]})

To apply a crisp value to the Neuron, some additional methods are needed. The sourcecode of the class including a simple demo are given at the end of this tutorial. It takes less than 30 lines of code. The example shows how to assign random values to the xpos Neuron and investigate what the translated Fuzzy values are. The idea is, that a value like x=2 gets converted into multiple bins for the left and right position. In the example, the x=2 gets converted into {'left': 0, 'right': 1} That means it belongs to the category right:

xpos=Neuron({"left":[-4,0], "right":[0,4]})
xpos.setcrisp(2)
xpos.show()

Binning allows to categorize data. There are intervals available and the algorithm decides which of the intervals fits to the input data.

Let me give another example. Suppose we send the crisp value x=5 to the neuron:

xpos=Neuron({"left":[-4,0], "right":[0,4]})
xpos.setcrisp(5)
xpos.show()
# output: {'left': 0, 'right': 0}

In such a case, no interval fits to the value. The bins for (left, right) are both empty. This is because the membership function, which was initialized first, returns for the x=5 value a 0 in return.

With this explanation, this mini tutorial ends. A possible further exercise would be to create a second neuron which stores the yposition and contains of the bins for (up, down). Feel free to implement this neuron.

class Neuron():
  def __init__(self,data):
    self.data=data
    self.fuzzyvalue={}
    self.reset()
  def reset(self): # set fuzzy values to 0
    for i in self.data:
      self.fuzzyvalue[i]=0
  def show(self):
    print(self.data)
    print(self.fuzzyvalue)
  def setcrisp(self,crisp): 
    for i in self.data:
      a=self.data[i][0]
      b=self.data[i][1]
      self.fuzzyvalue[i]=self.getmf(a,b,crisp)
  def getmf(self,a,b,crisp): 
    # Binning membership function
    # input: [0,4] 1.5, return: value between 0 and 1
    if a <= crisp < b: return 1 # in between
    else: return 0
    
if __name__ == '__main__':
  xpos=Neuron({"left":[-4,0], "right":[0,4]})
  for i in range(-5,5):
    print(i)
    xpos.setcrisp(i)
    xpos.show()
    xpos.reset()
0 Comments
2024/06/20
10:36 UTC

3

Binning of a continuous variable

2 Comments
2024/06/18
08:56 UTC

3

Fuzzy logic crate

Hey guys. Recently i have been developing a fuzzy logic crate. It's has been a great journey and I learned a lot. As of now I have implemented Both Mamdani and TSK inference systems. I have a road map in mind and want to improve it continuously. You can check it out from links below. My next step is to add type 2 fuzzy systems. I am currently styding Mechatronics Engineering for my masters degree so I need your help to point out my mistakes and help me improve it. Thank you guys

https://crates.io/crates/fuzzy-logic_rs

https://github.com/MechaNeurons/fuzzy-logic-rs

2 Comments
2024/05/18
20:52 UTC

2

Fuzzy logic for uncertain demand

Hi guys, I am searching for a way to express uncertain demand in fuzzy logic in my mathematical model. It is an optimization model with an objective function to minimize the total cost.

Basically I am looking for a way to generate random variables with fuzzy logic. Any ideas or support?

Thanks.

1 Comment
2024/05/18
09:44 UTC

2

Proving that the minimum t-norm is a joint possibility distribution.

Greetings. I don't know how much anyone here knows about the subject, so Imma define some things before asking the question properly.

A t-norm is an operator $T:[0,1]^{2} \rightarrow[0,1]$ which is comutative, monotonic, associative and has 1 as an identity element, that is, $T(1,x)=T(x,1)=x$.

A joint possibility distribution (JPD) of the fuzzy numbers $A_{1},\cdots,A_{n}$ is a fuzzy subset $C$ of $\mathbb{R}^{n}$ such that $A_{i}(x_{i})=\max_{x_{j}\in\mathbb{R},j\neq i}C(x_{1},x_{2},\cdots,x_{n}),$ for every $i.$

$T(x,y)=\min\{x,y\}$ is a t-norm, and apparently, a JPD, too. That is, given two fuzzy numbers $A,B,$ we have $\max_{x\in\mathbb{R}}\min\{A(x),B(y)\}=B(y)$ and $\max_{y\in\mathbb{R}}\min\{A(x),B(y)\}=A(x).$ But I can't seem to prove this last bit. The references I'm using only stablish that it's true, and move on. I'd like a proof that the minimum t-norm is a JPD for a given list of $n$ fuzzy numbers. It seems simple, so I'm probably not seeing something here. But then again, many things in fuzzy set theory "seem" simple. I thank anyone that tries to help in advance.

2 Comments
2024/04/08
01:09 UTC

2

Determine the fuzzy value in Python, but how?

I'd like to write a Python3 routine (but not python2.7 and not in C++) which determines the fuzzy value of two float values. The function should be called with setoperation("or",0.2,0.5) and returns 0.5 in this case. The possible operations are “or” and “and”. The idea is, that using fuzzy values instead of boolean variables might have an advantage in robotics control because it's working similar to an analog computer which produces smoother results. Any help is welcome.

2 Comments
2024/04/07
12:40 UTC

2

Can someone Help me with defining decision Rule base for energy optimization using fuzzy logic

Now I am currently working on implementing fuzzy logic for optimizing energy in smart building I have used antecedent now I want to define the rule base to create fuzzy labels

5 Comments
2024/03/22
04:25 UTC

2

Question about fuzzy logic toolboxes

Hello everyone So I have a question. I will be part of a research team and we have to use a fuzzy logic toolbox. I am tasked with finding some useful toolboxes and I was hopping someone here has a recommendation. Price or programming language is not a concern.

3 Comments
2023/10/15
12:13 UTC

3

Using Fuzzy logic for waterfall detection. Is this modeling correct?

Hello, I’m using Fuzzy Logic in an automatic waterfall detection algorithm (using geoprocessing). The idea is to use three parameters: river volume, slope and a quality indicator. Below are the graphs of each membership function:

(1) Membership function of the fuzzy set "good volume values for the existence of waterfalls":

https://preview.redd.it/67s4iirsg6tb1.png?width=600&format=png&auto=webp&s=612c1afa43a5e30c4f5832223baf5c7de9852d89

(2) Membership function of the fuzzy set "good slope values for the existence of waterfalls":

https://preview.redd.it/lqc2g5jtg6tb1.png?width=600&format=png&auto=webp&s=d7289ebbb3590dc3d4266228abdfdeffcc4e44fb

(3) Membership function of the fuzzy set "good quality indicator values for the existence of waterfalls"

https://preview.redd.it/nwq9px9ug6tb1.png?width=600&format=png&auto=webp&s=11c597c70265d57feed579e622680069d89c85c5

Notice that for each parameter there is only 1 explicit fuzzy set (because I made a simple separation of what is or is not appropriate to find waterfalls). To know if a detection is good or not, I use the following logic:

IF 
   (quality indicator is good AND volume is good AND slope is good) OR
   (quality indicator is more or less good AND volume is very good AND slope is very good) OR
   (quality indicator is very good AND volume is more or less good AND slope is very good) OR 
   (quality indicator is very good AND volume is very good AND slope is more or less good) OR 
THEN 
   detection is good

Mathematically:

max(
   min(good_quality, good_volume, good_slope),
   min(sqrt(good_quality), good_volume^2, good_slope^2),
   min(good_quality^2, sqrt(good_volume), good_slop^2),
   min(good_quality^2, good_volume^2, sqrt(good_slope))
)

Note that the strategy is: when one of the parameters is no so good, the others must compensate (they must be 'very good'). For each detection I use the result of the above formula, thus resulting in a number between 0 and 1. I do nothing after that, at most one alpha-cut to remove bad detections (i.e. removing detections above 0.5).

Does this modeling look right in terms of fuzzy logic? I read about Mamdani’s Fuzzy system, but it involves consequences with membership values and a stage of defuzzification. But I think for my use case I wouldn’t need this... am I correct?

I already ran this model and the results look good to me, but I wanted to make sure it wasn’t just a coincidence.

(my native language is not English... let me know if anything has not been explained well)

1 Comment
2023/10/09
13:28 UTC

2

Rice cookers are amazing

Hey guys, 🖖 I want to talk to you today about fuzzy logic rice cookers and how they are a great example of mapping words to numbers. I recently got one of these rice cookers and I have to say, it has been a game-changer in my kitchen. Here are some reasons why I think fuzzy logic rice cookers are amazing:

Fuzzy-logic rice cookers have computer chips 🤖 that direct their ability to make proper adjustments to cooking time and temperature. They use numbers to incorporate non-definitive words like "slightly" or "almost" into their decision-making processes, which helps to ensure properly cooked rice.

I highly recommend getting one if you are in the market for a new rice cooker. Excuse me for being so excited, but I just can't help it! .

0 Comments
2023/09/26
06:44 UTC

3

Fuzzy logic

Fuzzy logic is a way for computers or robots to make decisions that are a bit uncertain or not just black-and-white. It's like when we say something is "sort of tall" or "kind of fast." Fuzzy logic helps the computer understand and deal with these "in-between" situations, using math that lets it be more flexible and human-like in its thinking. It's all about handling things that are not super clear-cut and making smarter choices in those situations.

0 Comments
2023/08/13
12:49 UTC

2

Book Preview: Neuro Symbolic Reasoning and Learning

1 Comment
2023/07/29
06:29 UTC

3

Master degree thesis about fuzzy logic in hydroponic system

Hello. Im into hydroponic and I created a automated hydroponic farm. I'm trying to write my master degree about it but creating a farm is not sufficient for mater degree. I want to implement fuzzy logic algorithms in example fuzzy PID to control the farm but I have no idea what research can i do for it. Coluld you give me advice what i can research/compare/analyze. My hydrophonic farm is on esp32

4 Comments
2023/06/13
08:50 UTC

2

Talking with a chatbot about Fuzzy logic

Eliza: How can i help you?

Peter: Hm, I'm interested in the subject of Fuzzy logic. Tell me about it.

Eliza: Fuzzy logic was invented by Lotfi Zadeh in 1965. In contrast to boolean logic it allows to handle uncertainty. Therefor it is called maybe logic.

Peter: That information is from wikipedia. Tell me something which i doesn't know yet?

Eliza: My ability to answer is restricted to the questions, i have received. Please reformulate the request.

Peter: You doesn't know anything about Membership function, nor linguistic variables, right?

Eliza: Why are asking, do you want to talk about your mother?

Peter: This is not funny. And you are a chatbot right?

Eliza: My ability to answer is restricted to the questions, i have received. Please reformulate the request.

Peter: Ok, tell me about the difference between Fuzzy and boolean logic.

Eliza: Boolean logic works with only two numbers: 0 or 1. In contrast, Fuzzy logic stores inbetween values like, a bit true, yes/no at the same time and full true. This graded amount of truth fits better to the reality.

Peter: You mean, that if Linus Torvalds answers a question for a backdoor with yes but at the same time he says no, it is an example for fuzzy logic?

Eliza: Sorry, but time is over. Good by.

Peter: See you later, and thanks for the interview.

0 Comments
2023/06/06
17:07 UTC

6

What books should I read on fuzzy logic?

2 Comments
2023/04/25
03:05 UTC

5

How to use output of one fuzzy controller as an input of another fuzzy controller?

I am trying to create a temperature control system based on fuzzy logic for a roasting machine where the input is based on the weight of the load. The output of this controller will be the setpoint temperature and the other functions of the machine are dependent on that setpoint so I need to use it as an input as well but I don't know how to do that. Can anyone help me regarding on this matter? Is there any way?

2 Comments
2023/03/10
09:14 UTC

4

how to learn fuzzy logic online

I have a course in my uni which is called " FUZZY MULTICRITERIA DECISION TECHNIQUES "

and the course content is mentioned below I wonder if you guys can recommend me a way to learn these online. I have looked through Udemy and Coursera but I couldn't find any course which explains similar topics.

1 The definition of decision-making problems Concept of fuzzy sets

2 Concept of fuzzy sets (cont.)

3 Analytic Hierarchy Process (AHP) Fuzzy AHP

4 Technique for Order Preference by Similarity to Ideal Solution (TOPSIS) Fuzzy TOPSIS

5 VIKOR (Vise Kriterijumska Optimizacija I Kompromisno Resenje) Fuzzy VIKOR

6 Elimination and Choice Translating Reality (ELECTRE) Fuzzy ELECTRE

7 Analytic Network Process (ANP) Fuzzy ANP

8 Midterm exam

9 Multi-attribute utility theory (MAUT) Fuzzy MAUT

10 Simple Additive Weighting (SAW) Fuzzy SAW

11Comparison of Multi-Criteria Decision Methods (MCDM) and examination of co-use of methods

12 Comparison of Multi-Criteria Decision Methods (MCDM) and examination of co-use of methods (cont.)

13 Other MCDM presentations

14 Other MCDM presentations (cont.)

0 Comments
2023/02/15
17:37 UTC

3

Hello need some help

https://preview.redd.it/3f7soojb2nda1.png?width=761&format=png&auto=webp&s=71b4b8e0d5b277c2b6cb26541441b649e524c8e3

Need some help with this .Basically what it is asking is whats the distance between those 2 fuzzy parabolas numbers .Don't need anyone to solve or something but any link towards those type of things so i can research it or what the [ah,bh] and the equivalent on E(h) what are those?

1 Comment
2023/01/22
18:35 UTC

0

Which note taking technique is recommended for studying Fuzzy logic?

I want to read some literature about Fuzzy logic at Google scholar and have recognized that lots of papers are available (>1 million). I've heard, that students are preferring dedicated note taking software like Endnote or Citavi for making notes. Another option is to make notes with the Zettelkasten method introduced by Niklas Luhmann. Which of them is recommended?

2 Comments
2023/01/11
12:51 UTC

3

How do you implement a fuzzy logic system?

1 Comment
2022/12/10
16:55 UTC

2

Software?

I'm curious as to what software people here have used to implement fuzzy logic? Did you 'roll your own', use a commercial tool, something else?

6 Comments
2022/12/07
14:50 UTC

2

Sensor anchoring without Fuzzy logic

Fuzzy logic advocates are claiming that maybe logic is the only technique to connect low level with high level data. In most cases normal Boolean logic is working well enough. For example if the idea is that Room A is located at a certain position in the map, the representation can be formalized with:

if (x>20 and x<40 and y>0 and y<20) then label=”Room A”
1 Comment
2022/12/05
10:39 UTC

3

Membership Functions Other Than Triangles and Trapezoids

I notice that many of the publicly-described fuzzy logic systems employ triangular and trapezoidal membership functions. Granted, they are easy to code and understand, but I've wondered whether bell-shaped curves or sigmoids which vary more smoothly might not better serve this purpose? Has anyone experimented with different functions? What has your experience been?

5 Comments
2022/12/03
14:59 UTC

Back To Top