0% found this document useful (0 votes)
62 views

AIML - Unit 1 Notes

This document discusses artificial intelligence and machine learning. It begins by defining AI as creating intelligent machines, especially computer programs, that can perform tasks in a similar manner to humans. It then discusses some problems with AI like limited perception and understanding. Next, it lists applications of AI like robotics, natural language processing, and machine learning. The document proceeds to discuss techniques in AI like knowledge representation and using a move table to solve tic-tac-toe. It concludes by defining problems, problem spaces, and different search strategies used in problem solving.

Uploaded by

Harshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

AIML - Unit 1 Notes

This document discusses artificial intelligence and machine learning. It begins by defining AI as creating intelligent machines, especially computer programs, that can perform tasks in a similar manner to humans. It then discusses some problems with AI like limited perception and understanding. Next, it lists applications of AI like robotics, natural language processing, and machine learning. The document proceeds to discuss techniques in AI like knowledge representation and using a move table to solve tic-tac-toe. It concludes by defining problems, problem spaces, and different search strategies used in problem solving.

Uploaded by

Harshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

21EC1401 - ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING (LAB

INTEGRATED)

Unit 1
Introduction to Artificial Intelligence

What is Artificial Intelligence?


It is a branch of Computer Science that pursues creating computers or machines as intelligent as human
beings.
It is the science and engineering of making intelligent machines, especially intelligent computer programs.
It is related to the similar task of using computers to understand human intelligence, but AI does not have
to confine itself to methods that are biologically observable

Definition: Artificial Intelligence is the study of how to make computers do things, which, at the moment,
people do better.
According to the father of Artificial Intelligence, John McCarthy, it is “The science and engineering of
making intelligent machines, especially intelligent computer programs”.

Artificial Intelligence is a way of making a computer, a computer-controlled robot, or a software think


intelligently, in the similar manner the intelligent humans think.

Problems of AI: Intelligence does not imply perfect understanding; every intelligent being has limited
perception, memory and computation. Many points on the spectrum of intelligence versus cost are viable,
from insects to humans. AI seeks to understand the computations required from intelligent behaviour and
to produce computer systems that exhibit intelligence. Aspects of intelligence studied by AI include
perception, communication using human languages, reasoning, planning, learning and memory.

The following questions are to be considered before we can step forward:


1. What are the underlying assumptions about intelligence?
2. What kinds of techniques will be useful for solving AI problems?
3. At what level human intelligence can be modelled?
4. When will it be realized when an intelligent program has been built?

Applications of AI
AI has applications in all fields of human study, such as finance and economics, environmental
engineering, chemistry, computer science, and so on. Some of the applications of AI are listed below:

Perception
■ Machine vision
■ Speech understanding
■ Touch ( tactile or haptic) sensation
Robotics
Natural Language Processing
■ Natural Language Understanding
■ Speech Understanding
1
■ Language Generation
■ Machine Translation
Planning
Expert Systems
Machine Learning
Theorem Proving
Symbolic Mathematics
Game Playing

Physical System Hypothesis

AI Technique:
Artificial Intelligence research during the last three decades has concluded that Intelligence requires
knowledge. To compensate for overwhelming quality, knowledge possesses less desirable properties.
A. It is huge.
B. It is difficult to characterize correctly.
C. It is constantly varying.
D. It differs from data by being organized in a way that corresponds to its application.
E. It is complicated.

An AI technique is a method that exploits knowledge that is represented so that:

2
1. The knowledge captures generalizations that share properties, are grouped together, rather
than being allowed separate representation.
2. It can be understood by people who must provide it—even though for many programs bulk
of the data comes automatically from readings.
3. In many AI domains, how the people understand the same people must supply the
knowledge to a program.
4. It can be easily modified to correct errors and reflect changes in real conditions.
5. It can be widely used even if it is incomplete or inaccurate.
6. It can be used to help overcome its own sheer bulk by helping to narrow the range of
possibilities that must be usually considered.

Tic-Tac-Toe Problem using AI Technique.


It’s a paper and pencil game of two players X and O, who choose to mark a space on a grid of 3×3.The game is
won by the player who succeeds in putting three of their marks in a horizontal line, vertical line, or diagonal line.

This game is played by two players – One is a human and the other is a computer.The objective is to write a
computer program in such a way that the computer wins most of the time.
Assume ,
Player 1 – XPlayer 2 – O
So, a player who gets 3 consecutive marks first, they will win the game.Let’s have a discussion about how a
board’s data structure looks and how the Tic Tac Toe algorithm works.
Board Data Structure

3
Each square is numbered, which starts from 1 to 9 like following

image
Data Structures: Board and Move Table
Consider a Board having nine elements vector. Each element will contain

 0 for blank
 1 indicating X player move
 2 indicating O player move

The computer may play as an X or O player.The first player is always X.


Move Table:
It is a vector of 39 elements, each element of which is a nine-element vector representing board position.
Total of 39=19683 elements in move table
Sample Move Table looks like as shown

below:

4
When the computer receives the current board position the computer will go to the new board position as per the

entry.
ADVANTAGE
Program is very efficient in time
DISADVANTAGE
A lot of space and store the move table
A lot of no out to specify all the entries in the move table
ANOTHER APPROACH FOR Tic-Tac-Toe
Data Structures:
Board position is a structure containing,

1. A 9-element array representing the board


2. A list of board position that could result from the next move
3. A number or rating representing an estimate of how likely the board position is to lead an ultimate win for
the players to move

If the game goes Draw: return 0 to parent


If the game goes Win: return 1 to parent
This approach Backtracks and decides which path is best.

