BITS Pilani
Pilani Campus
ACI – Lab Session 2
BITS Pilani
Pilani Campus
Optimizing attributes for travel cost
Hill ClimbingAlgorithm
Autonomous car driving agent
BITS Pilani
Pilani Campus
The algorithm – Hill climbing
BITS Pilani, Pilani Campus
Hill climbing algorithm is a local search algorithm
which continuously moves in the direction of
increasing 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.
One of the widely discussed examples of Hill
climbing algorithm is Traveling-salesman
Problem in which we need to minimize the
distance traveled by the salesman.
Hill Climbing Algorithm
BITS Pilani, Pilani Campus
Hill climbing algorithm is also called greedy local search
as it only looks to its good immediate neighbor state and
not beyond that.
Its core components include the initial state, successor
function, and objective function.
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.
The limitation of Hill Climbing is that it can get stuck in
local optima, which are suboptimal solutions.
Hill Climbing Algorithm
BITS Pilani, Pilani Campus
HillClimbing(problem){
currentState = problem.startState
goal = false
while(!goal){
neighbour = highest valued successor of currentState
if neighbour.value <= currentState.value
goal = true
else
currentState = neighbour
}
}
Hill climbing - function
BITS Pilani, Pilani Campus
Limitation of hill climbing is local best solution
Hill climbing - function
BITS Pilani
Pilani Campus
The Problem
Optimizing attributes of autonomous vehicle
8
BITS Pilani, Pilani Campus
Performance measure of an automated driver
Desirable qualities include getting to the correct destination;
minimizing fuel consumption and wear and tear;
minimizing the trip time or cost; minimizing violations of traffic
laws and disturbances to other drivers; maximizing safety
and passenger comfort; maximizing profits. Obviously, some
of these goals conflict, so tradeoffs will be required.
Fuel is one of the main factors contributing to travel cost.
Fuel economy is calculated by using the formula
Average Fuel Consumption (Km/L) = Distance travelled / Litres
of fuel consumed
Travel cost depends on other attributes like speed of the
vehicle, time taken for the travel, type of roads, traffic, driving
behavior, weather, weight, air conditioning, etc.
The problem - Attributes for
fuel economy
BITS Pilani, Pilani Campus
Let us take a few of these for our sub-problem.
A few attributes that influence travel cost are:
Time to travel
Speed
Fuel consumed
Distance travelled
Optimizing these objectives is known as Multi-objective
optimization
The attributes/objectives for
optimization in the example
BITS Pilani, Pilani Campus
A problem that takes more than one objective for
optimization
The objectives can be contradicting ones
Meaning, it is desirable to increase the value of one
objective while decreasing the value of others
Solution representation and evaluation:
The objectives are represented as a vector and can be
evaluated using
– The vector as a whole
– Defining a evaluation function using the objectives
Multi-objective optimization
problem
BITS Pilani, Pilani Campus
In the current autonomous car agent problem the cost or
evaluation function is taken as follows:
Travel Cost = Time + Speed + Fuel – Distance
Here our goal is to reduce the cost. (Minimization problem)
Time travelled and fuel consumed must have lower values
for cost reduction.
Speed must be average (not less not more)
Distance travelled must be maximum to reduce the cost
Cost function / Evaluation
function
BITS Pilani, Pilani Campus
Choose the optimal attribute values to get minimum cost
Goal = 0 cost
Heuristic – Hill climbing
Choose attribute vectors (neighbors) at random (Stochastic
hill climbing)
Evaluate them
Retain the best while forgetting the less optimal values
Heuristic
BITS Pilani, Pilani Campus
Performance – Safety, time, legal drive, comfort.
– In the algorithm, reducing the cost of travel by optimizing the
attributes taken
Environment – Roads, other cars, pedestrians, road signs.
– Acceptable range of values for the attributes Time, Speed, Fuel
and Distance travelled represented as vector of combination of
these values
Actuators – Steering, accelerator, brake, signal, horn.
– Initial solution, Candidate solution, Evaluation function.
Sensors – Fuel indicator, Speedometer, Digital clock in the
display
– Candidate solution with the acceptable range of values (from the
sensors), number of iterations.
PEAS
BITS Pilani, Pilani Campus
Step 1: Evaluate the initial state, if it is goal state then
return success and Stop.
Step 2: Loop Until a solution is found or there is no new
operator left to apply.
Step 3: Select and apply an operator to the current state.
Step 4: Check new state
Step 5: Exit.
Hill climbing algorithm - Steps
BITS Pilani, Pilani Campus
Code Walk through
Stochastic hill climbing in
PYTHON
BITS Pilani, Pilani Campus
import numpy as np
import random;
#The PEAS
#Time, Speed, Fuel, Distance
#Init_solution = [10, 100, 25, 10];
#Init_solution = [2,10,5,100]
#Init_solution = [0, 0, 0, 0];
Iterations = 0
max_itr=1000 #Can be increased or decreased
Final_solution = None #Stores best of each iteration
#PEAS and the initial states
BITS Pilani, Pilani Campus
#Contains an objective vector of attributes to be optimized and
the cost associated with the objectives
class Solu_space:
def __init__(self):
self.obj_vector = None
self.cost = None
def setObj_vector(self, val):
self.obj_vector = val
def setCost(self, cost):
self.cost = cost
def getAttr(self):
return {'Time, Speed, Fuel, Distance': self.obj_vector, 'Cost':
self.cost }
#Class : Solution space
BITS Pilani, Pilani Campus
# Function for calculating the cost - The evaluation function
def calc_cost(objVector):
cost = round((objVector[0] + objVector[1] +
objVector[2] - objVector[3]),2)
#print("Cost"+str(cost))
return abs(cost)
#Function for cost evaluation
BITS Pilani, Pilani Campus
def gen_new_solution():
distance_step=0
speed_step=random.randint(10,100)
time_step=round((random.randint(1,100))/60,2)
try:
distance_step=random.randint(1,round(speed_step*time_step,2))
except:
pass
fuel_step=random.randint(1,25)
objVector=[time_step,speed_step,fuel_step,distance_step]
#print("new solution: "+str(objVector))
return objVector
#Function to generate new
solution
BITS Pilani, Pilani Campus
def Hill_Climb(objVector):
soln_space = Solu_space()
soln_space.setObj_vector(objVector)
oldCost = calc_cost(objVector);
soln_space.setCost(oldCost)
newobjVector = gen_new_solution()
newcost= calc_cost(newobjVector)
#Function Hill climb
BITS Pilani, Pilani Campus
if(newcost < oldCost):
soln_space.setObj_vector(newobjVector)
soln_space.setCost(newcost)
print(str(soln_space.getAttr()))
return soln_space.obj_vector
print(str(soln_space.getAttr()))
return soln_space.obj_vector
#Function Hill climb –
compare solution costs
BITS Pilani, Pilani Campus
def stopit():
globals()
cost=calc_cost(Final_solution)
if cost == 0:
return True
if Iterations == max_itr:
return True
else:
return False
#Function – The stopping
criterion
CALLING ALL THE DEFINED
FUNCTIONS
BITS Pilani, Pilani Campus
#Time,speed,fuel,distance
Init_soln=
[random.randint(1,10),
random.randint(1,100),
random.randint(1,25),
random.randint(1,500)]
#Generate initial solution
BITS Pilani, Pilani Campus
Final_solution= Hill_Climb(Init_solution) #Initialize variable Final_solution
print(Final_solution)
while not stopit():
solution=Final_solution
Final_solution=Hill_Climb(solution)
print("Iteration no: "+str(Iterations))
Iterations+=1
print("Solution")
print(str(Final_solution[0])+ " Hrs " +str(Final_solution[1])+ "
Km/Hr " + str(Final_solution[2]) + " Litres " +
str(Final_solution[3]) + " Kms")
print("Cost" + str(calc_cost(Final_solution)))
#Loop the Hill Climb
BITS Pilani, Pilani Campus
{'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 35
{'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 36
{'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 37
{'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 172
{'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 173
{'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 174
{'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 175
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 991
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 992
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 993
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 994
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 995
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 996
{'Time, Speed, Fuel,Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 997
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 998
{'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 999
Solution 1.5 Hrs 66 Km/Hr 2 Litres 70 Kms Cost 0.5
Partial sample output
BITS Pilani, Pilani Campus
Add code to convert stochastic hill climb into Random
restart hill climb
Solve the problem using Simulated annealing
Implement the solution for the given problem using genetic
algorithm
That’s all for Hill Climbing
Food for thought

ACI-LabSession2A-HillClimbing-TravelOptimization.pptx

  • 1.
  • 2.
    BITS Pilani Pilani Campus Optimizingattributes for travel cost Hill ClimbingAlgorithm Autonomous car driving agent
  • 3.
    BITS Pilani Pilani Campus Thealgorithm – Hill climbing
  • 4.
    BITS Pilani, PilaniCampus Hill climbing algorithm is a local search algorithm which continuously moves in the direction of increasing 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. One of the widely discussed examples of Hill climbing algorithm is Traveling-salesman Problem in which we need to minimize the distance traveled by the salesman. Hill Climbing Algorithm
  • 5.
    BITS Pilani, PilaniCampus Hill climbing algorithm is also called greedy local search as it only looks to its good immediate neighbor state and not beyond that. Its core components include the initial state, successor function, and objective function. 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. The limitation of Hill Climbing is that it can get stuck in local optima, which are suboptimal solutions. Hill Climbing Algorithm
  • 6.
    BITS Pilani, PilaniCampus HillClimbing(problem){ currentState = problem.startState goal = false while(!goal){ neighbour = highest valued successor of currentState if neighbour.value <= currentState.value goal = true else currentState = neighbour } } Hill climbing - function
  • 7.
    BITS Pilani, PilaniCampus Limitation of hill climbing is local best solution Hill climbing - function
  • 8.
    BITS Pilani Pilani Campus TheProblem Optimizing attributes of autonomous vehicle 8
  • 9.
    BITS Pilani, PilaniCampus Performance measure of an automated driver Desirable qualities include getting to the correct destination; minimizing fuel consumption and wear and tear; minimizing the trip time or cost; minimizing violations of traffic laws and disturbances to other drivers; maximizing safety and passenger comfort; maximizing profits. Obviously, some of these goals conflict, so tradeoffs will be required. Fuel is one of the main factors contributing to travel cost. Fuel economy is calculated by using the formula Average Fuel Consumption (Km/L) = Distance travelled / Litres of fuel consumed Travel cost depends on other attributes like speed of the vehicle, time taken for the travel, type of roads, traffic, driving behavior, weather, weight, air conditioning, etc. The problem - Attributes for fuel economy
  • 10.
    BITS Pilani, PilaniCampus Let us take a few of these for our sub-problem. A few attributes that influence travel cost are: Time to travel Speed Fuel consumed Distance travelled Optimizing these objectives is known as Multi-objective optimization The attributes/objectives for optimization in the example
  • 11.
    BITS Pilani, PilaniCampus A problem that takes more than one objective for optimization The objectives can be contradicting ones Meaning, it is desirable to increase the value of one objective while decreasing the value of others Solution representation and evaluation: The objectives are represented as a vector and can be evaluated using – The vector as a whole – Defining a evaluation function using the objectives Multi-objective optimization problem
  • 12.
    BITS Pilani, PilaniCampus In the current autonomous car agent problem the cost or evaluation function is taken as follows: Travel Cost = Time + Speed + Fuel – Distance Here our goal is to reduce the cost. (Minimization problem) Time travelled and fuel consumed must have lower values for cost reduction. Speed must be average (not less not more) Distance travelled must be maximum to reduce the cost Cost function / Evaluation function
  • 13.
    BITS Pilani, PilaniCampus Choose the optimal attribute values to get minimum cost Goal = 0 cost Heuristic – Hill climbing Choose attribute vectors (neighbors) at random (Stochastic hill climbing) Evaluate them Retain the best while forgetting the less optimal values Heuristic
  • 14.
    BITS Pilani, PilaniCampus Performance – Safety, time, legal drive, comfort. – In the algorithm, reducing the cost of travel by optimizing the attributes taken Environment – Roads, other cars, pedestrians, road signs. – Acceptable range of values for the attributes Time, Speed, Fuel and Distance travelled represented as vector of combination of these values Actuators – Steering, accelerator, brake, signal, horn. – Initial solution, Candidate solution, Evaluation function. Sensors – Fuel indicator, Speedometer, Digital clock in the display – Candidate solution with the acceptable range of values (from the sensors), number of iterations. PEAS
  • 15.
    BITS Pilani, PilaniCampus Step 1: Evaluate the initial state, if it is goal state then return success and Stop. Step 2: Loop Until a solution is found or there is no new operator left to apply. Step 3: Select and apply an operator to the current state. Step 4: Check new state Step 5: Exit. Hill climbing algorithm - Steps
  • 16.
    BITS Pilani, PilaniCampus Code Walk through Stochastic hill climbing in PYTHON
  • 17.
    BITS Pilani, PilaniCampus import numpy as np import random; #The PEAS #Time, Speed, Fuel, Distance #Init_solution = [10, 100, 25, 10]; #Init_solution = [2,10,5,100] #Init_solution = [0, 0, 0, 0]; Iterations = 0 max_itr=1000 #Can be increased or decreased Final_solution = None #Stores best of each iteration #PEAS and the initial states
  • 18.
    BITS Pilani, PilaniCampus #Contains an objective vector of attributes to be optimized and the cost associated with the objectives class Solu_space: def __init__(self): self.obj_vector = None self.cost = None def setObj_vector(self, val): self.obj_vector = val def setCost(self, cost): self.cost = cost def getAttr(self): return {'Time, Speed, Fuel, Distance': self.obj_vector, 'Cost': self.cost } #Class : Solution space
  • 19.
    BITS Pilani, PilaniCampus # Function for calculating the cost - The evaluation function def calc_cost(objVector): cost = round((objVector[0] + objVector[1] + objVector[2] - objVector[3]),2) #print("Cost"+str(cost)) return abs(cost) #Function for cost evaluation
  • 20.
    BITS Pilani, PilaniCampus def gen_new_solution(): distance_step=0 speed_step=random.randint(10,100) time_step=round((random.randint(1,100))/60,2) try: distance_step=random.randint(1,round(speed_step*time_step,2)) except: pass fuel_step=random.randint(1,25) objVector=[time_step,speed_step,fuel_step,distance_step] #print("new solution: "+str(objVector)) return objVector #Function to generate new solution
  • 21.
    BITS Pilani, PilaniCampus def Hill_Climb(objVector): soln_space = Solu_space() soln_space.setObj_vector(objVector) oldCost = calc_cost(objVector); soln_space.setCost(oldCost) newobjVector = gen_new_solution() newcost= calc_cost(newobjVector) #Function Hill climb
  • 22.
    BITS Pilani, PilaniCampus if(newcost < oldCost): soln_space.setObj_vector(newobjVector) soln_space.setCost(newcost) print(str(soln_space.getAttr())) return soln_space.obj_vector print(str(soln_space.getAttr())) return soln_space.obj_vector #Function Hill climb – compare solution costs
  • 23.
    BITS Pilani, PilaniCampus def stopit(): globals() cost=calc_cost(Final_solution) if cost == 0: return True if Iterations == max_itr: return True else: return False #Function – The stopping criterion
  • 24.
    CALLING ALL THEDEFINED FUNCTIONS
  • 25.
    BITS Pilani, PilaniCampus #Time,speed,fuel,distance Init_soln= [random.randint(1,10), random.randint(1,100), random.randint(1,25), random.randint(1,500)] #Generate initial solution
  • 26.
    BITS Pilani, PilaniCampus Final_solution= Hill_Climb(Init_solution) #Initialize variable Final_solution print(Final_solution) while not stopit(): solution=Final_solution Final_solution=Hill_Climb(solution) print("Iteration no: "+str(Iterations)) Iterations+=1 print("Solution") print(str(Final_solution[0])+ " Hrs " +str(Final_solution[1])+ " Km/Hr " + str(Final_solution[2]) + " Litres " + str(Final_solution[3]) + " Kms") print("Cost" + str(calc_cost(Final_solution))) #Loop the Hill Climb
  • 27.
    BITS Pilani, PilaniCampus {'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 35 {'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 36 {'Time, Speed, Fuel, Distance': [1.3, 70, 12, 55], 'Cost': 28.3} Iteration no: 37 {'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 172 {'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 173 {'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 174 {'Time, Speed, Fuel, Distance': [0.13, 10, 2, 0], 'Cost': 12.13} Iteration no: 175 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 991 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 992 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 993 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 994 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 995 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 996 {'Time, Speed, Fuel,Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 997 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 998 {'Time, Speed, Fuel, Distance': [1.5, 66, 2, 70], 'Cost': 0.5} Iteration no: 999 Solution 1.5 Hrs 66 Km/Hr 2 Litres 70 Kms Cost 0.5 Partial sample output
  • 28.
    BITS Pilani, PilaniCampus Add code to convert stochastic hill climb into Random restart hill climb Solve the problem using Simulated annealing Implement the solution for the given problem using genetic algorithm That’s all for Hill Climbing Food for thought