Showing posts with label Artificial intelligence GTU. Show all posts
Showing posts with label Artificial intelligence GTU. Show all posts

Sunday, 16 June 2019

Write a program for a flight route planner and imaginary flight time table using structure. PROLOG GTU ARTIFICIAL INTELLEGENCE


:- op( 50, xfy, :).

route( P1, P2, Day, [ P1 / P2 / Fnum / Deptime ] ) :- flight( P1, P2, Day, Fnum, Deptime, _).

route( P1, P2, Day, [ (P1 / P3 / Fnum1 / Dep1) | RestRoute] ) :- route( P3, P2, Day, RestRoute),
flight( P1, P3, Day, Fnum1, Dep1, Arr1), deptime( RestRoute, Dep2),
transfer( Arr1, Dep2).

flight( Place1, Place2, Day, Fnum, Deptime, Arrtime) :- timetable( Place1, Place2, Flightlist),
member( Deptime / Arrtime / Fnum / Daylist , Flightlist), flyday( Day, Daylist).

flyday( Day, Daylist) :- member( Day, Daylist).

flyday( Day, alldays) :-
member( Day, [mo,tu,we,th,fr,sa,su] ). deptime( [ P1 / P2 / Fnum / Dep | _], Dep).
transfer( Hours1:Mins1, Hours2:Mins2) :-
60 * (Hours2 - Hours1) + Mins2 - Mins1 >= 40. member( X, [X | L] ).
member( X, [Y | L] ) :- member( X, L).

timetable( edinburgh, london,
[ 9:40 / 10:50 / ba4733 / alldays, 13:40 / 14:50 / ba4773 / alldays,
19:40 / 20:50 / ba4833 / [mo,tu,we,th,fr,su] ] ).

timetable( london, edinburgh,
[ 9:40 / 10:50 / ba4732 / alldays, 11:40 / 12:50 / ba4752 / alldays,
18:40 / 19:50 / ba4822 / [mo,tu,we,th,fr] ] ).

timetable( london, ljubljana,
[ 13:20 / 16:20 / jp212 / [mo,tu,we,fr,su], 16:30 / 19:30 / ba473 / [mo,we,th,sa] ] ).

timetable( london, zurich,
[ 9:10 / 11:45 / ba614 / alldays, 14:45 / 17:20 / sr805 / alldays ] ).

timetable( london, milan,
[ 8:30 / 11:20 / ba510 / alldays, 11:00 / 13:50 / az459 / alldays ] ).

timetable( ljubljana, zurich,
[ 11:30 / 12:40 / jp322 / [tu,th] ] ).

timetable( ljubljana, london,
[ 11:10 / 12:20 / jp211 / [mo,tu,we,fr,su], 20:30 / 21:30 / ba472 / [mo,we,th,sa] ] ).

timetable( milan, london,
[ 9:10 / 10:00 / az458 / alldays, 12:20 / 13:10 / ba511 / alldays ] ).

timetable( milan, zurich,
[ 9:25 / 10:15 / sr621 / alldays, 12:45 / 13:35 / sr623 / alldays ] ).

timetable( zurich, ljubljana,
[ 13:30 / 14:40 / jp323 / [tu,th] ] ).

timetable( zurich, london,
[ 9:00 / 9:40 / ba613 / [mo,tu,we,th,fr,sa], 16:10 / 16:55 / sr806 / [mo,tu,we,th,fr,su] ] ).

timetable( zurich, milan,
[ 7:55 / 8:45 / sr620 / alldays ] ).


query3(City1,City2,City3,FN1,FN2,FN3,FN4) :- permutation( [milan,ljubljana,zurich],[City1,City2,City3]), flight( london, City1, tu, FN1, Dep1, Arr1),
flight( City1, City2, we, FN2, Dep2, Arr2), flight( City2, City3, th, FN3, Dep3, Arr3), flight( City3, london, fr, FN4, Dep4, Arr4).


conc([], L, L).

conc([X|L1],L2, [X|L3]) :- conc(L1,L2,L3).

permutation( [], []).

permutation( L, [X | P]) :-
del( X, L, L1),
permutation( L1, P). del( X, [X|L], L).
del( X, [Y|L], [Y|L1]) :- del( X, L, L1).

Input: flight(zurich , london , fr , ba613 , Y , X).
Output:  Y = 9:0,
X = 9:40

Introduce concept and use of structures. GTU Prolog Artificial Intellegence



Write a program to manipulate structure-FAMILY. [create family structure first]