5
6
PROBLEMS, PROBLEM SPACES AND SEARCH
To solve the problem of building a system you should take the following steps:
1. Define the problem accurately including detailed specifications of what is the initial
situation and what constitutes a suitable solution.
2. Scrutinize the problem carefully, for some features may have a central affect on the
chosen method of solution.
3. Segregate and represent the background knowledge needed in the solution of the
problem.
4. Choose the best solving techniques for the problem to solve to a solution.

Problem solving is a process of generating solutions from observed data.


• a ‘problem’ is characterized by a set of goals,
• a set of objects, and
• a set of operations.
These could be ill-defined and may evolve during problem solving.
• A ‘problem space’ is an abstract space.
 A problem space encompasses all valid states that can be generated by the
application of any combination of operators on any combination of objects.
 The problem space may contain one or more solutions. A solution is a
combination of operations and objects that achieve the goals.
• A ‘search’ refers to the search for a solution in a problem space.
 Search proceeds with different types of ‘search control strategies’.
 The depth-first search and breadth-first search are the two common search
strategies.

Problem definitions
A problem is defined by its ‘elements’ and their ‘relations’. To provide a formal description of a
problem, we need to do the following:

a. Define a state space that contains all the possible configurations of the relevant objects,
including some impossible ones.
b. Specify one or more states that describe possible situations, from which the problem-
solving process may start. These states are called initial states.
c. Specify one or more states that would be acceptable solution to the problem.

These states are called goal states.

DEFINING PROBLEM AS A STATE SPACE SEARCH

To solve the problem of playing a game, we require the rules of the game and targets for winning
as well as representing positions in the game. The opening position can be defined as the initial
state and a winning position as a goal state. Moves from initial state to other states leading to the
goal state follow legally. However, the rules are far too abundant in most games— especially in
chess, where they exceed the number of particles in the universe. Thus, the rules cannot be
supplied accurately and computer programs cannot handle easily. The storage also presents
7
another problem but searching can be achieved by hashing.

The number of rules that are used must be minimized and the set can be created by expressing
each rule in a form as possible. The representation of games leads to a state space representation
and it is common for well-organized games with some structure. This representation allows for
the formal definition of a problem that needs the movement from a set of initial positions to one
of a set of target positions. It means that the solution involves using known techniques and a
systematic search. This is quite a common method in Artificial Intelligence.

State Space Search


A state space represents a problem in terms of states and operators that change states.
A state space consists of:
 A representation of the states the system can be in. For example, in a
board game, the board represents the current state of the game.
 A set of operators that can change one state into another state. In a board
game, the operators are the legal moves from any given state. Often the
operators are represented as programs that change a state representation to
represent the new state.
 An initial state.
 A set of final states; some of these may be desirable, others undesirable.
This set is often represented implicitly by a program that detects terminal
states.

The Water Jug Problem (Production Rules - Example)

In this problem, we use two jugs called four litre and three litre; four holds a maximum of four
litres of water and three a maximum of three litres of water. How can we get two litres of water
in the four litre jug?

Representation of water Jug Problem in terms of state-space search,


State: (x, y)
where x represents the quantity of water in a 4-liter jug and y represents the quantity of water in a 3-liter jug.

That is, x = 0, 1, 2, 3, or 4 y = 0, 1, 2, 3

Start state: (0, 0).

Goal state: (2, n) for any n.

Here need to start from the current state and end up in a goal state.

8
Production Rules for Water Jug Problem in Artificial Intelligence

1 (x, y) is X<4 ->(4, Y) Fill the 4-liter jug

2 (x, y) if Y<3 -> (x, 3) Fill the 3-liter jug

3 (x, y) if x>0 -> (x-d, y) Pour some water out of the 4-liter jug.

4 (x, y) if Y>0 -> (x, y-d) Pour some water out of the 3-liter jug.

5 (x, y) if x>0 -> (0, y) Empty the 4-liter jug on the ground

6 (x, y) if y>0 -> (x,0) Empty the 3-liter jug on the ground

(x, y) if X+Y >= 4 and Pour water from the 3-liter jug into the 4-liter jug until the 4-liter
7 y>0 -> (4, y-(4-x)) jug is full

(x, y) if X+Y>=3 and Pour water from the 4-liter jug into the 3-liter jug until the 3-liter
8 x>0 -> (x-(3-y), 3)) jug is full.

(x, y) if X+Y <=4 and y>0 ->


9 (x+y, 0) Pour all the water from the 3-liter jug into the 4-liter jug.

(x, y) if X+Y<=3 and x>0 -> (0,


10 x+y) Pour all the water from the 4-liter jug into the 3-liter jug.

11 (0, 2) -> (2, 0) Pour the 2-liter water from the 3-liter jug into the 4-liter jug.

12 (2, Y) -> (0, y) Empty the 2-liter in the 4-liter jug on the ground.

The solution to Water Jug Problem in Artificial Intelligence

1. Current state = (0, 0)


2. Loop until the goal state (2, 0) reached
– Apply a rule whose left side matches the current state
– Set the new current state to be the resulting state
(0, 0) – Start State
(0, 3) – Rule 2, Fill the 3-liter jug
(3, 0) – Rule 9, Pour all the water from the 3-liter jug into the 4-liter jug.
(3, 3) – Rule 2, Fill the 3-liter jug
(4, 2) – Rule 7, Pour water from the 3-liter jug into the 4-liter jug until the 4-liter jug is full.
(0, 2) – Rule 5, Empty the 4-liter jug on the ground
(2, 0) – Rule 9, Pour all the water from the 3-liter jug into the 4-liter jug.
Goal State reached

