1
$\begingroup$

I am new to prolog and need help on how to write a prolog program. Here is what i am trying to do. I have downloaded the dataset from this link - https://archive.ics.uci.edu/ml/datasets/Blood+Transfusion+Service+Center

My goal is

  1. Use Any of the decision tree algorithms to build a decision tree for the given data
  2. Create rules from the decision tree.
  3. Code the rules into a Prolog Knowledge base
  4. Get the data about the blood donor from the user and predict if the donor has donated in March 2007.

I need some guidance on how to start the work. I have developed algorithms in python and understand #1 and #2 but have no idea on work with #3 and #4.

$\endgroup$
3
  • $\begingroup$ Wow, I didn't think I would hear about prolog on DataScienceSE :) Why do you need to implement the decision tree in Prolog? This looks like a very cumbersome way to use the model. $\endgroup$
    – Erwan
    Commented Mar 20, 2021 at 21:45
  • $\begingroup$ Assignment at school.We dont have a subject on prolog, not sure why i have been given this assignment. $\endgroup$
    – kumar
    Commented Mar 21, 2021 at 17:13
  • $\begingroup$ It's strange, prolog is a programming paradigm which needs a good bit of practice to understand. I'd suggest you ask your professor about this: do they really want you to learn the language? This is usually taught as a full module. $\endgroup$
    – Erwan
    Commented Mar 21, 2021 at 23:53

2 Answers 2

0
$\begingroup$

I can give you some basics but the proper way to do this is to learn the prolog language, and it's very different from any other programming language.

In Prolog you define predicates (similar to functions) which contain conditions for the predicate to be satisfied, for example:

dt(Feat1, Feat2, Feat3, labelA) :-
    Feat1 =< 3,
    Feat2 == 'green',
    Feat3 >= 0.54.

dt(Feat1, Feat2, Feat3, labelB) :-
    Feat1 =< 3,
    Feat2 == 'green',
    Feat3 < 0.54.

dt(Feat1, Feat2, _Feat3, labelC) :-
    Feat1 > 3,
    Feat2 == 'green'.

dt(_Feat1, Feat2, _Feat3, labelB) :-
    Feat2 == 'blue'.
  • Here the predictate is dt and it has 4 clauses. In order for dt to be satisfied at least one of the clauses needs to to be true (i.e. all the conditions it contains must be true).
  • Variables are represented with a capital, for instance Feat1, Feat2, Feat3. Prolog will try to find an instanciation of the variables which satisfies one of the clauses (it will actually try all the possibilities until one of them works, it's a solver).
  • In each clause above the label is the last parameter of the predicate. Basically each clause means "if all the conditions after :- are satisfied, then the label is labelX".

Assuming the 4 clauses above have been saved in a file dt.pl, one can use for instance the SWI Prolog interpreter like this:

?- consult('dt.pl').
true.

Now the way to use this toy decision tree would be:

?- dt(4,'green',0.3, Label).
Label = labelC .

?- dt(4,'blue',0.3, Label).
Label = labelB.
  • Note the capital L for Label: it means that now we let prolog find a value of Label which satisfies the predicate dt given the values provided for the features.

This is the basic idea. A more advanced and more generic representation of the DT would be possible using recursion and unification, but this would require an advanced understanding of Prolog.

$\endgroup$
0
$\begingroup$

Prolog is a theorem proving system using first order predicate logic - a term you can ignore for now. It is better to start by thinking of Prolog as a logical inference system which uses facts and rules to determine if something of interest can be inferred from that information. Facts are expressed as

father(bill, ted).

Note the lower case. Here father is a predicate, and bill, ted are atoms. It is useful to think of atoms as constants. Whether bill is ted's father or vice versa is a matter of interpretation. You will need to enforce the idea that e.g., bill is ted's father by consistent use and interpretation. Inconsistent use is a serious source of bugs. Another fact might be

mother(sandra,ted).

This leads to the idea of the following rule: Someone is ted's parent if they are either his father or mother, expressed like this:

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).

Note the upper case which is used for variables. Prolog uses the term goal for something that is to be satisfied. Satisfaction means Prolog can find sufficient facts to ultimately determine values (called bindings) for your variables

Similarly,

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

comma (",") here indicates sequential goal satisfaction with previous variable bindings ("instantiations" in Prolog-speak) carried forward (think about it).

E.g., if I ask Prolog to satisfy the goal

?- father(X, ted).

It will come back and print

X = bill

meaning it has found a solution (proved a theorem) that there is a father for ted that can be inferred from the facts and rules you have stated.

This works the other way also such that the goal

?- father(bill, X).

It will come back and print

X = ted

meaning it has found a solution that bill is someones father.

If you ask Prolog

?- father(X, joe).

It will come back and say

False

meaning no solution exists from your provided facts and rules.

Finally if you ask Prolog

?- father(F, S).

It will come back and print

F = bill, S = ted

If you ask Prolog

?- parent(P, C).

It will come back and print

P = bill, C = ted

as the first solution since that is the order of the facts you gave that support a conclusion to this theorem. There are higher level you can write that will find all possible solution. many if these built-in to Prolog so you almost would never have to actually do that.

If you require Prolog to find a second conclusion (satisfaction) it will It will come back and print

P = sandra, C = ted

Realistic programs will lead to a series of goal satisfactions in the prescribed manner carring their variable bindings forward to subsequent goals, which is where backtracking (built-in to the mechanics of Prolog) comes in. After some goals have been satisfied, a subsequent one may fail, then Prolog will automatically backtrack and find an alternate solution (variable bindings) to some antecedent goal as far back as necessary to find a solution. If it exhausts all backtracking possibilities it will declare a grand failure meaning to you have no solution.

Thus to craft a Prolog program is to state all the facts of the case, plus whatever rules represent domain knowledge of your problem then ask Prolog to satisfy some top level goal that will provoke Prolog to find a solution.

Finally, Prolog is a serious system. I once reduced a 10K line C++ simulation which was never correct to 1500 lines of Prolog.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.