'Need to create a Sorting list in ascending order using Hill Climbing (Steepest ascent)
I need to create a Sorting list in ascending order using Hill Climbing (Steepest ascent). But I can't build logic in the steepest ascent. I can initialize a list and calculate_cost. But can't make state generation part.
For State generation I need to build logic like this
(Update)
#Init():
initialize a list -> [2, 1, 5, 0, 8, 4, 10, 0, 20, 10]
#calc_cost(state):
# You will require a nested for loop
for each element of the list:
look forward in the list and see how many elements are smaller
than this element (they are in the wrong order)
Add up the number of disorders and return
#state_generation(current_state, current_state_cost):
# You will need a nested for loop
for each element in the list:
swap with the forward elements of the list with this element one by one and generate one state for each swap.
calculate cost for each generated state using calc_cost() function.
# Do not store the states and the corresponding costs to determine smallest cost state
# Rather use check and replace by last smallest cost state approach in the above nested loop (think about minimum number #determination from array)
take that generated state which has the smallest cost
if smallest cost is smaller than current_state_cost:
return that state along with the new cost
else :
return current_state, None
#goal_test(state):
if calc_cost(state) == 0:
return True
else:
retrun False
state = init()
while(!goal_test(state)):
cost = calc_cost(state)
state, cost = state_generation(state, cost)
# When you are stuck, your cost will be None
if cost is None:
print(state)
FINISH
print(state)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
