Documentos de Académico
Documentos de Profesional
Documentos de Cultura
ARTIFICIAL
INTELLIGENCE
CONTENTS
Theory –
In this method simple clauses are defined to relate a person with another.
Then the other relations are found based upon this.
Examples –
Language –
PROLOG
Program –
PREDICATES
son(string,string).
father(string,string).
married(string,string).
mother(string,string).
brother(string,string).
sister_in_law(string,string).
CLAUSES
son("John", "Dan").
brother("Dan","Max").
married("John", "Suzie").
married("Dan", "Mary").
married("Max","Julie").
father(A,B):-son(B,A).
mother(A,C):-father(A,B),married(B,C).
sister_in_law(A,C):-brother(A,B),married(B,C).
GOAL
sister_in_law("Dan",P).
Output –
1 solution
Julie
Experiment 2
Aim –
Theory –
A salesman has a list of cities, each of which he must exactly visit once. There
are direct road between each pair of cities on the list. This program finds the
root the salesman should follow for the shortest possible path between any
pair of cities.
Examples –
Language –
PROLOG
Program –
DOMAINS
town = symbol
distance = integer
PREDICATES
nondeterm road(town,town,distance)
nondeterm route(town,town,distance)
CLAUSES
road(delhi,mumbai,200).
road(kolkata,delhi,300).
road(mumbai,kolkata,100).
road(mumbai,raipur,120).
road(kolkata,raipur,130).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,!.
GOAL
route(delhi,raipur,Z).
Output –
1 Solution
320
Experiment 3
Aim –
Theory –
In this we are given two jugs, a 4- litre one and a 3-litre one. Neither has any
measuring markers on it. There is a pump that can be used to fill the jugs with
water. The task is to get exactly 2- litres of water in the 4- litre jug.
Examples –
0 0 Start
Language –
Program –
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
while(x!=2 || y!=0)
y=3;
x=x+y;
y=0;
printf("Pour all the water from the three litre jug into the four litre jug\n");
y=y-(4-x);
x=4;
printf("Pour water from the three litre jug into the four litre jug until the four
litre jug is full\n");
if(y==2)
{
x=0;
x=2;
y=0;
printf("Pour the 2 litre water from the three litre jug into the four litre jug\n");
getch();
Output –
Experiment 4
Aim –
To implement the Breadth first search algorithm for finding the goal node from
the tree.
Theory –
Examples –
B C
D E F G
Language –
C
Program –
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
/* The tree is
/ \
b c
/\ /\
d e f g
*/
char bintree[15]={'a','b','c','d','e','f','g',' ',' ','h',' ',' ',' ', ' ',' '};
char TRAV_LIST[15];
char temp1,temp2;
int i=0;
{
TRAV_LIST[i]=bintree[i];
temp1=bintree[2*i+1];
temp2=bintree[2*i+2];
i++;
TRAV_LIST[i]='h';
for(int j=0;j<i+1;j++)
printf("%c\n",TRAV_LIST[j]);
getch();
Output –
Experiment 5
Aim –
To implement the Depth first search algorithm for finding the goal node from
the tree.
Theory –
Examples –
B C
D E F G
Language –
Program –
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
/* The tree is
/ \
b c
/\ /\
d e f g
*/
char bintree[15]={'a','b','c','d','e','f','g',' ',' ',' ',' ',' ',' ', 'h',' '};
char TRAV_LIST[15];
char temp1,temp2;
int i=0,k=0;
TRAV_LIST[0]=bintree[0];
while(TRAV_LIST[i]!='h')
for(k=0;k<15;k++)
{
if(TRAV_LIST[i]==bintree[k])
break;
if(bintree[2*k+1]!=' ')
for(int j=13;j>=i+1;j--)
TRAV_LIST[j+1]=TRAV_LIST[j];
TRAV_LIST[i+1]=bintree[2*k+1];
if(bintree[2*k+2]!=' ')
for(int j=13;j>=i+2;j--)
TRAV_LIST[j+1]=TRAV_LIST[j];
TRAV_LIST[i+2]=bintree[2*k+2];
i++;
printf("%c\n",TRAV_LIST[j]);
getch();
Output –
Experiment 6
Aim –
To solve the missionaries and cannibals problem by taking suitable start state
and goal state.
Theory –
The problem says that we have three missionaries and three cannibals who
have to be transported across a river in a boat. Only two persons are allowed
to go in the boat at a given time. The number of cannibals should never be
more than that of the missionaries , otherwise the missionaries will be killed.
Examples –
Language –
Program –
#include <iostream>
#include <list>
struct StateSTR
{
int Mlhs; //nr missionaries on LHS of river
(pos == rhs.pos));
Mlhs = rhs.Mlhs;
Clhs = rhs.Clhs;
pos = rhs.pos;
Mrhs = rhs.Mrhs;
Crhs = rhs.Crhs;
parent= rhs.parent;
return *this;
};
ostream & operator<< (ostream & out, const StateSTR & s)
return out;
bool validState(StateSTR S)
return false;
return true;
}
//Apply each of ten possible operations to state.
m c side(0=left,1=right)
case 2: C(1,0,0)
case 3: C(2,0,0)
case 4: C(1,1,0)
case 5: C(0,1,1)
case 6: C(0,2,1)
case 8: C(2,0,1)
case 9: C(1,1,1)
StateSTR S = X;
switch (j)
case 0: { S.pos -= 1;
S.Mlhs += 0;
S.Clhs += 1;
S.Mrhs -= 0;
S.Crhs -= 1;}
break;
case 1: { S.pos -= 1;
S.Mlhs += 0;
S.Clhs += 2;
S.Mrhs -= 0;
S.Crhs -= 2;}
break;
case 2: { S.pos -= 1;
S.Mlhs += 1;
S.Clhs += 0;
S.Mrhs -= 1;
S.Crhs -= 0;}
break;
case 3: { S.pos -= 1;
S.Mlhs += 2;
S.Clhs += 0;
S.Mrhs -= 2;
S.Crhs -= 0;}
break;
case 4: { S.pos -= 1;
S.Mlhs += 1;
S.Clhs += 1;
S.Mrhs -= 1;
S.Crhs -= 1;}
break;
case 5: { S.pos += 1;
S.Mrhs += 0;
S.Crhs += 1;
S.Mlhs -= 0;
S.Clhs -= 1;}
break;
case 6: { S.pos += 1;
S.Mrhs += 0;
S.Crhs += 2;
S.Mlhs -= 0;
S.Clhs -= 2;}
break;
case 7: { S.pos += 1;
S.Mrhs += 1;
S.Crhs += 0;
S.Mlhs -= 1;
S.Clhs -= 0;}
break;
case 8: { S.pos += 1;
S.Mrhs += 2;
S.Crhs += 0;
S.Mlhs -= 2;
S.Clhs -= 0;}
break;
case 9: { S.pos += 1;
S.Mrhs += 1;
S.Crhs += 1;
S.Mlhs -= 1;
S.Clhs -= 1;}
break;
return S;
list<StateSTR> CLOSED)
list<StateSTR>::iterator sRslt2
return true;
return false;
}
StateSTR & X )
StateSTR tmpState;
tmpState.parent = &X;
OPEN.push_front(tmpState);
return;
//MAIN section
int main() {
StateSTR X;
StateSTR tempState;
list<StateSTR> OPEN;
list<StateSTR> CLOSED;
OPEN.push_front(START);
while(!OPEN.empty())
OPEN.pop_front(); //
if (X == GOAL)
searchResult = true;
GOAL = X;
break;
else
CLOSED.push_back(X);
//Display results
if(searchResult == true)
return 0;
Output –
Experiment 7
Aim –
To solve the simple hill climbing problem by taking suitable start state and goal
state.
Theory –
The problem says that we are on the bottom of a hill. We have to go on the top
of it i.e to reach the highest point on the hill. For this we will perform breadth
first search algorithm to find the highest node in the tree which represents the
hill.
Examples –
Language –
Program –
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
int value;
float weight;
int parent;
struct tree *next;
};
main()
char ch;
int MAX,MIN_WEIGHT;
int flag=0;
while(1)
printf("Enter the new node (value weight parent) (Use . to indicate a parent
node):");
scanf("%d%f%d",&temp->value,&temp->weight,parent);
if(flag==0)
HEAD=temp;
PAR=temp;
PTR=temp;
flag=1;
else
PTR->next=temp;
PTR=temp;
while(PAR->next!=null)
if(PAR->value==parent)
PAR->next=temp;
scanf("%c",&ch);
if(ch=='n' || ch=='N')
PTR->next=null;
break;
MAX=HEAD->value;
PTR=HEAD;
MIN_WEIGHT=HEAD->weight;
while(1)
if(PTR->next==null)
break;
if(PTR->weight<MIN_WEIGHT)
printf("<%d>",PTR->value;
MAX=PTR->value;
MIN_WEIGHT=PTR->weight;
PTR=PTR->next;
else
PTR=PTR->next;
Output –
10
14
13
Experiment 8
Aim –
To solve the steepest hill climbing problem by taking suitable start state and
goal state.
Theory –
The problem says that we are on the bottom of a hill. We have to go on the top
of it i.e to reach the highest point on the hill. But the twist is that we hwve
more than one peaks in the hill and hence we have to find out the highest peak
of the hill. For this we will perform best first search algorithm to find the
highest node in the tree which represents the hill.
Examples –
Language –
Program –
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
int value;
float weight;
int parent;
int type;
};
main()
char ch;
int MAX,MIN_WEIGHT;
int flag=0;
while(1)
printf("Enter the new node (value weight Parent Type) (Use 0 to AND Arc and 1
to indicate an OR arc):");
scanf("%d%f%d%d",&temp->value,&temp->weight,parent,type);
if(flag==0)
HEAD=temp;
PAR=temp;
PTR=temp;
flag=1;
else
{
PTR->next=temp;
PTR=temp;
while(PAR->next!=null)
if(PAR->value==parent)
PAR->next=temp;
scanf("%c",&ch);
if(ch=='n' || ch=='N')
PTR->next=null;
break;
MAX=HEAD->value;
PTR=HEAD;
MIN_WEIGHT=HEAD->weight;
while(1)
if(PTR->next==null)
break;
if(PTR->type==0)
weight=PTR->weight*PAR->weight;
else
weight=PTR->weight;
if(weight<MIN_WEIGHT)
printf("<%d>",PTR->value);
MAX=PTR->value;
MIN_WEIGHT=weight;
PTR=PTR->next;
else
{
PTR=PTR->next;
getch();
Output –
16
10
18
Experiment 9
Aim –
To implement the AO* algorithm.
Theory –
Examples –
Language –
Program –
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<tree.h>
struct tree
int value;
float weight;
int parent;
int type;
};
main()
char ch;
int MAX,MIN_WEIGHT;
int flag=0;
int weight;
while(1)
printf("Enter the new node (value weight parent type) (Use 0 to AND Arc and 1
to indicate an OR arc):");
scanf("%d%f%d%d",&temp->value,&temp->weight,parent,type);
if(flag==0)
HEAD=temp;
PAR=temp;
PTR=temp;
flag=1;
}
else
PTR->next=temp;
PTR=temp;
while(PAR->next!=null)
if(PAR->value==parent)
PAR->next=temp;
scanf("%c",&ch);
if(ch=='n' || ch=='N')
PTR->next=null;
break;
PTR=HEAD;
MIN_WEIGHT=HEAD->weight;
while(1)
if(PTR->next==null)
break;
if(PTR->type==0)
weight=PTR->weight*PAR->weight;
else
weight=PTR->weight;
if(weight<MIN_WEIGHT)
printf("<%d>",PTR->value);
MAX=PTR->value;
MIN_WEIGHT=weight;
PTR=PTR->next;
else
{
PTR=PTR->next;
getch();
Output –
10
12
Path traversed:
2 4 12
Node Desired: 12
Experiment 10
Aim –
Language –
Java
Program –
import java.io.*;
import java.util.*;
new Connect4();
}
public Connect4() {
int column;
int player=1;
while(true) {
if(player==1) {
printGrid();
do {
column=readInt()-1;
} else {
moves.removeAllElements();
column=0;
int prevValue=-MAX_RECURDEPTH-1;
if(columnHeight[x]>=HEIGHT) continue;
if(value>prevValue) {
moves.removeAllElements();
prevValue=value;
}
if(moves.size()>0) {
Collections.shuffle(moves);
column=((Integer)moves.get(0)).intValue();
if(moves.size()==0) {
System.out.println("Its a draw.");
break;
grid[column][columnHeight[column]]=player;
columnHeight[column]++;
int won=0;
won=fourInARow();
if(won>0) {
printGrid();
player=3-player;
}
int minmax(int player, int column) {
int value=0;
if(columnHeight[column]>=HEIGHT) return 0;
recurDepth++;
grid[column][columnHeight[column]]=player;
columnHeight[column]++;
if(fourInARow()>0) {
if(player==2)
value=MAX_RECURDEPTH+1-recurDepth;
else value=-MAX_RECURDEPTH-1+recurDepth;
value=MAX_RECURDEPTH+1;
if(columnHeight[x]>=HEIGHT) continue;
if(value==(MAX_RECURDEPTH+1)) value=v;
}
recurDepth--;
columnHeight[column]--;
grid[column][columnHeight[column]]=0;
return value;
int fourInARow() {
num=0; player=0;
if(grid[x][y]==player) num++;
num=0; player=0;
if(grid[x][y]==player) num++;
num=0; player=0;
if(grid[x][y]==player) num++;
if(yStart==0) xStart++;
else yStart--;
num=0; player=0;
if(grid[x][y]==player) num++;
if(yStart==HEIGHT-1) xStart++;
else yStart++;
}
return 0;
void printGrid() {
if(grid[x][y]==0) System.out.print(".");
System.out.println();
System.out.println("1234567");
int readInt() {
try {
String input=in.readLine();
int number=Integer.parseInt(input);
return number;
} catch(NumberFormatException e) {
return -1;
} catch(IOException e) {
return -1;
}}}
Output