9
Another solution to Water Jug Problem in Artificial Intelligence
(0, 0) – Start State
(4, 0) – Rule 1, Fill the 4-liter jug
(1, 3) – Rule 8, Pour water from the 4-liter jug into the 3-liter jug until the 3-liter jug is full.
(1, 0) – Rule 6, Empty the 3-liter jug on the ground
(0, 1) – Rule 10, Pour all the water from the 4-liter jug into the 3-liter jug.
(4, 1) – Rule 1, Fill the 4-liter jug
(2, 3) – Rule 8, Pour water from the 4-liter jug into the 3-liter jug until the 3-liter jug is full.
Goal State reached

The problem solved by using the production rules in combination with an appropriate control
strategy, moving through the problem space until a path from an initial state to a goal state is
found. In this problem solving process, search is the fundamental concept. For simple problems
it is easier to achieve this goal by hand but there will be cases where this is far too difficult.

PRODUCTION SYSTEMS

Production systems provide appropriate structures for performing and describing search
processes. A production system has four basic components as enumerated below.

 A set of rules each consisting of a left side that determines the applicability of the
rule and a right side that describes the operation to be performed if the rule is
applied.
 A database of current facts established during the process of inference.
 A control strategy that specifies the order in which the rules will be compared
with facts in the database and also specifies how to resolve conflicts in selection
of several rules or selection of more facts.
 A rule firing module.

The production rules operate on the knowledge database. Each rule has a precondition—that is,
either satisfied or not by the knowledge database. If the precondition is satisfied, the rule can be
applied. Application of the rule changes the knowledge database. The control system chooses
which applicable rule should be applied and ceases computation when a termination condition on
the knowledge database is satisfied.

Control Strategies

The word ‘search’ refers to the search for a solution in a problem space.
• Search proceeds with different types of ‘search control strategies’.
• A strategy is defined by picking the order in which the nodes expand.
The Search strategies are evaluated along the following dimensions: Completeness, Time
complexity, Space complexity, Optimality (the search- related terms are first explained, and then
the search algorithms and control strategies are illustrated next).
The control system checks the applicability of a rule. It helps decide which rule should be
applied and terminates the process when the system gives the correct output. It also resolves the
conflict of multiple conditions arriving at the same time. The strategy of the control system
specifies the sequence of rules that compares the condition from the global database to reach the
10
correct result.

Search-related terms
• Algorithm’s performance and complexity
Ideally we want a common measure so that we can compare approaches in order to select
the most appropriate algorithm for a given situation.
 Performance of an algorithm depends on internal and external factors.

Internal factors/ External factors


 Time required to run
 Size of input to the algorithm
 Space (memory) required to run
 Speed of the computer
 Quality of the compiler
 Complexity is a measure of the performance of an algorithm. Complexity
measures the internal factors, usually in time than space.
• Computational complexity
It is the measure of resources in terms of Time and Space.

 If A is an algorithm that solves a decision problem f, then run-time of A is the number of


steps taken on the input of length n.
 Time Complexity T(n) of a decision problem f is the run-time of the ‘best’ algorithm A
for f.
 Space Complexity S(n) of a decision problem f is the amount of memory used by the
‘best’ algorithm A for f.

CHARACTERISTICS OF PRODUCTION SYSTEMS


Production systems provide us with good ways of describing the operations that can be
performed in a search for a solution to a problem.
At this time, two questions may arise:
1. Can production systems be described by a set of characteristics? And how can they be
easily implemented?
2. What relationships are there between the problem types and the types of production
systems well suited for solving the problems?
To answer these questions, first consider the following definitions of classes of production
systems:
1. A monotonic production system is a production system in which the use of one
rule never prevents the involvement of another rule when both the rules are
selected at the same time. Hence, it enables the system to apply rules
simultaneously.
2. A non-monotonic production system : This production system increases the
problem-solving efficiency of the machine by not keeping a record of the changes
made in the previous search process. These types of production systems are useful
from an implementation point of view as they do not backtrack to the previous
state when it is found that an incorrect path was followed.
11
3. A partially communicative production system: In this production system if a set
of rules is used to change state A to state B then any allowable combination of
these rules will also produce the same results (convert state A to state B).
4. A commutative production system: These type of production systems is used
when the order of operation is not important, and the changes are reversible.

Is there any relationship between classes of production systems and classes of problems?
For any solvable problems, there exist an infinite number of production systems that show how
to find solutions. Any problem that can be solved by any production system can be solved by a
commutative one, but the commutative one is practically useless. It may use individual states to
represent entire sequences of applications of rules of a simpler, non-commutative system. In the
formal sense, there is no relationship between kinds of problems and kinds of production systems
Since all problems can be solved by all kinds of systems. But in the practical sense, there is
definitely such a relationship between the kinds of problems and the kinds of systems that lend
themselves to describing those problems.

Partially commutative, monotonic productions systems are useful for solving ignorable
problems. These are important from an implementation point of view without the ability to
backtrack to previous states when it is discovered that an incorrect path has been followed. Both
types of partially commutative production systems are significant from an implementation point;
they tend to lead to many duplications of individual states during the search process. Production
systems that are not partially commutative are useful for many problems in which permanent
changes occur.

Search Algorithms

Many traditional search algorithms are used in AI applications. For complex problems, the
traditional algorithms are unable to find the solutions within some practical time and space
limits. Consequently, many special techniques are developed, using heuristic functions.
The algorithms that use heuristic functions are called heuristic algorithms.

Heuristics — A heuristic is a way of trying to discover something or an idea embedded in a


program. The term is used variously in AI. Heuristic functions are used in some approaches to
search or to measure how far a node in a search tree seems to be from a goal. Heuristic
predicates that compare two nodes in a search tree to see if one is better than the other, i.e.
constitutes an advance toward the goal, and may be more useful.

