Barniferous
0
Q:

tsp simulated annealing code

/// <span class="code-SummaryComment"><summary></span>
/// Annealing Process
/// <span class="code-SummaryComment"></summary></span>
public void Anneal()
{
    int iteration = -1;

    double temperature = 10000.0;
    double deltaDistance = 0;
    double coolingRate = 0.9999;
    double absoluteTemperature = 0.00001;

    LoadCities();

    double distance = GetTotalDistance(currentOrder);

    while (temperature > absoluteTemperature)
    {
        nextOrder = GetNextArrangement(currentOrder);

        deltaDistance = GetTotalDistance(nextOrder) - distance;

        //if the new order has a smaller distance
        //or if the new order has a larger distance but 
        //satisfies Boltzman condition then accept the arrangement
        if ((deltaDistance < 0) || (distance > 0 && 
             Math.Exp(-deltaDistance / temperature) > random.NextDouble()))
        {
            for (int i = 0; i < nextOrder.Count; i++)
                currentOrder[i] = nextOrder[i];

            distance = deltaDistance + distance;
        }

        //cool down the temperature
        temperature *= coolingRate;

        iteration++;
    }

    shortestDistance = distance;
}
0

Tags

New to Communities?

Join the community