回顾模拟退火的优化步骤

时间:2014-10-14 03:36:30

标签: algorithm optimization simulated-annealing

我正在使用模拟退火来帮助解决旅行商问题等问题。我得到了一个解决方案,我很满意,但我现在想知道解决方案空间内的路径(也就是算法达到解决方案所采取的步骤),算法从随机算起,远离最佳行程,非常优秀。

换句话说,就旅行商问题而言:

  • 从城市之间的随机行程开始
  • 步骤1:交换城市A-C和E-F之间的路径
  • 步骤2:交换城市G-U和S-Q之间的路径
  • ...
  • 最终在城市之间建立一个非常优化的行程

有没有办法,使用模拟退火,来保存优化过程的每个步骤,以便我可以逐个回溯每次对系统所做的更改,最终导致该特定解决方案?或者这与模拟退火的工作原理相反?

这是@perrygeo的一个很棒的模拟退火算法,使用旅行推销员的例子https://github.com/perrygeo/simanneal/blob/master/simanneal/anneal.pyhttps://github.com/perrygeo/simanneal/blob/master/examples/salesman.py略微修改了:{{3}} (我的所有更改之后都是以“”“开头的单行注释)

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
import copy
import math
import sys
import time
import random


def round_figures(x, n):
    """Returns x rounded to n significant figures."""
    return round(x, int(n - math.ceil(math.log10(abs(x)))))


def time_string(seconds):
    """Returns time in seconds as a string formatted HHHH:MM:SS."""
    s = int(round(seconds))  # round to nearest second
    h, s = divmod(s, 3600)   # get hours and remainder
    m, s = divmod(s, 60)     # split remainder into minutes and seconds
    return '%4i:%02i:%02i' % (h, m, s)


class Annealer(object):

    """Performs simulated annealing by calling functions to calculate
    energy and make moves on a state.  The temperature schedule for
    annealing may be provided manually or estimated automatically.
    """

    Tmax = 25000.0
    Tmin = 2.5
    steps = 50000
    updates = 100
    copy_strategy = 'deepcopy'

    def __init__(self, initial_state):
        self.initial_state = initial_state
        self.state = self.copy_state(initial_state)

    def set_schedule(self, schedule):
        """Takes the output from `auto` and sets the attributes
        """
        self.Tmax = schedule['tmax']
        self.Tmin = schedule['tmin']
        self.steps = int(schedule['steps'])

    def copy_state(self, state):
        """Returns an exact copy of the provided state
        Implemented according to self.copy_strategy, one of

        * deepcopy : use copy.deepcopy (slow but reliable)
        * slice: use list slices (faster but only works if state is list-like)
        * method: use the state's copy() method
        """
        if self.copy_strategy == 'deepcopy':
            return copy.deepcopy(state)
        elif self.copy_strategy == 'slice':
            return state[:]
        elif self.copy_strategy == 'method':
            return state.copy()


    def update(self, step, T, E, acceptance, improvement):
        """Prints the current temperature, energy, acceptance rate,
        improvement rate, elapsed time, and remaining time.

        The acceptance rate indicates the percentage of moves since the last
        update that were accepted by the Metropolis algorithm.  It includes
        moves that decreased the energy, moves that left the energy
        unchanged, and moves that increased the energy yet were reached by
        thermal excitation.

        The improvement rate indicates the percentage of moves since the
        last update that strictly decreased the energy.  At high
        temperatures it will include both moves that improved the overall
        state and moves that simply undid previously accepted moves that
        increased the energy by thermal excititation.  At low temperatures
        it will tend toward zero as the moves that can decrease the energy
        are exhausted and moves that would increase the energy are no longer
        thermally accessible."""

        elapsed = time.time() - self.start
        if step == 0:
            print(' Temperature        Energy    Accept   Improve     Elapsed   Remaining')
            print('%12.2f  %12.2f                      %s            ' % \
                (T, E, time_string(elapsed)))
        else:
            remain = (self.steps - step) * (elapsed / step)
            print('%12.2f  %12.2f  %7.2f%%  %7.2f%%  %s  %s' % \
                (T, E, 100.0 * acceptance, 100.0 * improvement,
                    time_string(elapsed), time_string(remain)))

    def anneal(self):
        """Minimizes the energy of a system by simulated annealing.

        Parameters
        state : an initial arrangement of the system

        Returns
        (state, energy): the best state and energy found.
        """
        step = 0
        self.start = time.time()

        steps = [] ### initialise a list to save the steps taken by the algorithm to find a good solution


        # Precompute factor for exponential cooling from Tmax to Tmin
        if self.Tmin <= 0.0:
            raise Exception('Exponential cooling requires a minimum "\
                "temperature greater than zero.')
        Tfactor = -math.log(self.Tmax / self.Tmin)

        # Note initial state
        T = self.Tmax
        E = self.energy()
        prevState = self.copy_state(self.state)
        prevEnergy = E
        bestState = self.copy_state(self.state)
        bestEnergy = E
        trials, accepts, improves = 0, 0, 0
        if self.updates > 0:
            updateWavelength = self.steps / self.updates
            self.update(step, T, E, None, None)

        # Attempt moves to new states
        while step < self.steps:
            step += 1
            T = self.Tmax * math.exp(Tfactor * step / self.steps)
            a,b = self.move()
            E = self.energy()
            dE = E - prevEnergy
            trials += 1
            if dE > 0.0 and math.exp(-dE / T) < random.random():
                # Restore previous state
                self.state = self.copy_state(prevState)
                E = prevEnergy
            else:
                # Accept new state and compare to best state
                accepts += 1
                if dE < 0.0:
                    improves += 1
                prevState = self.copy_state(self.state)
                prevEnergy = E

                steps.append([a,b]) ### append the "good move" to the list of steps

                if E < bestEnergy:
                    bestState = self.copy_state(self.state)
                    bestEnergy = E
            if self.updates > 1:
                if step // updateWavelength > (step - 1) // updateWavelength:
                    self.update(
                        step, T, E, accepts / trials, improves / trials)
                    trials, accepts, improves = 0, 0, 0

        # Return best state and energy
        return bestState, bestEnergy, steps ### added steps to what should be returned

