1

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?

WaveOnyx
  • 61
  • 2
  • 8
  • It indeed only finds local minimum (we are looking for minimal distance). I would rename `VisitAllCities` by something like `ComputePathLengthAroundAllCities`. code swaps city with (arbitrary) the last city, if new path is shorter than previous, we record that one and reiterate the processus,else we rollback and test the next swap. – Jarod42 Apr 26 '17 at 12:57
  • Wait, so it's sliding downwards? I thought the whole point of this algorithm is to go upwards? – WaveOnyx Apr 26 '17 at 13:04
  • You try to minimize the distance to visit all cities. and minimizing is similar to maximizing (as minimizing `cost` is maximize `-cost`). – Jarod42 Apr 26 '17 at 13:12
  • can you please tell which book, as am fed up of code in Python that not helps a beginner like me. – jiten Mar 22 '19 at 03:44

1 Answers1

2

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?

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • Thank you, I understand now and I feel confident moving forward from this point. :) – WaveOnyx Apr 26 '17 at 13:24
  • One last thing though, how does this exit the outer loop iterations? if (CurrentDistance == temp) { break; } – WaveOnyx Apr 26 '17 at 13:36
  • Sure, that's the idea. A break command will break the innermost possible looping scope, which at this position is the outer loop. By the way, might be a good learning experience for you if you go through a run of the program by using a debugger, seeing which variables change when and how. – Aziuth Apr 26 '17 at 14:44