Explore Library
Code QuizAdvanced

Hill Climbing Wrong Direction

A hill-climbing search meant to maximize keeps sliding downhill instead.

Codejavascript
// Goal: maximize value(state)
function hillClimb(start, neighbors, value) {
  let current = start;
  while (true) {
    let best = current;
    for (const n of neighbors(current)) {
      if (value(n) < value(best)) best = n;
    }
    if (best === current) return current;
    current = best;
  }
}

This is supposed to climb toward the highest value but gets stuck at bad states. What is the bug?