• Heuristic algorithms are not really intelligent; they appear to be intelligent because they
12
achieve better performance.
• Heuristic algorithms are more efficient because they take advantage of feedback from the data
to direct the search path.

• Uninformed search algorithms or Brute-force algorithms, search through the search


space all possible candidates for the solution checking whether each candidate satisfies
the problem’s statement. Also called blind, exhaustive or brute-force search, it uses no
information about the problem to guide the search and therefore may not be very efficient.

• Informed search algorithms use heuristic functions that are specific to the problem, apply
them to guide the search through the search space to try to reduce the amount of time spent in
searching. Also called heuristic or intelligent search, this uses information about the problem
to guide the search—usually guesses the distance to a goal state and is therefore efficient, but
the search may not be always possible.
A good heuristic will make an informed search dramatically outperform any uninformed search:
for example, the Traveling Salesman Problem (TSP), where the goal is to find is a good solution
instead of finding the best solution.


Uninformed search algorithms

1. Breadth-First Search algorithm


Breadth-First Search algorithm is a graph traversing technique, where we can select a random initial node (source
or root node) and start traversing the graph layer-wise in such a way that all the nodes and their respective children
nodes are visited and explored.

13
1. Visiting a node: Just like the name suggests, visiting a node means to visit or select a node.

2. Exploring a node: Exploring the adjacent nodes (child nodes) of a selected node.

BFS is based on Queue data structure.


A queue is an abstract data structure that follows the First-In-First-Out methodology (data inserted first will
be accessed first). It is open on both ends, where one end is always used to insert data (enqueue) and the
other is used to remove data (dequeue).

enqueue operation: to insert data


dequeue operation: to remove data

Algorithm - Breadth-First Search

Take a look at the below graph, we will use the Breadth-First Search algorithm to traverse through the graph. Take
a look at the below graph, we will use the Breadth-First Search algorithm to traverse through the graph.

14
The following are the steps that explains the end-to-end process of Breadth-First Search

1. Assign ‘a’ as the root node and insert it into the Queue.
2. Extract node ‘a’ from the queue and insert the child nodes of ‘a’, i.e., ‘b’ and ‘c’.
3. Print node ‘a’.
4. The queue is not empty and has node ‘b’ and ‘c’. Since ‘b’ is the first node in the queue, let’s extract it and
insert the child nodes of ‘b’, i.e., node ‘d’ and ‘e’.
5. Repeat these steps until the queue gets empty. Note that the nodes that are already visited should not be
added to the queue again.
15
Example : 8 puzzle problem

 The breadth first search uses a queue to hold all expanded nodes
Advantages

Disadvantages
BFS cannot be effectively used unless the search space is quiet small

2.Depth-First Search algorithm


Depth First search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm
starts at the root (top) node of a tree and goes as far as it can down a given branch (path), then backtracks until it
finds an unexplored path, and then explores it. The algorithm does this until the entire graph has been explored.
Algorithm - Depth-First Search

Take a look at the below graph, we will use the Breadth-First Search algorithm to traverse through the graph
16
17
 The DFS algorithm is a recursive algorithm that uses the idea of backtracking. It involves exhaustive
searches of all the nodes by going ahead, if possible, else by backtracking.

 Here, the word backtrack means that when you are moving forward and there are no more nodes along the
current path, you move backwards on the same path to find nodes to traverse. All the nodes will be visited
on the current path till all the unvisited nodes have been traversed after which the next path will be
selected.

 This recursive nature of DFS can be implemented using stacks. The basic idea is as follows:

Pick a starting node and push all its adjacent nodes into a stack.
Pop a node from stack to select the next node to visit and push all its adjacent nodes into a stack.
Repeat this process until the stack is empty. However, ensure that the nodes that are visited are marked.
This will prevent you from visiting the same node more than once. If you do not mark the nodes that are
visited and you visit the same node more than once, you may end up in an infinite loop.

 Some Applications of DFS include: Topological sorting, Finding connected components, Finding
articulation points (cut vertices) of the graph, Solving puzzles such as maze and Finding strongly
connected components.
Advantages - DFS

Disadvantages - DFS
May find a sub-optimal solution (one that is deeper or more costly than the best solution)
Incomplete - without a depth bound, that is one may not find a best solution even if it exists

Difference Between DFS And BFS


BASIS OF COMPARISON DFS BFS
It starts the traversal from the BFS starts traversal from the
root node and explores the root node and then explore
search as far as possible from the search in the level by
the root node i.e depth wise. level manner i.e as close as
possible from the root node.
Stages Of Algorithm Algorithm works in two Algorithm works in a single
Working stages. In the first stage, the stage. The visited vertices are

18
visited vertices are pushed removed from the queue and
onto the stack and later on then displayed at once.
when there is no vertex
further to visit those that are
popped-off.
Search Its search can be done with Its search can be done with
the help of stack i.e LIFO the help of queue i.e FIFO
implementations. implementation.
Usefulness It is not useful in finding It is useful in finding shortest
shortest path path.
Speed It is comparatively faster It is comparatively faster
when compared to BFS. when compared to BFS.
Application Topological sorting Finding connected
Finding connected components in a graph
components Finding all nodes within one
Finding articulation points connected component.
(cut vertices) of the graph. Finding the shortest path
Solving puzzles such as between two nodes
maze.
Finding strongly connected
components.

3.Iterative deepening depth-first Search Algorithm (IDDFS)


The iterative deepening algorithm is a combination of DFS and BFS algorithms. This search algorithm finds out
the best depth limit and does it by gradually increasing the limit until a goal is found.
Iterative Deepening Search (IDS) is an iterative graph searching strategy that takes advantage of the completeness
of the Breadth-First Search (BFS) strategy but uses much less memory in each iteration (similar to Depth-First
Search).
It gradually increases limits from 0,1,...d, until the goal node is found.
It terminates in the following two cases:
1. When the goal node is found
2. The goal node does not exist in the graph/tree.

