gkhayes / mlrose

Python package for implementing a number of Machine Learning, Randomized Optimization and SEarch algorithms.
https://mlrose.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
234 stars 243 forks source link

Randomized Hill Climbing improvement #51

Open wtld opened 4 years ago

wtld commented 4 years ago

According to referenced "Clever Algorithms: Nature-Inspired Programming Recipes":

neighbors with better or equal cost should be accepted, allowing the technique to navigate across plateaus in the response surface

I suggest a change in the code like this:

while (attempts < max_attempts) and (iters < max_iters):
    iters += 1
    attempts += 1

    # Find random neighbor and evaluate fitness
    next_state = problem.random_neighbor()
    next_fitness = problem.eval_fitness(next_state)

    improvement = next_fitness - problem.get_fitness()

    # If the neighbor is better or equal move to that state
    if improvement >= 0:
        problem.set_state(next_state)
        # if better than reset attempts counter
        if improvement > 0: 
            attempts = 0

I personally work with problems where this small change has a big impact on results.

walintonc commented 4 years ago

Thanks, @wtld Can you create Pull Request with your proposed changes so that it can be properly reviewed?