Professional Documents
Culture Documents
Objective: - Theory:-: Artificial Intelligence (Cs-1771)
Objective: - Theory:-: Artificial Intelligence (Cs-1771)
Objective: - Theory:-: Artificial Intelligence (Cs-1771)
Program:-
#include <stdio.h>
#include <conio.h>
char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int checkwin();
void board();
int main()
char mark;
do
board();
player = (player % 2) ? 1 : 2;
scanf("%d", &choice);
square[1] = mark;
square[2] = mark;
square[3] = mark;
square[4] = mark;
square[5] = mark;
square[6] = mark;
square[7] = mark;
square[8] = mark;
square[9] = mark;
else
printf("Invalid mo”);
player--;
getch();
i = checkwin();
player++;
}while (i == - 1);
board();
if (i == 1)
else
printf("==>\aGame draw");
getch();
return 0;
int checkwin()
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7]
return 0;
else
return - 1;
void board()
system("cls");
printf(" | | \n");
printf("_____|_____|_____\n");
printf(" | | \n");
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" | | \n\n");
Objective:- Write a program to implement BFS(for 8 puzzle problem on water Jug problem or
any AI search problem).
Theory:-
You are given a m litre jug and a n litre jug . Both the jugs are initially empty. The jugs don’t
have markings to allow measuring smaller quantities. You have to use the jugs to measure d
litres of water where d is less than n.
(X, Y) corresponds to a state where X refers to amount of water in Jug1 and Y refers to amount
of water in Jug2
Determine the path from initial state (xi, yi) to final state (xf, yf), where (xi, yi) is (0, 0) which
indicates both Jugs are initially empty and (xf, yf) indicates a state which could be (0, d) or (d,
0).
Program:-
#include <bits/stdc++.h>
#define mp make_pair
// before or not
map<pii, int> m;
vector<pii> path;
while (!q.empty()) {
continue;
continue;
isSolvable = true;
if (u.first == target) {
if (u.second != 0)
else {
if (u.first != 0)
int sz = path.size();
break;
q.push({ c, d });
c = u.first - ap;
d = u.second + ap;
q.push({ c, d });
if (!isSolvable)
// Driver code
int main()
return 0;
Program:-
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <iso646.h>
#include <math.h>
#define map_size_rows 10
#define map_size_cols 10
char map[map_size_rows][map_size_cols] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
struct stop {
/* array of indexes of routes from this stop to neighbours in array of all routes */
int * n;
int n_len;
double f, g, h;
int from;
};
int ind[map_size_rows][map_size_cols] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
struct route {
double d;
};
int main() {
int i, j, k, l, b, found;
int p_len = 0;
int c_len = 0;
int o_len = 1;
int s;
int e;
int current;
int s_len = 0;
int r_len = 0;
if (!map[i][j]) {
++s_len;
int t = s_len - 1;
stops[t].col = j;
stops[t].row = i;
stops[t].from = -1;
stops[t].g = DBL_MAX;
stops[t].n_len = 0;
stops[t].n = NULL;
ind[i][j] = t;
s = 0;
e = s_len - 1;
if (ind[i][j] >= 0) {
continue;
if (ind[k][l] >= 0) {
++r_len;
int t = r_len - 1;
routes[t].x = ind[i][j];
routes[t].y = ind[k][l];
++stops[routes[t].x].n_len;
stops[routes[t].x].n[stops[routes[t].x].n_len - 1] = t;
open[0] = s;
stops[s].g = 0;
found = 0;
min = DBL_MAX;
current = open[i];
min = stops[open[i]].f;
if (current == e) {
found = 1;
++p_len;
path[p_len - 1] = current;
current = stops[current].from;
++p_len;
path[p_len - 1] = current;
if (open[i] == current) {
--o_len;
break;
++c_len;
closed[c_len - 1] = current;
b = 0;
if (routes[stops[current].n[i]].y == closed[j]) {
b = 1;
if (b) {
continue;
b = 1;
if (o_len > 0) {
if (routes[stops[current].n[i]].y == open[j]) {
b = 0;
stops[routes[stops[current].n[i]].y].from = current;
stops[routes[stops[current].n[i]].y].g = tempg;
stops[routes[stops[current].n[i]].y].f = stops[routes[stops[current].n[i]].y].g +
stops[routes[stops[current].n[i]].y].h;
if (b) {
++o_len;
open[o_len - 1] = routes[stops[current].n[i]].y;
if (map[i][j]) {
putchar(0xdb);
} else {
b = 0;
if (ind[i][j] == path[k]) {
++b;
if (b) {
putchar('x');
} else {
putchar('.');
putchar('\n');
if (not found) {
puts("IMPOSSIBLE");
} else {
free(stops[i].n);
free(stops);
free(routes);
free(path);
free(open);
free(closed);
return 0;
Program:-
% queens(+N,-Queens): Queens is a solution to the N-queens problem
queens(N,Qs) :-
range(1,N,Us),
queens(Us,[],Qs).
% queens(+Unplaced,?Placed,?Queens)
queens([],Qs,Qs).
queens(Us,Ps,Qs) :-
select(Q,Us,Us1),
\+ attack(Q,Ps),
queens(Us1,[Q|Ps],Qs).
range(J,J,[J]).
range(I,J,[I|Ns]) :-
I < J, I1 is I + 1, range(I1,J,Ns).
attack(Q,Qs) :- attack(Q,1,Qs).
attack(X,N,[Y|_]) :- X is Y + N.
attack(X,N,[Y|_]) :- X is Y - N.
attack(X,N,[_|Ys]) :-
N1 is N + 1, attack(X,N1,Ys).
Program:-
DOMAINS
cell=c(integer,integer)
list=cell*
int_list=integer*
PREDICATES
solution(list)
member(integer,int_list)
nonattack(cell,list)
CLAUSES
solution([]).
solution([c(X,Y)|Others]):-
solution(Others),
member(Y,[1,2,3,4,5,6,7,8]),
nonattack(c(X,Y),Others).
nonattack(_,[]).
nonattack(c(X,Y),[c(X1,Y1)|Others]):-
Y<>Y1,
Y1-Y<>X1-X,
Y1-Y<>X-X1,
nonattack(c(X,Y),Others).
member(X,[X|_]).
member(X,[_|Z]):-
member(X,Z).
GOAL
solution([c(1,A),c(2,B),c(3,C),c(4,D),c(5,E),c(6,F),c(7,G),c(8,H)]).
Program:-
domains
/* will allow us cooperate with better names, for me this is like #typedef in C++ */
town = symbol
distance = unsigned
rib = r(town,town,distance)
tlist = town*
rlist = rib*
predicates
nondeterm way(town,town,rlist,distance)
nondeterm route(town,town,rlist,tlist,distance)
nondeterm route1(town,tlist,rlist,tlist,distance)
nondeterm ribsmember(rib,rlist)
nondeterm townsmember(town,tlist)
nondeterm tsp(town,town,tlist,rlist,tlist,distance)
nondeterm ham(town,town,tlist,rlist,tlist,distance)
nondeterm shorterRouteExists(town,town,tlist,rlist,distance)
nondeterm alltown(tlist,tlist)
nondeterm write_list(tlist)
clauses
write_list([]).
write_list([H|T]):-
write(H,‘ ‘),
write_list(T).
townsmember(X,[X|_]).
townsmember(X,[_|L]):-
townsmember(X,L).
ribsmember(r(X,Y,D),[r(X,Y,D)|_]).
ribsmember(X,[_|L]):-
ribsmember(X,L).
alltown(_,[]).
alltown(Route,[H|T]):-
townsmember(H,Route),
alltown(Route,T).
/* Is true if there is a way from Town1 to Town2, and also return distance between them */
way(Town1,Town2,Ways,OutWayDistance):-
ribsmember(r(Town1,Town2,D),Ways),
OutWayDistance = D.
%/*
way(Town1,Town2,Ways,OutWayDistance):-
OutWayDistance = D.
%*/
route(Town1,Town2,Ways,OutRoute,OutDistance):-
route1(Town1,[Town2],Ways,OutRoute,T1T2Distance),
%SWITCH HERE
route1(Town1,[Town1|Route1],_,[Town1|Route1],OutDistance):-
OutDistance = 0.
route1(Town1,[Town2|PassedRoute],Ways,OutRoute,OutDistance):-
way(TownX,Town2,Ways,WayDistance),
not(townsmember(TownX,PassedRoute)),
route1(Town1,[TownX,Town2|PassedRoute],Ways,OutRoute,CompletingRoadDistance),
shorterRouteExists(Town1,Town2,Towns,Ways,Distance):-
ham(Town1,Town2,Towns,Ways,_,Other),
/* calling tsp(a,a,…. picks any one connected to a town and calls another tsp*/
tsp(Town1,Town1,Towns,Ways,BestRoute,MinDistance):-
way(OtherTown,Town1,Ways,_),
tsp(Town1,OtherTown,Towns,Ways,BestRoute,MinDistance).
/*Travelling Salesman Problem is Hammilton way which is the shortes of other ones.*/
tsp(Town1,Town2,Towns,Ways,BestRoute,MinDistance):-
ham(Town1,Town2,Towns,Ways,Route,Distance),
not(shorterRouteExists(Town1,Town2,Towns,Ways,Distance)),
BestRoute = Route,
MinDistance = Distance.
/*Hammilton route from Town1 to Town2 assuming that Town2->Town1 way exists.*/
ham(Town1,Town2,Towns,Ways,Route,Distance):-
route(Town1,Town2,Ways,Route,Distance),
%SWITCH HERE
alltown(Route,Towns), % if you want simple road without including all towns you could uncomment
this line
write_list(Route),
write(” tD = “,Distance,“n“).
% fail.
Goal;
/* EXAMPLE 2 */
AllTowns = [a,b,c,d,e],
AllWays = [r(a,c,1),r(a,b,6),r(a,e,5),r(a,d,8),r(c,b,2),r(c,d,7),r(c,e,10),r(b,d,3),r(b,e,9),r(d,e,4)],
tsp(a,a,AllTowns,AllWays,Route,Distance),
%SWITCH HERE
% tsp(a,b,AllTowns,AllWays,Route,Distance),
write(“Finally:n“),
write_list(Route),
(a):- John gives Marry a Book. (b):- john gave Marry the book yesterday.
Conceptual Dependency originally developed to represent knowledge acquired from natural language
input.
• That is to say: For any 2 (or more) sentences that are identical in meaning there should be only
one representation of that meaning.
CD provides:
Now let us consider a more complex sentence: Since smoking can kill you, I stopped Lets look at how we
represent the inference that smoking can kill:
• Killing is a transition from being alive to dead. We use triple arrows to indicate a transition from
one state to another.
• Have a conditional, c causality link. The triple arrow indicates dependency of one concept on
another.
• Arrows indicate the direction of dependency. Letters above indicate certain relationships:
o-- object.
R-- recipient-donor.
• Double arrows ( ) indicate two-way links between the actor (PP) and action (ACT).
• The actions are built from the set of primitive acts (see above).
The use of tense and mood in describing events is extremely important and schank introduced the
following modifiers:
P -- past
f-- future
t-- transition
k-- continuing
?-- interrogative
/-- negative
delta-- timeless
such as: Neural Networks – e.g. brain modelling, time series prediction, classification Evolutionary
Computation – e.g. genetic algorithms,genetic programming Vision – e.g. object recognition, image
understanding Robotics – e.g. intelligent control, autonomous exploration Expert Systems – e.g. decision
support systems, teaching systems Speech Processing– e.g. speech recognition and production Natural
Language Processing – e.g. machine translation Planning – e.g. scheduling, game playing Machine
Learning – e.g. decision tree learning, version space learning.
Building trust
AI is all related to science and algorithms, which lies on the technical side. People who are completely
unaware of these algorithms and technology that lies behind the working of Artificial intelligence find it
difficult to understand its functioning.
Here is how artificial intelligence can face trust issues with humans, in spite of its ability to cut down on
tasks. It is a basic human psychology that we often neglect something that we don’t understand. We as
humans tend to stay away from anything complicated.
AI human interface
The challenge here is the shortage of data science skills within humans to get maximum output from
artificial intelligence. As for the businesses, there is a shortage of advanced skills. Business owners need
to train their professionals to be able to leverage the benefits of this technology.
Statistics reveal that 55% of survey respondents felt the biggest challenge was the changing scope of
human jobs when everything will be automated.
Investment
Another challenge of artificial intelligence is that not all business owners or managers are willing to
invest in it. The funds required to set up and implement Artificial Intelligence is very high, thus not every
business owner or organization can invest in it or can try it for their own business.
Software malfunction
No technology or human is perfect. In case of software or hardware crashes, it is difficult to put a finger
on what went wrong. On the other hand, tasks performed by humans can be traced.
However, with machines and inbuilt algorithms in the picture, it is difficult to blame someone or find the
cause of a software/hardware crash. A recent example of this is the self-driving cars that took the life of
a pedestrian.
AI is more like a tool that helps increase the productivity of a task. It has the ability to replace all the
worldly tasks with machines and lets you do more productive tasks with your time. This is a tool that
strengthens and boost the performance and efficiency of an average worker.
Higher expectations
AI could have serious issues with the expectations of the people around. People, in general, don’t have a
detailed understanding of how AI works and hence they have extremely high expectations; some of
which are not even possible.
Humans have a tendency to expect high from something that is trending and the outputs from it are also
going to be excellent. However, like any other technology, there are certain limitations associated with
AI.