family(
person( tom, fox,male, date(7,may,1960), works(bbc, 15200) ), person( ann, fox,female, date(9,may, 1961), unemployed),
[ person( pat, fox,male, date(5,may,1983), unemployed), person( iim, fox,male, date(5,may,1983), unemployed) ]).
family(
person( john, finn,male, date(1,april,1970), works(cnn, 20000) ), person( jane, finn,female, date(9,august, 1965), works(ndtv, 10000)), [ person( drake, finn,male, date(5,june,1996), works(ndtv, 15000)), person( kim, finn,female, date(15,october,1999), unemployed), person( kate, finn,female, date(31,december,2001), unemployed)]).

family(
person( james, morrison,male, date(7,may,1965), unemployed), person( gill, morrison,female, date(9,jan, 1966),works(sony, 152000)), [ person( marshall, morrison,male, date(5,may,1983), unemployed), person( michael, morrison,male, date(5,june,1985), unemployed) ]).

family(
person( chris, lynn,male, date(17,may,1990), works(sony, 192000)), person( marie, lynn,female, date(10,jan, 1992),works(sony, 152000)), [ ]).

member(X,[X|T]). member(X,[H|T]):-member(X,T).

husband(X) :- family(X,_,_).
wife(X) :- family(_,X,_).
child(X) :- family(_,_,Y),member(X,Y).
exists(P) :- husband(P);wife(P);child(P).
dob(person(_,_,_,Date,_),Date).
salary(person( _, _, _,_, works( _, S) ), S). salary(person( _, _, _,_, unemployed ), 0). twins(Child1,Child2) :-
family(_,_,Y),member(Child1,Y),member(Child2,Y),dob(Child1,D1),dob(Child2,D2),D1==D2.

1.     Find names of all persons in family database

Input:exists( person( Name, Surname, _,_, _) ).
Output: Name = tom, Surname = fox Name = john, Surname = finn Name = james,
Surname = morrison Name = chris, Surname = lynn Name = ann, Surname = fox Name = jane, Surname = finn Name = gill, Surname = morrison Name = marie, Surname = lynn Name = pat, Surname = fox Name = iim, Surname = fox

2.     Find all children born in specific year

Input: child(X),dob( X, date(_,_,2001)).
Output: X = person(kate, finn, female, date(31, december, 2001), unemployed)

3.     Find all employed wives

Input: wife( person( Name, Surname, _,_, works( _, _) ) ).
Output: Name = jane, Surname = finn Name = gill,
Surname = morrison Name = marie, Surname = lynn.

4.     Find name of all unemployed people who were born before specific year

Input: exists( person( Name, Surname,_, date( _, _, Year), unemployed) ),Year <1973.
Output: Name = james, Surname = morrison, Year = 1965 Name = ann, Surname = fox,
Year = 1961 false.

5.     Find name of families with at least 3 children

Input: family( person( _,Name,_,_,_),_,[_,_,_|_] ).
Output: Name = finn

6.     Find name of families without children

Input: family( person( _,Name,_,_,_),_,[] ).
Output: Name = lynn.

7.     Find out name of all employed children

Input: child( person( Name, Surname, _,_, works( _, _) ) ).
Output: Name = drake, Surname = finn

8.  Find out names of families having employed wives and unemployed husbands

Input: family( person( _,Name,_,_,unemployed), person(_,Name,_,_,works(_,_)),_ ).
Output: Name = Morrison

9.             Define relation “Twins” and find out twins in family database.

Input: twins( person( _, S, _,_, _ ), person( _, S, _,_, _ )).
Output: S = fox

Write a program for list manipulation using arithmetic and comparison operators.

1.  Find maximum number out of LIST of numbers
maxl([M|[]],M).
maxl([H|T],M):-
maxl(T,M1), H>M1,
M is H. maxl([H|T],M):-
maxl(T,M1), H<M1,
M is M1.

Input: maxl([8,6,4,2],X).
Output: X = 8

2.  Find minimum number out of LIST of numbers

minl([M|[]],M).
minl([H|T],M):-
minl(T,M1), H<M1,
M is H. minl([H|T],M):-
minl(T,M1), H>M1,
M is M1.

Input: maxl([8,6,4,2],X).
Output: X = 2

3.  Sort the given LIST in ascending order

bubblesort(L, L1) :- (bubble(L, L2)
-> bubblesort(L2, L1)
; L = L1 ).

