Untitled

 avatar
unknown
plain_text
3 years ago
11 kB
3
Indexable
//BASIC LAB1:
Predicates
nondeterm likes (symbol,symbol)

clauses
likes(ali,football).
likes(ali,cricket).
likes(hari,cricket).
likes(hari,tennis).
likes(mara,tennis).
likes(mara,hockey).
likes(haaland,football).
likes(marc,motogp).

goal
%likes(Person,G1),likes(Person,G2),G1<>G2.//for displaying persons that like more than one sports
%likes(ali,football).//check if ali likes football
%likes(ali,Game),likes(hari,Games).//list games liked by ali and hari
%likes(Person,football).//list persons who like football


//RULES LAB2:
A* 
Predicates
nondeterm likes(symbol,symbol)
nondeterm friends(symbol,symbol)

Clauses
likes(benzema,football).
likes(bale,football).
likes(bale,golf).
likes(verstappen,f1).
likes(marc,motogp).
likes(nate,mma).
likes(ramos,mma).
likes(ramos,football).

friends(P1,P2):-
likes(P1,G),likes(P2,G),P1<>P2.

Goal
%friends(nate,ramos).//checking if ramos and nate are friends
%likes(What,football).//for checking who likes football



B*
PREDICATES
husband(STRING,STRING)
father(STRING,STRING)
mother(STRING,STRING)
son(STRING,STRING)
CLAUSES
mother("Kushalya","Ram").
mother("Kaikai","Bharat").
mother("Sumitra","Laxman").
mother("Sumitra","Satrughan").
husband("Dasrath","Kaushalya").
husband("Dasrath","Kaikai").
husband("Dasrath","Sumitra").
son(A,C):- mother(C,A).
son(A,C):-husband(C,B), mother(B,A).
father(A,B):- husband(A,C), mother(C,B).
GOAL
son(X, "Kaikai").



//Visual Prolog Program to find the bigger of the Two Numbers.
PREDICATES
bigger (integer, integer, integer)
CLAUSES
bigger(X,Y,Z):-
X>Y,Z=X.
bigger(X,Y,Z):-
X<Y,Z=Y.
GOAL
bigger (4,9,X).








//LLab3
A person can buy a car if the person likes the car and the car is for sale.

PREDICATES
nondeterm can_buy(symbol, symbol)
nondeterm person(symbol)
nondeterm car(symbol)
likes(symbol, symbol)
for_sale(symbol)
CLAUSES
can_buy(X,Y):- person(X),
car(Y),
likes(X,Y),
for_sale(Y).
person(kelly).
person(judy).
person(ellen).
person(mark).
car(lemon).
car(hot_rod).
likes(kelly, hot_rod).
likes(judy, pizza).
likes(ellen, tennis).
likes(mark, tennis).
for_sale(pizza).
for_sale(lemon).
for_sale(hot_rod).

goals
can_buy(Who, What).
can_buy(judy, What).
can_buy(kelly, What).
can_buy(Who, hot_rod).//doesnt work








//LAB7:\
//hcf of a number
PREDICATES
hcf(integer, integer, integer)
CLAUSES
hcf(X, Y, X):-
Y mod X = 0.
hcf(X,Y,Z):-
S = Y mod X,S<>0, hcf(S, X, Z).
GOAL
hcf(5,10,X).



//factorial of number
Predicates
factorial(unsigned,real)

clauses
factorial(1,1):-!.
factorial(N,FactN):-
M=N-1,
factorial(M,FactM),
FactN=N*FactM.
Goal
factorial(5,F).


//distancefinding or higway model
Domains
node=symbol
distance=integer

Predicates
nondeterm link(node,node,distance)
nondeterm path(node,node,distance)

clauses
link(a,b,4).
link(a,c,2).
link(b,g,5).
link(c,g,6).
link(c,d,5).
link(d,g,3).
path(S,D,TDist):-link(S,D,TDist).
path(S,D,TDist):-
  link(S,X,TD1),path(X,D,TD2),TDist=TD1+TD2.
  
  goal
  path(a,g,Totaldistance).




