Vous êtes sur la page 1sur 16

130420107536

Date :
Practical : 1
Introduction to Prolog programming.
Write a program to insert different relationship in terms of facts and rules.
[ relationships such as: parent, child, sister, wife, son, daughter etc.]
Write a program to create friends relationship in terms of facts and rules.

Introduction
Prolog is a general-purpose logic programming language associated with artificial
intelligence and computational linguistics.
Prolog has its roots in first-order logic, a formal logic, and unlike many other programming
languages, Prolog is declarative: the program logic is expressed in terms of relations,
represented as facts and rules. A computation is initiated by running a query over these
relations.

Program: -
parent(tom,bob).
parent(pam,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
gender(liz,female).
gender(pam,female).
gender(tom,male).
gender(ann,female).
gender(liz,female).
gender(pat,male).
gender(jim,male).

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

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

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

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 1
130420107536

aunt(X,Y):-parent(Z,Y),parent(M,X),parent(M,Z),gender(X,female).

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

offspring(Y,X):-parent(X,Y).
daughter(X,Y):-offspring(Y,X),gender(Y,female),X\=Y.

happy(X):-parent(X,Y).

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

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

Output: -

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 2
130420107536

Date :
Practical : 2
Introduction to recursion. Write a program to enhance the program of relationships
including recursive rules. [ relationships such as- ancestors, descendants, predecessors etc.]

Introduction
Recursion occurs when a thing is defined in terms of itself or of its type.
While this apparently defines an infinite number of instances (function values), it is
often done in such a way that no loop or infinite chain of references can occur.

Program: -
parent(john,paul).
parent(paul,tom).
parent(tom,mary).

predecessor (X, Z): parent (X, Z).


predecessor (X, Z): -parent (X, Y), parent (Y, Z).
predecessor (X, Z): -parent (X, Y1), parent (Y1, Y2), parent (Y2, Z)

Output :-

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 3
130420107536

Date :
Practical : 3
Introduce data objects and declarative/ procedural meaning of prolog programs.
Write a program to solve monkey banana problem.

Introduction
The monkey and banana problem is a famous toy problem in artificial intelligence,
particularly in logic programming and planning.
A monkey is in a room. Suspended from the ceiling is a bunch of bananas, beyond the
monkey's reach. However, in the room there are also a chair and a stick. The ceiling is
just the right height so that a monkey standing on a chair could knock the bananas down
with the stick. The monkey knows how to move around, carry other things around,
reach for the bananas, and wave a stick in the air. What is the best sequence of actions
for the monkey?

Program: -
move(state(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)).
move(state(P1,onfloor,P1,H),climb,state(P1,onbox,P1,H)).
move(state(P1,onfloor,P1,H),push1(P1,P2),state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),walk(P1,P2),state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(S1):-move(S1,M,S2),canget(S2).

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 4
130420107536

Output: -

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 5
130420107536

Date :
Practical : 4
Introduce concept of list.
Write a program for list manipulation with following functions.
1. Read list
2. Write list
3. Member
4. Append
5. Insert
6. Delete
7. Search
8. Reverse input string
9. Find length

Introduction
A very important type of recursive term is the list. The recursive definition of a list is: a
list may be nil (i.e. empty) or it may be a term which has a head, which can be any term,
and a tail which is another list. Using standard Prolog notation, we could define the list as:
o is_list(nil).
o is_list(list(Head, Tail)) :-
o list(Tail).
A list of numbers [1, 2, 3] would look like:
o list(1, list(2, list(3, nil)))
Although this notation is consistent with the way Prolog treats all other data structures, it
can be rather clumsy at times. Because lists are used so often, most Prolog implementations
use the alternative, more convenient notation, [1, 2, 3]. Internally, Prolog still stores the list
as if it were entered in the prefix form.

Program: -

hello:-write('Enter name : '),read(X),write('Hello '),write(X).

member1(X,[X|Tail]).
member1(X,[Head|Tail]):-member1(X,Tail).
member2(X,L):-append(L1,[X|L2],L).

add(X,L,[X|L]).

del(X,[X|Tail],Tail).
del(X,[Y|Tail],[Y|Tail1]):-del(X,Tail,Tail1).

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 6
130420107536

search(X,L):-member(X,L),write('Found');not(member(X,L)),write('not Found').

reverse1(L,L1):-rev(L,[],L1).
rev([],L,L).
rev([H|T],L,M):-rev(T,[H|L],M).

length([], 0).
length([H|T], N):-length(T, N1),N is N1+1.

Output: -

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 7
130420107536

Date :
Practical : 5
Introduce concept of operator notation and arithmetic/ comparison operators.

Write a program to perform following operations.


1. Find maximum of 3 numbers
2. Find minimum of 3 numbers
3. Check given number is even/odd
4. Check given number is positive/negative
5. Calculate factorial of given number
6. Print Fibonacci series up to give number

Introduction

Operators are defined to improve the readability of source code. For example, without
operators, to write 2*3+4*5 one would have to write +(*(2,3),*(4,5)). In Prolog, a number
of operators have been predefined. All operators, except for the comma (,) can be redefined
by the user.

Program: -
max(A,B,C):- A>B,A>C, write(A),write(' is MAX').
max(A,B,C):- B>A,B>C, write(B),write(' is MAX').
max(A,B,C):- C>A,C>B, write(C),write(' is MAX').

min(A,B,C):- A<B,A<C, write(A),write(' is Min').


min(A,B,C):- B<A,B<C, write(B),write(' is Min').
min(A,B,C):- C<A,C<B, write(C),write(' is Min').

oddeve(A):- A mod 2 =:= 0, write('even');write('odd').

posneg(A):- A >= 0, write('pos');write('neg').

fact(N):-factorial(N,1).
factorial(0,F):-write('factorial is '),write(F).
factorial(N,A) :-
N > 0,
A1 is N*A,
N1 is N-1,
%nl,write(A1),
factorial(N1,A1).

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 8
130420107536

fibo(N):-fib(N,-1,1).
fib(N,A,B):-
N>0,
N1 is N-1,
B1 is A+B,
A1 is B,write(B1),write(' '),fib(N1,A1,B1).

Output: -

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 9
130420107536

Date :
Practical : 6
Write a program to implement BFS (for 8 puzzle problem or Water Jug problem or any AI
search problem).
Water Jug problem using BFS:
Given two unmarked jugs having capacities a and b liters respectively and a target
volumet liters, find the moves that get exactly t liters in any of the two jugs.

Program:
::::::::::::::::::::::::: WATER JUG PROBLEM USING BFS:::::::::::::::::::::::::

importjava.util.*;
publicclass Test
{

inta_max = 4;
intb_max = 3;
int a = 0;
int b = 0;
int goal = 2;

voidcheckGoal()
{

int fin = 0;

while(fin != 1) {

if((this.a == this.goal) || (this.b == this.goal)) { fin = 1; }

if(this.a==0) {
fillA();

} elseif ((this.a> 0) && (this.b != this.b_max)) {


transferAtoB();

} elseif ((this.a> 0) && (this.b == this.b_max)) {


emptyB();

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 10
130420107536

}
}

voidfillA() {

this.a = this.a_max;
System.out.println("{" + this.a + "," + this.b + "}");

voidfillB() {
this.b = this.b_max;
System.out.println("{" + this.a + "," + this.b + "}");
}

voidtransferAtoB() {

int fin = 0;

while(fin != 1) {

this.b += 1;
this.a -= 1;

if((this.b == this.b_max) || (this.a == 0)) { fin = 1;}

System.out.println("{" + this.a + "," + this.b + "}");

voidemptyA() {

this.a=0;
System.out.println("{" + this.a + "," + this.b + "}");

voidemptyB() {
this.b=0;
System.out.println("{" + this.a + "," + this.b + "}");
}
publicstaticvoid main(String args[])

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 11
130420107536

{
Test w = newTest();
w.checkGoal();
}
}

Output :-

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 12
130420107536

Date :
Practical : 7
Write a program to implement DFS (for 8 puzzle problem or Water Jug problem or any AI
search problem).

Introduction

The title of this practical refers to a familiar and popular sliding tile puzzle that has been
around for at least forty years. The most frequent older versions of this puzzle have
numbers or letters and the sliding tiles, and the player is supposed to slide tiles into new
positions in order to realign a scrambled puzzle back into a goal alignment. For illustration,
we use the 3 x 3 8-tile version, which is depicted here in goal configuration

Program :-
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.*;

class EightPuzzle {

Queue<String> agenda = new LinkedList<String>(); // Use of Queue Implemented using


LinkedList for Storing All the Nodes in BFS.
Map<String,Integer> stateDepth = new HashMap<String, Integer>(); // HashMap is used to
ignore repeated nodes
Map<String,String> stateHistory = new HashMap<String,String>(); // relates each position to
its predecessor
public static String str2 = new String();

public static void main(String args[])


{ // Input the Board State as a String with 0 as the Blank Space
String str = new String();
Scanner s = new Scanner(System.in);

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 13
130420107536

/* Enter Input Matrix with 0 as Blank Space.


123405678 */
System.out.print("\nEnter INPUT Matrix with 0 as Blank Space >> ");
str=s.next();
System.out.print("\nEnter GOAL Matrix with 0 as Blank Space >> ");
str2=s.next();

EightPuzzle e = new EightPuzzle(); // New Instance of the EightPuzzle


e.add(str, null); // Add the Initial State

while(!e.agenda.isEmpty()){
String currentState = e.agenda.remove();
e.up(currentState); // Move the blank space up and add new state
to queue
e.down(currentState); // Move the blank space down
e.left(currentState); // Move left
e.right(currentState); // Move right and remove the current node from
Queue
}

System.out.println("Solution doesn't exist");


}

//Add method to add the new string to the Map and Queue
void add(String newState, String oldState){
if(!stateDepth.containsKey(newState)){
int newValue = oldState == null ? 0 : stateDepth.get(oldState) + 1;
stateDepth.put(newState, newValue);
agenda.add(newState);
stateHistory.put(newState, oldState);
}
}

/* Each of the Methods below Takes the Current State of Board as String. Then the operation
to move the blank space is done if possible.
After that the new string is added to the map and queue.If it is the Goal State then the
Program Terminates.
*/
void up(String currentState){
int a = currentState.indexOf("0");
if(a>2){
String nextState = currentState.substring(0,a-3)+"0"+currentState.substring(a-
2,a)+currentState.charAt(a-3)+currentState.substring(a+1);
checkCompletion(currentState, nextState);
}

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 14
130420107536

void down(String currentState){


int a = currentState.indexOf("0");
if(a<6){
String nextState =
currentState.substring(0,a)+currentState.substring(a+3,a+4)+currentState.substring(a+1,a+3)+"0"
+currentState.substring(a+4);
checkCompletion(currentState, nextState);
}
}
void left(String currentState){
int a = currentState.indexOf("0");
if(a!=0 && a!=3 && a!=6){
String nextState = currentState.substring(0,a-1)+"0"+currentState.charAt(a-
1)+currentState.substring(a+1);
checkCompletion(currentState, nextState);
}
}
void right(String currentState){
int a = currentState.indexOf("0");
if(a!=2 && a!=5 && a!=8){
String nextState =
currentState.substring(0,a)+currentState.charAt(a+1)+"0"+currentState.substring(a+2);
checkCompletion(currentState, nextState);
}
}

void checkCompletion(String oldState, String newState) {


add(newState, oldState);
if(newState.equals(str2)) {
System.out.println("\nSolution Exists at Level "+stateDepth.get(newState)+" of the
tree");
String traceState = newState;
while (traceState != null) {

System.out.println("\n"+traceState.substring(0,3)+"\n"+traceState.substring(3,6)+"\n"+tra
ceState.substring(6,9));
System.out.println(">>: At Level " + stateDepth.get(traceState));
traceState = stateHistory.get(traceState);
}
System.exit(0);
}
}

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 15
130420107536

Output: -

SCET/CO/BE-IV/ARTIFICIAL INTELLIGENCE 16

Vous aimerez peut-être aussi