19
The trees are divided into many levels, and during the iteration only one particular level is considered. The search
is performed level wise.
Iteration 0: A
Iteration : A -->B-->C
Iteration : A -->B-->D-->E-->C-->F-->G
Example: The below figure shows how iterative depending works on a simple tree. We note from the figure that
the number of nodes expanded by iterative depending search is not much more than that would be using breadth
first search.

20
Advantages
 If the solution exists in the tree, IDDFS provides the hope of finding it.
 When solutions are found at lesser depths, say n, the approach shows to be efficient and time-efficient.
 IDDFS has a significant benefit in in-game tree searching, where the search operation strives to enhance
the depth definition in order to increase the search algorithm's efficiency.
 Despite the fact that there is more work to be done here, IDDFS outperforms single BFS and DFS
operations.
Disadvantages
 The time it takes to reach the goal node is exponential.
 The main issue with IDDFS is the amount of time and calculations wasted at each depth.
 When the branching factor is determined to be large, the situation is not as awful as we may imagine.

21
4.Bidirectional Search Algorithm:

Bidirectional search algorithm runs two simultaneous searches, one form initial state called as forward-search and
other from goal node called as backward-search, to find the goal node. Bidirectional search replaces one single
search graph with two small subgraphs in which one starts the search from an initial vertex and other starts from
goal vertex. The search stops when these two graphs intersect each other.

Bidirectional search can use search techniques such as BFS, DFS, DLS, etc.

Advantages:

o Bidirectional search is fast.


o Bidirectional search requires less memory

Disadvantages:

o Implementation of the bidirectional search tree is difficult.


o In bidirectional search, one should know the goal state in advance .

Example:

In the below search tree, bidirectional search algorithm is applied. This algorithm divides one graph/tree into two
sub-graphs. It starts traversing from node 1 in the forward direction and starts from goal node 16 in the backward
direction. The algorithm terminates at node 9 where two searches meet.

Completeness: Bidirectional Search is complete if we use BFS in both searches.

Optimal: Bidirectional search is Optimal.

22
Informed Search(Heuristics)
A heuristic is a method that improves the efficiency of the search process. These are like tour
guides. There are good to the level that they may neglect the points in general interesting
directions; they are bad to the level that they may neglect points of interest to particular
individuals. Some heuristics help in the search process without sacrificing any claims to entirety
that the process might previously had. Others may occasionally cause an excellent path to be
overlooked. By sacrificing entirety it increases efficiency. Heuristics may not find the best
solution every time but guarantee that they find a good solution in a reasonable time. These are
particularly useful in solving tough and complex problems, solutions of which would require
infinite time, i.e. far longer than a lifetime for the problems which are not solved in any other
way.
Heuristic search
To find a solution in proper time rather than a complete solution in unlimited time we use
heuristics. ‘A heuristic function is a function that maps from problem state descriptions to
measures of desirability, usually represented as numbers’. Heuristic search methods use
knowledge about the problem domain and choose promising operators first. These heuristic
search methods use heuristic functions to evaluate the next state towards the goal state. For
finding a solution, by using the heuristic technique, one should carry out the following steps:
1. Add domain—specific information to select what is the best path to continue searching along.
2. Define a heuristic function h(n) that estimates the ‘goodness’ of a node n.
Specifically, h(n) = estimated cost(or distance) of minimal cost path from n to a goal state.
3. The term, heuristic means ‘serving to aid discovery’ and is an estimate, based on domain
specific information that is computable from the current state description of how close we are to
a goal.
Finding a route from one city to another city is an example of a search problem in which
different search orders and the use of heuristic knowledge are easily understood.
1. State: The current city in which the traveller is located.
2. Operators: Roads linking the current city to other cities.
3. Cost Metric: The cost of taking a given road between cities.
4. Heuristic information: The search could be guided by the direction of the goal city from the
current city, or we could use airline distance as an estimate of the distance to the goal.
Heuristic search techniques
For complex problems, the traditional algorithms, presented above, are unable to find the
solution within some practical time and space limits. Consequently, many special techniques are
developed, using heuristic functions.
• Blind search is not always possible, because it requires too much time or Space (memory).

Heuristics are rules of thumb; they do not guarantee a solution to a problem.


• Heuristic Search is a weak technique but can be effective if applied correctly; it requires
domain specific information.

Characteristics of heuristic search


• Heuristics are knowledge about domain, which help search and reasoning in its domain.
• Heuristic search incorporates domain knowledge to improve efficiency over blind search.
• Heuristic is a function that, when applied to a state, returns value as estimated merit of state,
with respect to goal.
 Heuristics might (for reasons) underestimate or overestimate the merit of a state with

23
respect to goal.
 Heuristics that underestimate are desirable and called admissible.
• Heuristic evaluation function estimates likelihood of given state leading to goal state.
• Heuristic search function estimates cost from current state to goal, presuming function is
efficient.

1.Hill Climbing
Hill Climbing is heuristic search used for mathematical optimization problems in the field of
Artificial Intelligence .
Given a large set of inputs and a good heuristic function, it tries to find a sufficiently good
solution to the problem. This solution may not be the global optimal maximum.
 In the above definition, mathematical optimization problems implies that hill climbing
solves the problems where we need to maximize or minimize a given real function by
choosing values from the given inputs. Example-Travelling salesman problem where we
need to minimize the distance traveled by salesman.
 ‘Heuristic search’ means that this search algorithm may not find the optimal solution to
the problem. However, it will give a good solution in reasonable time.
 A heuristic function is a function that will rank all the possible alternatives at any
