Search code examples
c++artificial-intelligencehill-climbing

How does the Hill Climbing algorithm work?


I'm learning Artificial Intelligence from a book, the book vaguely explains the code I'm about to post here, I assume because the author assumes everyone has experienced hill climbing algorithm before. The concept is rather straightforward, but I just don't understand some of the code below and I'd like someone to help me understand this algorithm a bit clearer before I move on.

I commented next to the parts that confuses me most, a summary of what these lines are doing would be very helpful to me.

int HillClimb::CalcNodeDist(Node* A, Node* B)
{
    int Horizontal = abs(A->_iX - B->_iX);
    int Vertical = abs(A->_iY - B->_iY);
    return(sqrt(pow(_iHorizontal, 2) + pow(_iVertical, 2)));
}

 void HillClimb::StartHillClimb()
{   
    BestDistance = VisitAllCities();
    int CurrentDistance = BestDistance;

    while (true)
    {
        int i = 0;
        int temp = VisitAllCities();
        while (i < Cities.size())
        {
            //Swapping the nodes
            Node* back = Cities.back();
            Cities[Cities.size() - 1] = Cities[i];
            Cities[i] = back; // Why swap last city with first?
            CurrentDistance = VisitAllCities(); // Why visit all nodes again?

            if (CurrentDistance < BestDistance) // What is this doing?
            {
                BestDistance = CurrentDistance; //???
                break;
            }
            else
            {
                back = Cities.back();
                Cities[Cities.size() - 1] = Cities[i];
                Cities[i] = back;
            }
            i++;
        }

        if (CurrentDistance == temp)
        {
            break;
        }
    }

}

int HillClimb::VisitAllCities()
{
    int CurrentDistance = 0;    
    for (unsigned int i = 0; i < Cities.size(); i++)
    {
        if (i == Cities.size() - 1)//Check if last city, link back to first city
        {
            CurrentDistance += CalcNodeDist(Cities[i], Cities[0]);

        }
        else
        {
            CurrentDistance += CalcNodeDist(Cities[i], Cities[i + 1]);
        }
    }
    return(CurrentDistance);
}

Also the book doesn't state what type of hill climb this is. I assume it's basic hill climb as it doesn't restart when it gets stuck?


Solution

  • Essentially, it does this in pseudo-code:

    initialize an order of nodes (that is, a list) which represents a circle
    
    do{
        find an element in the list so that switching it with the last element of the
        list results in a shorter length of the circle that is imposed by that list
    }(until no such element could be found)
    

    VisitAllCities is a helper that computes the length of that circle, CalcNodeDist is a helper that computes the distance between two nodes the outer while loop is what I called do-until, the inner while loop iterates over all elements.

    The if (CurrentDistance < BestDistance) part simply checks whether changing that list by swapping results in a smaller length, if so, update the distance, if not, undo that change.

    Did I cover everything you wanted to know? Question about a particular part?