Fuzzy logic in prolog

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?

:- 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 ).

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)
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.)

2 Likes

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”

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

also a different implementation of fuzzy Prolog:

Also this:

1 Like

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

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.

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.

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/

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

You don’t need constraint programming for fuzzy logic. Are talking about the same thing?

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

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

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

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.

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.

For an example of medical diagnosis with probabilistic logic programming, you may look at
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 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