Artificial Intelligence
MODULE I
What is Intelligence???
Intelligence is the ability to learn about, to learn from, to understand about, and interact with
ones environment.Intelligence is the faculty of understanding
Page 1
CS010 802
Artificial Intelligence
Need for AI: To supplement natural intelligence for e.g we are building intelligence in an
object so that it can do what we want it to do, as for example-- robots, thus reducing human
labour and reducing human mistakes.
Page 2
CS010 802
Artificial Intelligence
Definitions
Definitions of artificial intelligence according to 8 text books are given below.
1.AI is an exciting effort to make computers think machines with minds, in the full
and literal sense.
2.AI is the automation of activities that we associate with human thinking, activities
such as decision making, problem solving, learning..etc.
5. AI is the art of creating machines that perform functions that require intelligence
when performed by people.
6. AI is the study of how to make computers do things at which , at the moment , people
do better.
Page 3
CS010 802
Artificial Intelligence
AI application areas
The 2 most fundamental concerns of AI researchers are
Knowledge representation
Search.
Other Areas
Game Playing
Heuristics
Automated reasoning & theorem proving
Expert systems
Natural language understanding & semantic modeling
Modeling human performance
Planning & robotics
Machine learning
Neural nets & genetic algorithms
Knowledge representation
It addresses the problem of capturing the full range of knowledge required for
intelligent behavior in a formal language, i.e. One suitable for computer manipulation.
Eg. Predicate calculus,LISP, Prolog, Python
Search
It is a problem solving technique that systematically explores a space of problem
states,ie, successive and alternative stages in the problem solving process.
The following explains the major application areas of AI.
Game playing
Much of the early research in AI was done using common board games such as checkers,
chess and the 15 puzzle. Board games have certain properties that made them ideal for AI
research. Most games are played using a well defined set of rules. This makes it easy to generate
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 4
CS010 802
Artificial Intelligence
the search space. The board configuration used in playing these games can be easily represented
on a computer. As games can be easily played, testing a game playing program presents no
financial or ethical burden.
Heuristics
Heuristic technique designed for solving problem more quickly when classic methods are too
slow, or find an approximate solution when classic methods fails to find any exact solution. This
is achieved by trading optimality, completeness, accuracy or precision for speed.
Games can generate extremely large search spaces. So we use powerful techniques called
heuristics to explore the problem space. Games provide a rich domain for the study of heuristic
search.
Automated reasoning and theorem proving
Examples for automatic theorem provers are Newell and Simons Logic
Theorist,General Problem Solver(GPS).Theorem proving research is responsible for the
development of languages such as predicate calculus and prolog.
The attraction of automated theorem proving lies in the rigor and generality of logic. A
wide variety of problems can be attacked by representing the problem description as logical
axioms and treating problem instances as theorems to be proved.
Reasoning based on formal mathematical logic is also attractive. Many important
problems such as design and verification of logic circuits, verification of the correctness of
computer programs and control of complex systems come in this category.
Task domains of AI
Page 5
CS010 802
Artificial Intelligence
Expert systems
Here comes the importance of domain specific knowledge. A doctor, for example, is
effective at diagnosing illness because she possesses some innate general problem solving skill;
she is effective because she knows a lot about medicine. A geologist is effective at discovering
mineral deposits.
Expert knowledge is a combination of theoretical understanding of the problem and a
collection of heuristic problem solving rules that experience has shown to be effective in the
domain. Expert systems are constructed by obtaining this knowledge from a human expert and
coding it into a form that a computer may apply to similar problems.
To develop such a system, we must obtain knowledge from a human domain expert.
Examples for domain experts are doctor, chemist, geologist, engineer etc.. The domain expert
provides the necessary knowledge of the problem domain. The AI specialist is responsible for
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 6
CS010 802
Artificial Intelligence
implementing this knowledge in a program. Once such a program has been written, it is
necessary to refine its expertise through a process of giving it example problems to solve and
making any required changes or modifications to the programs knowledge.
Dendral is an expert system designed to infer the structure of organic molecules from
their chemical formulas and mass spectrographic information about the chemical bonds
present in the molecules.
Mycin is an expert system which uses expert medical knowledge to diagnose and
prescribe treatment for spinal meningitis and bacterial infections of the blood.
Prospector is an expert system for determining the probable location and type of ore
deposits based on geological information about a site.
Internist is an expert system for performing diagnosis in the area of internal medicine.
The dipmeter advisor is an expert system for interpreting the results of oil well drilling
logs.
Xcon is an expert system for configuring VAX computers.
Page 7
CS010 802
Artificial Intelligence
their human counterparts. Still, the design of systems that explicitly model some aspect of human
performance has been a fertile area of research in both AI and psychology.
Planning and robotics
Research in planning began as an effort to design robots that could perform their tasks
with some degree of flexibility and responsiveness to outside world. Planning assumes a robot
that is capable of performing certain atomic actions.
Planning is a difficult problem because of the size of the space of possible sequences of
moves. Even an extremely simple robot is capable of generating a vast number of potential move
sequences.
One method that human beings use in planning is hierarchical problem decomposition.
If we are planning a trip to London, we will generally treat the problems of arranging a flight,
getting to the air port, making airline connections and finding ground transportation in London
separately. Each of these may be further decomposed into smaller sub problems.
Creating a computer program that can do the same is a difficult challenge.
A robot that blindly performs a sequence of actions without responding to changes in its
environment cannot be considered intelligent. Often, a robot will have to formulate a plan based
on the incomplete information and correct its behavior. A robot may not have adequate sensors
to locate all obstacles in the way of a projected path. Organizing plans in a fashion that allows
response to changing environmental conditions is a major problem for planning.
Machine learning
An expert system may perform extensive and costly computations to solve a problem.
But if it is given the same or similar problem a second time, it usually does not remember the
solution. It performs the same sequence of computations again. This is not the behavior of an
intelligent problem solver.
The programs must learn on their own. Learning is a difficult area. But there are several
programs that suggest that it is possible.
One program is AM, the automated mathematician which was designed to discover
mathematical laws. Initially given the concepts and axioms of set theory, AM was able to induce
important mathematical concepts such as cardinality, integer arithmetic and many of the results
of number theory. AM conjectured new theorems by modifying its current knowledge base.
Early work includes Winstons research on the induction of structural concepts such as
arch from a set of examples in the blocks world.
The ID3 algorithm has proved successful in learning general patterns from examples.
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 8
CS010 802
Artificial Intelligence
Meta dendral learns rules for interpreting mass spectrographic data in organic
chemistry from examples of data on compounds of known structure.
Teiresias, an intelligent front end for expert systems, converts high level advice into new
rules for its knowledge base.
There are also now many important biological and sociological models of learning.
Neural nets and genetic algorithms
An approach to build intelligent programs is to use models that parallel the structure of
neurons in the human brain.
A neuron consists of a cell body that has a number of branched protrusions called
dendrites and a single branch called the axon. Dendrites receive signals from other neurons.
When these combined impulses exceed a certain threshold, the neuron fires and an impulse or
spike passes down the axon.
This description of the neuron captures features that are relevant to neural models of
computation. Each computational unit computes some function of its inputs and passes the result
along to connected units in the network; the final results are produced by the parallel and
distributed processing of this network of neural connection and threshold weights.
Genetic algorithm- examples
PROBLEMS
Page 9
CS010 802
Artificial Intelligence
Initial state
Final state
Space including transition function or path function
Path cost
Solution of the problem
Path from initial state to goal state is the solution. It is guided by transition rules. Optimal
solution means among all solutions, whichever solution has least path cost.
Page 10
CS010 802
Artificial Intelligence
Representation of AI problems
It includes 4 parts
Lexical part
Structural part
Procedural part
Semantic part
1. Lexical part
Determines which symbols are allowed in the representation of problem
Example-Problem of playing chess
Board position
8x8 array
Official chess opening position
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 11
CS010 802
Artificial Intelligence
Goal :Opponent does not have a legal move and his king in under attack
2. Structural part
Constraints on how the symbols can be arranged
Example
Legal moves
Way of getting into initial sate & goal state
Set of rules
L.H.S pattern to be matched against current board position
R.H.S changes to be made to board position to reflect the move
3. Procedural part
Specifies access procedures that enable to create descriptions, to modify them, and to
answer questions using them
Example
Set of winning moves
Legal moves Vs useful moves
Legal moves
Moves which are permissible according to game rules
10 120
Useful moves
Legal moves which bring the game in a winning position
Useful moves subset of legal moves
4. Semantic part
Way of associating meaning with the descriptions
Example
No hidden meaning associated with any piece
PRODUCTION SYSTEM
AI system developed for solution of any problem
Used for application of rules and obtaining the solution
Components
Production rules
Knowledge/data
bases Control strategy
Rule
applier 1.Production rules
PQ
L.H.S-current problem state
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 12
CS010 802
Artificial Intelligence
Page 13
CS010 802
Artificial Intelligence
PROBLEM CHARATERISTICS
1. Path Finding Problems
2. Decomposable problems
3. Recoverable Problems
4. Predictable problems
5. Problems affecting the quality of solution
6 State Finding problems
7. Problems requiring interaction
8. Knowledge intensive problems
1. Path Finding problems
Solution involves reporting of path ( or sequence of steps used to obtain the solution).Ex:
Travelling salesperson problem
2. Decomposable problems
The problem can be broken into small subproblems and the solution of the main problem is
obtained by joining the solutions of small subproblems
Eg: mathematical equation
Suppose we want to solve the problem of computing the
2
2
2
expression ( x + 3x + Sin x . Cos x ) dx
2
We can solve this problem by breaking it down into 3 smaller problemsi) x , ii) 3x iii)
2
2
Sin x . Cos x each of which can then solve by using a small collection of specific rules.
3. Recoverable problems
Application of operator can be reverted if required and on initial state , a new operator
can be applied
Ex: 8-puzzle problem
The recoverability of a problem plays an important role in determining the control
structure necessary for problem solution.
Ignorable problem
Like theorem proving where the steps can be ignored. These can be solved using simple
control structures which is easy to implement.
Irrecoverable problem
Like medical diagnosis problem where if an operator is applied, it cannot be reverted to original
state. the selection of the operator is very critical because if a wrong operator is applied, it may
be possible that a solution never obtained even if it exists. Another example is chess playing.
Page 14
CS010 802
Artificial Intelligence
4. Predictable problems
Certain outcome problems (eg. 8 puzzle)
Suppose we are playing with the 8 puzzle problem. Every time we make a move,
we know exactly what will happen. This means that it is possible to plan an entire sequence
of moves and be confident that we know what the resulting state will be.
Uncertain outcome problems
(eg. Bridge)
However, in games such as bridge, this planning may not be possible. One of the
decisions we will have to make is which card to play on the first trick. What we would like to do
is to plan the entire hand before making that first play. But now it is not possible to do such
planning with certainty since we cannot know exactly where all the cards are or what the other
players will do on their turns.
One of the hardest types of problems to solve is the irrecoverable, uncertain outcome.
Examples of such problems are Playing bridge, Controlling a robot arm, Helping a lawyer
decide how to defend his client against a murder charge.
5. Problems affecting the quality of solution
There are certain type of AI problems where the process of finding solution stops by just
finding one solution and there is no need to ensure the validity of this solution by finding
other solutions.
Ex: database query application
In query applications whenever query is answered the possible answers are not checked.
Page 15
CS010 802
Artificial Intelligence
In water jug problem, it is not sufficient to report that we have solved the problem and
that the final state is (2,0). For this kind of problem, what we really must report is not the
final state, but the path that we found to that state.
Another example is medical diagnosis problem using expert system.
Solitary problems
Here the computer is given a problem description and produces an answer with no
intermediate communication and with no demand for an explanation for the reasoning
process.
Consider the problem of proving mathematical theorems. If
1. All we want is to know that there is a proof.
2. The program is capable of finding a proof by itself.
Then it does not matter what strategy the program takes to find the proof.
Conversational problems
In which there is intermediate communication between a person and the computer, either
to provide additional assistance to the computer or to provide additional information to the user.
Eg. Suppose we are trying to prove some new, very difficult theorem. Then the program may not
know where to start. At the moment, people are still better at doing the high level strategy
required for a proof. So the computer might like to be able to ask for advice. To exploit such
advice, the computers reasoning must be analogous to that of its human advisor, at least on a
few levels.
Page 16
CS010 802
Artificial Intelligence
Examples are
Vaccum world
8-puzzle problem
8-Queens problem
vacuum world
The agent is in one of the 2 locations, each of which might or might not contain dirt. Any state
can be designated as the initial state. After trying these actions (Left, Right, Suck), we get
another state. The goal test checks whether all squares are clean.
Page 17
CS010 802
Artificial Intelligence
8 puzzle problem
It consists of a 3 X 3 board with 8 numbered tiles and a blank space as shown below.
A tile adjacent to the blank space can slide into the space. The aim is to reach a specified goal
state, such as the one shown on the right of the figure.
8 Queens problem
The goal of 8- queens problem is to place 8 queens on a chess board such that no queen
attacks any other.
(a queen attacks any piece in the same row, column or diagonal). Figure shows an attempted
solution that that fails: the queen in the rightmost column is attacked by the queen at the top left.
Page 18
CS010 802
Artificial Intelligence
Visit every city in the figure at least once starting and ending in same location.
Traveling salesperson problem (TSP)
Is a touring problem in which each city must be visited exactly once. The aim is to .find the
shortest tour.
VLSI layout problem
It requires positioning of millions of components and connections on a chip to minimize area,
minimize circuit delays, minimize stray capacitances, and maximize manufacturing yield.
Robot navigation
It is a generalization of the route finding problem. Rather than a discrete set of routes, a robot
can move in a continuous space with an infinite set of possible actions and states.
Automatic assembly sequencing of complex objects by a robot
The assembly of intricate objects such as electric motors is economically feasible. In assembly
problems, the aim is to find an order in which to assemble the parts of some object. If the
wrong order is chosen, there will be no way to add some part later in the sequence without
undoing some of the work already done.
Protein design :Here the aim is to find a sequence of amino acids that will fold into a three
dimensional protein with the right properties to cure some disease.
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 19
CS010 802
Artificial Intelligence
Internet searching: It means looking for answers to questions for related information or for
shopping details. Software robots are being developed for performing this internet searching.
Other Examples of AI problems
1) Tic-Tac-Toe problem
3) 8- Puzzle problem
4) 8-Queens problem
5) Chess problem
9) Magic square
1)Tic-Tac-Toe problem
Starting with an empty board, the first player may place an X in any one of nine places. The
player who is able to make his marks in vertical, horizontal or diagonal straight line first is
declared as a winner. Each of these moves yields a different board that will allow the opponent 8
possible responses and so on. We can represent this collection of possible moves and responses
by regarding each board configuration as a node in a graph. The links of the graph represent legal
moves from one board configuration to another. These nodes correspond to different states of the
game board. The resulting structure is called a state space graph.
Page 20
CS010 802
Artificial Intelligence
Solution of the problem will include applying appropriate rules in a specific sequence to
transform the start state to goal state. One solution is applying the rules in the sequence
2,9,2,7,5,9.
Page 21
CS010 802
Artificial Intelligence
3) 8-puzzle problem
The 8-puzzle belongs to the category of sliding block puzzle types of problems.
It is described as
It has set of 3x3 board having 9 block spaces out of which, 8 blocks are having tiles
bearing number from 1 to 8. One space is left blank. The tile adjacent to blank space
can move into it. We have to arrange the tiles in a sequence
4.)8-Queens problem
This problem is presented as follows
We have 8 queens and a 8x8 chessboard having alternate black and white squares. The
queens are placed on the chessboard. Any queen can attack any other queen placed on
same row, or column, or diagonal. We have to find the proper placement of queens on the
chessboard in as such a way that no queen attacks other queen
Page 22
CS010 802
Artificial Intelligence
5) Chess problem
In a chess game problem, the start state is the initial configuration of chess board. The final or
goal state is any configuration, which is a winning position for any player. It is estimated that the
120
chess game has more than 10
possible states.
Page 23
CS010 802
Artificial Intelligence
Page 24
CS010 802
Artificial Intelligence
SEARCHING STRATEGIES
The process of finding a solution of AI problem is known as searching. It searches the path from
start state to goal state. This very important aspect of problem solving because search techniques
not only helps in finding most viable path to reach goal state, but also make entire process
efficient and economical.
Basic Search Techniques
1) Blind (or unguided or uninformed)
2) Heuristic (or guided or informed)
1) Blind Search
Search methodology having no additional information about states beyond that provided
in the problem definitions. In this search total search space is looked for solution.
It includes
1) Breadth-first Search(BFS)
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 25
CS010 802
Artificial Intelligence
2) Depth- First search(DFS)
3) Depth- Limited Search(DLS)
4) Bidirectional Search
States are labeled (A, B, C.).Breadth first search explores the space in a level by level fashion.
Only when there are no more states to be explored at a given level does the algorithm move on to
the next level. A breadth first search of the above graph considers the states in the order A, B, C,
D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U. We implement breadth first search using lists,
open and closed, to keep track of progress through the state space. open lists states that have
been generated but whose children have not been examined. The order in which states are
removed from open determines the order of the search. closed records states that have already
been examined.
void breadth_ first _ search ( )
{
open = [ start ];
closed = [ ];
while ( open not empty )
{
Remove the leftmost state from open, call it X;
Page 26
CS010 802
Artificial Intelligence
if X is a goal,
then return SUCCESS;
else
{
Generate children of X;
Put X on closed;
Discard children of x, if already on open or closed;
Put remaining children on right end of open;
}
}
return FAIL;
}
Child states are generated by inference rules, legal moves of a game or other state transition
operators. Each iteration produces all children of the state x and adds them to open. Note that
open is maintained as a queue (FIFO) data structure. States are added to the right of the list and
removed from the left. A trace of the breadth first search on the graph appears below.
Open
closed
empty
BCD
CDEF
BA
DEFGH
CBA
EFGHIJ
DCBA
FGHIJKL
EDCBA
GHIJKLM
FEDCBA
HIJKLMN
GFEDCBA
.
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 27
CS010 802
Artificial Intelligence
.
And so on until open = [ ].
Because breadth first search considers every node at each level of the graph before going deeper
in to the space, all states are first reached along the shortest path from the start state. Breadth
first search is therefore guaranteed to find the shortest path from the start state to goal.
2)Depth first search (DFS)
Dept first search goes deeper in to the search space whenever this is possible.
Consider the graph
Depth first search examines the states in the graph in the order A, B, E, K, S, L, T, F, M, C, G,
N, H, O, P, U, D, I, Q, J, R. In depth first search, the descendent states are added and removed
from the left end of open.
void depth_first_search ()
{
open = [start];
closed = [ ];
while (open not empty )
{
remove leftmost state from open, call it X;
if X is a goal state
then return SUCCESS;
else
{
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 28
CS010 802
Artificial Intelligence
generate children of X;
put X on closed;
discard children of X, if already on open or
closed; put remaining children on left end of open;
}
}
return FAIL;
}
A trace of depth first search on the above graph is shown below.
open
closed
empty
BCD
EFCD
BA
KLFCD
EBA
SLFCD
KEBA
LFCD
SKEBA
TFCD
LSKEBA
FCD TLSKEBA
MCDFTLSKEBA
CDMFTLSKEBAGH
DCMFTLSKEBA
Page 29
CS010 802
Artificial Intelligence
The most difficult case for bidirectional search is when the goal test gives only an implicit
description of some possibly large set of goal states. For example, all the states satisfying the
check mate goal test in chess.
ii)HEURISTICS
Search techniques where additional information about the problem is provided in order to
guide the search in a specific direction.
Types of heuristic search techniques
2) Best-first-search
3) Problem reduction
Page 30
CS010 802
Artificial Intelligence
5) Constraint satisfaction
possible states.
Heuristic function
It relates the current search state with the desired goal state.
It is the function that maps from problem state descriptions to measures of desirability
Usually represented as numbers
Well designed heuristic function can play an important part efficiently guiding a
search process towards a solution.
Page 31
CS010 802
Artificial Intelligence
Sometimes very simple heuristic function can provide a fairly good estimate of
whether the path is good or not.
The purpose of heuristic function is to guide the search process in the most profitable
direction by suggesting which path to follow first when more than one is available.
Page 32
CS010 802
Artificial Intelligence
An AI program developed in 1980 and known as DANDRAL uses a strategy called plangenerate-test, in which planning part uses constraint satisfaction techniques to create list
of recommended substructures, and generate and test method then uses these lists to
explore only limited set of structures. Thus in such type of combined uses, generate- andtest approach has proved to be very effective.
HILL CLIMBING
It is a variant of generate and test in which feedback from the test procedure is used in deciding
in which direction the search should proceed.
Hill climbing strategies expand the current state in the search and evaluate its children.
The best child is selected for further expansion; neither its siblings nor its parent is retained.
Search halts when it reaches a state that is better than any of its children. Hill climbing is named
for the strategy that might be used by an eager, but blind mountain climber: go uphill along the
steepest possible path until you can go no farther. Because it keeps no history, the algorithm
cannot recover from failures of its strategy.
There are three various strategies for hill climbing. They are
1)Simple hill climbing,
2)Steepest ascent hill climbing and
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 33
CS010 802
Artificial Intelligence
3)Simulated annealing.
Simple hill climbing
The simplest way to implement hill climbing is as follows.
Algorithm
1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise
continue with the initial state as the current state.
2. Loop until a solution is found or until there are no new operators left to be applied in
the current state:
a. Select an operator that has not yet been applied to the current state and apply it
to produce a new state.
b. Evaluate the new state,
i. If it is a goal state, then return it and quit.
ii. If it is not a goal state, but it is better than the current state, then make
it the current state.
iii. If it is not better than the current state, then continue in the loop.
Example:
A problem is given. Given the start state as a and the goal state as f. Suppose we have a
heuristic function h (n) for evaluating the states. Assume that a lower value of heuristic function
indicates a better state.
Page 34
CS010 802
Artificial Intelligence
Here a has an evaluation value of 5. a is set as the current state. Generate a successor of a. Here it
is b. The value of b is 3. It is less than that of a. that means b is better than the current state a. So
set b as the new current state. Generate a successor of b. it is d. D has a value of 4. It is not better
than the current state b (3). So generate another successor of b. it is e. It has a value of 6. It is not
better than the current state b (3). Then generate another successor of b. We get k. It has an
evaluation value of 2. k is better than the current state b (3). So set k as the new current state.
Now start hill climbing from k. Proceed with this, we may get the goal state f.
ii) Steepest ascent hill climbing
A useful variation on simple hill climbing considers all the moves from the current state
and selects the best one as the next state. It is also called gradient search. This contrasts with the
basic method in which first state is better then current state is selected.
Algorithm
1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise,
continue with the initial state as the current state.
2. Loop until a solution is found or until a complete iteration produces no change to current
state.
a. Let SUCC be a state such that any possible successor of the current will be better
than SUCC.
b. For each operator that applies to the current state, do:
i. Apply the operator and generate a new state.
ii. Evaluate the new state. If it is a goal state, then return it and quit. If not,
compare it to SUCC. If it is better, then set SUCC to this state. If it is not
better, leave SUCC alone.
c. if the SUCC is better than current state, then set current state to SUCC.
Page 35
CS010 802
Artificial Intelligence
In this, all the child states (successors) of a are generated first. The state d has the least
value of heuristic function. So d is the best among the successors of a. then d is better than the
current state a. so make d as the new current state. Then start hill climbing from d.
Both basic and steepest ascent hill climbing may fail to find a solution. Either algorithm
may stop not by finding a goal state but by getting to a state from which no better states can be
generated. This will happen if the program has reached either a local maximum, a plateau or a
ridge.
State space of hill climbing
Page 36
CS010 802
Artificial Intelligence
2)Ridge
It is a special kind of local maxima having very steep slope which is difficult to be traced in
one calculation of objective function.
3)Plateau
A plateau is a flat area of the search space in which a whole set of neighboring states
have the same value, where next move does not give better solution than present state. On a
plateau, it is not possible to determine the best direction in which to move by making local
comparisons.
Page 37
CS010 802
Artificial Intelligence
Page 38
CS010 802
Artificial Intelligence
P = e-E / kT
Where E is positive change in the energy level, T is the temperature and k is
Boltzmanns constant.
Physical annealing has some properties. The probability of a large uphill move is lower
than the probability of a small one. Also, the probability that an uphill move will be made
decreases as the temperature decreases. Large uphill moves may occur early on, but as the
process progresses, only relatively small upward moves are allowed until finally the process
converges to a local maximum configuration.
If cooling occurs so rapidly, stable regions of high energy will form. If however, a slower
schedule is used, a uniform crystalline structure which corresponds to a global minimum is more
likely to develop. If the schedule is too slow, time is wasted.
These properties of physical annealing can be used to define the process of simulated
annealing. In this process, E represents change in the value of heuristic evaluation function
instead of change in energy level. k can be integrated in to T. Hence we use the revised
probability formula
P = e-E / T
The proper annealing scheduling is maintained to monitor T.
differences from hill-climbing search
i)The annealing schedule is maintained.
ii)Moves to worse states are also accepted
iii)In addition to current state, the best state record is also maintained.
Algorithm
1. Evaluate the initial state. If it is also a goal state, then return it and quit.
Otherwise, continue with the initial state as the current state.
2. Initialise BEST-SO-FAR to the current state.
3. Initialize T according to the annealing schedule.
4. Loop until a solution is found or until there are no new operators left to be applied in
the current state.
a. Select an operator that has not yet been applied to the current state and apply it to
produce a new state.
b. Evaluate the new state. Compute
Page 39
CS010 802
Artificial Intelligence
If it is not a goal state, but is better than the current state, then make it the
current state. Also set BEST-SO-FAR to this new state.
If it is not better than the current state, then make it the current state with
probability p as defined above. This step is usually implemented by
invoking a random number generator to produce a number in the range [ 0,
1]. If that number is less than p, then the move is accepted. Otherwise, do
nothing.
PYTHON
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python
was designed to be highly readable which uses English keywords frequently where as other
languages use punctuation and it has fewer syntactical constructions than other languages.
Python is interpreted: This means that it is processed at runtime by the interpreter and you do
not need to compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: This means that you can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
Python is Object-Oriented: This means that Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
Page 40
CS010 802
Artificial Intelligence
Python is Beginner's Language: Python is a great language for the beginner programmers and
supports the development of a wide range of applications from simple text processing to WWW
browsers to games
Python Features: Python's feature highlights include:
Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language in a relatively short period of time.
Easy-to-read: Python code is much more clearly defined and visible to the eyes.
Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain.
A broad standard library: One of Python's greatest strengths are the bulk of the library is
very portable and cross-platform compatible on UNIX, Windows and Macintosh. Interactive
Mode: Support for an interactive mode in which you can enter results from a terminal right to
the language, allowing interactive testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface
on all platforms.
Extendable: You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh and the X
Window system of UNIX.
Scalable: Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are listed
below:
Support for functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
Very high-level dynamic data types and supports dynamic type checking.
Supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java.
Page 41
CS010 802
Artificial Intelligence
Page 42
CS010 802
Artificial Intelligence
Updating Lists:
You can update single or multiple elements of lists by giving the slice on the left-hand side of the
assignment operator, and you can add to elements in a list with the append() method. Following
is a simple example:
Page 43
CS010 802
Artificial Intelligence
Python Tuples
Tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only
difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses
and lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values and optionally you can
put these comma-separated values between parentheses also. For example:
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
The empty tuple is written as two parentheses containing nothing:
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 44
CS010 802
Artificial Intelligence
tup1 = ()
To write a tuple containing a single value you have to include a comma, even though there is
only one value:
tup1 = (50,)
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on
Accessing Values in Tuples:
To access values in tuple, use the square brackets for slicing along with the index or indices to
obtain value available at that index. Following is a simple example:
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
Page 45
CS010 802
Artificial Intelligence
Page 46
CS010 802
Artificial Intelligence
Python Dictionary
A dictionary is mutable and is another container type that can store any number of Python
objects, including other container types. Dictionaries consist of pairs (called items) of keys and
their corresponding values.
Python dictionaries are also known as associative arrays or hash tables. The general syntax of
a dictionary is as follows:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
You can create dictionary in the following way as well:
dict1 = { 'abc': 456 }
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 47
CS010 802
Artificial Intelligence
Page 48
CS010 802
Artificial Intelligence
You can update a dictionary by adding a new entry or item (i.e., a key-value pair), modifying
an existing entry, or deleting an existing entry as shown below in the simple example:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
When the above code is executed, it produces the following result:
dict['Age']: 8
dict['School']: DPS School
Delete Dictionary Elements:
You can either remove individual dictionary elements or clear the entire contents of a
dictionary. You can also delete entire dictionary in a single operation.
To explicitly remove an entire dictionary, just use the delstatement. Following is a simple
example:
ict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
deldict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
deldict ; # delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School'];
This will produce the following result. Note an exception raised, this is because after
deldict dictionary does not exist anymore:
dict['Age']:
Traceback (most recentcall last):
File "test.py", line 8, in <module>
Page 49
CS010 802
Artificial Intelligence
Page 50
CS010 802
Artificial Intelligence
a=input()
print (a)
Program No:2 IF STATEMENT IN PYTHON.
print ("IF STATEMENT IN PYTHON")
print ("enter a no")
a=int (input())
if a<0:
print ("-ve")
else:
print(" a is +ve")
Program No:3 To Find the Sum of Two Numbers.
print ("To Find the Sum of Two Numbers")
print ("enter a number")
a= input()
print (a)
print ("enter second
number") b= input()
c=a+b
print ( "sum is %s" %c)
Program No:4 To Find the Sum of Two Numbers.
print("pyhon program to find the sum of two numbers using function")
deffindsum(a,b):
s=a+b
print("result is",s)
return s
Jeena Thomas, Asst. Professor, CSE, SJCET, Palai
Page 51
CS010 802
Artificial Intelligence
Page 52
CS010 802
Artificial Intelligence
Page 53
CS010 802
Artificial Intelligence
SLICING IN TUPLES
"""
tup2=(1,2,3,4,5)
print (tup2)
#tup2[1]=100
print("tup2",tup2[0])
print("tup2 1st to 4 th element",tup2[0:4])
Program No: 7 TO IMPLEMENT TUPLE DATA STRUCTURE.
print ( "DICTIONARIES IN PYTHON")
tel={} #EMPTY DICTIONARY
tel={'anu':486,'minu':485,'rose':489,'1':768}
print (tel)
print (tel ['anu'])
tel['bin']=487
deltel['rose']
print (tel)
PYTHON IMPLEMENTATION OF HILL CLIMBING
fromutils import *
import math, random, sys, time, bisect, string
#_______________________________________________________________
class Problem(object):
Page 54
CS010 802
Artificial Intelligence
Page 55
CS010 802
Artificial Intelligence
Page 56
CS010 802
Artificial Intelligence
Page 57
CS010 802
Artificial Intelligence
Page 58
CS010 802
Artificial Intelligence
Page 59