LIST//to add content of an list
DOMAINS
int_list=Integer*
PREDICATES
add(int_list, Integer)
CLAUSES
add([],0).
add([H|T],Y):- add(T,Y1),Y=Y1+H.
GOAL
add([3,2,1,0],X).


//to append two list.
DOMAINS
int_list=Integer *
PREDICATES
append(int_list,int_list,int_list)
CLAUSES
append([],X,Z):-
Z=X.
append([H|T],X,Y):-
append(T,X,Z),Y=[H|Z].
GOAL
append([1,2,3,4,5],[6,7,8,9],X).


//Program to delete a given element from the list.
DOMAINS
int_list=Integer *
PREDICATES
delete(integer,int_list,int_list)
CLAUSES
delete(0,[H|T],T).
delete(P,[H|T],[H|Z]):-
P1=P-1,delete(P1,T,Z).
GOAL
delete(0,[1,2,3,4,5,6,7,8,9],X).




//lab9:
Prolog program to solve the 4-3 Gallon Water Jug Problem
in Artificial Intelligence.

database
visited_state(integer,integer)
predicates
nondeterm state(integer,integer)
clauses
state(2,0).
state(X,Y):- X < 4,
not(visited_state(4,Y)),
assert(visited_state(X,Y)),
write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\n"),
state(4,Y).
state(X,Y):- Y < 3,
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Fill the 3-Gallon Jug: (", X,",",Y,") --> (", X,",",3,")\n"),
state(X,3).
state(X,Y):- X > 0,
not(visited_state(0,Y)),
assert(visited_state(X,Y)),
write("Empty the 4-Gallon jug on ground: (", X,",",Y,") --> (", 0,",",Y,")\n"),
state(0,Y).
state(X,Y):- Y > 0,
not(visited_state(X,0)),
assert(visited_state(X,0)),
write("Empty the 3-Gallon jug on ground: (", X,",",Y,") --> (", X,",",0,")\n"),
state(X,0).

