# Fuzzy logic in prolog

**URL:** <https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194>\
**Category:** Help!\
**Created:** [January 25, 2024, 10:45am UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194 "2024-01-25T10:45:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 25, 2024, 10:45am UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/1 "2024-01-25T10:45:08Z")

</div>

I’m trying to create a fuzzy logic in Swi prolog for obtain the % of having COVID, based on number of symptoms, number of contacts and strict contacts. I’m also working with an ontology, so I’m taking data from there (so I know the number of symp,contacts and strict contatcs a Patient has). I tried with Bousi Prolog, but it gives me different problem , for example it doesn’t recognise some predicates that i have to use . In swi prolog this is what I did, but it doesn’t seem a very fuzzy logic. Any help on how I can implement rules for that?

```prolog
:- use_module(library(clpfd)).
:- consult(fatti). % consult facts from ontology

sintomi_fuzzy(X,Y):-
( X in 0..0 -> Y is 0 ;
  X in 1..2 -> Y is 0.45 ;
  X >= 3 -> Y is 0.6 ).

strict_contacts(X,Y):-
( X in 0..0 -> Y is 0 ;
  X in 1..2 -> Y is 0.5 ;
  X >= 3 -> Y is 0.8 ).

no_strict_contacts(X,Y):-
( X in 0..0 -> Y is 0 ;
  X in 1..2 -> Y is 0.3 ;
  X >= 3 -> Y is 0.4 ).

cerca_sintomi(Patient,List):- % find symptoms
	findall(Symptoms,propertyAssertion('http://www.isibang.ac.in/ns/codo#hasSymptom',Patient,Symptoms),List).

lunghezza_lista([], 0). % La lunghezza di una lista vuota è 0

lunghezza_lista([_|Coda], Lunghezza) :-
    lunghezza_lista(Coda, LunghezzaCoda), 
    Lunghezza is LunghezzaCoda + 1 . 

verifica_relazioni(Patient,[], []). 

verifica_relazioni(Patient,[Testa|Coda], ListaRisultato) :-
    	propertyAssertion('http://www.isibang.ac.in/ns/codo#hasCovid',Testa,literal(type('http://www.w3.org/2001/XMLSchema#boolean',true))),

    verifica_relazioni(Patient,Coda, ListaRisultatoCoda), % Continua a controllare la coda della lista
    ListaRisultato = [Testa | ListaRisultatoCoda]. % Aggiungi Testa alla lista risultato

verifica_relazioni(Patient,[_|Coda], ListaRisultato) :-
    verifica_relazioni(Patient,Coda, ListaRisultato). 
	
	
trova_relazioni(Patient,L1):-
			findall(People,propertyAssertion('http://www.isibang.ac.in/ns/codo#hasRelationship',Patient,People),L1).

find_prob(Patient,P):-
       # S is the Y from sympthoms (the probability to have covid with X symptoms)
       # C is the Y from strict contacts ( ..)
       # C1 is the Y from contacts (..)
	       trova_relazioni(Patient,L1),
	verifica_relazioni(Patient,L1,Ris),
	lunghezza_lista(Ris,N1), % N1 is number of contacts the patient has

	cerca_sintomi(Patient,List),
	 lunghezza_lista(List,N), % N is number of symptoms the patients has

       sintomi_fuzzy(N,S),
       strict_contacts(2,C),
       no_strict_conctacts(n1,C1),
      (C1 > 0 -> P is (S + C + C1) / 3 ; 
       P is (S + C) / 2 ).

```

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [January 26, 2024, 4:32pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/2 "2024-01-26T16:32:01Z")

</div>

I was hoping somebody who knew something about fuzzy logic would step up, but that hasn’t happened yet, so here’s an uninformed perspective.

As far as I’m aware, neither (SWI-)Prolog or `library(clpfd)`natively support the notion of fuzzy logic so you’re going to have to provide more information on what you mean by that term, hopefully with some actual examples (queries and answers) of what you want to achieve.

Googling “fuzzy logic SWI-Prolog” I came across this paper [  
Fuzzy Prolog: A Simple General Implementation using CLP(_R_)  
 ](https://cliplab.org/papers/fuzzy-lpar02_bitmap.pdf) which might help and also includes an introductory summary of how the problem has been tackled in general. I suggest you start there. (I do know a bit about CLP if you get to that point.)

---

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 26, 2024, 5:26pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/3 "2024-01-26T17:26:38Z")

</div>

Hi, thank you for your replying! I read it but didn’t find something for my project. Summarising my project, I want that when I ask "find\_prob(Patient, P) it gives me something like P=0.7 (70% to have covid). Based on number of symptoms, number of contacts and strict contacts. I did this just using math and then I Got P adding those values (each one with a weight) and then take the average. But I don’t think this is “fuzzy”

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [January 26, 2024, 5:45pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/4 "2024-01-26T17:45:56Z")

</div>

> [@MrAlex](#):
>
> I read it but didn’t find something for my project.

I suggest you read it again; skip the theory and start with section 3. The fuzzy logic Prolog library appears to allow you to specify facts with a probability “interval” between 0 and 1 (which could be a single value in the range), and rules with aggregation operators for accumulating the probabilities of the subgoals.

It also appears that all software described in the paper is included with Ciao Prolog; sounds to me like it’s worth a try. (Perhaps it’s portable to SWIP with some effort if there’s a reason for doing that.)

EDIT: see also [https://www.cl.uni-heidelberg.de/courses/ws08/logik/seminarslides/10\_Knapp.pdf](https://www.cl.uni-heidelberg.de/courses/ws08/logik/seminarslides/10_Knapp.pdf)

also a different implementation of fuzzy Prolog:

> [@BPL 3.2 Released](https://swi-prolog.discourse.group/t/bpl-3-2-released/519):
>
> Hello. This is just for informing that the new release of: Bousi~Prolog version 3.2 [https://dectau.uclm.es/bousi-prolog](https://dectau.uclm.es/bousi-prolog) has been launched on April, 6th, 2019, and tested for: SWI-Prolog 7.6.4 running on: Ubuntu 18 LTS, CentOS 7.6-1810, Windows 10, and macOS High Sierra Bousi~Prolog (BPL) is a fuzzy logic programming language that replaces the syntactic unification mechanism of classical SLD-resolution by a fuzzy unification algorithm. This algorithm provides a weak most general unifier…

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [January 26, 2024, 6:35pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/5 "2024-01-26T18:35:04Z")

</div>

Also this:

> [@DES 6.3 Released](https://swi-prolog.discourse.group/t/des-6-3-released/1388):
>
> Hello. This is a mail just for informing that the new release Datalog Educational System version 6.3 [http://des.sourceforge.net](http://des.sourceforge.net) has been launched on October, 14th, 2019, and tested for SICStus Prolog 4.4.1 SWI-Prolog 7.4.2 The current version features a novel contribution to fuzzy logic programming: Fuzzy Hypothetical Datalog. Hypothetical scenarios can be specified with both fuzzy rules and equations. In addition to assume new rules in a hypothetical context, the semantics of a exist…

---

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 26, 2024, 7:32pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/6 "2024-01-26T19:32:53Z")

</div>

Thank you again. I tried my code on Ciao prolog, but once again I have a lot of errors (same happened with Bousi), it only works on Swi prolog. It seems I can only use CLP, since the fuzzy logic library does not exist in Swi prolog. Also, when I try to run those example on Swi prolog (the ones explained in clp paper) it gives me errors, it doesn’t recognise some commands

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [January 26, 2024, 8:18pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/7 "2024-01-26T20:18:13Z")

</div>

> [@MrAlex](#):
>
> I tried my code on Ciao prolog, but once again I have a lot of errors (same happened with Bousi), it only works on Swi prolog. It seems I can only use CLP, since the fuzzy logic library does not exist in Swi prolog. Also, when I try to run those example on Swi prolog (the ones explained in clp paper) it gives me errors, it doesn’t recognise some commands

Sorry, too little information to say anything. You’re getting “a lot of errors” (unspecified) on all the systems you’ve tried. Again, CLP does not natively support fuzzy logic so you must be attempting to model fuzzy logic using CLP. If that’s not producing the expected results, then there’s something wrong with the model, i.e., your program.

And I wouldn’t expect example programs which assume some other fuzzy logic implementation, to work on SWI-Prolog unless that implementation has been ported to SWIP.

If you want to get any help here, you’re going to have to start with a small program (1-2 predicates) and an example query that shows how the expected result differs from the actual result.

---

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 27, 2024, 11:57am UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/8 "2024-01-27T11:57:59Z")

</div>

Yep true, but they give me so many errors that I should write them all xD. But the common thing, is that they don’t recognise some predicates that I have used for my program (Swi prolog does) or they have problems on how I have structured the program.  
So my question is how I can create a fuzzy model on swi prolog using Clp, or better, which step should I Follow?  
The things I did are :

- define variables and their range and for each range give a percentage , for example if you have 0 symptoms 0% you have covid, if you have 1…2 symptoms then 0.4…and I did the same with other variables  
After that, I created a rule where I put all this "single percentage " together, for obtaining the final % to have covid.  
I wanted to know if this is a correct approach.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 27, 2024, 12:15pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/9 "2024-01-27T12:15:22Z")

</div>

I wonder whether you are talking fuzzy logic or probabilistic logic. What you are trying to do might be related to the work of @friguzzi and his team. You find the demos at [http://cplint.ml.unife.it/](http://cplint.ml.unife.it/)

---

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 27, 2024, 12:30pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/10 "2024-01-27T12:30:43Z")

</div>

It’s a project I have for my university, where they asked me to use fuzzy logic for obtaining the final result. Btw I don’t know how to apply it, also cuz from my dataset I have the specific number of my variables , so it doesn’t seem so fuzzy

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [January 27, 2024, 12:35pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/11 "2024-01-27T12:35:31Z")

</div>

You don’t need constraint programming for fuzzy logic. Are talking about [the same thing](https://en.wikipedia.org/wiki/Fuzzy_logic)?

You need basic logic and math for fuzzy logic as far as I understand.

---

<div class="post-metadata">

**Author:** ![anon419083](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@anon419083](https://swi-prolog.discourse.group/u/anon419083)\
**Post date:** [January 27, 2024, 12:47pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/12 "2024-01-27T12:47:44Z")

</div>

It seems it’s fuzzy…

To get help you should share the code you’re working at. This way people might help you. Otherwise it’s like screaming “help” on the phone without saying where you are…

NB: unfortunately I’m not in the position to help…but others may possibly

---

<div class="post-metadata">

**Author:** ![MrAlex](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MrAlex](https://swi-prolog.discourse.group/u/MrAlex)\
**Post date:** [January 27, 2024, 12:56pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/13 "2024-01-27T12:56:59Z")

</div>

Yep, but How do I aggregate and defuzzificate if I am working on specific number? In my case, number of symptoms is a specific number and not something like (few,a lots…). That’s is my doubt, how do I apply fuzzy if I’m working on specific values and not on something that is uncertainy ? Tha’ts is why I don’t understand what kind of math formula apply in prolog for obtain the final result.

PS: i could post the entire code, but it’s very very long , since I have all the data from my ontology

---

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [January 27, 2024, 3:10pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/14 "2024-01-27T15:10:27Z")

</div>

You are confusing yourself. Just because the input variables are known (e.g. 3 symptoms), doesn’t mean downstream variables aren’t fuzzy. e.g. if they have 2 or 3 symptoms, the truth value that they covid is 0.4. It’s fuzzy because it isn’t either 1 or 0.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [January 27, 2024, 3:49pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/15 "2024-01-27T15:49:14Z")

</div>

It is possible that you need to read the available material a bit more carefully. The Wikipedia article I linked already seems to answer your question? Or maybe it doesn’t.

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [January 27, 2024, 4:56pm UTC](https://swi-prolog.discourse.group/t/fuzzy-logic-in-prolog/7194/16 "2024-01-27T16:56:59Z")

</div>

For an example of medical diagnosis with probabilistic logic programming, you may look at  
[https://www.cplint.it/e/03\_medical\_diagnosis\_solution.swinb](https://www.cplint.it/e/03_medical_diagnosis_solution.swinb)  
If you want fuzzy logic, you can look at [https://arxiv.org/pdf/1107.4747v1.pdf](https://arxiv.org/pdf/1107.4747v1.pdf) section 7 discussing possibilistic logic programming, which in a way is a form of fuzzy logic programming.  
That algorithm is not currently available in `cplint` but can be easily added modifying 2 lines of code in  
[https://github.com/friguzzi/cplint/blob/804265fc5156b3d6ab8c6e14c3484c7991e841d7/prolog/pitaind.pl](https://github.com/friguzzi/cplint/blob/804265fc5156b3d6ab8c6e14c3484c7991e841d7/prolog/pitaind.pl)