branching step in search algorithm based on the available information. It helps the
algorithm to select the best route out of possible routes.

Features of Hill Climbing


1. Variant of generate and test algorithm : It is a variant of generate and test algorithm.
The generate and test algorithm is as follows :
Generate a possible solutions.
Test to see if this is the expected solution.
If the solution has been found quit else go to step 1.

24
Hence we call Hill climbing as a variant of generate and test algorithm as it takes the feedback
from test procedure. Then this feedback is utilized by the generator in deciding the next move in
search space.
2. Uses the Greedy approach : At any point in state space, the search moves in that
direction only which optimizes the cost of function with the hope of finding the optimal
solution at the end.

Heuristic search compared with other search


The Heuristic search is compared with Brute force or Blind search techniques below:

Comparison of Algorithms

Brute force / Blind search Heuristic search


Can only search what it has knowledge Estimates ‘distance’ to goal state
about already through explored nodes

No knowledge about how far a node Guides search process toward goal
node from goal state
Prefers states (nodes) that lead
close to and not away from goal
state

Example: Travelling salesman


A salesman has to visit a list of cities and he must visit each city only once. There are different
routes between the cities. The problem is to find the shortest route between the cities so that the
salesman visits all the cities at once.

Suppose there are N cities, then a solution would be to take N! possible combinations to find the
shortest distance to decide the required route. This is not efficient as with N=10 there are
36,28,800 possible routes. This is an example of combinatorial explosion. There are better
methods for the solution of such problems using heuristics.

Types of Hill Climbing


1.Simple Hill climbing
2.Steepest-Ascent Hill climbing
25
3. Stochastic hill climbing

1.Simplest Hill-Climbing Search Algorithm

In hill climbing the basic idea is to always head towards a state which is better than the current one.
So, if you are in town A and you can get to town B and town C (and your target is town D) then you should make
a move IF town B or C appear nearer to town D than town A does.

Search Algorithm

1. Evaluate the initial state.

If it is also goal state then return it, otherwise continue with the initial state as the current state.

2. Loop until the solution is found or until there are no new operators 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 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 as current state

iii) If it is not better than the current state, then continue in loop.

26
Disadvantages

To understand the concept easily, we will take up a very simple example,

The key point while solving any hill-climbing problem is to choose an appropriate heuristic function. Let’s define
such function h: h(x) = +1 for all the blocks in the support structure if the block is correctly positioned otherwise -
1 for all the blocks in the support structure.

Let’s define such function h:


h(x) = +1 for all the blocks in the support structure if the block is correctly positioned
otherwise -1 for all the blocks in the support structure.

Since h(2)=-3 which is going towards the goal state and is better than the current state, this next
state is taken as the current state and is proceeded further. other possible neighboring states
are not considered.

2.Steepest-Ascent Hill Climbing Search Algorithm


A variation on simple hill climbing. Instead of moving to the first state that is better, move to the best possible
state that is one move away. The order of operators does not matter. Not just climbing to a better state, climbing up
the steepest slope. Considers all the moves from the current state. Selects the best one as the next state. It is called
as Steepest-Ascent Hill Climbing.

27
Basic hill-climbing first applies one operator n gets a new state. If it is better that becomes the current state
whereas the steepest climbing tests all possible solutions n chooses the best.
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 state 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.

successor

28
Hill climbing search algorithm-Drawbacks
Local Maximum is a state that is
better than all its neighbours but it is
not better than some other states
farther away. At a local maximum
all moves appear to make things
worse. They almost occur within the
sight of solution and are called as
foothills

A plateau is a flat area of the search


space in which a whole set of
neighbouring states have the same
value. on a plateau it is not possible
to determine te best direction where
to move by making local
comparisons

A ridge is a special kind of local


maximum. there are steep slopes and
the search direction is not towards
the top but towards the side

State Space diagram for Hill Climbing


State space diagram is a graphical representation of the set of states our search algorithm can
reach vs the value of our objective function(the function which we wish to maximize).
X- axis : denotes the state space ie states or configuration our algorithm may reach.
Y-axis : denotes the values of objective function corresponding to to a particular state.
The best solution will be that state space where objective function has maximum value(global
maximum).

29
Different regions in the State Space Diagram
1. Local maximum : It is a state which is better than its neighboring state however there
exists a state which is better than it(global maximum). This state is better because here
value of objective function is higher than its neighbors.
2. Global maximum : It is the best possible state in the state space diagram. This because
at this state, objective function has highest value.
3. Plateua/flat local maximum : It is a flat region of state space where neighboring
states have the same value.
4. Ridge : It is region which is higher than its neighbours but itself has a slope. It is a
special kind of local maximum.
5. Current state : The region of state space diagram where we are currently present
during the search.
6. Shoulder : It is a plateau that has an uphill
edge. Problems in different regions in Hill climbing
Hill climbing cannot reach the optimal/best state(global maximum) if it enters any of the
following regions :
1. Local maximum : At a local maximum all neighboring states have a values which is
worse than than the current state. Since hill climbing uses greedy approach, it will not
move to the worse state and terminate itself. The process will end even though a better
solution may exist.
To overcome local maximum problem : Utilize backtracking technique. Maintain a list of
visited states. If the search reaches an undesirable state, it can backtrack to the previous
configuration and explore a new path.
2. Plateau : On plateau all neighbors have same value . Hence, it is not possible to select the
best direction.
To overcome plateaus : Make a big jump. Randomly select a state far away from current state.
Chances are that we will land at a non-plateau region
3. Ridge : Any point on a ridge can look like peak because movement in all possible
directions is downward. Hence the algorithm stops when it reaches this state.
To overcome Ridge : In this kind of obstacle, use two or more rules before testing. It
implies moving in several directions at once.

