tsp simulated annealing code code example
Example: tsp simulated annealing code
///
/// Annealing Process
///
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;
}