# priyankakankal17/simulated-annealing

a year ago

## Simulated Annealing

This module performs simulated annealing optimization to find the optimal state of a system. It is inspired by the metallurgic process of annealing whereby metals must be cooled at a regular schedule in order to settle into their lowest energy state.

Simulated annealing is used to find a close-to-optimal solution among an extremely large (but finite) set of potential solutions. It is particularly useful for combinatorial optimization problems defined by complex objective functions that rely on external data.

The process involves::

1. Randomly move or alter the state
2. Assess the energy of the new state using an objective function
3. Compare the energy to the previous state and decide whether to accept the new solution or reject it based on the current temperature.
4. Repeat until you have converged on an acceptable answer
5. For a move to be accepted, it must meet one of two requirements

The move causes a decrease in state energy (i.e. an improvement in the objective function) The move increases the state energy (i.e. a slightly worse solution) but is within the bounds of the temperature. The temperature exponetially decreases as the algorithm progresses. In this way, we avoid getting trapped by local minima early in the process but start to hone in on a viable solution by the end.

In [ ]:
``#Goal : Given a list of locations, what is the shortest possible route that hits each location and returns to the starting city?``
In [3]:
``````from __future__ import print_function
import math
import random
from simanneal import Annealer``````
In [4]:
``````def distance(a, b):
"""Calculates distance between two latitude-longitude coordinates."""
R = 3963  # radius of Earth (miles)
return math.acos(math.sin(lat1) * math.sin(lat2) +
math.cos(lat1) * math.cos(lat2) * math.cos(lon1 - lon2)) * R

``````
In [5]:
``````class TravellingSalesmanProblem(Annealer):

"""Test annealer with a travelling salesman problem.
"""

# pass extra data (the distance matrix) into the constructor
def __init__(self, state, distance_matrix):
self.distance_matrix = distance_matrix
super(TravellingSalesmanProblem, self).__init__(state)  # important!

def move(self):
"""Swaps two cities in the route."""
# no efficiency gain, just proof of concept
# demonstrates returning the delta energy (optional)
initial_energy = self.energy()

a = random.randint(0, len(self.state) - 1)
b = random.randint(0, len(self.state) - 1)
self.state[a], self.state[b] = self.state[b], self.state[a]

return self.energy() - initial_energy

def energy(self):
"""Calculates the length of the route."""
e = 0
for i in range(len(self.state)):
e += self.distance_matrix[self.state[i-1]][self.state[i]]
return e

``````
In [8]:
``````if __name__ == '__main__':

# latitude and longitude for the twenty largest U.S. cities
cities = {
'New York City': (40.72, 74.00),
'Los Angeles': (34.05, 118.25),
'Chicago': (41.88, 87.63),
'Houston': (29.77, 95.38),
'Phoenix': (33.45, 112.07),
'San Antonio': (29.53, 98.47),
'Dallas': (32.78, 96.80),
'San Diego': (32.78, 117.15),
'San Jose': (37.30, 121.87),
'Detroit': (42.33, 83.05),
'San Francisco': (37.78, 122.42),
'Jacksonville': (30.32, 81.70),
'Indianapolis': (39.78, 86.15),
'Austin': (30.27, 97.77),
'Columbus': (39.98, 82.98),
'Fort Worth': (32.75, 97.33),
'Charlotte': (35.23, 80.85),
'Memphis': (35.12, 89.97),
'Baltimore': (39.28, 76.62)
}

# initial state, a randomly-ordered itinerary
init_state = list(cities.keys())
random.shuffle(init_state)

# create a distance matrix
distance_matrix = {}
for ka, va in cities.items():
distance_matrix[ka] = {}
for kb, vb in cities.items():
if kb == ka:
distance_matrix[ka][kb] = 0.0
else:
distance_matrix[ka][kb] = distance(va, vb)
#print(distance_matrix)

tsp = TravellingSalesmanProblem(init_state, distance_matrix)
tsp.set_schedule(tsp.auto(minutes=0.2))
# since our state is just a list, slice is the fastest way to copy
tsp.copy_strategy = "slice"
state, e = tsp.anneal()

while state[0] != 'New York City':
state = state[1:] + state[:1]  # rotate NYC to start

print()
print("%i mile route:" % e)
print(" ➞  ".join(state))

``````
``` Temperature Energy Accept Improve Elapsed Remaining 4.50000 6883.00 5.40% 0.00% 0:00:02 0:00:00 Temperature Energy Accept Improve Elapsed Remaining 4.50000 6886.95 5.39% 0.24% 0:00:05 0:00:00```
``` 6845 mile route: New York City ➞ Philadelphia ➞ Baltimore ➞ Charlotte ➞ Jacksonville ➞ Houston ➞ Austin ➞ San Antonio ➞ Phoenix ➞ San Diego ➞ Los Angeles ➞ San Jose ➞ San Francisco ➞ Fort Worth ➞ Dallas ➞ Memphis ➞ Indianapolis ➞ Chicago ➞ Detroit ➞ Columbus ```
In [ ]:
``````import jovian
jovian.commit()
``````
```[jovian] Saving notebook.. ```
In [ ]:
`` ``