simulated annealing tsp code example
Example: tsp simulated annealing code
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 ((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;
}
temperature *= coolingRate;
iteration++;
}
shortestDistance = distance;
}