Assignment - 05: Shivam Kamlesh Yadav BEIT-B4 77
Assignment - 05: Shivam Kamlesh Yadav BEIT-B4 77
Theory:
Hill climbing: It is one of the local search techniques.Hill climbing algorithms is a local
search algorithm which continuously moves in the direction of increasing elevation/value to
find the peak of the mountain or best solution to the problem. It terminates when it reaches a
peak value where no neighbor has a higher value.
Hill climbing algorithm is a technique which is used for optimizing mathematical problems. One
of the widely discussed examples of Hill climbing algorithms is the Traveling-salesman Problem
in which we need to minimize the distance traveled by the salesman.
It is also called greedy local search as it only looks to its good immediate neighbor state and
not beyond that.
A node of hill climbing algorithm has two components which are state and
value. Hill Climbing is mostly used when a good heuristic is available.
In this algorithm, we don't need to maintain and handle the search tree or graph as it only keeps
a single current state.
Plateau: A plateau is the flat area of the search space in which all the neighboring states of the
current state contains the same value, because of this algorithm does not find any best direction
to move. A hill-climbing search might be lost in the plateau area.
Solution: The solution for the plateau is to take big steps or very little steps while searching,
to solve the problem. Randomly select a state which is far away from the current state so it is
possible that the algorithm could find non-plateau region.
Ridges: A ridge is a special form of the local maximum. It has an area which is higher than
its surrounding areas, but itself has a slope, and cannot be reached in a single move.
Solution: With the use of bidirectional search, or by moving in different directions, we
can improve this problem.
Algorithm:
Step 1: Evaluate the Initial state. If the first state is a goal state then stop and return success.
Else, make the Initial state as Current state.
Step 2: Loop until the Solution state is found or if there are no new operators present which
can be applied to Current state.
Select a state that has not yet been applied to the Current state and then apply it to produce
a New state.
Perform these steps to evaluate New state: i. If the Current state is the Goal state, then Stop and
return success. ii. If the New state is better than the Current state, then make it the Current state
and proceed further. iii. If the New state is not better than the current state, then continue the
loop until a solution is found.
Step 3: Exit.
Program:
import math
increment = 0.1
startingPoint = [1, 1]
point1 = [1,5]
point2 = [6,4]
point3 = [5,2]
point4 = [2,1]
def sumOfDistances(x1, y1, px1, py1, px2, py2, px3, py3, px4, py4):
d1 = distance(x1, y1, px1, py1)
d2 = distance(x1, y1, px2, py2)
d3 = distance(x1, y1, px3, py3)
d4 = distance(x1, y1, px4, py4)
return d1 + d2 + d3 + d4
def newDistance(x1, y1, point1, point2, point3, point4):
d1 = [x1, y1]
d1temp = sumOfDistances(x1, y1, point1[0],point1[1], point2[0],point2[1],
point3[0],point3[1], point4[0],point4[1] )
d1.append(d1temp)
return d1
i=1
while flag:
d1 = newDistance(startingPoint[0]+increment, startingPoint[1], point1, point2, point3, point4)
d2 = newDistance(startingPoint[0]-increment, startingPoint[1], point1, point2, point3, point4)
d3 = newDistance(startingPoint[0], startingPoint[1]+increment, point1, point2, point3, point4)
d4 = newDistance(startingPoint[0], startingPoint[1]-increment, point1, point2, point3, point4)
print (i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2))
minimum = min(d1[2], d2[2], d3[2], d4[2])
if minimum < minDistance:
startingPoint = newPoints(minimum, d1, d2, d3, d4)
minDistance = minimum
#print i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2)
i+=1
else:
flag = False
Output:
Conclusion: Hence we have successfully studied and implemented Hill Climbing
Algorithm.