def distance(a, b):
    """Calculates distance between two latitude-longitude coordinates."""
    R = 3963  # radius of Earth (miles)
    lat1, lon1 = math.radians(a[0]), math.radians(a[1])
    lat2, lon2 = math.radians(b[0]), math.radians(b[1])
    return math.acos(math.sin(lat1) * math.sin(lat2) +
                     math.cos(lat1) * math.cos(lat2) * math.cos(lon1 - lon2)) * R


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."""
        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 a,b ### return the change made

    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

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),
        'Philadelphia': (39.95, 75.17),
        '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)
    reconstructed_state = 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)

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

    while state[0] != 'New York City':
        state = state[1:] + state[:1]  # rotate NYC to start
    print("Results:")
    for city in state:
        print("\t", city)

    ### recontructed the annealing process
    print("")
    print("nbr. of steps:",len(steps))
    print("Reconstructed results:")
    for s in steps:
        reconstructed_state[s[0]], reconstructed_state[s[1]] = reconstructed_state[s[1]], reconstructed_state[s[0]]
    while reconstructed_state[0] != 'New York City':
        reconstructed_state = reconstructed_state[1:] + reconstructed_state[:1]  # rotate NYC to start
    for city in reconstructed_state:
        print("\t", city)

每次移动时保存都会构建一个确实可追溯的大量步骤。然而,它显然模仿了算法如何在解空间中探索和跳转到许多不同的位置,特别是在高温下。

为了获得更直接的收敛步骤,我可以移动步骤保存线:

steps.append([a,b]) ### append the "good move" to the list of steps

if E < bestEnergy:

只保存到目前为止最佳找到解决方案的步骤。但是,最后的步骤列表不再有助于重建行程(缺少步骤)。

这个问题对于模拟退火的工作方式是否无望和固有,或者是否有希望能够构建从随机到准最优的收敛步骤列表?

4 个答案:

答案 0 :(得分:2)

保存步骤不依赖于算法,即模拟退火,而是取决于您使用的实现或软件。如果它是您自己的实现,那么保存这些步骤应该没有问题。如果使用事件监听器方法,则可以添加任意数字。听众/客户处理您的活动。但您也可以将数据写入一个客户端,例如文件。如果您不使用自己的实现,则取决于软件。如果您使用专有软件,则依赖于其API。如果您使用开源软件并且没有找到在其API中检索此类信息的方法,则通常允许您根据自己的需要修改软件。

答案 1 :(得分:1)

给定任何随机算法,如果你使用伪随机生成器,那么你可以保存生成器的种子(随机但很短)并通过以相同的方式播种生成器来重放执行。

答案 2 :(得分:1)

我已经开始使用Ingber ASA代码来解决更传统的SA问题,该问题涉及估计大约72个错误变量,以便最好地拟合测量数据。

就我而言,有限数量的试验可以降低成本。在数百万次试验中,通常少于40次。如果我想跟踪它们,我会在代码中添加一个步骤,接受一个新位置来记录当前和现在新位置之间的差异。

重复这些差异将为您提供路径。

答案 3 :(得分:1)

最终,除了在系统状态的其余部分的情况下,移动本身并不重要。而不是试图跟踪移动或改变导致每个低能量解决方案的状态(正如你所指出的那样,丢失了所有被接受的先前移动的信息),你可以跟踪每次遇到新的最低分时都要说明:

if E < bestEnergy:
    list_of_best_states.append(self.copy_state(self.state))