Artificial Intelligence and Machine Learning
ISBN 9788119221196

Highlights

Notes

  

Chapter 1: Logic Programming with Prolog and AI Problems

What is GNU:

    1. GNU Prolog is a free Prolog compiler with constraint solving over finite domains developed by Daniel Diaz.

    2. GNU Prolog accepts Prolog+constraint programs and produces native binaries (like gcc does from a C source). The obtained executable is then stand-alone. The size of this executable can be quite small since GNU Prolog can avoid to link the code of most unused built-in predicates. The performances of GNU Prolog are very encouraging (comparable to commercial systems).

    3. Beside the native-code compilation, GNU Prolog offers a classical interactive interpreter (top-level) with a debugger.

    4. The Prolog part conforms to the ISO standard for Prolog with many extensions very useful in practice (global variables, OS interface, sockets,...).

    5. GNU Prolog also includes an efficient constraint solver over Finite Domains (FD). This opens contraint logic programming to the user combining the power of constraint programming to the declarativity of logic programming.

Prolog: (Programming in logic)

    1. It is a logic programming language.

    2. Logic means Facts and Rules. And based on Facts and Rules we go to the conclusion.

    3. Prolog is a declarative programming language.

    4. Prolog was designed by Alain Colmerauer and Robert Kowaski. It was first implemented in 1973.

How does GNU Prolog work?

The GNU Prolog compiler is based on the Warren Abstract Machine (WAM). It first compiles a Prolog program to a WAM file which is then translated to a low-level machine independent language called mini-assembly specifically designed for GNU Prolog. The resulting file is then translated to the assembly language of the target machine (from which an object is obtained). This allows GNU Prolog to produce a native stand alone executable from a Prolog source (similarly to what does a C compiler from a C program). The main advantage of this compilation scheme is to produce native code and to be fast. Another interesting feature is that executables are small. Indeed, the code of most unused built-in predicates can be excluded from the executables at link-time.

GNU Prolog also includes an efficient constraint solver over Finite Domains (FD). The key feature of the GNU Prolog solver is the use of a single (low-level) primitive to define all (high-level) FD constraints. There are many advantages of this approach: constraints can be compiled, the user can define his own constraints (in terms of the primitive), the solver is open and extensible (as opposed to black-box solvers like CHIP),...Moreover, the GNU Prolog solver is rather efficient, often more than commercial solvers.

Installation of GNU Prolog:

Step 1: Search “download gnu prolog for windows” in google.

Step 2: Go to “GNU prolog website”.

Step 3: Click on the following link:

Step 4: Double-click on the “exe” file and install gnu as follows:

Demonstration of basic commands in prolog (Facts, Rule, and queries) ◀◀◀

Facts: Facts can be defined as explicit relationships between objects and properties of objects. Facts are unconditionally true in nature.

Example:

    1. Tom is a cat.  cat(tom)  This factual expression is called as clause.

    2. Harry is a student.

Note: Anything that is within the parameter that we pass is called as argument and that argument is an object.

Relation: Relation defines the way in which a collection of objects or variables belong together.

Example: student(harry)  here student is a relation with harry.

We can define more than one objects as: relation (object1, object2, object3,……)

Rules: Rules can be defined as explicit relationship between objects. Rules are conditionally true.

Example: Seema is happy if she sings.

We can write above rules as: happpy(seema):-sings(seema)

Create one file in notepad with.pl extension and file type =All type.

Go to GNU  File  Change dir  Select the folder where notepad file is saved.

Code:-

[p1].

 Student(X).

Intelligent(Y).

Demonstration of AND & OR Function in Prolog:◀◀◀

AND function(;)

OR function(,)

Create new notepad file.

likes(pooja,geeta).

 likes(geeta,pooja).

 likes(neha,aliya).

freindship(X,Y):- likes(X,Y);likes(Y,X).

Practice 1.1:

 likes(pooja,geeta).

 likes(geeta,pooja).

 likes(neha,aliya).

 freindship(X,Y):- likes(X,Y),likes(Y,X).

Practice 1.2:

 next_to(mumbai,pune).

 next_to(pune,satara).

 next_to(mumbai,nashik).

travel(A,C):- next_to(A,B),next_to(B,C).

1.3: Relationship in prolog: - Specify relationship between object and properties of objects.◀◀◀

Relationship can also be a rule.

 p3.pl

female(scarlet).

female(alice).

female(katherine).

female(fiona).

male(bob).

male(sean).

male(chris).

male(dravis).

parent(bob, alice).

parent(bob, sean).

parent(scarlet,alice).

parent(scarlet, sean).

parent(alice, katherine).

parent(sean, chris).

parent(katherine,fiona).

parent(chris,dravis).

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

sister(X,Y):- parent(Z,X), parent(Z,Y), female(X), X\=Y.

brother(X,Y):- parent(Z,X), parent(Z,Y), male(X), female(Y).

uncle(X,Y):- parent(Z,Y), brother(X,Z).

aunt(X,Y):- parent(Z,Y),sister(X,Z).

daughter(X,Y):- parent(Y,X), female(X).

son(X,Y):- parent(Y,X), male(X).

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

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

Breadth First Search Algorithm:

Implementation of BFS:◀◀◀

graph = {

‘A’: [‘B’, ‘C’],

‘B’: [‘D’, ‘E’],

‘C’: [‘F’],

‘D’: [],

‘E’: [‘F’],

‘F’: []

}

visited=set()

def dfs(visited,graph,node):

 if node not in visited:

  print(node)

  visited.add(node)

  for neighbour in graph[node]:

   print(“neighbour is” +neighbour)

   dfs(visited,graph,neighbour)

dfs(visited,graph,’A’)

Water Jug Problem:

Implementation of Water Jug Problem◀◀◀

def pour_water(juga, jugb):

 max1, max2, fill=3,4,2

 print(“%d\t\t\t%d” % (juga, jugb))

 if jugb == fill:

  return

 elif jugb == max2:

  pour_water(0, juga)

 elif juga!= 0 and jugb == 0:

  pour_water(0, juga)

 elif juga == fill:

  pour_water(juga,0)

 elif juga < max1:

  pour_water(max1,jugb)

 elif juga < (max2-jugb):

  pour_water(0, (juga+jugb))

 else:

  pour_water(juga-(max2-jugb), (max2-jugb)+jugb)

 print(“Jug A 3ltrs \t Jug B 4ltrs”)

  pour_water(0,0)

Download Annaconda using the following link to use Jupyter Notebook:

https://www.anaconda.com/products/distribution