Sunday, January 30, 2011

Tuesday, January 11, 2011

Prolog Examples II(AI,CS409)

Towers of Hanoi Problem

Fig. 2.3

you have N rings of increasing size and three pegs. Initially the three rings are
stacked in order of decreasing size on the first peg. You can move them between pegs but you must never stack a big ring onto a smaller one. What is the sequence of moves to move from all the rings from the first to the the third peg.

answers:

hanoi( N ):- 
      move( N, left, middle, right ).
move(1,X,Y,_) :-
      write('Move top disk from '),
      write(X),
      write(' to '),
      write(Y),
      nl.
move(N,X,Y,Z) :-
      N>1,
      M is N-1,
      move(M,X,Z,Y),
      move(1,X,Y,_),
      move(M,Z,Y,X).

Find Factoria (N!) of a number

factorial(0, 1).                                          % Factorial of 0 is 1.
factorial(N, FactN) :-
N > 0, % N is positive
Nminus1 is N - 1, % Calculate N minus 1
factorial(Nminus1, FactNminus1), % recursion
FactN is N * FactNminus1. % N! = N * (N - 1)!

Prolog Examples (AI,CS409)

monkeys banana problem

There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use.

The monkey can perform the following actions: 

Walk on the floor
Climb the box
Push the box around (if it is already at the box)
Grasp the banana if standing on the box directly under the banana. 

Answers code:

move(state(middel, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).
move(state(P1, onfloor, P1, H),
push(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(State1) :-
move(State1, Move, State2),
canget(State2).

 

Prolog Examples (AI,CS409)

monkeys banana problem

There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use.

The monkey can perform the following actions: 

Walk on the floor
Climb the box
Push the box around (if it is already at the box)
Grasp the banana if standing on the box directly under the banana. 

Answers code:

move(state(middel, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).
move(state(P1, onfloor, P1, H),
push(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(State1) :-
move(State1, Move, State2),
canget(State2).

 

Prolog Examples (AI,CS409)

monkeys banana problem

There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey is hungry and wants to get the banana, but he cannot stretch high enough from the floor. At the window of the room there is a box the monkey may use.
The monkey can perform the following actions: 
Walk on the floor
Climb the box
Push the box around (if it is already at the box)
Grasp the banana if standing on the box directly under the banana. 
Answers code:
move(state(middel, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).
move(state(P1, onfloor, P1, H),
push(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(State1) :-
move(State1, Move, State2),
canget(State2).

Saturday, January 8, 2011

Prolog Introduction-I(AI ,CS409)

 

  Hi guys after very long time I’m posting for this blog because My Computer so speed to do blogging or web developing [you know what i mean:) ].

  Any way I came to know this SW with AI lecture  series.First I thought it will be boring one as it has text editor with hard compiling manner but after doing some exercise it makes some sense. That would be the reason why I’m posting this to encourage people to use it.

Before the Starting this use following link download it.It’s come under lesser GNU public license  freely available with SWI-Prolog site and nearly 8Mb.Also don’t forget to check the version before download it’s compatible to your PC.

http://www.swi-prolog.org/download/stable 

 

When you open swi-prolog you can see window like above but when you click on menu item you might get some doubts because it’s refer some menu item for different purpose ex:-Run menu not involve for executing program.

First we need to create new file (extension will be .pl) by file->new then it will open some editor and after editing  save it(it appears as save buffer; Ctrl+S works).

image

Prolog has its roots in 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.

There are some basic rules.

after every line finish with full stop(.)
define thing. 
to declare thing use following format declare_what(thing) 
male(nimal). 
this means "Nimal is a male" is true.we can write this one as 
male(Nimal):-true.





 




After saving this have to compile that by file->consults and then open source file then it gives message then type above formula then check it replies true.





1 ?- % j:/Documents and Settings/common/My Documents/Prolog/Ex1.pl compiled 0.00 sec, 136 byte
1 ?- male(nimal).
true . 
2 ?- male(namal).
false .



















For the time being I stop this if you interested check part II(if not you have to best things happen last)  :)