BASIC SEARCHING
TECHNIQUE- PART 2
(CONSTRUCTIVE HEURISTIC)
Professor of Lecturer: Prof Dr Masri Ayob
Computational
Intelligence Email:
[email protected]Research area: http://www.ftsm.ukm.my/masri/
Scheduling,
Optimization,
Meta-heuristic,
Timetabling
CONSTRUCTIVE HEURISTIC
• work by constructing a solution
step by step, evaluating that
solution for (a) feasibility and
(b)value for objective function.
• Greedy or myopic (narrow-
Constructive minded), i.e. they take the best
thing next without regard for the
rest of the solution.
• Example: take the nearest city
next (for TSP).
2
EXAMPLE: The traveling
salesman problem (TSP)
A salesman needs to visit n cities
(incl. his home city). He wishes to
determine a minimum length (time/cost)
tour that visits each city exactly once
(except that it begins and ends in his
home city).
Example - Traveling Salesman
Problem (TSP)
Traveler needs to visit n cities.
Know the distance between each pair of cities.
Want to know the shortest route that visits all the cities once.
E.g. n=80 will take millions of days to solve exhaustively! ….i.e.
80! 4
Example: A Traveling Salesman Problem
3
1 2
4 3
2 1 4 2
2
5 3
3 5
4
How might we construct initial tour for TSP?
Nearest neighbour
Furthest Insetion
Random
The traveling salesman problem
Nearest neighbor (construction) heuristic
1. Begin with any city, say x.
2. Select among all currently unvisited
cities, the one which is closest to x.
Call this city y, and add the route (x,y)
to the tour.
3. If all cities are visited, STOP.
Otherwise let x = y be the most recently
added city and go to 2.
Illustration of the nearest neighbor
heuristic (start city: 1)
3
1 2
4 3
2 1 4 2
2
5 3
3 5
Total distance = 1+3+2+2+3=11
Illustration of the nearest neighbor heuristic
Different start (start city: 3) - different tour
3
1 2
4 3
2 1 4 2
2
5 3
start
3 5
4
Total distance = 13
Best-First Search
Combines the advantages of Breadth-First and Depth-First searchs.
• DFS: follows a single path, don’t need to generate all competing paths.
• BFS: doesn’t get caught in loops or dead-end-paths.
Best First Search: explore the most promising path seen so far.
While goal not reached:
• 1. Generate all potential successor states and add to a list of states.
• 2. Pick the best state in the list and go to it.
Similar to steepest-ascent, but don’t throw away states that are not
chosen. 10
A* Algorithm
The A* algorithm uses a modified evaluation function and a Best-First
search.
A* minimizes the total path cost.
Under the right conditions A* provides the cheapest cost solution in the
optimal time!
The evaluation function f is an estimate of the value of a node x given by:
• f(x) = g(x) + h’(x)
• g(x) is the cost to get from the start state to state x.
• h’(x) is the estimated cost to get from state x to the goal state (the heuristic).
11
Modified State Evaluation
Value of each state is a combination of:
• the cost of the path to the state
• estimated cost of reaching a goal from the state.
The idea is to use the path to a state to determine (partially)
the rank of the state when compared to other states.
This doesn’t make sense for DFS or BFS, but is useful for
Best-First Search.
12
Why we need modified evaluation
Consider a best-first search that generates
the same state many times.
Which of the paths leading to the state is
the best ?
Recall that often the path to a goal is the
answer.
13
A* Algorithm
Best First Search with the modified evaluation function.
h’(x) is an estimate of the number of steps from state x to a goal
state.
loops are avoided - we don’t expand the same state twice.
Information about the path to the goal state is retained.
14
A* Algorithm
1. Create a priority queue of search nodes (initially the start state).
Priority is determined by the function f )
2. While queue not empty and goal not found:
Get the best state x from the queue.
If x is not goal state:
Generate all possible children of x (and save path
information with each node).
Apply f to each new node and add to queue.
Remove duplicates from queue (using f to pick the best).
15
Example - Maze
START GOAL
16
Example - Maze
START GOAL
17
A* Optimality and Completeness
If the heuristic function h’ is admissible (acceptable) the
algorithm will find the optimal (shortest path) to the solution
in the minimum number of steps possible (no optimal
algorithm can do better given the same heuristic).
An admissible heuristic is one that never overestimates the cost
of getting from a state to the goal state (is pessimistic).
18
ARAD
Example: A*
BUCHAREST
Romania with step costs in km
Open List:
Arad
A* search example
We start with our initial state Arad. We make a node
and add it to the open list. Since it’s the only thing on
the open list, we expand the node.
Think of the open list as a priority queue (or heap)
that sorts the nodes inside of it according to their
g()+h() score.
Open List:
A* search example
Sibiu (393)
Timisoara (447)
Zerind (449)_
ARAD ARAD ARAD
• 140 • 118 • 75
SIBIU TIMISOARA ZERIND
We add the three nodes we found to the open list.
We sort them according to the g()+h() calculation.
Open List:
Rimricu Vicea (413)
A* search example
Fagaras (415)
Timisoara (447)
Zerind (449)
Arad
Oradea (671)
We’ve been
to Arad
before. Do
not list it
again on
the open
list.
When we expand Sibiu, we run into Arad again. But
we’ve already expanded this node once; so, we don’t
add it to the open list again.
A* search example
Open List:
Rimricu Vicea (413)
Fagaras (415)
Timisoara (447)
Zerind (449)
Oradea (671)
ARAD RIMRICU
• 140 VICEA
SIBIU 140+80=220
• 80
We see that Rimricu Vicea is at the top of the open list
(sorted); so, it’s the next node we will expand.
Open List:
Fagaras A* search example
Pitesti
Timisoara
Zerind
Craiova
Sibiu
Oradea
When we expand Rimricu Vicea, we run into Sibiu again.
But we’ve already expanded this node once; so, we don’t
add it to the open list again.
Open List: A* search example
Fagaras (415)
Pitesti (417)
Timisoara (447)
Zerind (449)
Craiova(526)
Oradea (671)
Fagaras will be the next node we should
expand – it’s at the top of the sorted open list.
Open List: A* search example
Pitesti
Timisoara
Zerind
Bucharest
Craiova
Sibiu
Oradea
When we expand Fagaras, we find Sibiu again. We don’t add it to the
open list.
We also find Bucharest, but we’re not done yet.
The algorithm doesn’t end until we “expand” the goal node – it has to
be at the top of the open list.
Open List:
A* search example
Pitesti
Timisoara
Zerind
Bucharest
Craiova
Oradea
It looks like Pitesti is the next node we should
expand.
Open List: A* search example
***Bucharest (418)
Timisoara (607)
Zerind
Craiova
Rimricu Vicea
Oradea
We just found a better value for Bucharest; so, it got moved higher in the list.
We also found a worse value for Craiova – we just ignore this.
We ran into Rimricu Vicea again. Since it’s already been expanded once, we don’t
re-add it to the Open List.
A* search example
Open List:
Bucharest
Timisoara
Zerind
Craiova
Oradea
Now it looks like Bucharest is at GOAL
the top of the open list….. ACHIEVED
Open List:
Bucharest
A* search example
Timisoara
Zerind
Craiova
Oradea
Now we “expand” the node for Bucharest.
We’re done! (And we know the path that
we’ve found is optimal….418)
Colouring Graphs
Coloring maps and graphs
Chromatic number
Applications of graph coloring
Colouring maps
0 Color a map such that two regions with a common
border are assigned different colors.
0 Each map can be represented by a graph:
0 Each region of the map is represented by a vertex;
0 Edges connect two vertices if the regions represented by these
vertices have a common border.
Colouring Graphs
0 Definition: A graph has been colored if a color has been assigned
to each vertex in such a way that adjacent vertices have different
colors.
0 Definition: The chromatic number of a graph is the smallest
number of colors with which it can be colored.
In the example above, the chromatic number is 4.
Graph Problem – An Example
How many colors do we need
for this graph?
Graph Colouring applications
so that if tween
map coloring – this is the direct application.
Countries are vertices, and adjacent countries
(that share a boundary) are connected by an edge.
In real maps, adjacent countries are assigned
different colors.
Graph colouring application:
Timetabling problem
Given is a list of
The goal is to find a If a student is
courses, and for each The resulting class
time slot for each registered for courses
course the list of can be coloured with
course (using as few p and q, add an edge
students who want to k number of colour.
slots as possible). between p and q.
register for it.
An application of graph colouring: EXAMPLE 1
Twelve faculty members in a mathematics department serve on the
following committees:
• Undergraduate education: Sineman, Limitson, Axiomus, Functionini
• Graduate Education: Graphian, Vectorades, Functionini, Infinitescu
• Colloquium: Lemmeau, Randomov, Proofizaki
• Library: Van Sum, Sineman, Lemmeau
• Staffing: Graphian, Randomov, Vectorades, Limitson
• Promotion: Vectorades, Van Sum, Parabolton
The committees must all meet during the first week of classes, but there are only
three time slots available.
Using a graph coloring algorithm, find a schedule that will allow all faculty
members to attend the meetings of all committees on which they serve.
3 time slots An application of graph colouring: EXAMPLE 1
Undergraduate education (UE): Sineman, Limitson, Axiomus, Functionini
Graduate Education (GE): Graphian, Vectorades, Functionini, Infinitescu
Colloquium (C): Lemmeau, Randomov, Proofizaki
Library (L): Van Sum, Sineman, Lemmeau
Staffing (S): Graphian, Randomov, Vectorades, Limitson
Promotion (P): Vectorades, Van Sum, Parabolton
GE
SLOT1 SLOT2 SLOT3
S
• UE • GE •S
UE •P •L
•C
P
C L
Assignment 1: An application of graph colouring
(EXAMPLE 1)
Twelve faculty members in a mathematics department serve on the following
committees:
Undergraduate education: Sineman, Limitson, Axiomus, Functionini
Graduate Education: Graphian, Vectorades, Functionini, Infinitescu
Colloquium: Lemmeau, Randomov, Proofizaki
Library: Van Sum, Sineman, Lemmeau
Staffing: Graphian, Randomov, Vectorades, Limitson
Promotion: Vectorades, Van Sum, Parabolton
The committees must all meet during the first week of classes, but there are
only three time slots available. Using a graph coloring algorithm, find a
schedule that will allow all faculty members to attend the meetings of all
committees on which they serve.
Write a pseudocode for the graph colouring algorithm.
Assignment 2: Graph colouring for exam
timetabling
Suppose that in a particular quarter there are students taking each of the following
combinations of courses:
S1: Math, English, Biology, Chemistry
S2: Math, English, Computer Science, Geography
S3: Biology, Psychology, Geography, Spanish
S4: Biology, Computer Science, History, French
S5: English, Psychology, Computer Science, History
S6: Psychology, Chemistry, Computer Science, French
S7: Psychology, Geography, History, Spanish
Q1: What is the minimum number of examination periods required
for the exams of the ten courses specified so that students
taking any of the given combinations of courses have no
conflicts?
Q2: Find a schedule that uses this minimum number of periods.
http://www.ftsm.ukm.my/cait/DMOLab.html
https://www.facebook.com/dmoukm/
41