bubble([A, B|T], L) :- ( A > B
-> L = [B, A|T]
; L = [A | L1],
bubble([B|T], L1)).
Input: bubblesort([8,6,4,2],X).
Output: X = [2, 4, 6, 8].

4.  Sort given list in descending order

bubblesort(L, L1) :- (bubble(L, L2)
-> bubblesort(L2, L1)
; L = L1 ).

bubble([A, B|T], L) :- ( A < B
-> L = [B, A|T]
; L = [A | L1],
bubble([B|T], L1)).

Input: bubblesort([8,6,4,2],X).
Output: X = [8, 6, 4, 2].

5.  Add two lists and store result in third list.

list_sum([],[],[]).
list_sum([H1|T1],[H2|T2],[X|L3]):- list_sum(T1,T2,L3),
X is H1+H2.

Input: list_sum([1,2,3],[4,5,6],X).
Output: X = [5, 7, 9].

6.  Apply subtraction between TWO lists and store result in third list.

list_sub([],[],[]).
list_sub([H1|T1],[H2|T2],[X|L3]):- list_sub(T1,T2,L3),
X is H1-H2.

Input: list_sub([9,6,3],[4,5,6],X).
Output: X = [5, 1, -3].

7.  Apply multiplication between TWO lists and store result in third list.

list_mul([],[],[]).
list_mul([H1|T1],[H2|T2],[X|L3]):- list_mul(T1,T2,L3),
X is H1*H2.
Input: list_sub([9,6,3],[4,5,6],X).
Output: X = [36, 30, 18].

Introduction to Recursion. Write a program to enhance the program of relationships including recursive rules. [Relationships such as ancestors, descendants, predecessors etc.]

Introduction to recursion in prolog:
The recursion in any language is a function that can call itself until the goal has been succeed. In Prolog, recursion appears when a predicate contains a goal that refers to itself.
As we have seen in the earlier chapters when a rule is called Prolog create a new query with new variables. So, it makes no difference whether a rule calls another rule or calls itself.
In Prolog and in any language, a recursive definition always has at least two parts. A first fact that acts like a stopping condition and a rule that call itself simplified. At each level the first fact is checked. If the fact is true then the recursion ends. If not, then recursion continues.
A recursive rule must never call itself with the same arguments! If that happens then the program will never end.

A Prolog program including Recursion:

parent(john,tom). parent(john,bob). parent(john,sue). parent(mary,bob). parent(mary,sue). parent(tom,mercy). parent(bob,clue). parent(bob,ray). parent(sue,clue). parent(sue,ray).

parent(sou,vedik). parent(vedik,harsh). parent(harsh,ved). parent(ved,milan). parent(milan,vishal). parent(vishal,chakki).

male(john). male(tom). male(ray). male(bob). female(mary). female(mercy). female(sue). female(clue).

grandp(X,Y):-
parent(X,A),parent(A,Y).


brother(X,Y):-
parent(A,Y),parent(A,X),male(X).

sister(X,Y):-
parent(A,Y)parent(A,X),female(X).

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

aunty(X,Y):-
parent(A,X),sister(Y,A).

cousins(X,Y):-
uncle(X,A),parent(A,Y), Y\=X.

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

ansc(X,Y):-
ansc(A,Y),parent(X,A).

Input:

parent(X,tom). parent(X,mercy). parent(X,harsh). parent(X,ved). male(mary). ansc(X,chakki).
;
;
;
;

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 friend relationship in terms of facts and rules

Prolog Program to insert different relationships in terms of facts and rules:

parent(john,tom). parent(john,bob). parent(john,sue). parent(mary,bob). parent(mary,sue). parent(tom,mercy). parent(bob,clue). parent(bob,ray). parent(sue,clue). parent(sue,ray).
parent(sou,vedik). parent(ved,harsh). parent(harsh,vijay).

parent(vijay,milan). parent(milan,vishal). parent(vishal,chakki).

male(john). male(tom). male(ray). male(bob). female(mary). female(mercy). female(sue). female(clue).

grandp(X,Y):-
parent(X,A),parent(A,Y).

brother(X,Y):-
parent(A,Y),parent(A,X),male(X).

sister(X,Y):-
parent(A,Y)parent(A,X),female(X).

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

aunty(X,Y):-
parent(A,X),sister(Y,A).

cousins(X,Y):-
uncle(X,A),parent(A,Y), Y\=X.

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

ansc(X,Y):-
ansc(A,Y),parent(X,A).
Input:

parent(X,tom). parent(X,mercy). parent(X,harsh). parent(X,vijay). male(mary).

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...