Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Simple hill climbing and steepest ascent algorithms in AI -> Algorithms #47 #63

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions AI/Algorithms/HillClimbing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def hill_climbing(f, x0):
x = x0 # initial solution

while True:
neighbors = generate_neighbors(x) # generate neighbors of x

# find the neighbor with the highest function value
best_neighbor = max(neighbors, key=f)

if f(best_neighbor) <= f(x): # if the best neighbor is not better than x, stop
return x
x = best_neighbor # otherwise, continue with the best neighbor
15 changes: 15 additions & 0 deletions AI/Algorithms/StepestAscentHillClimbing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def steepest_ascent_hill_climbing(problem):

current = problem.initial_state()

while True:
neighbors = problem.get_all_neighbors(current)
if not neighbors:
break # No neighbors found

next_state = max(neighbors, key=problem.evaluate)
if problem.evaluate(next_state) <= problem.evaluate(current):
break # No better neighbor found

current = next_state
return current
2 changes: 1 addition & 1 deletion AI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| S.No | Algorithm | S.No. | Algorithm | S.No. | Algorithm |
|-------|-----------|-------|-----------|-------|-----------|
| 1 | | 2 | | 3 | |
| 1 | Simple Hill Climbing | 2 | Steepest Ascent Hill Climbing | 3 | |
| 4 | | 5 | | 6 | |

## Available Documentations
Expand Down