state(X,Y):- X + Y >= 4,
Y > 0,
NEW_Y = Y - (4 - X),
not(visited_state(4,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour water from 3-Gallon jug to 4-gallon until it is full: (", X,",",Y,") --> (",
4,",",NEW_Y,")\n"),
state(4,NEW_Y).
state(X,Y):- X + Y >=3,
X > 0,
NEW_X = X - (3 - Y),
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Pour water from 4-Gallon jug to 3-gallon until it is full: (", X,",",Y,") --> (",
NEW_X,",",3,")\n"),
state(NEW_X,3).
state(X,Y):- X + Y <=4,
Y > 0,
NEW_X = X + Y,
not(visited_state(NEW_X,0)),
assert(visited_state(X,Y)),
write("Pour all the water from 3-Gallon jug to 4-gallon: (", X,",",Y,") --> (", NEW_X,",",0,")\n"),
state(NEW_X,0).
state(X,Y):- X+Y<=3,
X > 0,
NEW_Y = X + Y,
not(visited_state(0,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour all the water from 4-Gallon jug to 3-gallon: (", X,",",Y,") --> (", 0,",",NEW_Y,")\n"),

state(0,NEW_Y).
state(0,2):- not(visited_state(2,0)),
assert(visited_state(0,2)),
write("Pour 2 gallons from 3-Gallon jug to 4-gallon: (", 0,",",2,") --> (", 2,",",0,")\n"),
state(2,0).
state(2,Y):- not(visited_state(0,Y)),
assert(visited_state(2,Y)),
write("Empty 2 gallons from 4-Gallon jug on the ground: (", 2,",",Y,") --> (", 0,",",Y,")\n"),
state(0,Y).

goal
write("Initially state(0,0)"),nl,
state(0,0),
save("F:\ output.txt").



//C/program to solve N Queen Problem using
// backtracking */
#define N 4
#include <stdbool.h>
#include <stdio.h>

/* A utility function to print solution */
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}
/* A utility function to check if a queen can
be placed on board[row][col]. Note that this
function is called when "col" queens are
already placed in columns from 0 to col -1.
So we need to check only left side for
attacking queens */
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
/* Check this row on left side */
for (i = 0; i < col; i++)
if (board[row][i])
return false;

/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
/* A recursive utility function to solve N
Queen problem */
bool solveNQUtil(int board[N][N], int col)
{
/* base case: If all queens are placed
then return true */
if (col >= N)
return true;
/* Consider this column and try placing
this queen in all rows one by one */
for (int i = 0; i < N; i++) {
/* Check if the queen can be placed on
board[i][col] */
if (isSafe(board, i, col)) {
/* Place this queen in board[i][col] */
board[i][col] = 1;
/* recur to place rest of the queens */
if (solveNQUtil(board, col + 1))
return true;

/* If placing queen in board[i][col]
doesn't lead to a solution, then
remove queen from board[i][col] */
board[i][col] = 0; // BACKTRACK
}
}

/* If the queen cannot be placed in any row in
this colum col then return false */
return false;
}
/* This function solves the N Queen problem using
Backtracking. It mainly uses solveNQUtil() to
solve the problem. It returns false if queens
cannot be placed, otherwise, return true and
prints placement of queens in the form of 1s.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");

return false;
}
printSolution(board);
return true;
}
// driver program to test above function
int main()
{
solveNQ();
return 0;
}



Tic-tac-toe playing Game in AI

#include <stdio.h>
int move=0;
char gridChar(int i) {
switch(i) {
case -1:
return 'X';
case 0:
return ' ';
case 1:
return 'O';
}
}

void draw(int b[9]) {
printf(" %c | %c | %c\n",gridChar(b[0]),gridChar(b[1]),gridChar(b[2]));
printf("---+---+---\n");
printf(" %c | %c | %c\n",gridChar(b[3]),gridChar(b[4]),gridChar(b[5]));
printf("---+---+---\n");
printf(" %c | %c | %c\n",gridChar(b[6]),gridChar(b[7]),gridChar(b[8]));
}

int win(const int board[9]) {
//determines if a player has won, returns 0 otherwise.
unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int i;
for(i = 0; i < 8; ++i) {
if(board[wins[i][0]] != 0 &&
board[wins[i][0]] == board[wins[i][1]] &&

board[wins[i][0]] == board[wins[i][2]])
return board[wins[i][2]];
}
return 0;
}

int minimax(int board[9], int player) {
//How is the position like for player (their turn) on board?
int winner = win(board);
if(winner != 0) return winner*player;

move = -1;
int score = -2;//Losing moves are preferred to no move
int i;
for(i = 0; i < 9; ++i) {//For all moves,
if(board[i] == 0) {//If legal,
board[i] = player;//Try the move
int thisScore = -minimax(board, player*-1);
if(thisScore > score) {
score = thisScore;
move = i;
}//Pick the one that's worst for the opponent
board[i] = 0;//Reset board after try
}
}
if(move == -1) return 0;
return score;
}

void computerMove(int board[9]) {
int move = -1;
int score = -2;
int i;
for(i = 0; i < 9; ++i) {
if(board[i] == 0) {
board[i] = 1;
int tempScore = -minimax(board, -1);
board[i] = 0;
if(tempScore > score) {
score = tempScore;
move = i;
}
}
}
//returns a score based on minimax tree at a given node.
board[move] = 1;
}

void playerMove(int board[9]) {
int move = 0;
do {
printf("\nInput move ([0..8]): ");
scanf("%d", &move);
printf("\n");
} while (move >= 9 || move < 0 && board[move] == 0);
board[move] = -1;
}

int main() {
int board[9] = {0,0,0,0,0,0,0,0,0};
//computer squares are 1, player squares are -1.
printf("Computer: O, You: X\nPlay (1)st or (2)nd? ");
int player=0;
scanf("%d",&player);
printf("\n");
unsigned turn;
for(turn = 0; turn < 9 && win(board) == 0; ++turn) {
if((turn+player) % 2 == 0)
computerMove(board);
else {
draw(board);
playerMove(board);
}
}
switch(win(board)) {
case 0:
printf("A draw. How droll.\n");
break;
case 1:
draw(board);
printf("You lose.\n");
break;
case -1:
printf("You win. Inconceivable!\n");
break;
}
}
Editor is loading...