Hill climbing search using local and global heuristic function

To understand the concept easily, we will take up a very simple example,


To understand the concept easily, we will take up a very simple example,

30
The key point while solving any hill-climbing problem is to choose an appropriate heuristic function. Let’s
define such function h: h(x) = +1 for all the blocks in the support structure if the block is correctly positioned
otherwise -1 for all the blocks in the support structure.
Hill climbing using local information

Hill climbing using global information


Global Heuristic:
Let’s define such function h:
h(x) = +1 for all the blocks in the support structure if the block is correctly positioned
otherwise -1 for all the blocks in the support structure.

31
3. Stochastic hill climbing
Simulated Annealing (SA) is an effective and general form of optimization. It is useful in finding
global optima in the presence of large numbers of local optima. “Annealing” refers to an analogy with
thermodynamics, specifically with the way that metals cool and anneal. Simulated annealing uses the
objective function of an optimization problem instead of the energy of a material.

Implementation of SA is surprisingly simple. The algorithm is basically hill-climbing except instead


of picking the best move, it picks a random move. If the selected move improves the solution, then it
is always accepted. Otherwise, the algorithm makes the move anyway with some probability less than
1. The probability decreases exponentially with the “badness” of the move, which is the amount
deltaE by which the solution is worsened (i.e., energy is increased.)

p=∆E/T

where, p-probability function


∆E-change in the value of the function
T-parameter T is also used to determine this probability
T is analogous to temperature in an annealing system. At higher values of T, uphill moves are more
likely to occur. As T tends to zero, they become more and more unlikely, until the algorithm behaves
more or less like hill-climbing. In a typical SA optimization, T starts high and is gradually decreased
according to an “annealing schedule”. The parameter k is some constant that relates temperature to
energy (in nature it is Boltzmann’s constant.)

32
A Star ( A*) Search Algorithm

It is a searching algorithm that is used to find the shortest path between an initial and a final point.It is used for
minimizing the total estimated solution cost.It is the most widely known form of best-first search.

It is a handy algorithm that is often used for map traversal to find the shortest path to be taken. A* was initially
designed as a graph traversal problem, to help build a robot that can find its own course. It still remains a widely
popular algorithm for graph traversal.

It searches for shorter paths first, thus making it an optimal and complete algorithm. An optimal algorithm will
find the least cost outcome for a problem, while a complete algorithm finds all the possible outcomes of a problem.
Heuristics are basically educated guesses. It is crucial to understand that we do not know the distance to the finish
point until we find the route since there are so many things that might get in the way

Another aspect that makes A* so powerful is the use of weighted graphs in its implementation. A weighted graph
uses numbers to represent the cost of taking each path or course of action. This means that the algorithms can take
the path with the least cost, and find the best route in terms of distance and time.

33
A* requires the heuristic function to evaluate the cost of the path that passes through the particular state. This
algorithm is complete if the branching factor is finite and every action has a fixed cost. A* requires the heuristic
function to evaluate the cost of the path that passes through the particular state. It can be defined by the following
formula.

f(n) = g(n) + h(n)

Where
 g(n): The actual cost path from the start state to the current state(n state).
 h(n): The estimated cost path from the current state(n state) to the goal state.
 f(n): The actual cost path from the start state to the goal state.
For the implementation of the A* algorithm, we will use two arrays namely OPEN and CLOSE.
For the implementation of the A* algorithm, we will use two arrays namely OPEN and CLOSE.

OPEN: An array that contains the nodes that have been generated but have not been yet examined.

CLOSE: An array that contains the nodes that have been examined

Algorithm

Step 1: Place the starting node into OPEN and find its f (n) value.

Step 2: Remove the node from OPEN, having the smallest f (n) value. If it is a goal node then stop and return
success.

Step 3: Else remove the node from OPEN, find all its successors.

Step 4: Find the f (n) value of all successors; place them into OPEN and place the removed node into CLOSE.

Step 5: Go to Step-2.

Step 6: Exit.
34
Advantages of A* Star

 It is complete and optimal.


 It is the best one from other techniques.
 It is used to solve very complex problems.
 It is optimally efficient, i.e. there is no other optimal algorithm guaranteed to expand fewer nodes than A*.

Disadvantages of A* Star

 This algorithm is complete if the branching factor is finite and every action has a fixed cost.
 The speed execution of the A* search is highly dependent on the accuracy of the heuristic algorithm that is
used to compute h (n).
 It has complexity problems.

Example 2:

35
5

5
4

A major drawback of the algorithm is its space and time complexity. It takes a large amount of space to store all
possible paths and a lot of time to find them

Introduction to Genetic algorithm


Genetic Algorithms (GAs) are adaptive heuristic search algorithms that belong to the larger part of
evolutionary algorithms. Genetic algorithms are based on the ideas of natural selection and genetics.
These are intelligent exploitation of random search provided with historical data to direct the search into
the region of better performance in solution space. They are commonly used to generate high-quality
solutions for optimization problems and search problems.

36
Genetic algorithms use an iterative process to arrive at the best solution. Finding the best solution out
of multiple best solutions (best of best). Compared with Natural selection, it is natural for the fittest to
survive in comparison with others.

Genetic algorithms simulate the process of natural selection which means those species who can
adapt to changes in their environment are able to survive and reproduce and go to next generation. In
simple words, they simulate “survival of the fittest” among individual of consecutive generation for
solving a problem. Each generation consist of a population of individuals and each individual
represents a point in search space and possible solution. Each individual is represented as a string of
character/integer/float/bits. This string is analogous to the Chromosome.

Foundation of Genetic Algorithms


Genetic algorithms are based on an analogy with genetic structure and behavior of chromoso mes of the
population. Following is the foundation of GAs based on this analogy –
1. Individual in population compete for resources and mate
2. Those individuals who are successful (fittest) then mate to create more offspring than others
3. Genes from “fittest” parent propagate throughout the generation that is sometimes parents create
offspring which is better than either parent.
4. Thus each successive generation is more suited for their environment.
Search space
The population of individuals is maintained within search space. Each individual represents a solution in
search space for given problem. Each individual is coded as a finite length vector (analogous to
chromosome) of components. These variable components are analogous to Genes. Thus a chromosome
(individual) is composed of several genes (variable components).

Fitness Score
A Fitness Score is given to each individual who shows the ability of an individual to “compete”.
The individual having optimal fitness score (or near optimal) are sought.
The GAs maintains the population of n individuals (chromosome/solutions) along with their fitness
scores. The individuals having better fitness scores are given more chance to reproduce than others. The
individuals with better fitness scores are selected who mate and produce better offspring by combining
chromosomes of parents. The population size is static so the room has to be created for new arrivals. So,
some individuals die and get replaced by new arrivals eventually creating new generation when all the
mating opportunity of the old population is exhausted. It is hoped that over successive generations better
solutions will arrive while least fit die.
Each new generation has on average more “better genes” than the individual (solution) of
previous generations. Thus each new generation have better “partial solutions” than previous
generations. Once the offspring produced having no significant difference from offspring produced by
previous populations, the population is converged. The algorithm is said to be converged to a set of
solutions for the problem.
37
There are several phases of a genetic algorithm. They include:
Initial population
It is where the whole process begins. Each group has different sets of individuals from the population.
Each can help you solve the problem you want to solve using various traits. Each individual has genes
that form a chromosome when joined to the string. When using the algorithm, the strings get represented
by 0 or 1s or both.
Fitness function
It is a function they use to determine whether one is fit to compete with other individuals. Each
individual gets a fitness score. Most of those selected for reproduction rely on their score. There is some
situation where the object size exceeds the knapsack forcing the representation to become invalid. In
cases where the definition of fitness function becomes hard, we can use methods like simulation to find
the fitness score. We can use phenotypes like computational fluid dynamics and interactive genetic
algorithms.
Selection
We have to select the best individuals who will create the next generation. Each pair of individuals get s
selected depending on different fitness scores. Having a higher fitness score improves the chances of
getting selected.
Crossover
When performing mating, we must choose the point randomly from the genes. It is a critical phase when
using genetic algorithms. We create offspring by exchanging the parent's genes with themselves until
you achieve the cross-over point. After that, we will get new offspring added to the population later.
Mutation
It is when we flip some of the strings. We select the genes at this stage by considering those with low
probability. It mainly avoids premature convergence and maintains the existing diversity within the
population.
Heuristics
We use different approaches to solve all the problems. The techniques may be practical, but there is no
guarantee that they are perfect. They make the calculation processes faster and more robust. There are
some types of approaches that we can use to penalize the crossovers that happen between the candidate
solutions if there is some similarity.
Termination
It is a repetitive process that occurs until we have the termination symptoms or conditions showing up.
This includes:

 The available solution satisfies the minimum criteria.


 It has a fixed number of generations.
 The allocated budget for the project is over.
 Use of manual inspection.
 The ranking solutions have reached a depletion making the iterations produce bad results.
 We detect termination when we get a combination of the abovementioned issues.
38
Operators of Genetic Algorithms
Once the initial generation is created, the algorithm evolves the generation using following operators –
1) Selection Operator: The idea is to give preference to the individuals with good fitness scores and
allow them to pass their genes to successive generations.
2) Crossover Operator: This represents mating between individuals. Two individuals are selected using
selection operator and crossover sites are chosen randomly. Then the genes at these crossover sites are
exchanged thus creating a completely new individual (offspring). For example –

3) Mutation Operator: The key idea is to insert random genes in offspring to maintain the diversity in the
population to avoid premature convergence. For example –

The whole algorithm can be summarized as –


1) Randomly initialize populations p
2) Determine fitness of population
3) Until convergence repeat:
a) Select parents from population
b) Crossover and generate new population
c) Perform mutation on new population
d) Calculate fitness for new population
Example problem and solution using Genetic Algorithms
Given a target string, the goal is to produce target string starting from a random string of the same length.
In the following implementation, following analogies are made –
 Characters A-Z, a-z, 0-9, and other special symbols are considered as genes
 A string generated by these characters is considered as chromosome/solution/Individual
Fitness score is the number of characters which differ from characters in target string at a particular
index. So individual having lower fitness value is given more preference.

The basic Genetic algorithm is :


39
Initialize a random population of individuals
Compute fitness of each individual
WHILE NOT finished DO
BEGIN /* produce new generation */
FOR population_size DO
BEGIN /* reproductive cycle */
Select two individuals from old generation, recombine the
two individuals to give two offspring
Make a mutation for selected individuals by altering a
random bit in a string
Create a new generation (new populations)
END
IF population has converged THEN
finished := TRUE
END

40
Advantages
 They are Robust
 Provide optimization over large space state.
 Unlike traditional AI, they do not break on slight change in input or presence of noise

Application of Genetic Algorithms

A typical application of this technology would be to try to determine the best route for delivering product
from point A to point B, when the conditions may periodically change. These conditions could be related
to weather, road conditions, traffic flow, rush hour, etc. Many times the best route could be the fastest,
shortest, most scenic, most cost effective, or a combination thereof.

Genetic algorithms have many applications, some of them are –


 Recurrent Neural Network
 Mutation testing
 Code breaking
 Filtering and signal processing
 Learning fuzzy rule base etc

41

You might also like